Skip to content

Commit 96c7446

Browse files
committed
Added RAF
1 parent 247a837 commit 96c7446

3 files changed

Lines changed: 122 additions & 2 deletions

File tree

v3/src/boot/Config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ function Config (config)
6363
this.hidePhaser = getValue(banner, 'hidePhaser', false);
6464
this.bannerTextColor = getValue(banner, 'text', defaultBannerTextColor);
6565
this.bannerBackgroundColor = getValue(banner, 'background', defaultBannerColor);
66+
67+
this.forceSetTimeOut = getValue(config, 'forceSetTimeOut', false);
68+
6669
}
6770

6871
Config.prototype.constructor = Config;

v3/src/boot/Game.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
var Config = require('./Config');
88
var DebugHeader = require('./DebugHeader');
9+
var RequestAnimationFrame = require('./RequestAnimationFrame');
910

1011
var Game = function (config)
1112
{
@@ -30,7 +31,7 @@ var Game = function (config)
3031
* @property {Phaser.RequestAnimationFrame} raf - Automatically handles the core game loop via requestAnimationFrame or setTimeout
3132
* @protected
3233
*/
33-
this.raf = null;
34+
this.raf = new RequestAnimationFrame(this);
3435

3536
/**
3637
* @property {Phaser.TextureManager} textures - Reference to the Phaser Texture Manager.
@@ -66,7 +67,6 @@ var Game = function (config)
6667

6768
// this.device.whenReady(this.boot, this);
6869

69-
7070
DebugHeader(this);
7171
};
7272

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)