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
113 lines (85 loc) · 2.56 KB
/
Copy pathRequestAnimationFrame.js
File metadata and controls
113 lines (85 loc) · 2.56 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var NOOP = require('../utils/NOOP');
/**
* Abstracts away the use of RAF or setTimeOut for the core game update loop.
*
* @class Phaser.RequestAnimationFrame
* @constructor
* @param {boolean} [forceSetTimeOut=false] - Tell Phaser to use setTimeOut even if raf is available.
*/
var RequestAnimationFrame = function ()
{
/**
* @property {boolean} isRunning - true if RequestAnimationFrame is running, otherwise false.
* @default
*/
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
* @method Phaser.RequestAnimationFrame#start
*/
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.
* @method Phaser.RequestAnimationFrame#stop
*/
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;