forked from BYU-ARCLITE/Ayamel.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimelineView.js
81 lines (76 loc) · 1.9 KB
/
TimelineView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
(function(Timeline){
"use strict";
if(!Timeline){
throw new Error("Timeline Uninitialized");
}
function View(tl, start, end){
var startTime = 0, endTime = tl.length;
this.tl = tl;
Object.defineProperties(this,{
startTime: {
get: function(){ return startTime; },
set: function(val){
var maxtime;
if(val < 0){ val = 0; }
else{
maxtime = endTime - tl.width/1000; // 1 ms/px maximum zoom
if(val > maxtime){ val = maxtime; }
}
return startTime = val;
}, enumerable: true
},
endTime: {
get: function(){ return endTime; },
set: function(val){
var mintime;
if(val > tl.length){ val = tl.length; }
else{
mintime = startTime + tl.width/1000; // 1 ms/px maximum zoom
if(val < mintime){ val = mintime; }
}
return endTime = val;
}, enumerable:true
},
move: {
value: function(delta){
if(delta > 0){
if(endTime+delta <= tl.length){
endTime += delta;
startTime += delta;
}else{
startTime += tl.length-endTime;
endTime = tl.length;
}
}else{
if(startTime+delta >= 0){
startTime += delta;
endTime += delta;
}else{
endTime -= startTime;
startTime = 0;
}
}
},enumerable: true
},
center: {
value: function(time){
this.move(time - (this.startTime + this.length/2));
},enumerable: true
}
});
if(start < end){
this.startTime = start;
this.endTime = end;
}else{
this.endTime = start;
this.startTime = end;
}
}
View.prototype = {
get length() { return this.endTime - this.startTime; }, // seconds
get zoom() { return this.length/this.tl.width; }, // seconds per pixel
pixelToTime: function(pixel) { return pixel * this.zoom + this.startTime; },
timeToPixel: function(time) { return Math.round((time-this.startTime) / this.zoom); }
};
Timeline.View = View;
}(Timeline));