@canvas3/nuxt 0.0.1

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.
Files changed (53) hide show
  1. package/README.md +70 -0
  2. package/dist/module.d.mts +5 -0
  3. package/dist/module.json +9 -0
  4. package/dist/module.mjs +32 -0
  5. package/dist/runtime/components/canvas3-image.vue +71 -0
  6. package/dist/runtime/components/canvas3-image.vue.d.ts +5 -0
  7. package/dist/runtime/components/canvas3-text.vue +95 -0
  8. package/dist/runtime/components/canvas3-text.vue.d.ts +11 -0
  9. package/dist/runtime/constants/default-canvas3-options.d.ts +40 -0
  10. package/dist/runtime/constants/default-canvas3-options.js +40 -0
  11. package/dist/runtime/constants/shaders/TextBlurFragment.glsl +126 -0
  12. package/dist/runtime/constants/shaders/TextBlurVertex.glsl +10 -0
  13. package/dist/runtime/constants/shaders/projectBlurFragment.glsl +143 -0
  14. package/dist/runtime/constants/shaders/projectBlurVertex.glsl +11 -0
  15. package/dist/runtime/constants/shaders/scrollFragment.glsl +7 -0
  16. package/dist/runtime/constants/shaders/scrollVertex.glsl +8 -0
  17. package/dist/runtime/directives/action-on-scroll.d.ts +2 -0
  18. package/dist/runtime/directives/action-on-scroll.js +42 -0
  19. package/dist/runtime/directives/canvas3-image-directive.client.d.ts +6 -0
  20. package/dist/runtime/directives/canvas3-image-directive.client.js +104 -0
  21. package/dist/runtime/directives/set-data-attributes.d.ts +3 -0
  22. package/dist/runtime/directives/set-data-attributes.js +34 -0
  23. package/dist/runtime/layouts/canvas3-layout.vue +44 -0
  24. package/dist/runtime/layouts/canvas3-layout.vue.d.ts +11 -0
  25. package/dist/runtime/plugin.d.ts +2 -0
  26. package/dist/runtime/plugin.js +3 -0
  27. package/dist/runtime/plugins/directives.d.ts +2 -0
  28. package/dist/runtime/plugins/directives.js +9 -0
  29. package/dist/runtime/server/tsconfig.json +3 -0
  30. package/dist/runtime/types/shaders.d.ts +4 -0
  31. package/dist/runtime/types/three-msdf-text-utils.d.ts +36 -0
  32. package/dist/runtime/types/types.d.ts +71 -0
  33. package/dist/runtime/types/types.js +0 -0
  34. package/dist/runtime/utils/MSDFFont/MSDFTextGeometry/TextLayout.d.ts +1 -0
  35. package/dist/runtime/utils/MSDFFont/MSDFTextGeometry/TextLayout.js +0 -0
  36. package/dist/runtime/utils/MSDFFont/MSDFTextGeometry/createIndices.d.ts +1 -0
  37. package/dist/runtime/utils/MSDFFont/MSDFTextGeometry/createIndices.js +0 -0
  38. package/dist/runtime/utils/MSDFFont/MSDFTextGeometry/index.d.ts +1 -0
  39. package/dist/runtime/utils/MSDFFont/MSDFTextGeometry/index.js +0 -0
  40. package/dist/runtime/utils/MSDFFont/MSDFTextGeometry/utils.d.ts +1 -0
  41. package/dist/runtime/utils/MSDFFont/MSDFTextGeometry/utils.js +0 -0
  42. package/dist/runtime/utils/MSDFFont/MSDFTextGeometry/vertices.d.ts +1 -0
  43. package/dist/runtime/utils/MSDFFont/MSDFTextGeometry/vertices.js +0 -0
  44. package/dist/runtime/utils/canvas3/canvas3.d.ts +65 -0
  45. package/dist/runtime/utils/canvas3/canvas3.js +512 -0
  46. package/dist/runtime/utils/canvas3/helpers.d.ts +12 -0
  47. package/dist/runtime/utils/canvas3/helpers.js +96 -0
  48. package/dist/runtime/utils/canvas3/scroll.d.ts +37 -0
  49. package/dist/runtime/utils/canvas3/scroll.js +191 -0
  50. package/dist/runtime/utils/exports.d.ts +10 -0
  51. package/dist/runtime/utils/exports.js +14 -0
  52. package/dist/types.d.mts +7 -0
  53. package/package.json +64 -0
