-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimeline.js
80 lines (64 loc) · 1.78 KB
/
timeline.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
'use strict';
var requestAnimationFrame,
cancelAnimationFrame,
startTimeline,
DEFAULT_INTERVAL = 1000 / 60;
function Timeline() {
this.animationHandler = 0;
}
Timeline.prototype.onenterframe = function (time) {
// body...
};
Timeline.prototype.start = function (interval) {
var me = this;
me.interval = interval || DEFAULT_INTERVAL;
startTimeline(me, +new Date())
};
Timeline.prototype.restart = function () {
// body...
var me = this;
if (!me.dur || !me.interval) return;
me.stop();
startTimeline(me, +new Date() - me.dur);
};
Timeline.prototype.stop = function () {
if (this.startTime) {
this.dur = +new Date() - this.startTime;
}
cancelAnimationFrame(this.animationHandler);
};
requestAnimationFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
// if all else fails, use setTimeout
function (callback) {
return window.setTimeout(callback, (callback.interval || DEFAULT_INTERVAL) / 2); // make interval as precise as possible.
};
})();
// handle multiple browsers for cancelAnimationFrame()
cancelAnimationFrame = (function () {
return window.cancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.oCancelAnimationFrame ||
function (id) {
window.clearTimeout(id);
};
})();
startTimeline = function(timeline, startTime) {
var lastTick = +new Date();
timeline.startTime = startTime;
nextTick.interval = timeline.interval;
nextTick();
function nextTick() {
var now = +new Date();
timeline.animationHandler = requestAnimationFrame(nextTick);
if (now - lastTick >= timeline.interval) {
timeline.onenterframe(now - startTime);
lastTick = now;
}
}
};
module.exports = Timeline;