|
| 1 | +/** |
| 2 | +* @author Richard Davey <rich@photonstorm.com> |
| 3 | +* @copyright 2016 Photon Storm Ltd. |
| 4 | +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | +*/ |
| 6 | + |
| 7 | +/** |
| 8 | +* Abstracts away the use of RAF or setTimeOut for the core game update loop. |
| 9 | +* |
| 10 | +* @class Phaser.RequestAnimationFrame |
| 11 | +* @constructor |
| 12 | +* @param {Phaser.Game} game - A reference to the currently running game. |
| 13 | +* @param {boolean} [forceSetTimeOut=false] - Tell Phaser to use setTimeOut even if raf is available. |
| 14 | +*/ |
| 15 | +function RequestAnimationFrame (game) |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @property {Phaser.Game} game - The currently running game. |
| 19 | + */ |
| 20 | + this.game = game; |
| 21 | + |
| 22 | + /** |
| 23 | + * @property {boolean} isRunning - true if RequestAnimationFrame is running, otherwise false. |
| 24 | + * @default |
| 25 | + */ |
| 26 | + this.isRunning = false; |
| 27 | + |
| 28 | + var vendors = [ |
| 29 | + 'ms', |
| 30 | + 'moz', |
| 31 | + 'webkit', |
| 32 | + 'o' |
| 33 | + ]; |
| 34 | + |
| 35 | + for (var x = 0; x < vendors.length && !window.requestAnimationFrame; x++) |
| 36 | + { |
| 37 | + window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame']; |
| 38 | + window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame']; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @property {boolean} isSetTimeOut - True if the browser is using setTimeout instead of rAf. |
| 43 | + */ |
| 44 | + this.isSetTimeOut = false; |
| 45 | + |
| 46 | + /** |
| 47 | + * @property {number} timeOutID - The callback setTimeout or rAf callback ID used when calling cancel. |
| 48 | + */ |
| 49 | + this.timeOutID = null; |
| 50 | + |
| 51 | + // timestamp = DOMHighResTimeStamp |
| 52 | + this.step = function (timestamp) |
| 53 | + { |
| 54 | + this.timeOutID = window.requestAnimationFrame(this.step); |
| 55 | + |
| 56 | + this.game.update(timestamp); |
| 57 | + }; |
| 58 | + |
| 59 | + this.stepTimeout = function () |
| 60 | + { |
| 61 | + this.game.update(Date.now()); |
| 62 | + |
| 63 | + this.timeOutID = window.setTimeout(this.stepTimeout, this.game.time.timeToCall); |
| 64 | + }; |
| 65 | + |
| 66 | + /** |
| 67 | + * Starts the requestAnimationFrame running or setTimeout if unavailable in browser |
| 68 | + * @method Phaser.RequestAnimationFrame#start |
| 69 | + */ |
| 70 | + this.start = function () |
| 71 | + { |
| 72 | + this.isRunning = true; |
| 73 | + |
| 74 | + if (this.game.config.forceSetTimeOut) |
| 75 | + { |
| 76 | + this.isSetTimeOut = true; |
| 77 | + |
| 78 | + this.timeOutID = window.setTimeout(this.stepTimeout, 0); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + this.isSetTimeOut = false; |
| 83 | + |
| 84 | + this.timeOutID = window.requestAnimationFrame(this.step); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + /** |
| 89 | + * Stops the requestAnimationFrame from running. |
| 90 | + * @method Phaser.RequestAnimationFrame#stop |
| 91 | + */ |
| 92 | + this.stop = function () |
| 93 | + { |
| 94 | + this.isRunning = false; |
| 95 | + |
| 96 | + if (this.isSetTimeOut) |
| 97 | + { |
| 98 | + clearTimeout(this.timeOutID); |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + window.cancelAnimationFrame(this.timeOutID); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + this.destroy = function () |
| 107 | + { |
| 108 | + this.stop(); |
| 109 | + |
| 110 | + this.game = undefined; |
| 111 | + }; |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +RequestAnimationFrame.prototype.constructor = RequestAnimationFrame; |
| 116 | + |
| 117 | +module.exports = RequestAnimationFrame; |
0 commit comments