@canvas3/nuxt 0.1.11 → 0.1.12
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 +1 -1
- package/dist/runtime/components/canvas3-image.vue +19 -12
- package/dist/runtime/directives/canvas3-image.client.js +35 -25
- package/dist/runtime/utils/canvas3/canvas3.js +25 -10
- package/dist/runtime/utils/exports.d.ts +1 -1
- package/dist/runtime/utils/exports.js +1 -1
- package/package.json +1 -1
package/dist/module.json
CHANGED
|
@@ -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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
image.value
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
el.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
meshId =
|
|
23
|
-
|
|
24
|
-
|
|
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
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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);
|
|
@@ -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: (
|
|
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.
|
|
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),
|