@canvas3/nuxt 0.1.11 → 0.1.13

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.11",
4
+ "version": "0.1.13",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.0"
@@ -29,19 +29,26 @@ const reducedMotion = computed(() => Canvas3.options?.disabled ?? null);
29
29
  const image = ref();
30
30
  const generatedMeshId = "image_" + crypto.randomUUID();
31
31
  const imgAddedToCanvas = ref(false);
32
+ const imgIsAdding = ref(false);
32
33
  const addImageToCanvas = async () => {
33
- await nextTick();
34
- if (!image.value || Canvas3.canvasInitiated.value === false || Canvas3.options?.disabled || imgAddedToCanvas.value) return;
35
- if (image.value.naturalWidth === 0) return;
36
- image.value.dataset.meshId = generatedMeshId;
37
- await Canvas3.addImageAsMesh(
38
- image.value,
39
- props.options.shaderName ?? null,
40
- generatedMeshId,
41
- props.options.uniforms ?? {},
42
- props.options.activateMeshUniforms ?? {}
43
- );
44
- imgAddedToCanvas.value = true;
34
+ if (imgAddedToCanvas.value || imgIsAdding.value) return;
35
+ imgIsAdding.value = true;
36
+ try {
37
+ await nextTick();
38
+ if (!image.value || Canvas3.canvasInitiated.value === false || Canvas3.options?.disabled || imgAddedToCanvas.value) return;
39
+ if (image.value.naturalWidth === 0) return;
40
+ image.value.dataset.meshId = generatedMeshId;
41
+ await Canvas3.addImageAsMesh(
42
+ image.value,
43
+ props.options.shaderName ?? null,
44
+ generatedMeshId,
45
+ props.options.uniforms ?? {},
46
+ props.options.activateMeshUniforms ?? {}
47
+ );
48
+ imgAddedToCanvas.value = true;
49
+ } finally {
50
+ imgIsAdding.value = false;
51
+ }
45
52
  };
