forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestAnimationFrame.js
More file actions
129 lines (98 loc) · 2.21 KB
/
Copy pathRequestAnimationFrame.js
File metadata and controls
129 lines (98 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Phaser - RequestAnimationFrame
*
* Abstracts away the use of RAF or setTimeOut for the core game update loop.
*/
Phaser.RequestAnimationFrame = function(game) {
this.game = game;
this._isSetTimeOut = false;
this.isRunning = false;
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'];
}
};
Phaser.RequestAnimationFrame.prototype = {
/**
* The function called by the update
* @private
**/
_onLoop: null,
/**
* Starts the requestAnimatioFrame running or setTimeout if unavailable in browser
* @method start
**/
start: function () {
this.isRunning = true;
var _this = this;
if (!window.requestAnimationFrame)
{
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);
};
window.requestAnimationFrame(this._onLoop);
}
},
/**
* The update method for the requestAnimationFrame
* @method RAFUpdate
**/
updateRAF: function (time) {
this.game.update(time);
window.requestAnimationFrame(this._onLoop);
},
/**
* The update method for the setTimeout
* @method SetTimeoutUpdate
**/
updateSetTimeout: function () {
this.game.update(Date.now());
this._timeOutID = window.setTimeout(this._onLoop, this.game.time.timeToCall);
},
/**
* Stops the requestAnimationFrame from running
* @method stop
**/
stop: function () {
if (this._isSetTimeOut)
{
clearTimeout(this._timeOutID);
}
else
{
window.cancelAnimationFrame;
}
this.isRunning = false;
},
/**
* Is the browser using setTimeout?
* @method isSetTimeOut
* @return bool
**/
isSetTimeOut: function () {
return this._isSetTimeOut;
},
/**
* Is the browser using requestAnimationFrame?
* @method isRAF
* @return bool
**/
isRAF: function () {
return (this._isSetTimeOut === false);
}
};