-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (47 loc) · 958 Bytes
/
index.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
// Holds last iteration timestamp.
var time = 0;
/**
* Calls `fn` on next frame.
*
* @param {Function} fn The function
* @return {int} The request ID
* @api private
*/
function raf(fn) {
return window.requestAnimationFrame(function() {
var now = Date.now();
var elapsed = now - time;
if (elapsed > 999) {
elapsed = 1 / 60;
} else {
elapsed /= 1000;
}
time = now;
fn(elapsed);
});
}
module.exports = {
/**
* Calls `fn` on every frame with `elapsed` set to the elapsed
* time in milliseconds.
*
* @param {Function} fn The function
* @return {int} The request ID
* @api public
*/
start: function(fn) {
return raf(function tick(elapsed) {
fn(elapsed);
raf(tick);
});
},
/**
* Cancels the specified animation frame request.
*
* @param {int} id The request ID
* @api public
*/
stop: function(id) {
window.cancelAnimationFrame(id);
}
};