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
208 lines (174 loc) · 5.43 KB
/
Copy pathRequestAnimationFrame.js
File metadata and controls
208 lines (174 loc) · 5.43 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var NOOP = require('../utils/NOOP');
/**
* @classdesc
* Abstracts away the use of RAF or setTimeOut for the core game update loop.
* This is invoked automatically by the Phaser.Game instance.
*
* @class RequestAnimationFrame
* @memberof Phaser.DOM
* @constructor
* @since 3.0.0
*/
var RequestAnimationFrame = new Class({
initialize:
function RequestAnimationFrame ()
{
/**
* True if RequestAnimationFrame is running, otherwise false.
*
* @name Phaser.DOM.RequestAnimationFrame#isRunning
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.isRunning = false;
/**
* The callback to be invoked each step.
*
* @name Phaser.DOM.RequestAnimationFrame#callback
* @type {FrameRequestCallback}
* @since 3.0.0
*/
this.callback = NOOP;
/**
* The most recent timestamp. Either a DOMHighResTimeStamp under RAF or `Date.now` under SetTimeout.
*
* @name Phaser.DOM.RequestAnimationFrame#tick
* @type {number}
* @default 0
* @since 3.0.0
*/
this.tick = 0;
/**
* True if the step is using setTimeout instead of RAF.
*
* @name Phaser.DOM.RequestAnimationFrame#isSetTimeOut
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.isSetTimeOut = false;
/**
* The setTimeout or RAF callback ID used when canceling them.
*
* @name Phaser.DOM.RequestAnimationFrame#timeOutID
* @type {?number}
* @default null
* @since 3.0.0
*/
this.timeOutID = null;
/**
* The previous time the step was called.
*
* @name Phaser.DOM.RequestAnimationFrame#lastTime
* @type {number}
* @default 0
* @since 3.0.0
*/
this.lastTime = 0;
/**
* The target FPS rate in ms.
* Only used when setTimeout is used instead of RAF.
*
* @name Phaser.DOM.RequestAnimationFrame#target
* @type {number}
* @default 0
* @since 3.21.0
*/
this.target = 0;
var _this = this;
/**
* The RAF step function.
* Updates the local tick value, invokes the callback and schedules another call to requestAnimationFrame.
*
* @name Phaser.DOM.RequestAnimationFrame#step
* @type {FrameRequestCallback}
* @since 3.0.0
*/
this.step = function step ()
{
// Because we cannot trust the time passed to this callback from the browser and need it kept in sync with event times
var timestamp = window.performance.now();
// DOMHighResTimeStamp
_this.lastTime = _this.tick;
_this.tick = timestamp;
_this.callback(timestamp);
_this.timeOutID = window.requestAnimationFrame(step);
};
/**
* The SetTimeout step function.
* Updates the local tick value, invokes the callback and schedules another call to setTimeout.
*
* @name Phaser.DOM.RequestAnimationFrame#stepTimeout
* @type {function}
* @since 3.0.0
*/
this.stepTimeout = function stepTimeout ()
{
var d = Date.now();
var delay = Math.min(Math.max(_this.target * 2 + _this.tick - d, 0), _this.target);
_this.lastTime = _this.tick;
_this.tick = d;
_this.callback(d);
_this.timeOutID = window.setTimeout(stepTimeout, delay);
};
},
/**
* Starts the requestAnimationFrame or setTimeout process running.
*
* @method Phaser.DOM.RequestAnimationFrame#start
* @since 3.0.0
*
* @param {FrameRequestCallback} callback - The callback to invoke each step.
* @param {boolean} forceSetTimeOut - Should it use SetTimeout, even if RAF is available?
* @param {number} targetFPS - The target fps rate (in ms). Only used when setTimeout is used.
*/
start: function (callback, forceSetTimeOut, targetFPS)
{
if (this.isRunning)
{
return;
}
this.callback = callback;
this.isSetTimeOut = forceSetTimeOut;
this.target = targetFPS;
this.isRunning = true;
this.timeOutID = (forceSetTimeOut) ? window.setTimeout(this.stepTimeout, 0) : window.requestAnimationFrame(this.step);
},
/**
* Stops the requestAnimationFrame or setTimeout from running.
*
* @method Phaser.DOM.RequestAnimationFrame#stop
* @since 3.0.0
*/
stop: function ()
{
this.isRunning = false;
if (this.isSetTimeOut)
{
clearTimeout(this.timeOutID);
}
else
{
window.cancelAnimationFrame(this.timeOutID);
}
},
/**
* Stops the step from running and clears the callback reference.
*
* @method Phaser.DOM.RequestAnimationFrame#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.stop();
this.callback = NOOP;
}
});
module.exports = RequestAnimationFrame;