46
53
  watch(() => Canvas3.canvasInitiated.value, (ready) => {
47
54
  if (ready) addImageToCanvas();
@@ -2,38 +2,48 @@ import { watch, nextTick } from "vue";
2
2
  import { Canvas3 } from "#canvas3-nuxt/utils/canvas3/canvas3";
3
3
  const meshIdMap = /* @__PURE__ */ new WeakMap();
4
4
  const addedMap = /* @__PURE__ */ new WeakMap();
5
+ const loadingSet = /* @__PURE__ */ new WeakSet();
5
6
  const stopMap = /* @__PURE__ */ new WeakMap();
6
7
  const srcMap = /* @__PURE__ */ new WeakMap();
7
8
  function getImgSrc(el) {
8
9
  return el.currentSrc || el.src || "";
9
10
  }
10
11
  async function tryAdd(el, options) {
11
- if (addedMap.get(el)) return;
12
- await nextTick();
13
- if (!el.isConnected) return;
14
- if (Canvas3.options?.disabled) {
15
- el.classList.add("reduced-motion");
16
- return;
17
- }
18
- if (Canvas3.canvasInitiated.value === false) return;
19
- if (!el.complete || el.naturalWidth === 0) return;
20
- let meshId = meshIdMap.get(el);
21
- if (!meshId) {
22
- meshId = "image_" + crypto.randomUUID();
23
- meshIdMap.set(el, meshId);
24
- el.dataset.meshId = meshId;
12
+ if (addedMap.get(el) || loadingSet.has(el)) return;
13
+ loadingSet.add(el);
14
+ try {
15
+ await nextTick();
16
+ if (!el.isConnected) return;
17
+ if (Canvas3.options?.disabled) {
18
+ el.classList.add("reduced-motion");
19
+ return;
20
+ }
21
+ if (Canvas3.canvasInitiated.value === false) return;
22
+ if (!el.complete || el.naturalWidth === 0) return;
23
+ let meshId = meshIdMap.get(el);
24
+ if (!meshId) {
25
+ meshId = "image_" + crypto.randomUUID();
26
+ meshIdMap.set(el, meshId);
27
+ el.dataset.meshId = meshId;
28
+ }
29
+ await Canvas3.addImageAsMesh(
30
+ el,
31
+ options.shaderName ?? null,
32
+ meshId,
33
+ options?.uniforms ?? {},
34
+ options?.activateMeshUniforms ?? {}
35
+ );
36
+ if (!el.isConnected) {
37
+ Canvas3.removeMesh(meshId);
38
+ return;
39
+ }
40
+ addedMap.set(el, true);
41
+ srcMap.set(el, getImgSrc(el));
42
+ el.classList.add("webgl-img");
43
+ el.style.opacity = "0";
44
+ } finally {
45
+ loadingSet.delete(el);
25
46
  }
26
- await Canvas3.addImageAsMesh(
27
- el,
28
- options.shaderName ?? null,
29
- meshId,
30
- options?.uniforms ?? {},
31
- options?.activateMeshUniforms ?? {}
32
- );
33
- addedMap.set(el, true);
34
- srcMap.set(el, getImgSrc(el));
35
- el.classList.add("webgl-img");
36
- el.style.opacity = "0";
37
47
  }
38
48
  function remove(el) {
39
49
  const meshId = meshIdMap.get(el);
@@ -223,19 +223,34 @@ class Canvas3Class {
223
223
  }
224
224
  }
225
225
  removeMesh(id) {
226
- const meshToRemove = this.scene.getObjectByName(id);
227
- if (!meshToRemove) return;
228
- this.scene.remove(meshToRemove);
229
- meshToRemove.geometry.dispose();
230
- const material = getShaderMaterial(meshToRemove);
231
- material.dispose();
232
- for (const [index, item] of this.imageStore.entries()) {
226
+ const meshesToRemove = /* @__PURE__ */ new Set();
227
+ let meshInScene = this.scene.getObjectByName(id);
228
+ while (meshInScene) {
229
+ meshesToRemove.add(meshInScene);
230
+ this.scene.remove(meshInScene);
231
+ meshInScene = this.scene.getObjectByName(id);
232
+ }
233
+ for (const item of this.imageStore) {
233
234
  if (item.name === id) {
234
- this.imageStore.splice(index, 1);
235
- this.materials.splice(index, 1);
236
- break;
235
+ meshesToRemove.add(item.mesh);
236
+ }
237
+ }
238
+ for (const mesh of meshesToRemove) {
239
+ mesh.geometry?.dispose();
240
+ const material = getShaderMaterial(mesh);
241
+ if (material) {
242
+ const materialIndex = this.materials.indexOf(material);
243
+ if (materialIndex !== -1)
244
+ this.materials.splice(materialIndex, 1);
245
+ if (material.uniforms?.uImage?.value) {
246
+ material.uniforms.uImage.value.dispose();
247
+ }
248
+ material.dispose();
237
249
  }
250
+ this.scene.remove(mesh);
238
251
  }
252
+ this.activateMeshUniformMap.delete(id);
253
+ this.imageStore = this.imageStore.filter((item) => item.name !== id);
239
254
  }
240
255
  addAnimationToRender(callBackName, callBackFunction) {
241
256
  this.animations.set(callBackName, callBackFunction);
@@ -430,6 +445,7 @@ class Canvas3Class {
430
445
  this.height = this.canvasContainer?.offsetHeight ?? 0;
431
446
  if (this.camera) {
432
447
  this.camera.aspect = this.width / this.height;
448
+ this.camera.fov = 2 * Math.atan(this.height / 2 / 600) * (180 / Math.PI);
433
449
  this.camera.updateProjectionMatrix();
434
450
  }
435
451
  if (this.renderer) {
@@ -7,7 +7,7 @@ export declare const Canvas3: {
7
7
  removeAnimationFromRender: (callBackName: string) => void;
8
8
  addImageAsMesh: (imgHtmlEl: HTMLImageElement, shaderName: string | null, meshId: string, meshUniforms: import("../types/types.js").MeshMaterialUniform, activateMeshUniforms: import("../types/types.js").MeshMaterialUniform) => Promise<void>;
9
9
  getShaderMaterial: typeof getShaderMaterial;
10
- removeMesh: (imgHtmlEl: HTMLImageElement, shaderName: string | null, meshId: string, meshUniforms: import("../types/types.js").MeshMaterialUniform, activateMeshUniforms: import("../types/types.js").MeshMaterialUniform) => Promise<void>;
10
+ removeMesh: (id: string) => void;
11
11
  resizeOnChange: () => void;
12
12
  scrollToElBySelector: (elQuerySelector: string, delay?: number) => void;
13
13
  setMeshPositionsUpdate: (state: boolean) => void;
@@ -11,7 +11,7 @@ export const Canvas3 = {
11
11
  removeAnimationFromRender: mainClass.removeAnimationFromRender.bind(mainClass),
12
12
  addImageAsMesh: mainClass.addImageAsMesh.bind(mainClass),
13
13
  getShaderMaterial,
14
- removeMesh: mainClass.addImageAsMesh.bind(mainClass),
14
+ removeMesh: mainClass.removeMesh.bind(mainClass),
15
15
  resizeOnChange: mainClass.resizeOnChange.bind(mainClass),
16
16
  scrollToElBySelector: mainClass.scrollToElBySelector.bind(mainClass),
17
17
  setMeshPositionsUpdate: mainClass.setMeshPositionsUpdate.bind(mainClass),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canvas3/nuxt",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },