@uxfront/scene 0.3.0 → 0.4.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.
package/README.md CHANGED
@@ -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
- | `[data-progress]` under `root` | Gets `--ux-progress`: 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, each once it's on screen. |
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.
@@ -1,5 +1,5 @@
1
1
  import { clamp, damp, projectToScreen } from "./gl/math.js";
2
- import { progressOf, stageAt } from "./gl/stage.js";
2
+ import { progressOf, sectionSpan, stageAt, stepAt } from "./gl/stage.js";
3
3
  //#region src/experience.ts
4
4
  const initialExperienceState = () => ({
5
5
  chapter: 0,
@@ -18,6 +18,17 @@ const STATE_KEYS = [
18
18
  "fps"
19
19
  ];
20
20
  const INTERACTIVE = "a, button, input, select, textarea, label, summary";
21
+ /**
22
+ * The bottom of the screen, as a fraction of its height, that copy has to
23
+ * clear to count as arrived: to be revealed, and for a step to light up.
24
+ */
25
+ const ARRIVAL_MARGIN = .12;
26
+ /** The element's page offset, leaving out transforms like the reveals' rise. */
27
+ function pageTop(el) {
28
+ let top = 0;
29
+ for (let node = el; node instanceof HTMLElement; node = node.offsetParent) top += node.offsetTop;
30
+ return top;
31
+ }
21
32
  function detectQuality() {
22
33
  if (Math.min(screen.width, screen.height) < 700) return "low";
23
34
  if (matchMedia("(pointer: coarse)").matches) return "medium";
@@ -73,11 +84,14 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
73
84
  const scrollY = window.scrollY;
74
85
  sections = Array.from(root.querySelectorAll("[data-stage]")).map((el) => {
75
86
  const rect = el.getBoundingClientRect();
76
- const start = rect.top + scrollY;
87
+ const bottoms = [];
88
+ el.querySelectorAll("[data-step-item]").forEach((item) => {
89
+ bottoms[Number(item.dataset.stepItem)] = pageTop(item) + item.offsetHeight;
90
+ });
77
91
  return {
78
92
  el,
79
- start,
80
- end: Math.max(start, start + rect.height - viewport)
93
+ bottoms,
94
+ ...sectionSpan(rect.top + scrollY, rect.height, viewport, maxScroll)
81
95
  };
82
96
  });
83
97
  engine?.resize();
@@ -93,7 +107,7 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
93
107
  entry.target.classList.add("is-in");
94
108
  revealObserver.unobserve(entry.target);
95
109
  }
96
- }, { rootMargin: "0px 0px -12% 0px" });
110
+ }, { rootMargin: `0px 0px -${ARRIVAL_MARGIN * 100}% 0px` });
97
111
  root.querySelectorAll("[data-reveal]").forEach((el) => revealObserver.observe(el));
98
112
  let pointerX = 0;
99
113
  let pointerY = 0;
@@ -178,7 +192,8 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
178
192
  }
179
193
  ({stage, local} = stageAt(sections, followY));
180
194
  const held = Math.round(stage);
181
- const section = sections[held]?.el ?? null;
195
+ const current = sections[held];
196
+ const section = current?.el ?? null;
182
197
  const activeId = section?.id || null;
183
198
  if (held !== state.chapter || activeId !== state.activeId) set({
184
199
  chapter: held,
@@ -193,11 +208,13 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
193
208
  activeSection?.removeAttribute("data-step");
194
209
  activeSection = section;
195
210
  }
196
- if (section) {
211
+ if (current && section) {
197
212
  const steps = Number(section.dataset.steps || 0);
198
213
  if (steps > 0) {
199
- const step = String(Math.min(steps - 1, Math.floor(clamp(local, 0, .999) * steps)));
200
- if (section.dataset.step !== step) section.dataset.step = step;
214
+ const reach = viewport * .88;
215
+ const step = stepAt(scrollY, current, steps, current.bottoms, reach);
216
+ if (step < 0) section.removeAttribute("data-step");
217
+ else if (section.dataset.step !== `${step}`) section.dataset.step = `${step}`;
201
218
  }
202
219
  }
203
220
  if (engine) {
@@ -12,17 +12,39 @@ 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;
26
38
  local: number;
27
39
  };
40
+ /**
41
+ * The item a section with `steps` items highlights at scroll offset `y`, or
42
+ * -1 for none. An item is on screen once its bottom (`bottoms`, page offsets
43
+ * by step) is less than `reach` below `y`. The items light up one after
44
+ * another from the moment the first one is on screen until the end of the
45
+ * section's span, and never ahead of the copy: on stacked layouts, they're
46
+ * still below the fold for much of the span. Past the span, the section stays
47
+ * on its last step.
48
+ */
49
+ export declare function stepAt(y: number, span: SectionSpan, steps: number, bottoms: number[], reach: number): number;
28
50
  //#endregion
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++) {
@@ -51,5 +70,21 @@ function stageAt(sections, y) {
51
70
  local: 1
52
71
  };
53
72
  }
73
+ /**
74
+ * The item a section with `steps` items highlights at scroll offset `y`, or
75
+ * -1 for none. An item is on screen once its bottom (`bottoms`, page offsets
76
+ * by step) is less than `reach` below `y`. The items light up one after
77
+ * another from the moment the first one is on screen until the end of the
78
+ * section's span, and never ahead of the copy: on stacked layouts, they're
79
+ * still below the fold for much of the span. Past the span, the section stays
80
+ * on its last step.
81
+ */
82
+ function stepAt(y, span, steps, bottoms, reach) {
83
+ const from = clamp((bottoms[0] ?? -Infinity) - reach, span.start, span.end);
84
+ const read = span.end > from ? (y - from) / (span.end - from) : y < from ? 0 : 1;
85
+ let step = Math.min(steps - 1, Math.floor(clamp(read, 0, .999) * steps));
86
+ while (step >= 0 && (bottoms[step] ?? -Infinity) > y + reach) step--;
87
+ return step;
88
+ }
54
89
  //#endregion
55
- export { approachOf, localOf, presenceOf, progressOf, stageAt };
90
+ export { HOLD_MARGIN, approachOf, localOf, presenceOf, progressOf, sectionSpan, stageAt, stepAt };
@@ -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, stepAt } 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, stepAt };
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, stepAt } 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, stepAt };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxfront/scene",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
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",