@uxfront/scene 0.2.0 → 0.4.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
 
@@ -54,13 +54,15 @@ experience.destroy();
54
54
 
55
55
  ### Page contract
56
56
 
57
- | Markup | Effect |
58
- | -------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
59
- | `[data-stage]` under `root` | One section per formation, in DOM order. A section holds its formation while pinned, and the next one morphs in as it scrolls in. |
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
- | `[data-reveal]` under `root` | Gets `.is-in` once scrolled into view. |
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. |
57
+ | Markup | Effect |
58
+ | -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
59
+ | `[data-stage]` under `root` | One section per formation, in DOM order, about a screen tall. It holds its formation while it fills the screen, give or take `HOLD_MARGIN`. |
60
+ | `data-steps="n"` on a section | While it holds its formation, the section gets `data-step="0…n-1"` as it is read, to highlight `[data-step-item]`s. |
61
+ | `[data-reveal]` under `root` | Gets `.is-in` once scrolled into view. |
62
+ | `[data-anchor="key:id"]` under `anchorRoots` | Positioned (`transform`, `opacity`, `visibility`) over the formation's anchor while it shows. |
63
+ | `[data-progress]` under `root` | Gets `--ux-progress`: page scroll, 0 → 1. |
64
+
65
+ Sections scroll freely, so don't pin them: the copy should move with the page. The scene holds a formation from a quarter of a screen (`HOLD_MARGIN`) before its section fills the screen until a quarter after, and morphs into the next formation in between, while both sections share the screen.
64
66
 
65
67
  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
68
 
@@ -122,7 +124,7 @@ Particle $main(uint id, float sel, Frame f) {
122
124
 
123
125
  - `$name` identifiers are private: the scene rewrites them to `key_name`, so formations never clash.
124
126
  - `$main` returns the position, colour, size and `refl` (mirror reflectivity) of particle `id`. `sel` spreads evenly over [0, 1) across the particles, to split them between the parts of a formation.
125
- - `Frame f` holds `local` (progress while pinned: 0 before, 1 after), `approach` (0 → 1 while the section scrolls in) and `time`.
127
+ - `Frame f` holds `local` (progress while the section holds the formation: 0 before, 1 after), `approach` (0 → 1 while the section scrolls in) and `time`.
126
128
  - Shared helpers: `hashU`, `rnd`, `seedOf`, `gauss`, `noised`, `curl`, `bitangent`, `bezier3`, `rrectPoint`, `shapePoint`, `PI`, `TAU` and `FLOOR_Y`.
127
129
  - `glslFloat`, `glslVec` and `glslArray` write numbers into the GLSL as constants. `packShapes` packs line art (segments, rounded rectangles, circles, discs and fills) into rows for `shapePoint`, which places a particle on it. `stack` and `plates` draw their art this way.
128
130
  - A formation that follows the prism can take its `RAILS` and `SPECTRUM` from `@uxfront/scene/formations`, so rail `i` keeps its colour.
@@ -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
- import { progressOf, stageAt } from "./gl/stage.js";
3
- import Lenis from "lenis";
2
+ import { localOf, progressOf, sectionSpan, stageAt } from "./gl/stage.js";
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,39 +55,27 @@ 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();
86
- const start = rect.top + scrollY;
87
76
  return {
88
77
  el,
89
- start,
90
- end: Math.max(start, start + rect.height - viewport)
78
+ ...sectionSpan(rect.top + scrollY, rect.height, viewport, maxScroll)
91
79
  };
92
80
  });
93
81
  engine?.resize();
@@ -147,15 +135,13 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
147
135
  0
148
136
  ];
