@canvas3/nuxt 0.1.31 → 0.1.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "canvas-nuxt-layout",
3
3
  "configKey": "canvas3Layout",
4
- "version": "0.1.31",
4
+ "version": "0.1.32",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.0"
@@ -16,6 +16,8 @@ declare class Canvas3Class {
16
16
  canvasContainer: HTMLElement | null;
17
17
  scrollableContent: HTMLElement | null;
18
18
  time: number;
19
+ rafId: number | null;
20
+ lastTime: number;
19
21
  scene: THREE.Scene;
20
22
  materials: ShaderMaterial[];
21
23
  imageStore: MeshType[];
@@ -65,7 +67,9 @@ declare class Canvas3Class {
65
67
  setAnimationsToRender(state: boolean): void;
66
68
  setAnimationToRender(callBackName: string, state: boolean, animationId: string): void;
67
69
  externalAnimationsRender(): void;
68
- render(): void;
70
+ startRenderLoop(): void;
71
+ stopRenderLoop(): void;
72
+ render(delta: number): void;
69
73
  }
70
74
  export declare const Canvas3: Canvas3Class;
71
75
  export {};
@@ -24,6 +24,8 @@ class Canvas3Class {
24
24
  canvasContainer = null;
25
25
  scrollableContent = null;
26
26
  time = 0;
27
+ rafId = null;
28
+ lastTime = 0;
27
29
  scene = new THREE.Scene();
28
30
  // MSDFTextGeometryAtlas: any
29
31
  // MSDFTextGeometryFont: any
@@ -63,7 +65,7 @@ class Canvas3Class {
63
65
  this.setCanvasAndCamera();
64
66
  this.setSize();
65
67
  this.composerPass();
66
- this.render();
68
+ this.startRenderLoop();
67
69
  window.addEventListener("mousemove", (event) => {
68
70
  this.mouseMoveInProgress = true;
69
71
  this.mouse.x = event.clientX / this.width;
@@ -83,9 +85,10 @@ class Canvas3Class {
83
85
  document.addEventListener("visibilitychange", () => {
84
86
  if (document.hidden) {
85
87
  this.documentVisibility = false;
88
+ this.stopRenderLoop();
86
89
  } else {
87
90
  this.documentVisibility = true;
88
- this.render();
91
+ this.startRenderLoop();
89
92
  }
90
93
  });
91
94
  this.canvasInitiated.value = true;
@@ -94,9 +97,11 @@ class Canvas3Class {
94
97
  setRenderDisabled(status) {
95
98
  if (!status && this.renderDisabled) {
96
99
  this.renderDisabled = false;
97
- this.render();
100
+ this.startRenderLoop();
101
+ } else if (status && !this.renderDisabled) {
102
+ this.renderDisabled = true;
103
+ this.stopRenderLoop();
98
104
  }
99
- this.renderDisabled = status;
100
105
  }
101
106
  setCanvasAndCamera() {
102
107
  this.width = this.canvasContainer?.offsetWidth ?? 0;
@@ -436,8 +441,28 @@ class Canvas3Class {
436
441
  }
437
442
  }
438
443
  }
439
- render() {
440
- this.time += 0.05;
444
+ startRenderLoop() {
445
+ if (this.rafId !== null) return;
446
+ const loop = (timestamp) => {
447
+ this.rafId = null;
448
+ if (!this.documentVisibility || this.renderDisabled) return;
449
+ if (!this.lastTime) this.lastTime = timestamp;
450
+ const delta = (timestamp - this.lastTime) / 1e3;
451
+ this.lastTime = timestamp;
452
+ this.render(delta);
453
+ this.rafId = requestAnimationFrame(loop);
454
+ };
455
+ this.rafId = requestAnimationFrame(loop);
456
+ }
457
+ stopRenderLoop() {
458
+ if (this.rafId !== null) {
459
+ cancelAnimationFrame(this.rafId);
460
+ this.rafId = null;
461
+ }
462
+ this.lastTime = 0;
463
+ }
464
+ render(delta) {
465
+ this.time += delta;
441
466
  this.scroll?.render(null, null);
442
467
  if (this.scroll?.scrollInProgress) {
443
468
  if (this.customPass && this.customPass.uniforms.scrollSpeed)
@@ -472,14 +497,6 @@ class Canvas3Class {
472
497
  } else if (this.renderer && this.camera) {
473
498
  this.renderer.render(this.scene, this.camera);
474
499
  }
475
- try {
476
- if (!this.documentVisibility) return;
477
- if (this.renderDisabled) return;
478
- requestAnimationFrame(this.render.bind(this));
479
- } catch (e) {
480
- console.error(e);
481
- setImmediate(this.render.bind(this));
482
- }
483
500
  }
484
501
  }
485
502
  export const Canvas3 = new Canvas3Class();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canvas3/nuxt",
3
- "version": "0.1.31",
3
+ "version": "0.1.32",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },