Skip to content

Commit cc6cec5

Browse files
committed
Added in forceSetTimeout fps value (phaserjs#4179)
1 parent 360640e commit cc6cec5

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* `Tilemap.getImageLayerNames` is a new method that returns a list of all valid imagelayer names loaded in the Tilemap (thanks @Olliebrown)
1212
* `Tilemap.getObjectLayerNames` is a new method that returns a list of all valid objectgroup names loaded in the Tilemap (thanks @Olliebrown)
1313
* `Tilemap.getTileLayerNames` is a new method that returns a list of all valid tilelayer names loaded in the Tilemap (thanks @Olliebrown)
14+
* When `forceSetTimeOut` is set to `true` in the Game Config, you can now set the target frame rate by setting the `fps.target` value (thanks @pavels)
1415

1516
### Updates
1617

src/core/TimeStep.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ var TimeStep = new Class({
447447

448448
this.callback = callback;
449449

450-
this.raf.start(this.step.bind(this), this.forceSetTimeOut);
450+
this.raf.start(this.step.bind(this), this.forceSetTimeOut, this._target);
451451
},
452452

453453
/**

src/dom/RequestAnimationFrame.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ var RequestAnimationFrame = new Class({
8282
*/
8383
this.lastTime = 0;
8484

85+
/**
86+
* The target FPS rate in ms.
87+
* Only used when setTimeout is used instead of RAF.
88+
*
89+
* @name Phaser.DOM.RequestAnimationFrame#target
90+
* @type {number}
91+
* @default 0
92+
* @since 3.21.0
93+
*/
94+
this.target = 0;
95+
8596
var _this = this;
8697

8798
/**
@@ -119,7 +130,7 @@ var RequestAnimationFrame = new Class({
119130
{
120131
var d = Date.now();
121132

122-
var delay = Math.max(16 + _this.lastTime - d, 0);
133+
var delay = Math.min(Math.max(_this.target * 2 + _this.tick - d, 0), _this.target);
123134

124135
_this.lastTime = _this.tick;
125136

@@ -139,8 +150,9 @@ var RequestAnimationFrame = new Class({
139150
*
140151
* @param {FrameRequestCallback} callback - The callback to invoke each step.
141152
* @param {boolean} forceSetTimeOut - Should it use SetTimeout, even if RAF is available?
153+
* @param {number} targetFPS - The target fps rate (in ms). Only used when setTimeout is used.
142154
*/
143-
start: function (callback, forceSetTimeOut)
155+
start: function (callback, forceSetTimeOut, targetFPS)
144156
{
145157
if (this.isRunning)
146158
{
@@ -151,6 +163,8 @@ var RequestAnimationFrame = new Class({
151163

152164
this.isSetTimeOut = forceSetTimeOut;
153165

166+
this.target = targetFPS;
167+
154168
this.isRunning = true;
155169

156170
this.timeOutID = (forceSetTimeOut) ? window.setTimeout(this.stepTimeout, 0) : window.requestAnimationFrame(this.step);

0 commit comments

Comments
 (0)