149
137
  const updateAnchors = (stage, local) => {
150
- const width = canvas.clientWidth;
151
- const height = canvas.clientHeight;
152
138
  const time = engine?.time ?? 0;
153
139
  for (const { el, anchor, index } of pins) {
154
140
  const progress = progressOf(index, stage, local, time);
155
141
  let visible = engine ? progress.presence * progress.presence : 0;
156
142
  if (visible > .001) {
157
143
  visible *= anchor.place(progress, anchorPos);
158
- if (!projectToScreen(screenPos, engine.viewProjection, anchorPos, width, height)) visible = 0;
144
+ if (!projectToScreen(screenPos, engine.viewProjection, anchorPos, canvasWidth, canvasHeight)) visible = 0;
159
145
  }
160
146
  if (visible > .001) {
161
147
  el.style.transform = `translate3d(${screenPos[0].toFixed(1)}px, ${screenPos[1].toFixed(1)}px, 0)`;
@@ -169,35 +155,38 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
169
155
  let last = performance.now();
170
156
  let stage = -1;
171
157
  let local = 0;
158
+ /** The scroll position the scene follows, eased while Motion is on. */
159
+ let followY = 0;
172
160
  let lastScrollY = window.scrollY;
173
161
  let activeSection = null;
174
162
  let statsClock = 0;
175
- const html = document.documentElement;
163
+ const progressEls = Array.from(root.querySelectorAll("[data-progress]"));
164
+ let progressText = "";
176
165
  const tick = (now) => {
177
166
  raf = requestAnimationFrame(tick);
178
167
  const dt = Math.min(.1, Math.max(0, (now - last) / 1e3));
179
168
  last = now;
180
- lenis?.raf(now);
181
169
  const scrollY = window.scrollY;
182
170
  const scrolled = Math.abs(scrollY - lastScrollY) / viewport;
183
171
  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;
172
+ if (stage < 0 || !state.motion) followY = scrollY;
173
+ else {
174
+ followY = damp(followY, scrollY, 5, dt);
175
+ if (Math.abs(followY - scrollY) < .5) followY = scrollY;
192
176
  }
177
+ ({stage, local} = stageAt(sections, followY));
193
178
  const held = Math.round(stage);
194
179
  const section = sections[held]?.el ?? null;
195
- set({
180
+ const activeId = section?.id || null;
181
+ if (held !== state.chapter || activeId !== state.activeId) set({
196
182
  chapter: held,
197
- activeId: section?.id || null
183
+ activeId
198
184
  });
199
- const maxScroll = Math.max(1, html.scrollHeight - viewport);
200
- root.style.setProperty("--ux-progress", clamp(scrollY / maxScroll, 0, 1).toFixed(4));
185
+ const nextProgress = clamp(scrollY / maxScroll, 0, 1).toFixed(4);
186
+ if (nextProgress !== progressText) {
187
+ progressText = nextProgress;
188
+ for (const el of progressEls) el.style.setProperty("--ux-progress", progressText);
189
+ }
201
190
  if (section !== activeSection) {
202
191
  activeSection?.removeAttribute("data-step");
203
192
  activeSection = section;
@@ -205,7 +194,8 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
205
194
  if (section) {
206
195
  const steps = Number(section.dataset.steps || 0);
207
196
  if (steps > 0) {
208
- const step = String(Math.min(steps - 1, Math.floor(clamp(local, 0, .999) * steps)));
197
+ const read = clamp(localOf(held, stage, local), 0, .999);
198
+ const step = String(Math.min(steps - 1, Math.floor(read * steps)));
209
199
  if (section.dataset.step !== step) section.dataset.step = step;
210
200
  }
211
201
  }
@@ -295,8 +285,6 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
295
285
  document.documentElement.removeEventListener("pointerleave", onPointerLeave);
296
286
  window.removeEventListener("blur", onPointerLeave);
297
287
  canvas.removeEventListener("webglcontextrestored", onContextRestored);
298
- lenis?.destroy();
299
- lenis = null;
300
288
  engine?.destroy();
301
289
  engine = null;
302
290
  }
@@ -12,14 +12,26 @@ export declare const presenceOf: (k: number, stage: number) => number;
12
12
  export declare const approachOf: (k: number, stage: number) => number;
13
13
  export declare function progressOf(k: number, stage: number, local: number, time: number): FormationProgress;
14
14
  export interface SectionSpan {
15
- /** Scroll offset at which the section's top reaches the top of the viewport. */
15
+ /** Scroll offset at which the section starts holding its formation. */
16
16
  start: number;
17
- /** Scroll offset at which its bottom reaches the bottom of the viewport. */
17
+ /** Scroll offset at which it stops. */
18
18
  end: number;
19
19
  }
20
20
  /**
21
- * Scroll → stage. A section holds its formation while pinned (local 0 → 1)
22
- * and morphs into the next one while the next section scrolls in.
21
+ * How far, in viewport heights, a section holds its formation before it
22
+ * fills the viewport and after it starts to leave. The rest of the scroll
23
+ * between two sections morphs one formation into the next.
24
+ */
25
+ export declare const HOLD_MARGIN = .25;
26
+ /**
27
+ * The span over which a section holds its formation: while it fills the
28
+ * viewport, plus `HOLD_MARGIN` on either side, within the page's scroll range.
29
+ * The page scrolls freely: the copy moves with it while the formation holds.
30
+ */
31
+ export declare function sectionSpan(top: number, height: number, viewport: number, maxScroll: number): SectionSpan;
32
+ /**
33
+ * Scroll → stage. A section holds its formation across its span (local 0 → 1)
34
+ * and morphs into the next one between the two spans.
23
35
  */
24
36
  export declare function stageAt(sections: SectionSpan[], y: number): {
25
37
  stage: number;
package/dist/gl/stage.js CHANGED
@@ -20,8 +20,27 @@ function progressOf(k, stage, local, time) {
20
20
  };
21
21
  }
22
22
  /**
23
- * Scroll → stage. A section holds its formation while pinned (local 0 → 1)
24
- * and morphs into the next one while the next section scrolls in.
23
+ * How far, in viewport heights, a section holds its formation before it
24
+ * fills the viewport and after it starts to leave. The rest of the scroll
25
+ * between two sections morphs one formation into the next.
26
+ */
27
+ const HOLD_MARGIN = .25;
28
+ /**
29
+ * The span over which a section holds its formation: while it fills the
30
+ * viewport, plus `HOLD_MARGIN` on either side, within the page's scroll range.
31
+ * The page scrolls freely: the copy moves with it while the formation holds.
32
+ */
33
+ function sectionSpan(top, height, viewport, maxScroll) {
34
+ const margin = viewport * HOLD_MARGIN;
35
+ const start = clamp(top - margin, 0, maxScroll);
36
+ return {
37
+ start,
38
+ end: clamp(top + height - viewport + margin, start, maxScroll)
39
+ };
40
+ }
41
+ /**
42
+ * Scroll → stage. A section holds its formation across its span (local 0 → 1)
43
+ * and morphs into the next one between the two spans.
25
44
  */
26
45
  function stageAt(sections, y) {
27
46
  for (let k = 0; k < sections.length; k++) {
@@ -52,4 +71,4 @@ function stageAt(sections, y) {
52
71
  };
53
72
  }
54
73
  //#endregion
55
- export { approachOf, localOf, presenceOf, progressOf, stageAt };
74
+ export { HOLD_MARGIN, approachOf, localOf, presenceOf, progressOf, sectionSpan, stageAt };
@@ -29,7 +29,7 @@ export interface FormationLook {
29
29
  export interface FormationProgress {
30
30
  /** Scene stage: a formation index plus the progress of the morph to the next one. */
31
31
  stage: number;
32
- /** Progress through the formation while its section is pinned: 0 before, 1 after. */
32
+ /** Progress through the formation while its section holds it: 0 before, 1 after. */
33
33
  local: number;
34
34
  /** 0 → 1 while the formation's section scrolls in, 1 from then on. */
35
35
  approach: number;
package/dist/index.d.ts CHANGED
@@ -6,6 +6,6 @@ import { ShaderPart, composeParticleShader, namespaceGlsl } from "./gl/compose.j
6
6
  import { BASE_PARTICLES, FLOOR_Y } from "./gl/constants.js";
7
7
  import { defineFormation, mergeViews } from "./gl/formation.js";
8
8
  import { glslArray, glslFloat, glslVec } from "./gl/glsl.js";
9
- import { SectionSpan, approachOf, localOf, presenceOf, progressOf, stageAt } from "./gl/stage.js";
9
+ import { HOLD_MARGIN, SectionSpan, approachOf, localOf, presenceOf, progressOf, sectionSpan, stageAt } from "./gl/stage.js";
10
10
  import { STACKED_QUERY } from "./layout.js";
11
- export { type AnchorDef, BASE_PARTICLES, type Experience, type ExperienceOptions, type ExperienceState, FLOOR_Y, type Formation, type FormationDef, type FormationFrame, type FormationLook, type FormationOptions, type FormationProgress, type FormationShader, type FormationViews, type GlStatus, type Mat4, type PackedShapes, STACKED_QUERY, type SectionSpan, type ShaderPart, type Shape, type StageView, type Uniforms, type Vec2, type Vec3, type Vec4, approachOf, clamp, composeParticleShader, createExperience, damp, defineFormation, glslArray, glslFloat, glslVec, initialExperienceState, lerp, lerp3, localOf, mergeViews, namespaceGlsl, packShapes, presenceOf, progressOf, projectToScreen, smoothstep, stageAt };
11
+ export { type AnchorDef, BASE_PARTICLES, type Experience, type ExperienceOptions, type ExperienceState, FLOOR_Y, type Formation, type FormationDef, type FormationFrame, type FormationLook, type FormationOptions, type FormationProgress, type FormationShader, type FormationViews, type GlStatus, HOLD_MARGIN, type Mat4, type PackedShapes, STACKED_QUERY, type SectionSpan, type ShaderPart, type Shape, type StageView, type Uniforms, type Vec2, type Vec3, type Vec4, approachOf, clamp, composeParticleShader, createExperience, damp, defineFormation, glslArray, glslFloat, glslVec, initialExperienceState, lerp, lerp3, localOf, mergeViews, namespaceGlsl, packShapes, presenceOf, progressOf, projectToScreen, sectionSpan, smoothstep, stageAt };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { clamp, damp, lerp, lerp3, projectToScreen, smoothstep } from "./gl/math.js";
2
- import { approachOf, localOf, presenceOf, progressOf, stageAt } from "./gl/stage.js";
2
+ import { HOLD_MARGIN, approachOf, localOf, presenceOf, progressOf, sectionSpan, stageAt } from "./gl/stage.js";
3
3
  import { createExperience, initialExperienceState } from "./experience.js";
4
4
  import { BASE_PARTICLES, FLOOR_Y } from "./gl/constants.js";
5
5
  import { packShapes } from "./gl/shapes.js";
@@ -7,4 +7,4 @@ import { glslArray, glslFloat, glslVec } from "./gl/glsl.js";
7
7
  import { composeParticleShader, namespaceGlsl } from "./gl/compose.js";
8
8
  import { defineFormation, mergeViews } from "./gl/formation.js";
9
9
  import { STACKED_QUERY } from "./layout.js";
10
- export { BASE_PARTICLES, FLOOR_Y, STACKED_QUERY, approachOf, clamp, composeParticleShader, createExperience, damp, defineFormation, glslArray, glslFloat, glslVec, initialExperienceState, lerp, lerp3, localOf, mergeViews, namespaceGlsl, packShapes, presenceOf, progressOf, projectToScreen, smoothstep, stageAt };
10
+ export { BASE_PARTICLES, FLOOR_Y, HOLD_MARGIN, STACKED_QUERY, approachOf, clamp, composeParticleShader, createExperience, damp, defineFormation, glslArray, glslFloat, glslVec, initialExperienceState, lerp, lerp3, localOf, mergeViews, namespaceGlsl, packShapes, presenceOf, progressOf, projectToScreen, sectionSpan, smoothstep, stageAt };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxfront/scene",
3
- "version": "0.2.0",
3
+ "version": "0.4.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",