@@ -11,7 +11,7 @@ var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
1111// deltaHistory: 10
1212// }
1313
14- var VariableTimeStep = function ( game , config )
14+ var TimeStep = function ( game , config )
1515{
1616 this . game = game ;
1717
@@ -32,6 +32,15 @@ var VariableTimeStep = function (game, config)
3232 // 8.333 / 1000 = 0.008333 (120fps)
3333 // 16.666 / 1000 = 0.01666 (60fps)
3434
35+ /**
36+ * @property {number } fps - An exponential moving average of the frames per second.
37+ * @readOnly
38+ */
39+ this . actualFps = this . targetFps ;
40+
41+ this . nextFpsUpdate = 0 ;
42+ this . framesThisSecond = 0 ;
43+
3544 this . callback = NOOP ;
3645
3746 this . forceSetTimeOut = GetValue ( config , 'forceSetTimeOut' , false ) ;
@@ -46,9 +55,9 @@ var VariableTimeStep = function (game, config)
4655 this . deltaSmoothingMax = GetValue ( config , 'deltaHistory' , 10 ) ;
4756} ;
4857
49- VariableTimeStep . prototype . constructor = VariableTimeStep ;
58+ TimeStep . prototype . constructor = TimeStep ;
5059
51- VariableTimeStep . prototype = {
60+ TimeStep . prototype = {
5261
5362 start : function ( callback )
5463 {
@@ -65,7 +74,8 @@ VariableTimeStep.prototype = {
6574 this . time = now ;
6675 this . startTime = now ;
6776 this . lastTime = now ;
68- this . runOff = 0 ;
77+ this . nextFpsUpdate = now + 1000 ;
78+ this . framesThisSecond = 0 ;
6979
7080 // Pre-populate smoothing array
7181
@@ -137,7 +147,26 @@ VariableTimeStep.prototype = {
137147 // Real-world timer advance
138148 this . time += avg ;
139149
140- this . callback ( this . time , avg ) ;
150+ // Update the estimate of the frame rate, `fps`. Every second, the number
151+ // of frames that occurred in that second are included in an exponential
152+ // moving average of all frames per second, with an alpha of 0.25. This
153+ // means that more recent seconds affect the estimated frame rate more than
154+ // older seconds.
155+ if ( time > this . nextFpsUpdate )
156+ {
157+ // Compute the new exponential moving average with an alpha of 0.25.
158+ // Using constants inline is okay here.
159+ this . actualFps = 0.25 * this . framesThisSecond + 0.75 * this . actualFps ;
160+ this . nextFpsUpdate = time + 1000 ;
161+ this . framesThisSecond = 0 ;
162+ }
163+
164+ this . framesThisSecond ++ ;
165+
166+ // Interpolation - how far between what is expected and where we are?
167+ var interpolation = avg / this . _target ;
168+
169+ this . callback ( this . time , avg , interpolation ) ;
141170
142171 // Shift time value over
143172 this . lastTime = time ;
@@ -197,7 +226,6 @@ VariableTimeStep.prototype = {
197226
198227 return this ;
199228 }
200-
201229} ;
202230
203- module . exports = VariableTimeStep ;
231+ module . exports = TimeStep ;
0 commit comments