-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnimator.js
More file actions
51 lines (43 loc) · 1.2 KB
/
Animator.js
File metadata and controls
51 lines (43 loc) · 1.2 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
// Animation helper
var Animator = (function ()
{
var Animator = {};
Animator.Animation = function(framesPerSecond, callback)
{
this.lastTime = new Date().getTime();
this.framesPerSecond = framesPerSecond;
this.callback = callback;
this.paused = true;
};
Animator.Animation.prototype.IsRunning = function()
{
return !this.paused;
}
Animator.Animation.prototype.start = function()
{
this.paused = false;
this.draw();
};
Animator.Animation.prototype.stop = function ()
{
this.paused = true;
};
Animator.Animation.prototype.draw = function()
{
var time = new Date().getTime();
// Ensure we only run as fast (or slower) than the caller asked
var diff = time - this.lastTime;
if (diff >= 1000 / this.framesPerSecond)
{
this.callback();
this.lastTime = time;
}
// Only request the next frame if we're still running
if (!this.paused)
{
// Bind to ensure correct 'this' in the callback
window.requestAnimationFrame(this.draw.bind(this));
}
};
return Animator;
})();