Animation.js 2.8 KB

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