Timer.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * This class is a new alternative to THREE.Clock with a different API design and behavior.
  3. * The goal is to avoid the conceptual flaws that became apparent in THREE.Clock over time.
  4. *
  5. * - THREE.Timer has an update() method that updates its internal state. That makes it possible to call .getDeltaTime() and
  6. * .getElapsedTime() multiple times per simulation step without getting different values.
  7. * - The class uses the Page Visibility API to avoid large time delta values when the app is inactive (e.g. tab switched or browser hidden).
  8. * - It's possible to configure a fixed time delta and a time scale value (similar to Unity's Time interface).
  9. */
  10. class Timer {
  11. constructor() {
  12. this._previousTime = 0;
  13. this._currentTime = 0;
  14. this._startTime = now();
  15. this._delta = 0;
  16. this._elapsed = 0;
  17. this._timescale = 1;
  18. this._useFixedDelta = false;
  19. this._fixedDelta = 16.67; // ms, corresponds to approx. 60 FPS
  20. // use Page Visibility API to avoid large time delta values
  21. this._usePageVisibilityAPI = ( typeof document !== 'undefined' && document.hidden !== undefined );
  22. if ( this._usePageVisibilityAPI === true ) {
  23. this._pageVisibilityHandler = handleVisibilityChange.bind( this );
  24. document.addEventListener( 'visibilitychange', this._pageVisibilityHandler, false );
  25. }
  26. }
  27. disableFixedDelta() {
  28. this._useFixedDelta = false;
  29. return this;
  30. }
  31. dispose() {
  32. if ( this._usePageVisibilityAPI === true ) {
  33. document.removeEventListener( 'visibilitychange', this._pageVisibilityHandler );
  34. }
  35. return this;
  36. }
  37. enableFixedDelta() {
  38. this._useFixedDelta = true;
  39. return this;
  40. }
  41. getDelta() {
  42. return this._delta / 1000;
  43. }
  44. getElapsed() {
  45. return this._elapsed / 1000;
  46. }
  47. getFixedDelta() {
  48. return this._fixedDelta / 1000;
  49. }
  50. getTimescale() {
  51. return this._timescale;
  52. }
  53. reset() {
  54. this._currentTime = now() - this._startTime;
  55. return this;
  56. }
  57. setFixedDelta( fixedDelta ) {
  58. this._fixedDelta = fixedDelta * 1000;
  59. return this;
  60. }
  61. setTimescale( timescale ) {
  62. this._timescale = timescale;
  63. return this;
  64. }
  65. update() {
  66. if ( this._useFixedDelta === true ) {
  67. this._delta = this._fixedDelta;
  68. } else {
  69. this._previousTime = this._currentTime;
  70. this._currentTime = now() - this._startTime;
  71. this._delta = this._currentTime - this._previousTime;
  72. }
  73. this._delta *= this._timescale;
  74. this._elapsed += this._delta; // _elapsed is the accumulation of all previous deltas
  75. return this;
  76. }
  77. }
  78. function now() {
  79. return ( typeof performance === 'undefined' ? Date : performance ).now();
  80. }
  81. function handleVisibilityChange() {
  82. if ( document.hidden === false ) this.reset();
  83. }
  84. export { Timer };