2013-08-29 02:52:59 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2016-04-04 21:15:01 +00:00
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-08-29 02:52:59 +00:00
|
|
|
* Abstracts away the use of RAF or setTimeOut for the core game update loop.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
2014-03-23 07:59:28 +00:00
|
|
|
* @class Phaser.RequestAnimationFrame
|
2013-10-01 12:54:29 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
2014-02-20 01:31:13 +00:00
|
|
|
* @param {boolean} [forceSetTimeOut=false] - Tell Phaser to use setTimeOut even if raf is available.
|
2013-08-29 02:52:59 +00:00
|
|
|
*/
|
2014-02-20 01:31:13 +00:00
|
|
|
Phaser.RequestAnimationFrame = function(game, forceSetTimeOut) {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (forceSetTimeOut === undefined) { forceSetTimeOut = false; }
|
2014-02-20 01:31:13 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - The currently running game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} isRunning - true if RequestAnimationFrame is running, otherwise false.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.isRunning = false;
|
|
|
|
|
2014-02-20 01:31:13 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} forceSetTimeOut - Tell Phaser to use setTimeOut even if raf is available.
|
|
|
|
*/
|
|
|
|
this.forceSetTimeOut = forceSetTimeOut;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
var vendors = [
|
|
|
|
'ms',
|
|
|
|
'moz',
|
|
|
|
'webkit',
|
|
|
|
'o'
|
|
|
|
];
|
|
|
|
|
|
|
|
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; x++)
|
|
|
|
{
|
|
|
|
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
|
|
|
|
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} _isSetTimeOut - true if the browser is using setTimeout instead of raf.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._isSetTimeOut = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {function} _onLoop - The function called by the update.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._onLoop = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _timeOutID - The callback ID used when calling cancel.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._timeOutID = null;
|
2013-11-04 00:04:19 +00:00
|
|
|
|
2013-10-03 00:21:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.RequestAnimationFrame.prototype = {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-01-27 09:25:12 +00:00
|
|
|
* Starts the requestAnimationFrame running or setTimeout if unavailable in browser
|
2013-11-25 04:40:04 +00:00
|
|
|
* @method Phaser.RequestAnimationFrame#start
|
|
|
|
*/
|
|
|
|
start: function () {
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this.isRunning = true;
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
var _this = this;
|
2013-08-29 06:06:16 +00:00
|
|
|
|
2014-02-20 01:31:13 +00:00
|
|
|
if (!window.requestAnimationFrame || this.forceSetTimeOut)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
|
|
|
this._isSetTimeOut = true;
|
2013-08-29 06:06:16 +00:00
|
|
|
|
|
|
|
this._onLoop = function () {
|
|
|
|
return _this.updateSetTimeout();
|
|
|
|
};
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this._timeOutID = window.setTimeout(this._onLoop, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._isSetTimeOut = false;
|
2013-08-29 06:06:16 +00:00
|
|
|
|
2013-08-30 01:00:30 +00:00
|
|
|
this._onLoop = function (time) {
|
|
|
|
return _this.updateRAF(time);
|
2013-08-29 06:06:16 +00:00
|
|
|
};
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this._timeOutID = window.requestAnimationFrame(this._onLoop);
|
|
|
|
}
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* The update method for the requestAnimationFrame
|
2014-03-23 07:59:28 +00:00
|
|
|
* @method Phaser.RequestAnimationFrame#updateRAF
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-08 18:52:02 +00:00
|
|
|
updateRAF: function (rafTime) {
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2016-06-03 12:00:18 +00:00
|
|
|
if (this.isRunning)
|
|
|
|
{
|
|
|
|
// floor the rafTime to make it equivalent to the Date.now() provided by updateSetTimeout (just below)
|
|
|
|
this.game.update(Math.floor(rafTime));
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2016-06-03 12:00:18 +00:00
|
|
|
this._timeOutID = window.requestAnimationFrame(this._onLoop);
|
|
|
|
}
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* The update method for the setTimeout.
|
|
|
|
* @method Phaser.RequestAnimationFrame#updateSetTimeout
|
|
|
|
*/
|
|
|
|
updateSetTimeout: function () {
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2016-06-03 12:00:18 +00:00
|
|
|
if (this.isRunning)
|
|
|
|
{
|
|
|
|
this.game.update(Date.now());
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2016-06-03 12:00:18 +00:00
|
|
|
this._timeOutID = window.setTimeout(this._onLoop, this.game.time.timeToCall);
|
|
|
|
}
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Stops the requestAnimationFrame from running.
|
|
|
|
* @method Phaser.RequestAnimationFrame#stop
|
|
|
|
*/
|
|
|
|
stop: function () {
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
if (this._isSetTimeOut)
|
|
|
|
{
|
|
|
|
clearTimeout(this._timeOutID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
window.cancelAnimationFrame(this._timeOutID);
|
|
|
|
}
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this.isRunning = false;
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Is the browser using setTimeout?
|
|
|
|
* @method Phaser.RequestAnimationFrame#isSetTimeOut
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isSetTimeOut: function () {
|
|
|
|
return this._isSetTimeOut;
|
|
|
|
},
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Is the browser using requestAnimationFrame?
|
|
|
|
* @method Phaser.RequestAnimationFrame#isRAF
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isRAF: function () {
|
|
|
|
return (this._isSetTimeOut === false);
|
|
|
|
}
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.RequestAnimationFrame.prototype.constructor = Phaser.RequestAnimationFrame;
|