-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationManager.js
More file actions
58 lines (48 loc) · 1.33 KB
/
AnimationManager.js
File metadata and controls
58 lines (48 loc) · 1.33 KB
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
class AnimationManager {
constructor() {
this.live = [];
this.waiting = [];
this.running = false;
this.lastTimestamp = null;
}
animate(highResTimeStamp) {
let multiplier = 1;
if (this.lastTimestamp != null){
multiplier = Math.round(
(highResTimeStamp - this.lastTimestamp) /
(1000/60)
);
}
this.lastTimestamp = highResTimeStamp;
this.running = true;
let newLive = [];
let length = this.live.length;
for (let i=0; i<length; i++) {
let obj = this.live.shift();
// Run the frame and re queue it if it returns true
if (obj.drawFrame(multiplier)) {
newLive.push(obj);
}
}
length = this.waiting.length;
for (let i=0; i<length; i++) {
newLive.push(this.waiting.shift());
}
this.live = newLive;
if (this.live.length == 0) {
this.running = false;
return;
}
window.requestAnimationFrame((ts)=>{
this.animate(ts);
});
}
add(obj) {
this.waiting.push(obj);
if (!this.running) {
window.requestAnimationFrame((ts)=>{
this.animate(ts);
});
}
}
}