-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRaf.js
74 lines (66 loc) · 1.11 KB
/
Raf.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
/**
* RequestAnimationFrame Interface.
*
* @param {Function} loop The function to call in a loop.
* @example
*
* const raf = new Raf(loop)
* raf.run()
* raf.stop()
*
* const loop = elapsed => console.log(elapsed)
*/
function Raf(loop) {
/**
* The callback to call.
* @type {Function}
*/
this.cb = loop;
/**
* requestAnimationFrame ID
* @type {*}
*/
this.r = null;
/**
* The start time.
* @type {*}
*/
this.s = null;
/**
* Is it the end?
* @type {boolean}
*/
this.E = false;
/**
* Tick
* @type {FrameRequestCallback}
* @private
*/
this._t = this._t.bind(this);
}
/**
* Start the loop.
*/
Raf.prototype.run = function() {
this.E = false;
this.s = performance.now();
this.r = requestAnimationFrame(this._t);
};
/**
* Break the loop.
*/
Raf.prototype.stop = function() {
this.E = true;
cancelAnimationFrame(this.r);
};
/**
* The tick function.
* @param {number} t the current time.
* @private
*/
Raf.prototype._t = function(t) {
if (this.E) return;
this.cb(t - this.s);
this.r = requestAnimationFrame(this._t);
};
export default Raf;