forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestAnimationFrame.js
More file actions
88 lines (61 loc) · 2.03 KB
/
Copy pathRequestAnimationFrame.js
File metadata and controls
88 lines (61 loc) · 2.03 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
80
81
82
83
84
85
86
87
88
var NOOP = require('../utils/NOOP');
// Abstracts away the use of RAF or setTimeOut for the core game update loop.
var RequestAnimationFrame = function ()
{
// @property {boolean} isRunning - true if RequestAnimationFrame is running, otherwise false.
this.isRunning = false;
this.callback = NOOP;
this.tick = 0;
// @property {boolean} isSetTimeOut - True if the browser is using setTimeout instead of rAf.
this.isSetTimeOut = false;
// @property {number} timeOutID - The callback setTimeout or rAf callback ID used when calling cancel.
this.timeOutID = null;
var _this = this;
// timestamp = DOMHighResTimeStamp
var step = function (timestamp)
{
_this.tick = timestamp;
_this.callback(timestamp);
_this.timeOutID = window.requestAnimationFrame(step);
};
var stepTimeout = function ()
{
var d = Date.now();
_this.tick = d;
_this.callback(d);
_this.timeOutID = window.setTimeout(stepTimeout, _this.timeToCall);
};
this.step = step;
this.stepTimeout = stepTimeout;
};
RequestAnimationFrame.prototype.constructor = RequestAnimationFrame;
RequestAnimationFrame.prototype = {
// Starts the requestAnimationFrame running or setTimeout if unavailable in browser
start: function (callback, forceSetTimeOut)
{
this.callback = callback;
this.isSetTimeOut = forceSetTimeOut;
this.isRunning = true;
var _this = this;
this.timeOutID = (forceSetTimeOut) ? window.setTimeout(_this.stepTimeout, 0) : window.requestAnimationFrame(_this.step);
},
// Stops the requestAnimationFrame from running.
stop: function ()
{
this.isRunning = false;
if (this.isSetTimeOut)
{
clearTimeout(this.timeOutID);
}
else
{
window.cancelAnimationFrame(this.timeOutID);
}
},
destroy: function ()
{
this.stop();
this.callback = NOOP;
}
};
module.exports = RequestAnimationFrame;