animation-loop.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { PluginContext } from './context';
  7. import { now } from '../mol-util/now';
  8. export class PluginAnimationLoop {
  9. private currentFrame: any = void 0;
  10. private _isAnimating = false;
  11. tick(t: number, isSynchronous?: boolean) {
  12. this.plugin.canvas3d?.tick(t as now.Timestamp, isSynchronous);
  13. return this.plugin.managers.animation.tick(t, isSynchronous);
  14. }
  15. private frame = () => {
  16. this.tick(now(), false);
  17. if (this._isAnimating) {
  18. this.currentFrame = requestAnimationFrame(this.frame);
  19. }
  20. }
  21. resetTime(t: number = now()) {
  22. this.plugin.canvas3d?.resetTime(t);
  23. }
  24. start() {
  25. this._isAnimating = true;
  26. this.resetTime();
  27. this.currentFrame = requestAnimationFrame(this.frame);
  28. }
  29. stop() {
  30. this._isAnimating = false;
  31. if (this.currentFrame !== void 0) {
  32. cancelAnimationFrame(this.currentFrame);
  33. this.currentFrame = void 0;
  34. }
  35. }
  36. constructor(private plugin: PluginContext) {
  37. }
  38. }