@canvas3/nuxt 0.1.3 → 0.1.5

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.3",
4
+ "version": "0.1.5",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.0"
@@ -69,6 +69,7 @@ export type scrollActionBindOptionType = {
69
69
  duration: number;
70
70
  };
71
71
  activateCallback?: (item: ScrollActionType) => void;
72
+ deactivateCallback?: (item: ScrollActionType) => void;
72
73
  onScrollCallback?: (item: ScrollActionType, scrollSpeed: number, currentPosition: number) => void;
73
74
  activeRangeMargin?: number;
74
75
  activeRangeOrigin?: number;
@@ -94,6 +95,7 @@ export type ScrollActionType = {
94
95
  margin?: number;
95
96
  containerBottom?: number;
96
97
  trackOnly?: boolean;
98
+ isInView: boolean;
97
99
  };
98
100
  export type myEffectType = {
99
101
  uniforms: BaseUniform;
@@ -1,6 +1,6 @@
1
- import type { ScrollActionType } from '#canvas3-nuxt/types/types';
1
+ import type { ScrollActionBinding, ScrollActionType } from '#canvas3-nuxt/types/types';
2
2
  import type { Mesh, ShaderMaterial, Texture } from 'three';
3
- export declare function generateBindingLogic(newBindingData: ScrollActionType): ScrollActionType;
3
+ export declare function generateBindingLogic(newBindingData: ScrollActionBinding): ScrollActionType;
4
4
  export declare function findMeshIDs(elParent: HTMLElement): false | string[];
5
5
  export declare function loadTexture(src: string): Promise<Texture>;
6
6
  export declare const heightPositionCoef = 1.38;
@@ -9,7 +9,7 @@ export declare function getMSDFFontMeshScales(boundsWidth: number, geometryWidth
9
9
  scaleY: number;
10
10
  };
11
11
  export declare function setScrollActiveElements(elNode: HTMLElement, meshIds: string[], state: string): void;
12
- export declare function elementNearViewport(bounds: DOMRect, margin?: number): boolean;
12
+ export declare function elementNearViewport(bounds: DOMRect, activeRangeInPx: number): boolean;
13
13
  export declare const lerp: (a: number, b: number, n: number) => number;
14
14
  export declare function getShaderMaterial(mesh: Mesh): ShaderMaterial;
15
15
  export declare function hasTransformTranslate(el: HTMLElement): boolean;
@@ -1,7 +1,7 @@
1
1
  import * as THREE from "three";
2
2
  import { gsap } from "gsap";
3
3
  export function generateBindingLogic(newBindingData) {
4
- const binding = newBindingData;
4
+ const binding = { ...newBindingData, isInView: false };
5
5
  binding.elNode.dataset.activeScroll = newBindingData?.elNode?.dataset?.activeScroll ?? "false";
6
6
  binding.containedMeshIds = [];
7
7
  binding.bounds = binding.elNode.getBoundingClientRect();
@@ -86,9 +86,8 @@ export function setScrollActiveElements(elNode, meshIds, state) {
86
86
  }
87
87
  }
88
88
  }
