Animation.js 790 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. class Animation {
  2. constructor() {
  3. this.nodes = null;
  4. this.animationLoop = null;
  5. this.requestId = null;
  6. this.isAnimating = false;
  7. this.context = self;
  8. }
  9. start() {
  10. if ( this.isAnimating === true || this.animationLoop === null || this.nodes === null ) return;
  11. this.isAnimating = true;
  12. const update = ( time, frame ) => {
  13. this.requestId = self.requestAnimationFrame( update );
  14. this.nodes.nodeFrame.update();
  15. this.animationLoop( time, frame );
  16. };
  17. this.requestId = self.requestAnimationFrame( update );
  18. }
  19. stop() {
  20. self.cancelAnimationFrame( this.requestId );
  21. this.isAnimating = false;
  22. }
  23. setAnimationLoop( callback ) {
  24. this.animationLoop = callback;
  25. }
  26. setNodes( nodes ) {
  27. this.nodes = nodes;
  28. }
  29. }
  30. export default Animation;