@@ -15,14 +15,26 @@ var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
1515// target: 60,
1616// forceSetTimeOut: false,
1717// deltaHistory: 10,
18- // panicMax: 120
18+ // panicMax: 120,
19+ // smoothStep: true
1920// }
2021
2122// http://www.testufo.com/#test=animation-time-graph
2223
2324/**
2425 * @classdesc
25- * [description]
26+ * The core runner class that Phaser uses to handle the game loop. It can use either Request Animation Frame,
27+ * or SetTimeout, based on browser support and config settings, to create a continuous loop within the browser.
28+ *
29+ * Each time the loop fires, `TimeStep.step` is called and this is then passed onto the core Game update loop,
30+ * it is the core heartbeat of your game. It will fire as often as Request Animation Frame is capable of handling
31+ * on the target device.
32+ *
33+ * Note that there are lots of situations where a browser will stop updating your game. Such as if the player
34+ * switches tabs, or covers up the browser window with another application. In these cases, the 'heartbeat'
35+ * of your game will pause, and only resume when focus is returned to it by the player. There is no way to avoid
36+ * this situation, all you can do is use the visibility events the browser, and Phaser, provide to detect when
37+ * it has happened and then gracefully recover.
2638 *
2739 * @class TimeStep
2840 * @memberof Phaser.Core
@@ -49,7 +61,7 @@ var TimeStep = new Class({
4961 this . game = game ;
5062
5163 /**
52- * [description]
64+ * The Request Animation Frame DOM Event handler.
5365 *
5466 * @name Phaser.Core.TimeStep#raf
5567 * @type {Phaser.DOM.RequestAnimationFrame }
@@ -141,7 +153,8 @@ var TimeStep = new Class({
141153 this . actualFps = this . targetFps ;
142154
143155 /**
144- * [description]
156+ * The time at which the next fps rate update will take place.
157+ * When an fps update happens, the `framesThisSecond` value is reset.
145158 *
146159 * @name Phaser.Core.TimeStep#nextFpsUpdate
147160 * @type {integer }
@@ -338,6 +351,22 @@ var TimeStep = new Class({
338351 * @since 3.18.0
339352 */
340353 this . now = 0 ;
354+
355+ /**
356+ * Apply smoothing to the delta value used within Phasers internal calculations?
357+ *
358+ * This can be changed in the Game Config via the `fps.smoothStep` property. The default is `true`.
359+ *
360+ * Smoothing helps settle down the delta values after browser tab switches, or other situations
361+ * which could cause significant delta spikes or dips. By default it has been enabled in Phaser 3
362+ * since the first version, but is now exposed under this property (and the corresponding game config
363+ * `smoothStep` value), to allow you to easily disable it, should you require.
364+ *
365+ * @name Phaser.Core.TimeStep#smoothStep
366+ * @type {boolean }
367+ * @since 3.22.0
368+ */
369+ this . smoothStep = GetValue ( config , 'smoothStep' , true ) ;
341370 } ,
342371
343372 /**
@@ -461,7 +490,7 @@ var TimeStep = new Class({
461490 step : function ( )
462491 {
463492 // Because the timestamp passed in from raf represents the beginning of the main thread frame that we’re currently in,
464- // not the actual time now. As we want to compare this time value against Event timeStamps and the like, we need a
493+ // not the actual time now, and as we want to compare this time value against Event timeStamps and the like, we need a
465494 // more accurate one:
466495
467496 var time = window . performance . now ( ) ;
@@ -485,55 +514,59 @@ var TimeStep = new Class({
485514 // delta time (time is in ms)
486515 var dt = before ;
487516
517+ // Delta Average
518+ var avg = before ;
519+
488520 // When a browser switches tab, then comes back again, it takes around 10 frames before
489521 // the delta time settles down so we employ a 'cooling down' period before we start
490522 // trusting the delta values again, to avoid spikes flooding through our delta average
491523
492- if ( this . _coolDown > 0 || ! this . inFocus )
493- {
494- this . _coolDown -- ;
495-
496- dt = Math . min ( dt , this . _target ) ;
497- }
498-
499- if ( dt > this . _min )
500- {
501- // Probably super bad start time or browser tab context loss,
502- // so use the last 'sane' dt value
503-
504- dt = history [ idx ] ;
505-
506- // Clamp delta to min (in case history has become corrupted somehow)
507- dt = Math . min ( dt , this . _min ) ;
508- }
509-
510- // Smooth out the delta over the previous X frames
511-
512- // add the delta to the smoothing array
513- history [ idx ] = dt ;
514-
515- // adjusts the delta history array index based on the smoothing count
516- // this stops the array growing beyond the size of deltaSmoothingMax
517- this . deltaIndex ++ ;
518-
519- if ( this . deltaIndex > max )
524+ if ( this . smoothStep )
520525 {
521- this . deltaIndex = 0 ;
526+ if ( this . _coolDown > 0 || ! this . inFocus )
527+ {
528+ this . _coolDown -- ;
529+
530+ dt = Math . min ( dt , this . _target ) ;
531+ }
532+
533+ if ( dt > this . _min )
534+ {
535+ // Probably super bad start time or browser tab context loss,
536+ // so use the last 'sane' dt value
537+
538+ dt = history [ idx ] ;
539+
540+ // Clamp delta to min (in case history has become corrupted somehow)
541+ dt = Math . min ( dt , this . _min ) ;
542+ }
543+
544+ // Smooth out the delta over the previous X frames
545+
546+ // add the delta to the smoothing array
547+ history [ idx ] = dt ;
548+
549+ // adjusts the delta history array index based on the smoothing count
550+ // this stops the array growing beyond the size of deltaSmoothingMax
551+ this . deltaIndex ++ ;
552+
553+ if ( this . deltaIndex > max )
554+ {
555+ this . deltaIndex = 0 ;
556+ }
557+
558+ // Loop the history array, adding the delta values together
559+ avg = 0 ;
560+
561+ for ( var i = 0 ; i < max ; i ++ )
562+ {
563+ avg += history [ i ] ;
564+ }
565+
566+ // Then divide by the array length to get the average delta
567+ avg /= max ;
522568 }
523569
524- // Delta Average
525- var avg = 0 ;
526-
527- // Loop the history array, adding the delta values together
528-
529- for ( var i = 0 ; i < max ; i ++ )
530- {
531- avg += history [ i ] ;
532- }
533-
534- // Then divide by the array length to get the average delta
535- avg /= max ;
536-
537570 // Set as the world delta value
538571 this . delta = avg ;
539572
@@ -667,7 +700,7 @@ var TimeStep = new Class({
667700 * @method Phaser.Core.TimeStep#stop
668701 * @since 3.0.0
669702 *
670- * @return {Phaser.Core.TimeStep } The TimeStep object.
703+ * @return {this } The TimeStep object.
671704 */
672705 stop : function ( )
673706 {
0 commit comments