89
- export function elementNearViewport(bounds, margin = 200) {
90
- const windowHeight = window.innerHeight;
91
- return bounds.top >= -margin && bounds.top <= windowHeight || bounds.bottom >= -margin && bounds.bottom <= windowHeight || bounds.top <= -margin && bounds.bottom >= windowHeight;
89
+ export function elementNearViewport(bounds, activeRangeInPx) {
90
+ return bounds.top <= activeRangeInPx && bounds.top >= -bounds.height;
92
91
  }
93
92
  export const lerp = (a, b, n) => (1 - n) * a + n * b;
94
93
  export function getShaderMaterial(mesh) {
@@ -67,17 +67,20 @@ export default class Scroll {
67
67
  }
68
68
  setElementActive(item, isActive) {
69
69
  if (!item.containedMeshIds) return;
70
+ item.isInView = isActive;
71
+ const isActiveString = isActive ? "true" : "false";
72
+ item.elNode.dataset.activeScroll = isActiveString;
73
+ setScrollActiveElements(item.elNode, item.containedMeshIds, isActiveString);
70
74
  if (isActive) {
71
- item.elNode.dataset.activeScroll = "true";
72
- setScrollActiveElements(item.elNode, item.containedMeshIds, "true");
73
75
  item.elNode.classList.add("active");
74
76
  if (item.options.activateCallback) {
75
77
  item.options.activateCallback(item);
76
78
  }
77
79
  } else {
78
- item.elNode.dataset.activeScroll = "false";
79
- setScrollActiveElements(item.elNode, item.containedMeshIds, "false");
80
80
  if (!item.trackOnly) item.elNode.classList.remove("active");
81
+ if (item.options.deactivateCallback) {
82
+ item.options.deactivateCallback(item);
83
+ }
81
84
  }
82
85
  if (item.containedMeshIds.length > 0 && !item.trackOnly) {
83
86
  for (const meshId of item.containedMeshIds) {
@@ -87,29 +90,20 @@ export default class Scroll {
87
90
  }
88
91
  setElementsScrollPositions() {
89
92
  if (this.DOM.onScrollActivateElements.length === 0) return;
93
+ const windowInnerHeight = window.innerHeight;
90
94
  for (const item of this.DOM.onScrollActivateElements) {
91
95
  const bounds = item.elNode.getBoundingClientRect();
92
96
  const itemRangeMargin = item.options.activeRangeMargin ?? 0;
93
- const elementTrack = elementNearViewport(bounds, 200 + itemRangeMargin);
94
- if (elementTrack) {
95
- const activeRange = item.options.activeRange ?? 1;
96
- const activeRangeOrigin = item.options.activeRangeOrigin ?? 1;
97
- const activeRangeOriginPx = activeRangeOrigin * window.innerHeight;
98
- const activeRangeInPx = (1 - activeRange) * activeRangeOriginPx;
99
- const itemRangeMargin2 = item.options.activeRangeMargin ?? 0;
100
- const activeFromTop = bounds.top - itemRangeMargin2 <= activeRangeOriginPx - activeRangeInPx;
101
- const activeFromBottom = !item.options.bidirectionalActivation || bounds.bottom - itemRangeMargin2 >= activeRangeInPx + activeRangeOriginPx;
102
- if (activeFromTop && activeFromBottom) {
103
- if (item.options.onScrollCallback && this.scrollInProgress)
104
- item.options.onScrollCallback(item, this.speed, this.current);
105
- if (item.elNode.dataset.activeScroll !== "true") {
106
- this.setElementActive(item, true);
107
- }
108
- } else {
109
- if (item.elNode.dataset.activeScroll === "true" && !item.options.activateOnce) {
110
- this.setElementActive(item, false);
111
- }
112
- }
97
+ const activeRange = item.options.activeRange ?? 1;
98
+ const activeRangeInPx = activeRange * windowInnerHeight;
99
+ const isInView = elementNearViewport(bounds, activeRangeInPx);
100
+ if (isInView) {
101
+ if (item.options.onScrollCallback && this.scrollInProgress)
102
+ item.options.onScrollCallback(item, this.speed, this.current);
103
+ if (!item.isInView)
104
+ this.setElementActive(item, true);
105
+ } else if (item.isInView && !item.options.activateOnce) {
106
+ this.setElementActive(item, false);
113
107
  }
114
108
  if (typeof item.options.scrollSpeed?.value === "number") {
115
109
  item.elNode.style.transition = `linear translate3d 0s`;
@@ -11,4 +11,5 @@ export declare const Canvas3: {
11
11
  scrollToElBySelector: (elQuerySelector: string, delay?: number) => void;
12
12
  setMeshPositionsUpdate: (state: boolean) => void;
13
13
  scrollToTop: (delay?: number) => void;
14
+ scrollSpeed: number | null;
14
15
  };
@@ -14,5 +14,6 @@ export const Canvas3 = {
14
14
  resizeOnChange: mainClass.resizeOnChange.bind(mainClass),
15
15
  scrollToElBySelector: mainClass.scrollToElBySelector.bind(mainClass),
16
16
  setMeshPositionsUpdate: mainClass.setMeshPositionsUpdate.bind(mainClass),
17
- scrollToTop: mainClass.scrollToTop.bind(mainClass)
17
+ scrollToTop: mainClass.scrollToTop.bind(mainClass),
18
+ scrollSpeed: mainClass.scroll?.speed ?? null
18
19
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canvas3/nuxt",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },