|
@@ -11,9 +11,6 @@ class Timer {
|
|
|
|
|
|
this._timescale = 1;
|
|
|
|
|
|
- this._useFixedDelta = false;
|
|
|
- this._fixedDelta = 16.67; // ms, corresponds to approx. 60 FPS
|
|
|
-
|
|
|
// use Page Visibility API to avoid large time delta values
|
|
|
|
|
|
this._usePageVisibilityAPI = ( typeof document !== 'undefined' && document.hidden !== undefined );
|
|
@@ -28,34 +25,6 @@ class Timer {
|
|
|
|
|
|
}
|
|
|
|
|
|
- disableFixedDelta() {
|
|
|
-
|
|
|
- this._useFixedDelta = false;
|
|
|
-
|
|
|
- return this;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- dispose() {
|
|
|
-
|
|
|
- if ( this._usePageVisibilityAPI === true ) {
|
|
|
-
|
|
|
- document.removeEventListener( 'visibilitychange', this._pageVisibilityHandler );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- return this;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- enableFixedDelta() {
|
|
|
-
|
|
|
- this._useFixedDelta = true;
|
|
|
-
|
|
|
- return this;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
getDelta() {
|
|
|
|
|
|
return this._delta / 1000;
|
|
@@ -68,15 +37,17 @@ class Timer {
|
|
|
|
|
|
}
|
|
|
|
|
|
- getFixedDelta() {
|
|
|
+ getTimescale() {
|
|
|
|
|
|
- return this._fixedDelta / 1000;
|
|
|
+ return this._timescale;
|
|
|
|
|
|
}
|
|
|
|
|
|
- getTimescale() {
|
|
|
+ setTimescale( timescale ) {
|
|
|
|
|
|
- return this._timescale;
|
|
|
+ this._timescale = timescale;
|
|
|
+
|
|
|
+ return this;
|
|
|
|
|
|
}
|
|
|
|
|
@@ -88,40 +59,44 @@ class Timer {
|
|
|
|
|
|
}
|
|
|
|
|
|
- setFixedDelta( fixedDelta ) {
|
|
|
+ dispose() {
|
|
|
+
|
|
|
+ if ( this._usePageVisibilityAPI === true ) {
|
|
|
|
|
|
- this._fixedDelta = fixedDelta * 1000;
|
|
|
+ document.removeEventListener( 'visibilitychange', this._pageVisibilityHandler );
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
- setTimescale( timescale ) {
|
|
|
+ update( timestamp ) {
|
|
|
|
|
|
- this._timescale = timescale;
|
|
|
+ this._previousTime = this._currentTime;
|
|
|
+ this._currentTime = ( timestamp !== undefined ? timestamp : now() ) - this._startTime;
|
|
|
+
|
|
|
+ this._delta = ( this._currentTime - this._previousTime ) * this._timescale;
|
|
|
+ this._elapsed += this._delta; // _elapsed is the accumulation of all previous deltas
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
- update( timestamp ) {
|
|
|
-
|
|
|
- if ( this._useFixedDelta === true ) {
|
|
|
-
|
|
|
- this._delta = this._fixedDelta;
|
|
|
+}
|
|
|
|
|
|
- } else {
|
|
|
+class FixedTimer extends Timer {
|
|
|
|
|
|
- this._previousTime = this._currentTime;
|
|
|
- this._currentTime = ( timestamp !== undefined ? timestamp : now() ) - this._startTime;
|
|
|
+ constructor( fps = 60 ) {
|
|
|
|
|
|
- this._delta = this._currentTime - this._previousTime;
|
|
|
+ super();
|
|
|
+ this._delta = ( 1 / fps ) * 1000;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- this._delta *= this._timescale;
|
|
|
+ update() {
|
|
|
|
|
|
- this._elapsed += this._delta; // _elapsed is the accumulation of all previous deltas
|
|
|
+ this._elapsed += ( this._delta * this._timescale ); // _elapsed is the accumulation of all previous deltas
|
|
|
|
|
|
return this;
|
|
|
|
|
@@ -141,4 +116,4 @@ function handleVisibilityChange() {
|
|
|
|
|
|
}
|
|
|
|
|
|
-export { Timer };
|
|
|
+export { Timer, FixedTimer };
|