This repository was archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationManager.js
More file actions
79 lines (65 loc) · 2.24 KB
/
AnimationManager.js
File metadata and controls
79 lines (65 loc) · 2.24 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
export default class AnimationManager {
constructor(timer){
this._currentRequesetAnimationFrame = null;
this._animations = [];
this._lastTime = 0;
this._fps = 100;
this._refreshRateInMilliseconds = 1000 / this._fps;
this._timer = timer || Date;
this._requestCallback = function () {
this.tick(this._timer.now());
};
this._requestCallback = this._requestCallback.bind(this);
this.setFramesPerSecond(this._fps);
}
setFramesPerSecond (fps) {
this._fps = fps;
this._refreshRateInMilliseconds = 1000 / fps;
}
getFramesPerSecond () {
return this._fps;
}
checkRequestToStartOrStop () {
let animations = this._animations;
if (this._currentRequesetAnimationFrame === null && animations.length > 0) {
this._currentRequesetAnimationFrame = requestAnimationFrame(this._requestCallback);
}
}
tick (time) {
let animationsCopy;
let animations = this._animations;
let length = animations.length;
let elapsedTime = time - this._lastTime;
// Throttle this to be the specified frames per second.
if (elapsedTime >= this._refreshRateInMilliseconds) {
this._lastTime = time;
if (length > 0) {
animationsCopy = animations.slice(0);
animationsCopy.forEach(function (animation) {
animation.tick(time);
});
this._currentRequesetAnimationFrame = requestAnimationFrame(this._requestCallback);
} else {
this._currentRequesetAnimationFrame = null;
}
} else {
this._currentRequesetAnimationFrame = requestAnimationFrame(this._requestCallback);
}
}
now () {
return this._timer.now();
}
register (animation) {
let index = this._animations.indexOf(animation);
if (index === -1) {
this._animations.push(animation);
this.checkRequestToStartOrStop();
}
}
unregister (animation) {
let index = this._animations.indexOf(animation);
if (index >= 0) {
this._animations.splice(index, 1);
}
}
}