animation-loop.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. get isAnimating() {
  12. return this._isAnimating;
  13. }
  14. async tick(t: number, options?: { isSynchronous?: boolean, manualDraw?: boolean }) {
  15. await this.plugin.managers.animation.tick(t, options?.isSynchronous);
  16. this.plugin.canvas3d?.tick(t as now.Timestamp, options);
  17. }
  18. private frame = () => {
  19. this.tick(now());
  20. if (this._isAnimating) {
  21. this.currentFrame = requestAnimationFrame(this.frame);
  22. }
  23. }
  24. resetTime(t: number = now()) {
  25. this.plugin.canvas3d?.resetTime(t);
  26. }
  27. start() {
  28. this._isAnimating = true;
  29. this.resetTime();
  30. this.currentFrame = requestAnimationFrame(this.frame);
  31. }
  32. stop() {
  33. this._isAnimating = false;
  34. if (this.currentFrame !== void 0) {
  35. cancelAnimationFrame(this.currentFrame);
  36. this.currentFrame = void 0;
  37. }
  38. }
  39. constructor(private plugin: PluginContext) {
  40. }
  41. }