@uxfront/scene 0.2.0 → 0.3.0

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/README.md CHANGED
@@ -4,7 +4,7 @@ The scroll-driven WebGL particle scene behind the uxfront.com family of homepage
4
4
 
5
5
  - **A pipeline.** Morphs between formations under curl noise, an intro that rises from a floor line, a cursor lens, a click shockwave, a mirror floor, light trails, bloom and a filmic composite.
6
6
  - **A catalog.** Eight formations: `monolith`, `plates`, `prism`, `tokens`, `ink`, `corridor`, `stack` and `threshold`.
7
- - **A controller.** `createExperience` maps the page's sections to formations, runs Lenis smooth scrolling, reveals copy, steps through highlights and pins DOM labels to the formations.
7
+ - **A controller.** `createExperience` maps the page's sections to formations, reveals copy, steps through highlights and pins DOM labels to the formations.
8
8
 
9
9
  Pages stay fast. The engine and each formation's GLSL are separate chunks, imported only once the page is idle. Shaders compile without blocking the main thread. Slow GPUs get fewer particles at a lower resolution, and without a capable GPU (no WebGL2, a software rasterizer, a lost context) the state switches to `"fallback"`, so the page can show a static image instead.
10
10
 
@@ -46,7 +46,7 @@ const experience = createExperience({
46
46
  experience.subscribe(({ chapter, status, motion }) => {
47
47
  // Update a HUD, a nav, the fallback…
48
48
  });
49
- experience.setMotion(false); // pause the clock and smooth scrolling
49
+ experience.setMotion(false); // pause the clock
50
50
  experience.destroy();
51
51
  ```
52
52
 
@@ -60,7 +60,7 @@ experience.destroy();
60
60
  | `data-steps="n"` on a section | While pinned, the section gets `data-step="0…n-1"` as it is read, to highlight `[data-step-item]`s. |
61
61
  | `[data-reveal]` under `root` | Gets `.is-in` once scrolled into view. |
62
62
  | `[data-anchor="key:id"]` under `anchorRoots` | Positioned (`transform`, `opacity`, `visibility`) over the formation's anchor while it shows. |
63
- | `--ux-progress` on `root` | Page scroll, 0 → 1. |
63
+ | `[data-progress]` under `root` | Gets `--ux-progress`: page scroll, 0 → 1. |
64
64
 
65
65
  Portrait, squarish and small screens use each formation's `mobile` framing, which leaves the top of the screen to the formation. Lay the copy out with the same breakpoints, exported as `STACKED_QUERY`.
66
66
 
@@ -14,8 +14,9 @@ export interface ExperienceState {
14
14
  }
15
15
  export interface ExperienceOptions {
16
16
  /**
17
- * Holds the `[data-stage]` sections, one per formation in DOM order, and the
18
- * `[data-reveal]` blocks. Receives `--ux-progress` (page scroll, 0 → 1).
17
+ * Holds the `[data-stage]` sections, one per formation in DOM order, the
18
+ * `[data-reveal]` blocks and the `[data-progress]` elements, which receive
19
+ * `--ux-progress` (page scroll, 0 → 1).
19
20
  */
20
21
  root: HTMLElement;
21
22
  canvas: HTMLCanvasElement;
@@ -27,14 +28,16 @@ export interface Experience {
27
28
  readonly state: Readonly<ExperienceState>;
28
29
  /** Called after every change of `state`. Returns an unsubscribe function. */
29
30
  subscribe(listener: (state: Readonly<ExperienceState>) => void): () => void;
31
+ /** Runs or pauses the scene's clock. Scrolling stays native either way. */
30
32
  setMotion(on: boolean): void;
31
33
  destroy(): void;
32
34
  }
33
35
  export declare const initialExperienceState: () => ExperienceState;
34
36
  /**
35
- * Drives a page: smooth scroll, the scroll → formation mapping, reveals and
36
- * step highlights, pointer input, the GPU engine (booted once the page is
37
- * idle) and the labels pinned to the formations. Client-only.
37
+ * Drives a page: the scroll → formation mapping, reveals and step
38
+ * highlights, pointer input, the GPU engine (booted once the page is idle)
39
+ * and the labels pinned to the formations. Client-only. Scrolling stays
40
+ * native, so the page scrolls smoothly even when the GPU drops frames.
38
41
  */
39
42
  export declare function createExperience({ root, canvas, formations, anchorRoots }: ExperienceOptions): Experience;
40
43
  //#endregion
@@ -1,6 +1,5 @@
1
1
  import { clamp, damp, projectToScreen } from "./gl/math.js";
2
2
  import { progressOf, stageAt } from "./gl/stage.js";
3
- import Lenis from "lenis";
4
3
  //#region src/experience.ts
5
4
  const initialExperienceState = () => ({
6
5
  chapter: 0,
@@ -34,9 +33,10 @@ function whenIdle(callback) {
34
33
  else window.addEventListener("load", schedule, { once: true });
35
34
  }
36
35
  /**
37
- * Drives a page: smooth scroll, the scroll → formation mapping, reveals and
38
- * step highlights, pointer input, the GPU engine (booted once the page is
39
- * idle) and the labels pinned to the formations. Client-only.
36
+ * Drives a page: the scroll → formation mapping, reveals and step
37
+ * highlights, pointer input, the GPU engine (booted once the page is idle)
38
+ * and the labels pinned to the formations. Client-only. Scrolling stays
39
+ * native, so the page scrolls smoothly even when the GPU drops frames.
40
40
  */
41
41
  function createExperience({ root, canvas, formations, anchorRoots = [] }) {
42
42
  const reducedQuery = matchMedia("(prefers-reduced-motion: reduce)");
@@ -55,31 +55,21 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
55
55
  listeners.forEach((listener) => listener(state));
56
56
  };
57
57
  let engine = null;
58
- let lenis = null;
59
58
  let disposed = false;
60
59
  let sections = [];
61
60
  let viewport = window.innerHeight;
62
- const syncLenis = () => {
63
- if (state.motion && !lenis) lenis = new Lenis({
64
- autoRaf: false,
65
- anchors: true,
66
- lerp: .085,
67
- wheelMultiplier: .9
68
- });
69
- else if (!state.motion && lenis) {
70
- lenis.destroy();
71
- lenis = null;
72
- }
73
- };
74
- syncLenis();
75
- const setMotion = (on) => {
76
- set({ motion: on });
77
- syncLenis();
78
- };
61
+ let maxScroll = 1;
62
+ let canvasWidth = 1;
63
+ let canvasHeight = 1;
64
+ const html = document.documentElement;
65
+ const setMotion = (on) => set({ motion: on });
79
66
  const onReducedChange = () => setMotion(!reducedQuery.matches);
80
67
  reducedQuery.addEventListener("change", onReducedChange);
81
68
  const measure = () => {
82
69
  viewport = window.innerHeight;
70
+ maxScroll = Math.max(1, html.scrollHeight - viewport);
71
+ canvasWidth = canvas.clientWidth;
72
+ canvasHeight = canvas.clientHeight;
83
73
  const scrollY = window.scrollY;
84
74
  sections = Array.from(root.querySelectorAll("[data-stage]")).map((el) => {
85
75
  const rect = el.getBoundingClientRect();
@@ -147,15 +137,13 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
147
137
  0
148
138
  ];
149
139
  const updateAnchors = (stage, local) => {
150
- const width = canvas.clientWidth;
151
- const height = canvas.clientHeight;
152
140
  const time = engine?.time ?? 0;
153
141
  for (const { el, anchor, index } of pins) {
154
142
  const progress = progressOf(index, stage, local, time);
155
143
  let visible = engine ? progress.presence * progress.presence : 0;
156
144
  if (visible > .001) {
157
145
  visible *= anchor.place(progress, anchorPos);
158
- if (!projectToScreen(screenPos, engine.viewProjection, anchorPos, width, height)) visible = 0;
146
+ if (!projectToScreen(screenPos, engine.viewProjection, anchorPos, canvasWidth, canvasHeight)) visible = 0;
159
147
  }
160
148
  if (visible > .001) {
161
149
  el.style.transform = `translate3d(${screenPos[0].toFixed(1)}px, ${screenPos[1].toFixed(1)}px, 0)`;
@@ -169,35 +157,38 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
169
157
  let last = performance.now();
170
158
  let stage = -1;
171
159
  let local = 0;
160
+ /** The scroll position the scene follows, eased while Motion is on. */
161
+ let followY = 0;
172
162
  let lastScrollY = window.scrollY;
173
163
  let activeSection = null;
174
164
  let statsClock = 0;
175
- const html = document.documentElement;
165
+ const progressEls = Array.from(root.querySelectorAll("[data-progress]"));
166
+ let progressText = "";
176
167
  const tick = (now) => {
177
168
  raf = requestAnimationFrame(tick);
178
169
  const dt = Math.min(.1, Math.max(0, (now - last) / 1e3));
179
170
  last = now;
180
- lenis?.raf(now);
181
171
  const scrollY = window.scrollY;
182
172
  const scrolled = Math.abs(scrollY - lastScrollY) / viewport;
183
173
  lastScrollY = scrollY;
184
- const target = stageAt(sections, scrollY);
185
- if (stage < 0 || !state.motion) {
186
- stage = target.stage;
187
- local = target.local;
188
- } else {
189
- stage = damp(stage, target.stage, 5, dt);
190
- local = damp(local, target.local, 6, dt);
191
- if (Math.abs(stage - target.stage) < 5e-4) stage = target.stage;
174
+ if (stage < 0 || !state.motion) followY = scrollY;
175
+ else {
176
+ followY = damp(followY, scrollY, 5, dt);
177
+ if (Math.abs(followY - scrollY) < .5) followY = scrollY;
192
178
  }
179
+ ({stage, local} = stageAt(sections, followY));
193
180
  const held = Math.round(stage);
194
181
  const section = sections[held]?.el ?? null;
195
- set({
182
+ const activeId = section?.id || null;
183
+ if (held !== state.chapter || activeId !== state.activeId) set({
196
184
  chapter: held,
197
- activeId: section?.id || null
185
+ activeId
198
186
  });
199
- const maxScroll = Math.max(1, html.scrollHeight - viewport);
200
- root.style.setProperty("--ux-progress", clamp(scrollY / maxScroll, 0, 1).toFixed(4));
187
+ const nextProgress = clamp(scrollY / maxScroll, 0, 1).toFixed(4);
188
+ if (nextProgress !== progressText) {
189
+ progressText = nextProgress;
190
+ for (const el of progressEls) el.style.setProperty("--ux-progress", progressText);
191
+ }
201
192
  if (section !== activeSection) {
202
193
  activeSection?.removeAttribute("data-step");
203
194
  activeSection = section;
@@ -295,8 +286,6 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
295
286
  document.documentElement.removeEventListener("pointerleave", onPointerLeave);
296
287
  window.removeEventListener("blur", onPointerLeave);
297
288
  canvas.removeEventListener("webglcontextrestored", onContextRestored);
298
- lenis?.destroy();
299
- lenis = null;
300
289
  engine?.destroy();
301
290
  engine = null;
302
291
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxfront/scene",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "The scroll-driven WebGL particle scene behind the uxfront.com family of homepages: a pluggable formation pipeline, a formation catalog and a framework-agnostic scroll controller.",
5
5
  "keywords": [
6
6
  "particles",
@@ -35,9 +35,6 @@
35
35
  "publishConfig": {
36
36
  "access": "public"
37
37
  },
38
- "dependencies": {
39
- "lenis": "^1.3.26"
40
- },
41
38
  "devDependencies": {
42
39
  "@uxfront/typescript-config": "0.1.0",
43
40
  "tsdown": "^0.23.0",