2017-01-25 12:02:18 +00:00
|
|
|
var NOOP = require('../utils/NOOP');
|
|
|
|
|
2017-08-01 12:10:08 +00:00
|
|
|
// Abstracts away the use of RAF or setTimeOut for the core game update loop.
|
2017-01-25 12:02:18 +00:00
|
|
|
var RequestAnimationFrame = function ()
|
2016-11-25 00:34:37 +00:00
|
|
|
{
|
2017-08-01 12:10:08 +00:00
|
|
|
// @property {boolean} isRunning - true if RequestAnimationFrame is running, otherwise false.
|
2016-11-25 00:34:37 +00:00
|
|
|
this.isRunning = false;
|
|
|
|
|
2017-01-25 12:02:18 +00:00
|
|
|
this.callback = NOOP;
|
|
|
|
|
2016-11-25 01:37:14 +00:00
|
|
|
this.tick = 0;
|
|
|
|
|
2017-08-01 12:10:08 +00:00
|
|
|
// @property {boolean} isSetTimeOut - True if the browser is using setTimeout instead of rAf.
|
2016-11-25 00:34:37 +00:00
|
|
|
this.isSetTimeOut = false;
|
|
|
|
|
2017-08-01 12:10:08 +00:00
|
|
|
// @property {number} timeOutID - The callback setTimeout or rAf callback ID used when calling cancel.
|
2016-11-25 00:34:37 +00:00
|
|
|
this.timeOutID = null;
|
|
|
|
|
2016-11-25 01:37:14 +00:00
|
|
|
var _this = this;
|
|
|
|
|
2016-11-25 00:34:37 +00:00
|
|
|
// timestamp = DOMHighResTimeStamp
|
2016-11-25 01:37:14 +00:00
|
|
|
var step = function (timestamp)
|
2016-11-25 00:34:37 +00:00
|
|
|
{
|
2016-11-25 01:37:14 +00:00
|
|
|
_this.tick = timestamp;
|
2016-11-25 00:34:37 +00:00
|
|
|
|
2017-01-25 12:02:18 +00:00
|
|
|
_this.callback(timestamp);
|
2017-04-28 02:13:32 +00:00
|
|
|
|
|
|
|
_this.timeOutID = window.requestAnimationFrame(step);
|
2016-11-25 00:34:37 +00:00
|
|
|
};
|
|
|
|
|
2016-11-25 01:37:14 +00:00
|
|
|
var stepTimeout = function ()
|
2016-11-25 00:34:37 +00:00
|
|
|
{
|
2017-01-25 12:02:18 +00:00
|
|
|
var d = Date.now();
|
|
|
|
|
|
|
|
_this.tick = d;
|
2016-11-25 01:37:14 +00:00
|
|
|
|
2017-01-25 12:02:18 +00:00
|
|
|
_this.callback(d);
|
2017-04-28 02:13:32 +00:00
|
|
|
|
|
|
|
_this.timeOutID = window.setTimeout(stepTimeout, _this.timeToCall);
|
2016-11-25 00:34:37 +00:00
|
|
|
};
|
|
|
|
|
2017-01-25 12:02:18 +00:00
|
|
|
this.step = step;
|
|
|
|
this.stepTimeout = stepTimeout;
|
|
|
|
};
|
|
|
|
|
|
|
|
RequestAnimationFrame.prototype.constructor = RequestAnimationFrame;
|
|
|
|
|
|
|
|
RequestAnimationFrame.prototype = {
|
|
|
|
|
2017-08-01 12:10:08 +00:00
|
|
|
// Starts the requestAnimationFrame running or setTimeout if unavailable in browser
|
2017-01-25 12:02:18 +00:00
|
|
|
start: function (callback, forceSetTimeOut)
|
2016-11-25 00:34:37 +00:00
|
|
|
{
|
2017-01-25 12:02:18 +00:00
|
|
|
this.callback = callback;
|
2016-11-25 00:34:37 +00:00
|
|
|
|
2017-01-25 12:02:18 +00:00
|
|
|
this.isSetTimeOut = forceSetTimeOut;
|
2016-11-25 00:34:37 +00:00
|
|
|
|
2017-01-25 12:02:18 +00:00
|
|
|
this.isRunning = true;
|
2016-11-25 00:34:37 +00:00
|
|
|
|
2017-01-25 12:02:18 +00:00
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
this.timeOutID = (forceSetTimeOut) ? window.setTimeout(_this.stepTimeout, 0) : window.requestAnimationFrame(_this.step);
|
|
|
|
},
|
2016-11-25 00:34:37 +00:00
|
|
|
|
2017-08-01 12:10:08 +00:00
|
|
|
// Stops the requestAnimationFrame from running.
|
2017-01-25 12:02:18 +00:00
|
|
|
stop: function ()
|
2016-11-25 00:34:37 +00:00
|
|
|
{
|
|
|
|
this.isRunning = false;
|
|
|
|
|
|
|
|
if (this.isSetTimeOut)
|
|
|
|
{
|
|
|
|
clearTimeout(this.timeOutID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
window.cancelAnimationFrame(this.timeOutID);
|
|
|
|
}
|
2017-01-25 12:02:18 +00:00
|
|
|
},
|
2016-11-25 00:34:37 +00:00
|
|
|
|
2017-01-25 12:02:18 +00:00
|
|
|
destroy: function ()
|
2016-11-25 00:34:37 +00:00
|
|
|
{
|
|
|
|
this.stop();
|
|
|
|
|
2017-01-25 12:02:18 +00:00
|
|
|
this.callback = NOOP;
|
|
|
|
}
|
2016-11-25 00:34:37 +00:00
|
|
|
|
2017-01-25 12:02:18 +00:00
|
|
|
};
|
2016-11-25 00:34:37 +00:00
|
|
|
|
|
|
|
module.exports = RequestAnimationFrame;
|