forked from ustbhuangyi/animation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimeline.js
69 lines (54 loc) · 1.77 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
define(function (require, exports, module) {
var requestAnimationFrame,
cancelAnimationFrame,
DEFAULT_INTERVAL = 1000 / 60;
function Timeline() {
this.animationHandler = 0;
}
Timeline.prototype.onenterframe = function (time) {
// body...
};
Timeline.prototype.start = function (interval) {
// body...
var startTime = +new Date(),
me = this,
lastTick = 0;
interval = interval || DEFAULT_INTERVAL;
//this.onenterframe(new Date - startTime);
me.stop();
nextTick();
function nextTick() {
var now = +new Date();
me.animationHandler = requestAnimationFrame(nextTick);
if (now - lastTick >= interval) {
me.onenterframe(now - startTime);
lastTick = now;
}
}
};
Timeline.prototype.stop = function () {
// body...
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, 1000 / 60); // shoot for 60 fps
};
})();
// handle multiple browsers for cancelAnimationFrame()
cancelAnimationFrame = (function () {
return window.cancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.oCancelAnimationFrame ||
function (id) {
window.clearTimeout(id);
};
})();
module.exports = Timeline;
});