phaser/src/system/RequestAnimationFrame.js

167 lines
4 KiB
JavaScript
Raw Normal View History

/**
2013-10-01 12:54:29 +00:00
* @author Richard Davey <rich@photonstorm.com>
2014-02-05 05:54:25 +00:00
* @copyright 2014 Photon Storm Ltd.
2013-10-01 12:54:29 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* 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.
* @param {boolean} [forceSetTimeOut=false] - Tell Phaser to use setTimeOut even if raf is available.
*/
Phaser.RequestAnimationFrame = function(game, forceSetTimeOut) {
2014-03-23 07:59:28 +00:00
if (typeof forceSetTimeOut === 'undefined') { forceSetTimeOut = false; }
/**
* @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;
/**
* @property {boolean} forceSetTimeOut - Tell Phaser to use setTimeOut even if raf is available.
*/
this.forceSetTimeOut = forceSetTimeOut;
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-10-03 00:21:08 +00:00
};
Phaser.RequestAnimationFrame.prototype = {
/**
* Starts the requestAnimationFrame running or setTimeout if unavailable in browser
* @method Phaser.RequestAnimationFrame#start
*/
start: function () {
this.isRunning = true;
var _this = this;
if (!window.requestAnimationFrame || this.forceSetTimeOut)
{
this._isSetTimeOut = true;
this._onLoop = function () {
return _this.updateSetTimeout();
};
this._timeOutID = window.setTimeout(this._onLoop, 0);
}
else
{
this._isSetTimeOut = false;
this._onLoop = function (time) {
return _this.updateRAF(time);
};
this._timeOutID = window.requestAnimationFrame(this._onLoop);
}
},
/**
* The update method for the requestAnimationFrame
2014-03-23 07:59:28 +00:00
* @method Phaser.RequestAnimationFrame#updateRAF
*/
2014-03-13 16:49:52 +00:00
updateRAF: function () {
this.game.update(Date.now());
this._timeOutID = window.requestAnimationFrame(this._onLoop);
},
/**
* The update method for the setTimeout.
* @method Phaser.RequestAnimationFrame#updateSetTimeout
*/
updateSetTimeout: function () {
this.game.update(Date.now());
this._timeOutID = window.setTimeout(this._onLoop, this.game.time.timeToCall);
},
/**
* Stops the requestAnimationFrame from running.
* @method Phaser.RequestAnimationFrame#stop
*/
stop: function () {
if (this._isSetTimeOut)
{
clearTimeout(this._timeOutID);
}
else
{
window.cancelAnimationFrame(this._timeOutID);
}
this.isRunning = false;
},
/**
* Is the browser using setTimeout?
* @method Phaser.RequestAnimationFrame#isSetTimeOut
* @return {boolean}
*/
isSetTimeOut: function () {
return this._isSetTimeOut;
},
/**
* Is the browser using requestAnimationFrame?
* @method Phaser.RequestAnimationFrame#isRAF
* @return {boolean}
*/
isRAF: function () {
return (this._isSetTimeOut === false);
}
};
Phaser.RequestAnimationFrame.prototype.constructor = Phaser.RequestAnimationFrame;