Timer.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. <h1>[name]</h1>
  11. <p class="desc">
  12. This class is an alternative to [page:Clock] with a different API design and behavior.
  13. The goal is to avoid the conceptual flaws that became apparent in [page:Clock] over time.
  14. <ul>
  15. <li>[name] has an [page:.update]() method that updates its internal state. That makes it possible to call [page:.getDelta]() and [page:.getElapsed]() multiple times per simulation step without getting different values.</li>
  16. <li>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).</li>
  17. <li>It's possible to configure a fixed time delta and a time scale value (similar to Unity's Time interface).</li>
  18. </ul>
  19. </p>
  20. <h2>Import</h2>
  21. <p>
  22. [name] is an add-on, and must be imported explicitly.
  23. See [link:#manual/introduction/Installation Installation / Addons].
  24. </p>
  25. <code>
  26. import { Timer } from 'three/addons/misc/Timer.js';
  27. </code>
  28. <h2>Code Example</h2>
  29. <code>
  30. const timer = new Timer();
  31. function animate( timestamp ) {
  32. requestAnimationFrame( animate );
  33. // timestamp is optional
  34. timer.update( timestamp );
  35. const delta = timer.getDelta();
  36. // do something with delta
  37. renderer.render( scene, camera );
  38. }
  39. </code>
  40. <h2>Examples</h2>
  41. <p>
  42. [example:webgl_morphtargets_sphere WebGL / morphtargets / sphere]
  43. </p>
  44. <h2>Constructor</h2>
  45. <h3>Timer()</h3>
  46. <h2>Methods</h2>
  47. <h3>[method:this disableFixedDelta]()</h3>
  48. <p>
  49. Disables the usage of a fixed delta time.
  50. </p>
  51. <h3>[method:this dispose]()</h3>
  52. <p>
  53. Can be used to free all internal resources. Usually called when the timer instance isn't required anymore.
  54. </p>
  55. <h3>[method:this enableFixedDelta]()</h3>
  56. <p>
  57. Enables the usage of a fixed delta time.
  58. </p>
  59. <h3>[method:Number getDelta]()</h3>
  60. <p>
  61. Returns the time delta in seconds.
  62. </p>
  63. <h3>[method:Number getElapsed]()</h3>
  64. <p>
  65. Returns the elapsed time in seconds.
  66. </p>
  67. <h3>[method:Number getFixedDelta]()</h3>
  68. <p>
  69. Returns the fixed time delta that has been previously configured via [page:.setFixedDelta]().
  70. </p>
  71. <h3>[method:this reset]()</h3>
  72. <p>
  73. Resets the time computation for the current simulation step.
  74. </p>
  75. <h3>[method:this setFixedDelta]( [param:Number delta] )</h3>
  76. <p>
  77. Defines a fixed time delta value which is used to update the timer per simulation step.
  78. </p>
  79. <h3>[method:this setTimescale]( [param:Number timescale] )</h3>
  80. <p>
  81. Sets a time scale that scales the time delta in [page:.update]().
  82. </p>
  83. <h3>[method:this update]( [param:Number timestamp] )</h3>
  84. <p>
  85. timestamp -- (optional) The current time in milliseconds. Can be obtained from the
  86. [link:https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame requestAnimationFrame]
  87. callback argument. If not provided, the current time will be determined with
  88. [link:https://developer.mozilla.org/en-US/docs/Web/API/Performance/now performance.now]. Please note that this
  89. parameter has no effect when using a fixed time delta.<br /><br />
  90. Updates the internal state of the timer. This method should be called once per simulation step
  91. and before you perform queries against the timer (e.g. via [page:.getDelta]()).
  92. </p>
  93. <h2>Source</h2>
  94. <p>
  95. [link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/misc/Timer.js examples/jsm/misc/Timer.js]
  96. </p>
  97. </body>
  98. </html>