@@ -0,0 +1,2 @@
1
+ import { type Directive } from 'vue';
2
+ export declare const vOnScrollActivate: Directive;
@@ -0,0 +1,42 @@
1
+ import { watch } from "vue";
2
+ import { Canvas3 } from "#canvas3-nuxt/utils/canvas3/canvas3";
3
+ export const vOnScrollActivate = {
4
+ mounted(el, binding) {
5
+ el.dataset.scrollActivateId = crypto.randomUUID();
6
+ if (binding.value?.scrollSpeedSetTo?.value) {
7
+ el.dataset.scrollSpeed = binding.value?.scrollSpeedSetTo?.value;
8
+ }
9
+ const addToCanvas = () => {
10
+ Canvas3.addOnScrollActivateElement({
11
+ elNode: el,
12
+ options: binding.value,
13
+ arg: binding.arg
14
+ });
15
+ };
16
+ if (Canvas3.canvasInitiated.value) {
17
+ addToCanvas();
18
+ } else {
19
+ watch(
20
+ () => Canvas3.canvasInitiated.value,
21
+ (newValue) => {
22
+ if (newValue) {
23
+ addToCanvas();
24
+ }
25
+ }
26
+ );
27
+ }
28
+ },
29
+ updated(el, binding) {
30
+ Canvas3.updateOnScrollActiveElement({
31
+ elNode: el,
32
+ options: binding.value,
33
+ arg: binding.arg
34
+ });
35
+ },
36
+ unmounted(el) {
37
+ Canvas3.removeScrollActiveElement(el);
38
+ },
39
+ getSSRProps() {
40
+ return {};
41
+ }
42
+ };
@@ -0,0 +1,6 @@
1
+ import type { Directive } from 'vue';
2
+ export declare const vCanvas3Image: Directive<HTMLImageElement, {
3
+ shaderName?: string;
4
+ uniforms?: Record<string, any>;
5
+ activateMeshUniforms?: Record<string, any>;
6
+ }>;
@@ -0,0 +1,104 @@
1
+ import { watch, nextTick } from "vue";
2
+ import { Canvas3 } from "#canvas3-nuxt/utils/canvas3/canvas3";
3
+ const meshIdMap = /* @__PURE__ */ new WeakMap();
4
+ const addedMap = /* @__PURE__ */ new WeakMap();
5
+ const stopMap = /* @__PURE__ */ new WeakMap();
6
+ const srcMap = /* @__PURE__ */ new WeakMap();
7
+ function getImgSrc(el) {
8
+ return el.currentSrc || el.src || "";
9
+ }
10
+ 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;
25
+ }
26
+ Canvas3.addImageAsMesh(
27
+ el,
28
+ options?.shaderName,
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
+ }
37
+ function remove(el) {
38
+ const meshId = meshIdMap.get(el);
39
+ if (meshId) {
40
+ Canvas3.removeMesh(meshId);
41
+ }
42
+ addedMap.delete(el);
43
+ meshIdMap.delete(el);
44
+ srcMap.delete(el);
45
+ const stop = stopMap.get(el);
46
+ if (stop) {
47
+ stop();
48
+ stopMap.delete(el);
49
+ }
50
+ }
51
+ export const vCanvas3Image = {
52
+ async mounted(el, binding) {
53
+ if (el.tagName !== "IMG") {
54
+ return;
55
+ }
56
+ const options = binding.value || {};
57
+ const stop = watch(
58
+ () => Canvas3.canvasInitiated.value,
59
+ (ready) => {
60
+ if (ready) tryAdd(el, options);
61
+ },
62
+ { immediate: true }
63
+ );
64
+ stopMap.set(el, stop);
65
+ const onLoad = () => tryAdd(el, options);
66
+ if (el.complete) {
67
+ await tryAdd(el, options);
68
+ } else {
69
+ el.addEventListener("load", onLoad, { once: true });
70
+ }
71
+ },
72
+ updated(el, binding) {
73
+ const options = binding.value || {};
74
+ const meshId = meshIdMap.get(el);
75
+ if (meshId && options?.uniforms) {
76
+ Canvas3.meshUniformsUpdate(meshId, options.uniforms);
77
+ }
78
+ const prevSrc = srcMap.get(el);
79
+ const current = getImgSrc(el);
80
+ if (prevSrc && current && prevSrc !== current) {
81
+ remove(el);
82
+ const stop = watch(
83
+ () => Canvas3.canvasInitiated.value,
84
+ (ready) => {
85
+ if (ready) tryAdd(el, options);
86
+ },
87
+ { immediate: true }
88
+ );
89
+ stopMap.set(el, stop);
90
+ if (el.complete) {
91
+ tryAdd(el, options);
92
+ } else {
93
+ const onLoad = () => tryAdd(el, options);
94
+ el.addEventListener("load", onLoad, { once: true });
95
+ }
96
+ }
97
+ },
98
+ unmounted(el) {
99
+ remove(el);
100
+ },
101
+ getSSRProps() {
102
+ return {};
103
+ }
104
+ };
@@ -0,0 +1,3 @@
1
+ import type { Directive } from 'vue';
2
+ export type AttrsType = Record<string, number | string>;
3
+ export declare const vSetDataAttribute: Directive;
@@ -0,0 +1,34 @@
1
+ const addDataSetToEl = (attrs, el) => {
2
+ if (!el) return;
3
+ if (el.nodeName == "#text") return;
4
+ for (const key in attrs) {
5
+ if (Object.prototype.hasOwnProperty.call(attrs, key) && el.setAttribute && attrs[key]?.toString()) {
6
+ el.setAttribute("data-" + key, attrs[key]?.toString());
7
+ }
8
+ }
9
+ };
10
+ const setElementsWithDataSets = (binding, el) => {
11
+ addDataSetToEl(binding.value, el);
12
+ if (el.childNodes) {
13
+ const setChildNodes = (el2) => {
14
+ for (const child of Array.from(el2.childNodes)) {
15
+ addDataSetToEl(binding.value, child ?? null);
16
+ if (child && child.childNodes && child.childNodes.length > 0) {
17
+ setChildNodes(child);
18
+ }
19
+ }
20
+ };
21
+ setChildNodes(el);
22
+ }
23
+ };
24
+ export const vSetDataAttribute = {
25
+ mounted(el, binding) {
26
+ setElementsWithDataSets(binding, el);
27
+ },
28
+ updated(el, binding) {
29
+ setElementsWithDataSets(binding, el);
30
+ },
31
+ getSSRProps() {
32
+ return {};
33
+ }
34
+ };
@@ -0,0 +1,44 @@
1
+ <template>
2
+ <div id="appContainer">
3
+ <div id="scrollContainer">
4
+ <div
5
+ id="scrollableContent"
6
+ ref="scrollableContent"
7
+ >
8
+ <slot />
9
+ </div>
10
+ </div>
11
+
12
+ <div
13
+ id="animationContainer"
14
+ ref="canvasEl"
15
+ :style="{ zIndex: props.canvas3options.canvasElement.zIndex }"
16
+ />
17
+ </div>
18
+ </template>
19
+
20
+ <script setup>
21
+ import { ref, watch } from "vue";
22
+ import { Canvas3 } from "#canvas3-nuxt/utils/canvas3/canvas3";
23
+ const emit = defineEmits(["canvas3-ready"]);
24
+ const props = useAttrs();
25
+ const canvasEl = ref("canvasEl");
26
+ const scrollableContent = ref("scrollableContent");
27
+ watch(
28
+ () => props.canvas3enabled,
29
+ async (newValue) => {
30
+ if (newValue) {
31
+ await Canvas3.init(
32
+ canvasEl.value,
33
+ scrollableContent.value,
34
+ props.canvas3options
35
+ );
36
+ emit("canvas3-ready");
37
+ }
38
+ }
39
+ );
40
+ </script>
41
+
42
+ <style>
43
+ #scrollContainer{height:100%;left:0;margin:0;overflow:hidden;padding:0;position:fixed;top:0;width:100%}#scrollableContent{will-change:transform}#animationContainer{height:100vh;left:0;margin:0 auto;max-height:100%;pointer-events:none;position:fixed;right:0;top:0;width:100%}
44
+ </style>
@@ -0,0 +1,11 @@
1
+ declare const _default: __VLS_WithSlots<typeof __VLS_component, __VLS_Slots>;
2
+ export default _default;
3
+ type __VLS_WithSlots<T, S> = T & (new () => {
4
+ $slots: S;
5
+ });
6
+ declare const __VLS_component: import("vue").DefineComponent<{}, {
7
+ $emit: (event: "canvas3-ready", ...args: any[]) => void;
8
+ }, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
9
+ type __VLS_Slots = {
10
+ default?: ((props: {}) => any) | undefined;
11
+ };
@@ -0,0 +1,2 @@
1
+ declare const _default: import("#app").Plugin<Record<string, unknown>> & import("#app").ObjectPlugin<Record<string, unknown>>;
2
+ export default _default;
@@ -0,0 +1,3 @@
1
+ import { defineNuxtPlugin } from "#app";
2
+ export default defineNuxtPlugin((_nuxtApp) => {
3
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("#app").Plugin<Record<string, unknown>> & import("#app").ObjectPlugin<Record<string, unknown>>;
2
+ export default _default;
@@ -0,0 +1,9 @@
1
+ import { vOnScrollActivate } from "../directives/action-on-scroll.js";
2
+ import { vSetDataAttribute } from "../directives/set-data-attributes.js";
3
+ import { vCanvas3Image } from "../directives/canvas3-image-directive.client.js";
4
+ import { defineNuxtPlugin } from "#app";
5
+ export default defineNuxtPlugin((nuxtApp) => {
6
+ nuxtApp.vueApp.directive("action-on-scroll", vOnScrollActivate);
7
+ nuxtApp.vueApp.directive("set-data-attributes", vSetDataAttribute);
8
+ nuxtApp.vueApp.directive("canvas3-image", vCanvas3Image);
9
+ });
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../../.nuxt/tsconfig.server.json",
3
+ }
@@ -0,0 +1,4 @@
1
+ declare module '*.glsl' {
2
+ const value: string
3
+ export default value
4
+ }
@@ -0,0 +1,36 @@
1
+ declare module 'three-msdf-text-utils' {
2
+ import * as THREE from 'three'
3
+
4
+ export interface GlyphGeometryOptions {
5
+ flipY?: boolean
6
+ negate?: boolean
7
+ multipage?: boolean
8
+ }
9
+
10
+ export class GlyphGeometry extends THREE.BufferGeometry {
11
+ constructor(text: string, font: any, options?: GlyphGeometryOptions)
12
+ layout: any
13
+ update(text: string): void
14
+ }
15
+
16
+ export interface MSDFShaderOptions {
17
+ map?: THREE.Texture
18
+ color?: THREE.Color | string | number
19
+ opacity?: number
20
+ alphaTest?: number
21
+ negate?: boolean
22
+ precision?: string
23
+ multipage?: boolean
24
+ side?: THREE.Side
25
+ transparent?: boolean
26
+ defines?: { [key: string]: any }
27
+ uniforms?: { [key: string]: any }
28
+ }
29
+
30
+ export function createMSDFShader(options?: MSDFShaderOptions): {
31
+ uniforms: { [key: string]: THREE.IUniform }
32
+ vertexShader: string
33
+ fragmentShader: string
34
+ defines: { [key: string]: any }
35
+ }
36
+ }
@@ -0,0 +1,71 @@
1
+ import type { Mesh } from 'three';
2
+ import type { DirectiveBinding } from 'vue';
3
+ export type AnimationCallback = () => void;
4
+ export type AnimationCallbacks = Map<string, AnimationCallback>;
5
+ export type MeshMaterialUniform = Record<string, {
6
+ duration: number;
7
+ ease: string;
8
+ value: number;
9
+ }>;
10
+ type Shader = {
11
+ fragmentShader: string;
12
+ vertexShader: string;
13
+ textFragment?: string;
14
+ textVertex?: string;
15
+ };
16
+ type Shaders = {
17
+ default: Shader;
18
+ } & Record<string, Shader>;
19
+ type Font = {
20
+ fnt: string;
21
+ atlas: string;
22
+ };
23
+ type MeshAnimation = {
24
+ value: number;
25
+ duration: number;
26
+ ease: string;
27
+ };
28
+ export type Canvas3OptionsType = {
29
+ font: Font;
30
+ shaders: Shaders;
31
+ activateMeshOptions: {
32
+ image: Record<string, MeshAnimation>;
33
+ text: Record<string, MeshAnimation>;
34
+ };
35
+ canvasElement: {
36
+ zIndex: number;
37
+ };
38
+ prefersReducedMotion: boolean;
39
+ isMobile: boolean;
40
+ disabled: boolean;
41
+ };
42
+ export type MeshType = {
43
+ name: string;
44
+ htmlEl: HTMLElement;
45
+ mesh: Mesh;
46
+ top: number;
47
+ left: number;
48
+ width: number;
49
+ height: number;
50
+ };
51
+ export type ScrollActionBinding = {
52
+ elNode: HTMLElement;
53
+ options: DirectiveBinding['value'];
54
+ arg: DirectiveBinding['arg'];
55
+ };
56
+ export type ScrollActionType = {
57
+ elNode: HTMLElement;
58
+ options: DirectiveBinding['value'];
59
+ arg: DirectiveBinding['arg'];
60
+ containedMeshIds?: string[];
61
+ bounds: DOMRect;
62
+ containerId?: string;
63
+ fixPosition?: number;
64
+ containerEl?: HTMLElement;
65
+ childEl?: HTMLElement;
66
+ childElBounds?: DOMRect;
67
+ margin?: number;
68
+ containerBottom?: number;
69
+ trackOnly?: boolean;
70
+ };
71
+ export {};
File without changes
@@ -0,0 +1,65 @@
1
+ import * as THREE from 'three';
2
+ import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
3
+ import type { Pass } from 'three/addons/postprocessing/Pass.js';
4
+ import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
5
+ import type { ShaderMaterial } from 'three';
6
+ import Scroll from '#canvas3-nuxt/utils/canvas3/scroll';
7
+ import type { AnimationCallback, AnimationCallbacks, Canvas3OptionsType, ScrollActionBinding, MeshMaterialUniform, MeshType } from '#canvas3-nuxt/types/types';
8
+ declare class Canvas3Class {
9
+ canvasInitiated: import("vue").Ref<boolean, boolean>;
10
+ animateImageMesh: boolean;
11
+ canvasContainer: HTMLElement | null;
12
+ scrollableContent: HTMLElement | null;
13
+ time: number;
14
+ scene: THREE.Scene;
15
+ MSDFTextGeometryAtlas: any;
16
+ MSDFTextGeometryFont: any;
17
+ materials: ShaderMaterial[];
18
+ imageStore: MeshType[];
19
+ textStore: MeshType[];
20
+ activateMeshUniformMap: Map<string, MeshMaterialUniform>;
21
+ scroll: Scroll | null;
22
+ currentScroll: number;
23
+ options: Canvas3OptionsType;
24
+ width: number;
25
+ height: number;
26
+ camera: THREE.PerspectiveCamera | null;
27
+ composer: EffectComposer | null;
28
+ renderPass: Pass | null;
29
+ renderer: any;
30
+ myEffect: any;
31
+ customPass: ShaderPass | null;
32
+ animations: AnimationCallbacks;
33
+ mouse: {
34
+ x: number;
35
+ y: number;
36
+ movementX: number;
37
+ movementY: number;
38
+ xPrev: number;
39
+ yPrev: number;
40
+ };
41
+ init(canvasElement: HTMLElement, scrollableContent: HTMLElement, canvas3Options: Canvas3OptionsType): Promise<void>;
42
+ setCanvasAndCamera(): void;
43
+ resizeOnChange(): void;
44
+ resizeImageStore(): void;
45
+ resizeTextStore(): void;
46
+ setImageMeshPositions(): void;
47
+ setTextMeshPositions(): void;
48
+ meshUniformsUpdate(id: string, uniforms: MeshMaterialUniform): void;
49
+ activateMesh(meshId: string, isActive: boolean): void;
50
+ addOnScrollActivateElement(binding: ScrollActionBinding): void;
51
+ updateOnScrollActiveElement(updatedBinding: ScrollActionBinding): void;
52
+ removeScrollActiveElement(elNode: HTMLElement): void;
53
+ removeMesh(id: string): void;
54
+ addAnimationToRender(callBackName: string, callBackFunction: AnimationCallback): void;
55
+ addImageAsMesh(imgHtmlEl: HTMLImageElement, shaderName: string, meshId: string, meshUniforms: MeshMaterialUniform, activateMeshUniforms: MeshMaterialUniform): Promise<void>;
56
+ composerPass(): void;
57
+ setSize(): void;
58
+ scrollToTop(delay?: number): void;
59
+ scrollTo(position: number, delay?: number): void;
60
+ scrollToElBySelector(elQuerySelector: string, delay?: number): void;
61
+ setMeshPositionsUpdate(state: boolean): void;
62
+ render(): void;
63
+ }
64
+ export declare const Canvas3: Canvas3Class;
65
+ export {};