-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRAF.ts
45 lines (35 loc) · 1.12 KB
/
RAF.ts
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
type Callback = (delta: number, time: number) => void;
class RAF
{
private raf!: number;
private paused = true;
private lastTime = 0.0;
private readonly callbacks: Callback[] = [];
private readonly onUpdate = this.update.bind(this);
public add(callback: Callback): void {
const index = this.callbacks.indexOf(callback);
index === -1 && this.callbacks.push(callback);
}
private update(time: number): void {
this.raf = requestAnimationFrame(this.onUpdate);
const delta = time - (this.lastTime || 0.0);
for (let c = this.callbacks.length; c--; )
this.callbacks[c](delta, time);
this.lastTime = time;
}
public remove(callback: Callback): void {
const index = this.callbacks.indexOf(callback);
index !== -1 && this.callbacks.splice(index, 1);
}
public dispose(): void {
cancelAnimationFrame(this.raf);
this.callbacks.length = 0;
}
public set pause(paused: boolean) {
if (this.paused !== paused)
((this.paused = paused))
? cancelAnimationFrame(this.raf)
: (this.raf = requestAnimationFrame(this.onUpdate));
}
}
export default new RAF();