-
Notifications
You must be signed in to change notification settings - Fork 34
/
ticker.js
59 lines (50 loc) · 1.68 KB
/
ticker.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
;(function(exports) {
var interval = 16;
function Ticker(coquette, gameLoop) {
setupRequestAnimationFrame();
var nextTickFn;
this.stop = function() {
nextTickFn = function() {};
};
this.start = function() {
var prev = Date.now();
var tick = function() {
var now = Date.now();
var interval = now - prev;
prev = now;
gameLoop(interval);
requestAnimationFrame(nextTickFn);
};
nextTickFn = tick;
requestAnimationFrame(nextTickFn);
};
this.start();
};
// From: https://gist.github.com/paulirish/1579671
// Thanks Erik, Paul and Tino
var setupRequestAnimationFrame = function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function(callback, element) {
var currTime = Date.now();
var timeToCall = Math.max(0, interval - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}
};
exports.Ticker = Ticker;
})(typeof exports === 'undefined' ? this.Coquette : exports);