Animation.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * This module manages the internal animation loop of the renderer.
  3. *
  4. * @private
  5. */
  6. class Animation {
  7. /**
  8. * Constructs a new animation loop management component.
  9. *
  10. * @param {Nodes} nodes - Renderer component for managing nodes related logic.
  11. * @param {Info} info - Renderer component for managing metrics and monitoring data.
  12. */
  13. constructor( nodes, info ) {
  14. /**
  15. * Renderer component for managing nodes related logic.
  16. *
  17. * @type {Nodes}
  18. */
  19. this.nodes = nodes;
  20. /**
  21. * Renderer component for managing metrics and monitoring data.
  22. *
  23. * @type {Info}
  24. */
  25. this.info = info;
  26. /**
  27. * A reference to the context from `requestAnimationFrame()` can
  28. * be called (usually `window`).
  29. *
  30. * @type {?(Window|XRSession)}
  31. */
  32. this._context = typeof self !== 'undefined' ? self : null;
  33. /**
  34. * The user-defined animation loop.
  35. *
  36. * @type {?Function}
  37. * @default null
  38. */
  39. this._animationLoop = null;
  40. /**
  41. * The requestId which is returned from the `requestAnimationFrame()` call.
  42. * Can be used to cancel the stop the animation loop.
  43. *
  44. * @type {?number}
  45. * @default null
  46. */
  47. this._requestId = null;
  48. }
  49. /**
  50. * Starts the internal animation loop.
  51. */
  52. start() {
  53. const update = ( time, xrFrame ) => {
  54. this._requestId = this._context.requestAnimationFrame( update );
  55. if ( this.info.autoReset === true ) this.info.reset();
  56. this.nodes.nodeFrame.update();
  57. this.info.frame = this.nodes.nodeFrame.frameId;
  58. if ( this._animationLoop !== null ) this._animationLoop( time, xrFrame );
  59. };
  60. update();
  61. }
  62. /**
  63. * Stops the internal animation loop.
  64. */
  65. stop() {
  66. this._context.cancelAnimationFrame( this._requestId );
  67. this._requestId = null;
  68. }
  69. /**
  70. * Returns the user-level animation loop.
  71. *
  72. * @return {?Function} The animation loop.
  73. */
  74. getAnimationLoop() {
  75. return this._animationLoop;
  76. }
  77. /**
  78. * Defines the user-level animation loop.
  79. *
  80. * @param {?Function} callback - The animation loop.
  81. */
  82. setAnimationLoop( callback ) {
  83. this._animationLoop = callback;
  84. }
  85. /**
  86. * Returns the animation context.
  87. *
  88. * @return {Window|XRSession} The animation context.
  89. */
  90. getContext() {
  91. return this._context;
  92. }
  93. /**
  94. * Defines the context in which `requestAnimationFrame()` is executed.
  95. *
  96. * @param {Window|XRSession} context - The context to set.
  97. */
  98. setContext( context ) {
  99. this._context = context;
  100. }
  101. /**
  102. * Frees all internal resources and stops the animation loop.
  103. */
  104. dispose() {
  105. this.stop();
  106. }
  107. }
  108. export default Animation;
粤ICP备19079148号