Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

time.ts 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * MARTINS.js
  3. * GPU-accelerated Augmented Reality for the web
  4. * Copyright (C) 2022 Alexandre Martins <alemartf(at)gmail.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * time.ts
  20. * Time utilities
  21. */
  22. /**
  23. * Time Manager
  24. */
  25. export class Time
  26. {
  27. /** time scale */
  28. private _scale: number = 1;
  29. /** time since the start of the session, in milliseconds */
  30. private _time: DOMHighResTimeStamp = 0;
  31. /** unscaled time since the start of the session, in milliseconds */
  32. private _unscaledTime: DOMHighResTimeStamp = 0;
  33. /** elapsed time between the current and the previous frame, in milliseconds */
  34. private _delta: DOMHighResTimeStamp = 0;
  35. /** time of the first update call, in milliseconds */
  36. private _firstUpdate: DOMHighResTimeStamp = 0;
  37. /** time of the last update call, in milliseconds */
  38. private _lastUpdate: DOMHighResTimeStamp = Number.POSITIVE_INFINITY;
  39. /**
  40. * Update the Time Manager
  41. * @param timestamp in milliseconds
  42. * @internal
  43. */
  44. _update(timestamp: DOMHighResTimeStamp): void
  45. {
  46. if(timestamp < this._lastUpdate) {
  47. this._firstUpdate = this._lastUpdate = timestamp;
  48. return;
  49. }
  50. this._delta = (timestamp - this._lastUpdate) * this._scale;
  51. this._time += this._delta;
  52. this._unscaledTime = timestamp - this._firstUpdate;
  53. this._lastUpdate = timestamp;
  54. }
  55. /**
  56. * Elapsed time since the start of the session, measured at the
  57. * beginning of the current animation frame and given in seconds
  58. */
  59. get elapsed(): number
  60. {
  61. return this._time * 0.001;
  62. }
  63. /**
  64. * Elapsed time between the current and the previous animation
  65. * frame, given in seconds
  66. */
  67. get delta(): number
  68. {
  69. return this._delta * 0.001;
  70. }
  71. /**
  72. * Time scale (defaults to 1)
  73. */
  74. get scale(): number
  75. {
  76. return this._scale;
  77. }
  78. /**
  79. * Time scale (defaults to 1)
  80. */
  81. set scale(scale: number)
  82. {
  83. this._scale = Math.max(0, +scale);
  84. }
  85. /**
  86. * Time scale independent elapsed time since the start of the session,
  87. * measured at the beginning of the current animation frame and given
  88. * in seconds
  89. */
  90. get unscaled(): number
  91. {
  92. return this._unscaledTime * 0.001;
  93. }
  94. }