@uxfront/scene 0.4.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 +7 -7
- package/dist/experience.js +25 -7
- package/dist/gl/stage.d.ts +10 -0
- package/dist/gl/stage.js +17 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -54,13 +54,13 @@ 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, 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.
|
|
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
64
|
|
|
65
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.
|
|
66
66
|
|
package/dist/experience.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { clamp, damp, projectToScreen } from "./gl/math.js";
|
|
2
|
-
import {
|
|
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,8 +84,13 @@ 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();
|
|
87
|
+
const bottoms = [];
|
|
88
|
+
el.querySelectorAll("[data-step-item]").forEach((item) => {
|
|
89
|
+
bottoms[Number(item.dataset.stepItem)] = pageTop(item) + item.offsetHeight;
|
|
90
|
+
});
|
|
76
91
|
return {
|
|
77
92
|
el,
|
|
93
|
+
bottoms,
|
|
78
94
|
...sectionSpan(rect.top + scrollY, rect.height, viewport, maxScroll)
|
|
79
95
|
};
|
|
80
96
|
});
|
|
@@ -91,7 +107,7 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
91
107
|
entry.target.classList.add("is-in");
|
|
92
108
|
revealObserver.unobserve(entry.target);
|
|
93
109
|
}
|
|
94
|
-
}, { rootMargin:
|
|
110
|
+
}, { rootMargin: `0px 0px -${ARRIVAL_MARGIN * 100}% 0px` });
|
|
95
111
|
root.querySelectorAll("[data-reveal]").forEach((el) => revealObserver.observe(el));
|
|
96
112
|
let pointerX = 0;
|
|
97
113
|
let pointerY = 0;
|
|
@@ -176,7 +192,8 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
176
192
|
}
|
|
177
193
|
({stage, local} = stageAt(sections, followY));
|
|
178
194
|
const held = Math.round(stage);
|
|
179
|
-
const
|
|
195
|
+
const current = sections[held];
|
|
196
|
+
const section = current?.el ?? null;
|
|
180
197
|
const activeId = section?.id || null;
|
|
181
198
|
if (held !== state.chapter || activeId !== state.activeId) set({
|
|
182
199
|
chapter: held,
|
|
@@ -191,12 +208,13 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
191
208
|
activeSection?.removeAttribute("data-step");
|
|
192
209
|
activeSection = section;
|
|
193
210
|
}
|
|
194
|
-
if (section) {
|
|
211
|
+
if (current && section) {
|
|
195
212
|
const steps = Number(section.dataset.steps || 0);
|
|
196
213
|
if (steps > 0) {
|
|
197
|
-
const
|
|
198
|
-
const step =
|
|
199
|
-
if (
|
|
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}`;
|
|
200
218
|
}
|
|
201
219
|
}
|
|
202
220
|
if (engine) {
|
package/dist/gl/stage.d.ts
CHANGED
|
@@ -37,4 +37,14 @@ export declare function stageAt(sections: SectionSpan[], y: number): {
|
|
|
37
37
|
stage: number;
|
|
38
38
|
local: number;
|
|
39
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;
|
|
40
50
|
//#endregion
|
package/dist/gl/stage.js
CHANGED
|
@@ -70,5 +70,21 @@ function stageAt(sections, y) {
|
|
|
70
70
|
local: 1
|
|
71
71
|
};
|
|
72
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
|
+
}
|
|
73
89
|
//#endregion
|
|
74
|
-
export { HOLD_MARGIN, approachOf, localOf, presenceOf, progressOf, sectionSpan, stageAt };
|
|
90
|
+
export { HOLD_MARGIN, approachOf, localOf, presenceOf, progressOf, sectionSpan, stageAt, stepAt };
|
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 { HOLD_MARGIN, SectionSpan, approachOf, localOf, presenceOf, progressOf, sectionSpan, 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, 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 };
|
|
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 { HOLD_MARGIN, approachOf, localOf, presenceOf, progressOf, sectionSpan, 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, 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 };
|
|
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.4.
|
|
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",
|