@uxfront/scene 0.4.0 → 0.4.2
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 +49 -9
- package/dist/experience.js +37 -11
- package/dist/formations/index.d.ts +2 -1
- package/dist/formations/index.js +2 -1
- package/dist/formations/outline/index.d.ts +15 -0
- package/dist/formations/outline/index.js +42 -0
- package/dist/formations/outline/shader.js +308 -0
- package/dist/gl/engine.js +29 -6
- package/dist/gl/formation.js +3 -2
- package/dist/gl/stage.d.ts +10 -0
- package/dist/gl/stage.js +17 -1
- package/dist/gl/types.d.ts +36 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
The scroll-driven WebGL particle scene behind the uxfront.com family of homepages (UXFront, Open Components, Unframework, Styleframe, Inkline). One GPU particle system turns into a new **formation** for each section of the page as you scroll. The package has no framework dependency: `@uxfront/ui` wraps it in Vue components, and any other page can drive it directly.
|
|
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
|
-
- **A catalog.**
|
|
6
|
+
- **A catalog.** Nine formations: `monolith`, `plates`, `prism`, `tokens`, `ink`, `corridor`, `stack`, `threshold` and `outline`.
|
|
7
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.
|
|
@@ -54,13 +54,36 @@ 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
|
+
| `[data-outline]` in an `outline` section | The element the `outline` formation's particles gather around. The outline follows the page's scroll each frame, so it stays on the element however fast you scroll. |
|
|
65
|
+
| `data-gather` on `[data-outline]` | Written by `outline`: how far the particles have gathered, 0 → 1. It reaches 1 only once every particle has arrived. |
|
|
66
|
+
| `--ux-reveal` on `[data-outline]` | Written by `outline`: 0 until the gather is complete, then 1 over the start of the hold. It's 1 at once without motion and in `"fallback"`. |
|
|
67
|
+
|
|
68
|
+
To fade an element's content in once the particles have gathered around it, read `--ux-reveal` with a fallback of 1, and only once the scene is `ready`, so the content shows without JavaScript, while the scene loads and in `"fallback"`:
|
|
69
|
+
|
|
70
|
+
```html
|
|
71
|
+
<section data-stage>
|
|
72
|
+
<img data-outline src="/figma.png" alt="The Figma plugin beside a component's code" />
|
|
73
|
+
</section>
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
experience.subscribe(({ status }) => {
|
|
78
|
+
document.body.toggleAttribute("data-scene-ready", status === "ready");
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```css
|
|
83
|
+
[data-scene-ready] [data-outline] {
|
|
84
|
+
opacity: var(--ux-reveal, 1);
|
|
85
|
+
}
|
|
86
|
+
```
|
|
64
87
|
|
|
65
88
|
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
89
|
|
|
@@ -80,6 +103,21 @@ Every factory takes `{ label, key?, views?, look? }`. `label` is shown in the HU
|
|
|
80
103
|
| `corridor` | A corridor of streaking light with long trails. Suits a closing section. | None |
|
|
81
104
|
| `stack` | Seven glass cards dealt as the chapter is read, one per rail, each holding the same component. `art` swaps it. | `stack:0`–`stack:6`, in the cards' headers |
|
|
82
105
|
| `threshold` | The prism's seven beams cross a soap film of light and converge into one white stream. | `threshold:stream`, above the stream |
|
|
106
|
+
| `outline` | Gathers around the section's `[data-outline]` element, like an image or a code panel. `lines` draws its code. | None |
|
|
107
|
+
|
|
108
|
+
`outline` places its particles on the element itself, so it doesn't need `views`. With `lines: true`, it also draws the text lines of the `pre` blocks inside the element, scattering particles along each line's characters, before the code fades in. Its look turns `streak` off, so the outline stays crisp while the page scrolls. In the page's first section, which holds from the top, `--ux-reveal` is 1 at rest.
|
|
109
|
+
|
|
110
|
+
### Look
|
|
111
|
+
|
|
112
|
+
`look` tunes the post-processing for one formation, and the scene blends it across the morphs on either side.
|
|
113
|
+
|
|
114
|
+
| Key | Default | Effect |
|
|
115
|
+
| ---------- | ------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
|
116
|
+
| `bloom` | `1` | Strength of the glow around bright particles. |
|
|
117
|
+
| `trail` | `0` | How long light trails persist, 0 (none) to 1. |
|
|
118
|
+
| `exposure` | `1` | Overall brightness. |
|
|
119
|
+
| `reflect` | `0` | Strength of the mirror-floor reflection. |
|
|
120
|
+
| `streak` | `1` | How much scrolling between formations lengthens the trail and widens the colour fringe, 0 (none) to 1. `outline` sets it to 0. |
|
|
83
121
|
|
|
84
122
|
## Writing a formation
|
|
85
123
|
|
|
@@ -128,7 +166,9 @@ Particle $main(uint id, float sel, Frame f) {
|
|
|
128
166
|
- Shared helpers: `hashU`, `rnd`, `seedOf`, `gauss`, `noised`, `curl`, `bitangent`, `bezier3`, `rrectPoint`, `shapePoint`, `PI`, `TAU` and `FLOOR_Y`.
|
|
129
167
|
- `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.
|
|
130
168
|
- A formation that follows the prism can take its `RAILS` and `SPECTRUM` from `@uxfront/scene/formations`, so rail `i` keeps its colour.
|
|
131
|
-
- `setup` uploads uniforms once
|
|
169
|
+
- `setup(uniforms, scene)` uploads uniforms once, and can start observers and listeners. `scene` holds the formation's `index` and the scene's `root`, so its section is `root.querySelectorAll("[data-stage]")[index]`. Measure the page there, on resize, since the frame loop must never force a layout.
|
|
170
|
+
- `update(uniforms, frame)` runs every frame with the formation's `index` (its place in the scene, and its section's), `local`, `approach`, `presence`, `time`, `dt`, `motion`, `scrolled` and `scrollY`. `scrollY` is the page's scroll position, read before the scene writes anything in the frame. Use it rather than `window.scrollY`, which makes the browser restyle the page after those writes.
|
|
171
|
+
- `teardown()` stops what `setup` started. The scene calls it when it's destroyed, and when it loses its WebGL context, before it loads the shaders again for a restored one.
|
|
132
172
|
- `anchors` pin DOM labels: `place(progress, out)` writes a world position and returns extra visibility, and `render(el, progress)` can update the label's content.
|
|
133
173
|
|
|
134
174
|
`composeParticleShader` builds the complete vertex shader, handy for testing a formation's GLSL.
|
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";
|
|
@@ -62,6 +73,12 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
62
73
|
let canvasWidth = 1;
|
|
63
74
|
let canvasHeight = 1;
|
|
64
75
|
const html = document.documentElement;
|
|
76
|
+
const fallBack = () => {
|
|
77
|
+
set({ status: "fallback" });
|
|
78
|
+
root.querySelectorAll("[data-outline]").forEach((el) => {
|
|
79
|
+
el.style.setProperty("--ux-reveal", "1");
|
|
80
|
+
});
|
|
81
|
+
};
|
|
65
82
|
const setMotion = (on) => set({ motion: on });
|
|
66
83
|
const onReducedChange = () => setMotion(!reducedQuery.matches);
|
|
67
84
|
reducedQuery.addEventListener("change", onReducedChange);
|
|
@@ -73,8 +90,13 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
73
90
|
const scrollY = window.scrollY;
|
|
74
91
|
sections = Array.from(root.querySelectorAll("[data-stage]")).map((el) => {
|
|
75
92
|
const rect = el.getBoundingClientRect();
|
|
93
|
+
const bottoms = [];
|
|
94
|
+
el.querySelectorAll("[data-step-item]").forEach((item) => {
|
|
95
|
+
bottoms[Number(item.dataset.stepItem)] = pageTop(item) + item.offsetHeight;
|
|
96
|
+
});
|
|
76
97
|
return {
|
|
77
98
|
el,
|
|
99
|
+
bottoms,
|
|
78
100
|
...sectionSpan(rect.top + scrollY, rect.height, viewport, maxScroll)
|
|
79
101
|
};
|
|
80
102
|
});
|
|
@@ -91,7 +113,7 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
91
113
|
entry.target.classList.add("is-in");
|
|
92
114
|
revealObserver.unobserve(entry.target);
|
|
93
115
|
}
|
|
94
|
-
}, { rootMargin:
|
|
116
|
+
}, { rootMargin: `0px 0px -${ARRIVAL_MARGIN * 100}% 0px` });
|
|
95
117
|
root.querySelectorAll("[data-reveal]").forEach((el) => revealObserver.observe(el));
|
|
96
118
|
let pointerX = 0;
|
|
97
119
|
let pointerY = 0;
|
|
@@ -176,7 +198,8 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
176
198
|
}
|
|
177
199
|
({stage, local} = stageAt(sections, followY));
|
|
178
200
|
const held = Math.round(stage);
|
|
179
|
-
const
|
|
201
|
+
const current = sections[held];
|
|
202
|
+
const section = current?.el ?? null;
|
|
180
203
|
const activeId = section?.id || null;
|
|
181
204
|
if (held !== state.chapter || activeId !== state.activeId) set({
|
|
182
205
|
chapter: held,
|
|
@@ -191,18 +214,20 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
191
214
|
activeSection?.removeAttribute("data-step");
|
|
192
215
|
activeSection = section;
|
|
193
216
|
}
|
|
194
|
-
if (section) {
|
|
217
|
+
if (current && section) {
|
|
195
218
|
const steps = Number(section.dataset.steps || 0);
|
|
196
219
|
if (steps > 0) {
|
|
197
|
-
const
|
|
198
|
-
const step =
|
|
199
|
-
if (
|
|
220
|
+
const reach = viewport * .88;
|
|
221
|
+
const step = stepAt(scrollY, current, steps, current.bottoms, reach);
|
|
222
|
+
if (step < 0) section.removeAttribute("data-step");
|
|
223
|
+
else if (section.dataset.step !== `${step}`) section.dataset.step = `${step}`;
|
|
200
224
|
}
|
|
201
225
|
}
|
|
202
226
|
if (engine) {
|
|
203
227
|
engine.render({
|
|
204
228
|
stage: clamp(stage, 0, formations.length - 1),
|
|
205
229
|
local,
|
|
230
|
+
scrollY,
|
|
206
231
|
scrolled,
|
|
207
232
|
pointerX,
|
|
208
233
|
pointerY,
|
|
@@ -224,7 +249,7 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
224
249
|
const boot = async () => {
|
|
225
250
|
if (disposed) return;
|
|
226
251
|
if (typeof WebGL2RenderingContext === "undefined") {
|
|
227
|
-
|
|
252
|
+
fallBack();
|
|
228
253
|
return;
|
|
229
254
|
}
|
|
230
255
|
set({ status: "loading" });
|
|
@@ -232,6 +257,7 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
232
257
|
const [{ createEngine }, shaders] = await Promise.all([import("./gl/engine.js"), Promise.all(formations.map((formation) => formation.shader()))]);
|
|
233
258
|
if (disposed) return;
|
|
234
259
|
const created = await createEngine(canvas, {
|
|
260
|
+
root,
|
|
235
261
|
formations,
|
|
236
262
|
shaders,
|
|
237
263
|
quality: detectQuality(),
|
|
@@ -239,7 +265,7 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
239
265
|
onContextLost: () => {
|
|
240
266
|
engine?.destroy();
|
|
241
267
|
engine = null;
|
|
242
|
-
|
|
268
|
+
fallBack();
|
|
243
269
|
}
|
|
244
270
|
});
|
|
245
271
|
if (disposed) {
|
|
@@ -247,7 +273,7 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
247
273
|
return;
|
|
248
274
|
}
|
|
249
275
|
if (!created) {
|
|
250
|
-
|
|
276
|
+
fallBack();
|
|
251
277
|
return;
|
|
252
278
|
}
|
|
253
279
|
engine = created;
|
|
@@ -257,7 +283,7 @@ function createExperience({ root, canvas, formations, anchorRoots = [] }) {
|
|
|
257
283
|
});
|
|
258
284
|
} catch (error) {
|
|
259
285
|
console.warn(error);
|
|
260
|
-
|
|
286
|
+
fallBack();
|
|
261
287
|
}
|
|
262
288
|
};
|
|
263
289
|
const onContextRestored = () => void boot();
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { corridor } from "./corridor/index.js";
|
|
2
2
|
import { ink } from "./ink/index.js";
|
|
3
3
|
import { monolith } from "./monolith/index.js";
|
|
4
|
+
import { OutlineOptions, outline } from "./outline/index.js";
|
|
4
5
|
import { OPEN_COMPONENTS_ART, PlateShape, circle, disc, fill, plateBorder, rrect, seg } from "./plates/art.js";
|
|
5
6
|
import { PlatesOptions, plates } from "./plates/index.js";
|
|
6
7
|
import { PLATE } from "./plates/geometry.js";
|
|
@@ -13,4 +14,4 @@ import { CARD, CARD_NAME } from "./stack/geometry.js";
|
|
|
13
14
|
import { threshold } from "./threshold/index.js";
|
|
14
15
|
import { THEMES, Theme } from "./tokens/geometry.js";
|
|
15
16
|
import { TokensFormation, tokens } from "./tokens/index.js";
|
|
16
|
-
export { CARD, CARD_NAME, CARD_ROLE, type CardRole, type CardShape, KNOB_TRAVEL, OPEN_COMPONENTS_ART, PLATE, type PlateShape, type PlatesOptions, RAILS, SIGN_UP_FORM_ART, SPECTRUM, type StackOptions, THEMES, type Theme, type TokensFormation, circle, corridor, disc, fill, ink, monolith, plateBorder, plates, prism, rrect, seg, stack, threshold, tokens };
|
|
17
|
+
export { CARD, CARD_NAME, CARD_ROLE, type CardRole, type CardShape, KNOB_TRAVEL, OPEN_COMPONENTS_ART, type OutlineOptions, PLATE, type PlateShape, type PlatesOptions, RAILS, SIGN_UP_FORM_ART, SPECTRUM, type StackOptions, THEMES, type Theme, type TokensFormation, circle, corridor, disc, fill, ink, monolith, outline, plateBorder, plates, prism, rrect, seg, stack, threshold, tokens };
|
package/dist/formations/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { corridor } from "./corridor/index.js";
|
|
2
2
|
import { ink } from "./ink/index.js";
|
|
3
3
|
import { monolith } from "./monolith/index.js";
|
|
4
|
+
import { outline } from "./outline/index.js";
|
|
4
5
|
import { PLATE } from "./plates/geometry.js";
|
|
5
6
|
import { plates } from "./plates/index.js";
|
|
6
7
|
import { OPEN_COMPONENTS_ART, circle, disc, fill, plateBorder, rrect, seg } from "./plates/art.js";
|
|
@@ -13,4 +14,4 @@ import { CARD_ROLE, KNOB_TRAVEL, SIGN_UP_FORM_ART } from "./stack/art.js";
|
|
|
13
14
|
import { threshold } from "./threshold/index.js";
|
|
14
15
|
import { THEMES } from "./tokens/geometry.js";
|
|
15
16
|
import { tokens } from "./tokens/index.js";
|
|
16
|
-
export { CARD, CARD_NAME, CARD_ROLE, KNOB_TRAVEL, OPEN_COMPONENTS_ART, PLATE, RAILS, SIGN_UP_FORM_ART, SPECTRUM, THEMES, circle, corridor, disc, fill, ink, monolith, plateBorder, plates, prism, rrect, seg, stack, threshold, tokens };
|
|
17
|
+
export { CARD, CARD_NAME, CARD_ROLE, KNOB_TRAVEL, OPEN_COMPONENTS_ART, PLATE, RAILS, SIGN_UP_FORM_ART, SPECTRUM, THEMES, circle, corridor, disc, fill, ink, monolith, outline, plateBorder, plates, prism, rrect, seg, stack, threshold, tokens };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Formation, FormationOptions } from "../../gl/types.js";
|
|
2
|
+
//#region src/formations/outline/index.d.ts
|
|
3
|
+
export interface OutlineOptions extends FormationOptions {
|
|
4
|
+
/** Also draws the text lines of the `pre` blocks inside the element, like a code sample. */
|
|
5
|
+
lines?: boolean;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Particles that gather into the outline of an element on the page, the one
|
|
9
|
+
* marked `data-outline` in the formation's `[data-stage]` section, as the
|
|
10
|
+
* section scrolls in. The outline stays on the element at any scroll speed,
|
|
11
|
+
* and the scene writes `data-gather` and `--ux-reveal` on it, so the page can
|
|
12
|
+
* fade the element's content in once the particles have gathered.
|
|
13
|
+
*/
|
|
14
|
+
export declare function outline(options: OutlineOptions): Formation;
|
|
15
|
+
//#endregion
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { defineFormation, mergeViews } from "../../gl/formation.js";
|
|
2
|
+
//#region src/formations/outline/index.ts
|
|
3
|
+
const VIEW = {
|
|
4
|
+
eye: [
|
|
5
|
+
0,
|
|
6
|
+
0,
|
|
7
|
+
8
|
|
8
|
+
],
|
|
9
|
+
target: [
|
|
10
|
+
0,
|
|
11
|
+
0,
|
|
12
|
+
0
|
|
13
|
+
],
|
|
14
|
+
fov: 40,
|
|
15
|
+
span: [0, 0],
|
|
16
|
+
shift: [0, 0]
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Particles that gather into the outline of an element on the page, the one
|
|
20
|
+
* marked `data-outline` in the formation's `[data-stage]` section, as the
|
|
21
|
+
* section scrolls in. The outline stays on the element at any scroll speed,
|
|
22
|
+
* and the scene writes `data-gather` and `--ux-reveal` on it, so the page can
|
|
23
|
+
* fade the element's content in once the particles have gathered.
|
|
24
|
+
*/
|
|
25
|
+
function outline(options) {
|
|
26
|
+
const lines = options.lines ?? false;
|
|
27
|
+
return defineFormation({
|
|
28
|
+
key: options.key ?? "outline",
|
|
29
|
+
label: options.label,
|
|
30
|
+
views: mergeViews({
|
|
31
|
+
desktop: VIEW,
|
|
32
|
+
mobile: VIEW
|
|
33
|
+
}, options.views),
|
|
34
|
+
look: {
|
|
35
|
+
streak: 0,
|
|
36
|
+
...options.look
|
|
37
|
+
},
|
|
38
|
+
shader: () => import("./shader.js").then((m) => m.createShader(lines))
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
export { outline };
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { clamp, smoothstep } from "../../gl/math.js";
|
|
2
|
+
import { sectionSpan } from "../../gl/stage.js";
|
|
3
|
+
//#region src/formations/outline/shader.ts
|
|
4
|
+
/** Text lines the particles draw, at most: four to a `vec4`. */
|
|
5
|
+
const MAX_LINES = 48;
|
|
6
|
+
/** How far the page scrolls while the content fades in, in screen heights. */
|
|
7
|
+
const FADE = .08;
|
|
8
|
+
/**
|
|
9
|
+
* How far the particles have gathered into the outline, 0 → 1. The engine
|
|
10
|
+
* moves them in from the formation before until `approach` reaches 1, so the
|
|
11
|
+
* gather is only complete then, whatever `$main`'s own schedule.
|
|
12
|
+
*/
|
|
13
|
+
const gatherOf = (approach) => clamp(approach, 0, 1);
|
|
14
|
+
/** The gather as `data-gather` shows it: floored, so it reads 1 only at 1. */
|
|
15
|
+
const gatherText = (approach) => (Math.floor(gatherOf(approach) * 1e3) / 1e3).toFixed(3);
|
|
16
|
+
/**
|
|
17
|
+
* How far the content has faded in: nothing until the gather is complete,
|
|
18
|
+
* then in over the first `span` of the section's hold (`local`).
|
|
19
|
+
*/
|
|
20
|
+
const revealOf = (approach, local, span) => {
|
|
21
|
+
if (approach < 1) return 0;
|
|
22
|
+
return span > 0 ? smoothstep(0, span, local) : 1;
|
|
23
|
+
};
|
|
24
|
+
const glsl = `
|
|
25
|
+
const int $MAX_LINES = ${MAX_LINES};
|
|
26
|
+
|
|
27
|
+
// The element in the canvas's CSS pixels: left, top, width, height.
|
|
28
|
+
uniform vec4 $rect;
|
|
29
|
+
// The canvas's size in CSS pixels.
|
|
30
|
+
uniform vec2 $screen;
|
|
31
|
+
// The element's corner radius in CSS pixels.
|
|
32
|
+
uniform float $radius;
|
|
33
|
+
// The text inside the element: its left and its first line's top (from the
|
|
34
|
+
// element's corner), the line height and the width of a character.
|
|
35
|
+
uniform vec4 $text;
|
|
36
|
+
// The text's width, past which a line is cut.
|
|
37
|
+
uniform float $clip;
|
|
38
|
+
// How far the content has faded in (revealOf).
|
|
39
|
+
uniform float $reveal;
|
|
40
|
+
// Each line, packed as indent + 64 * length + 8192 * row, in characters and
|
|
41
|
+
// rows of the line height.
|
|
42
|
+
uniform vec4 $lines[${MAX_LINES / 4}];
|
|
43
|
+
uniform float $count;
|
|
44
|
+
uniform float $chars;
|
|
45
|
+
|
|
46
|
+
const float $DEPTH = 8.0;
|
|
47
|
+
const float $OUTSET = 3.0;
|
|
48
|
+
|
|
49
|
+
// A point on pixel px of the canvas, depth in front of the camera: it stays on
|
|
50
|
+
// that pixel however the camera sways, so it keeps to the element.
|
|
51
|
+
vec3 $place(vec2 px, float depth) {
|
|
52
|
+
vec2 ndc = vec2(px.x / $screen.x * 2.0 - 1.0, 1.0 - px.y / $screen.y * 2.0);
|
|
53
|
+
vec3 right = vec3(uView[0][0], uView[1][0], uView[2][0]);
|
|
54
|
+
vec3 up = vec3(uView[0][1], uView[1][1], uView[2][1]);
|
|
55
|
+
vec3 forward = -vec3(uView[0][2], uView[1][2], uView[2][2]);
|
|
56
|
+
float x = (ndc.x + uProjection[2][0]) / uProjection[0][0];
|
|
57
|
+
float y = (ndc.y + uProjection[2][1]) / uProjection[1][1];
|
|
58
|
+
return uRayOrigin + (forward + right * x + up * y) * depth;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Character k of the text, counted over every line: its line's packed value
|
|
62
|
+
// and the character's place in it.
|
|
63
|
+
vec2 $char(float k) {
|
|
64
|
+
float left = k;
|
|
65
|
+
for (int i = 0; i < $MAX_LINES; i++) {
|
|
66
|
+
if (float(i) >= $count) break;
|
|
67
|
+
float packed = $lines[i / 4][i % 4];
|
|
68
|
+
float len = mod(floor(packed / 64.0), 128.0);
|
|
69
|
+
if (left < len || float(i) >= $count - 1.0) return vec2(packed, min(left, len));
|
|
70
|
+
left -= len;
|
|
71
|
+
}
|
|
72
|
+
return vec2(0.0);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
Particle $main(uint id, float sel, Frame f) {
|
|
76
|
+
uint s = seedOf(id, 0x0071E5u);
|
|
77
|
+
float a = rnd(s);
|
|
78
|
+
float b = rnd(s);
|
|
79
|
+
float c = rnd(s);
|
|
80
|
+
float d = rnd(s);
|
|
81
|
+
float e = rnd(s);
|
|
82
|
+
Particle P = blank();
|
|
83
|
+
|
|
84
|
+
// Gathered as the section comes in, each particle on its own beat; then
|
|
85
|
+
// the content fades in over them, and they dim.
|
|
86
|
+
float gather = smoothstep(d * 0.2, d * 0.2 + 0.5, f.approach);
|
|
87
|
+
float settle = $reveal;
|
|
88
|
+
// Without text, the outline takes the text's share of the particles.
|
|
89
|
+
float edge = $count > 0.0 ? 0.3 : 0.8;
|
|
90
|
+
|
|
91
|
+
vec2 size = $rect.zw;
|
|
92
|
+
vec2 px;
|
|
93
|
+
vec3 col;
|
|
94
|
+
if (sel < edge) {
|
|
95
|
+
// The outline, just outside the element, which hides what's behind it.
|
|
96
|
+
px = size * 0.5 + rrectPoint(vec4(0.0, 0.0, size * 0.5 + $OUTSET), $radius + $OUTSET, a);
|
|
97
|
+
px += (vec2(c, e) - 0.5) * 1.5;
|
|
98
|
+
col = vec3(0.85, 0.88, 0.96) * mix(0.09, 0.025, settle) * (0.3 / edge);
|
|
99
|
+
} else if (sel < 0.8) {
|
|
100
|
+
// The text, a character at a time.
|
|
101
|
+
vec2 ch = $char(floor(a * $chars));
|
|
102
|
+
float indent = mod(ch.x, 64.0);
|
|
103
|
+
float row = floor(ch.x / 8192.0);
|
|
104
|
+
float x = (indent + ch.y + b) * $text.w;
|
|
105
|
+
px = vec2($text.x + min(x, $clip), $text.y + (row + 0.3 + c * 0.4) * $text.z);
|
|
106
|
+
col = vec3(0.6, 0.86, 0.94) * 0.08 * (1.0 - settle);
|
|
107
|
+
} else {
|
|
108
|
+
// Dust that never settles, drifting around the element.
|
|
109
|
+
px = vec2(a, b) * size;
|
|
110
|
+
gather = 0.0;
|
|
111
|
+
col = vec3(0.7, 0.75, 0.95) * 0.006;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Loose: scattered around its place, nearer or deeper, in a slow current.
|
|
115
|
+
vec2 spread = gauss(c, e) * size * 0.15;
|
|
116
|
+
vec2 drift = curl(vec3(px * 0.004, f.time * 0.05 + d)).xy * 40.0;
|
|
117
|
+
vec2 loose = px + spread + drift;
|
|
118
|
+
float depth = $DEPTH * (1.0 + (e - 0.5) * 0.8);
|
|
119
|
+
|
|
120
|
+
P.pos = $place($rect.xy + mix(loose, px, gather), mix(depth, $DEPTH, gather));
|
|
121
|
+
P.col = col * mix(0.35, 1.0, gather);
|
|
122
|
+
P.size = 0.8;
|
|
123
|
+
return P;
|
|
124
|
+
}
|
|
125
|
+
`;
|
|
126
|
+
/** A corner radius in pixels; a percentage is of the shorter side. */
|
|
127
|
+
const radiusOf = (value, width, height) => {
|
|
128
|
+
const radius = Number.parseFloat(value) || 0;
|
|
129
|
+
return value.endsWith("%") ? radius * Math.min(width, height) / 100 : radius;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* The element's box on screen, less the rise of the reveals it's in: once
|
|
133
|
+
* the element has risen into place, the outline is already there. Only a
|
|
134
|
+
* reveal's children are taken back, since they settle at `transform: none`;
|
|
135
|
+
* any other transform stays, as the element keeps it at rest.
|
|
136
|
+
*/
|
|
137
|
+
const layoutBox = (el) => {
|
|
138
|
+
const box = el.getBoundingClientRect();
|
|
139
|
+
let x = 0;
|
|
140
|
+
let y = 0;
|
|
141
|
+
for (let node = el; node; node = node.parentElement) {
|
|
142
|
+
if (!node.parentElement?.hasAttribute("data-reveal")) continue;
|
|
143
|
+
const transform = getComputedStyle(node).transform;
|
|
144
|
+
if (!transform || transform === "none") continue;
|
|
145
|
+
const m = transform.slice(transform.indexOf("(") + 1, -1).split(",").map(Number);
|
|
146
|
+
const flat = transform.startsWith("matrix(");
|
|
147
|
+
x += m[flat ? 4 : 12] ?? 0;
|
|
148
|
+
y += m[flat ? 5 : 13] ?? 0;
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
left: box.left - x,
|
|
152
|
+
top: box.top - y,
|
|
153
|
+
width: box.width,
|
|
154
|
+
height: box.height
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
/** A block's text, one entry per line. */
|
|
158
|
+
const linesOf = (pre) => (pre.textContent ?? "").replace(/\n$/, "").split("\n");
|
|
159
|
+
/** Packs the text lines of the `pre` blocks in the element, null without any. */
|
|
160
|
+
const measureText = (element, box) => {
|
|
161
|
+
const blocks = Array.from(element.querySelectorAll("pre"));
|
|
162
|
+
const first = blocks[0];
|
|
163
|
+
if (!first) return null;
|
|
164
|
+
const style = getComputedStyle(first);
|
|
165
|
+
const fontSize = Number.parseFloat(style.fontSize);
|
|
166
|
+
const padding = Number.parseFloat(style.paddingTop);
|
|
167
|
+
const pitch = Number.parseFloat(style.lineHeight) || (first.clientHeight - 2 * padding) / linesOf(first).length;
|
|
168
|
+
const inset = Number.parseFloat(style.paddingLeft);
|
|
169
|
+
const firstBox = first.getBoundingClientRect();
|
|
170
|
+
const top = firstBox.top - box.top + padding;
|
|
171
|
+
const packed = [];
|
|
172
|
+
let chars = 0;
|
|
173
|
+
for (const pre of blocks) {
|
|
174
|
+
const rect = pre === first ? firstBox : pre.getBoundingClientRect();
|
|
175
|
+
const firstRow = Math.round((rect.top - box.top + padding - top) / pitch);
|
|
176
|
+
const rows = Math.round((pre.clientHeight - 2 * padding) / pitch);
|
|
177
|
+
linesOf(pre).slice(0, rows).forEach((line, i) => {
|
|
178
|
+
const row = firstRow + i;
|
|
179
|
+
const indent = Math.min(63, line.length - line.trimStart().length);
|
|
180
|
+
const length = Math.min(127, line.trim().length);
|
|
181
|
+
if (length === 0 || row > 63 || packed.length >= MAX_LINES) return;
|
|
182
|
+
packed.push(indent + 64 * length + 8192 * row);
|
|
183
|
+
chars += length;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
text: [
|
|
188
|
+
firstBox.left - box.left + inset,
|
|
189
|
+
top,
|
|
190
|
+
pitch,
|
|
191
|
+
fontSize * .6
|
|
192
|
+
],
|
|
193
|
+
clip: first.clientWidth - 2 * inset,
|
|
194
|
+
lines: Float32Array.from({ length: MAX_LINES }, (_, i) => packed[i] ?? 0),
|
|
195
|
+
count: packed.length,
|
|
196
|
+
chars
|
|
197
|
+
};
|
|
198
|
+
};
|
|
199
|
+
/**
|
|
200
|
+
* The GPU half of `outline`. It measures the element and its section when
|
|
201
|
+
* the page resizes, never per frame. Each frame, it moves the outline by the
|
|
202
|
+
* scroll position the scene read for the frame, which the page renders with,
|
|
203
|
+
* so the outline can't trail the element, and writes the gather and the
|
|
204
|
+
* reveal on the element.
|
|
205
|
+
*/
|
|
206
|
+
function createShader(lines) {
|
|
207
|
+
const layout = {
|
|
208
|
+
element: null,
|
|
209
|
+
span: 0,
|
|
210
|
+
left: 0,
|
|
211
|
+
top: 0,
|
|
212
|
+
width: 0,
|
|
213
|
+
height: 0,
|
|
214
|
+
screen: [0, 0],
|
|
215
|
+
radius: 0,
|
|
216
|
+
text: null
|
|
217
|
+
};
|
|
218
|
+
let measured = false;
|
|
219
|
+
let gather = "";
|
|
220
|
+
let reveal = "";
|
|
221
|
+
let stop = null;
|
|
222
|
+
const measure = (canvas, root, index) => {
|
|
223
|
+
if (!("getBoundingClientRect" in canvas)) return;
|
|
224
|
+
measured = true;
|
|
225
|
+
layout.screen = [canvas.clientWidth, canvas.clientHeight];
|
|
226
|
+
const section = root.querySelectorAll("[data-stage]")[index];
|
|
227
|
+
const element = section?.querySelector("[data-outline]");
|
|
228
|
+
if (!section || !element) return;
|
|
229
|
+
const viewport = window.innerHeight;
|
|
230
|
+
const maxScroll = Math.max(1, document.documentElement.scrollHeight - viewport);
|
|
231
|
+
const rect = section.getBoundingClientRect();
|
|
232
|
+
const hold = sectionSpan(rect.top + window.scrollY, rect.height, viewport, maxScroll);
|
|
233
|
+
const length = hold.end - hold.start;
|
|
234
|
+
layout.span = index > 0 && length > 0 ? Math.min(1, FADE * viewport / length) : 0;
|
|
235
|
+
if (element !== layout.element) gather = reveal = "";
|
|
236
|
+
const screen = canvas.getBoundingClientRect();
|
|
237
|
+
const place = layoutBox(element);
|
|
238
|
+
Object.assign(layout, {
|
|
239
|
+
element,
|
|
240
|
+
left: place.left - screen.left,
|
|
241
|
+
top: place.top - screen.top + window.scrollY,
|
|
242
|
+
width: place.width,
|
|
243
|
+
height: place.height
|
|
244
|
+
});
|
|
245
|
+
const corner = getComputedStyle(element).borderTopLeftRadius;
|
|
246
|
+
layout.radius = radiusOf(corner, place.width, place.height);
|
|
247
|
+
layout.text = lines ? measureText(element, element.getBoundingClientRect()) : null;
|
|
248
|
+
};
|
|
249
|
+
const upload = ({ gl, get }) => {
|
|
250
|
+
gl.uniform2f(get("screen"), ...layout.screen);
|
|
251
|
+
gl.uniform1f(get("radius"), layout.radius);
|
|
252
|
+
const { text } = layout;
|
|
253
|
+
if (!text) {
|
|
254
|
+
gl.uniform1f(get("count"), 0);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
gl.uniform4f(get("text"), ...text.text);
|
|
258
|
+
gl.uniform1f(get("clip"), text.clip);
|
|
259
|
+
gl.uniform4fv(get("lines"), text.lines);
|
|
260
|
+
gl.uniform1f(get("count"), text.count);
|
|
261
|
+
gl.uniform1f(get("chars"), text.chars);
|
|
262
|
+
};
|
|
263
|
+
return {
|
|
264
|
+
glsl,
|
|
265
|
+
setup({ gl }, scene) {
|
|
266
|
+
if (!scene) return;
|
|
267
|
+
const observer = new ResizeObserver(() => measure(gl.canvas, scene.root, scene.index));
|
|
268
|
+
observer.observe(document.body);
|
|
269
|
+
const onResize = () => {
|
|
270
|
+
observer.unobserve(document.body);
|
|
271
|
+
observer.observe(document.body);
|
|
272
|
+
};
|
|
273
|
+
window.addEventListener("resize", onResize);
|
|
274
|
+
stop = () => {
|
|
275
|
+
observer.disconnect();
|
|
276
|
+
window.removeEventListener("resize", onResize);
|
|
277
|
+
};
|
|
278
|
+
},
|
|
279
|
+
teardown() {
|
|
280
|
+
stop?.();
|
|
281
|
+
stop = null;
|
|
282
|
+
},
|
|
283
|
+
update(uniforms, frame) {
|
|
284
|
+
const { gl, get } = uniforms;
|
|
285
|
+
if (measured) {
|
|
286
|
+
measured = false;
|
|
287
|
+
upload(uniforms);
|
|
288
|
+
}
|
|
289
|
+
gl.uniform4f(get("rect"), layout.left, layout.top - (frame.scrollY ?? 0), layout.width, layout.height);
|
|
290
|
+
const amount = frame.motion ? revealOf(frame.approach, frame.local, layout.span) : 1;
|
|
291
|
+
gl.uniform1f(get("reveal"), amount);
|
|
292
|
+
const { element } = layout;
|
|
293
|
+
if (!element) return;
|
|
294
|
+
const nextGather = gatherText(frame.approach);
|
|
295
|
+
const nextReveal = amount.toFixed(3);
|
|
296
|
+
if (nextGather !== gather) {
|
|
297
|
+
gather = nextGather;
|
|
298
|
+
element.dataset.gather = gather;
|
|
299
|
+
}
|
|
300
|
+
if (nextReveal !== reveal) {
|
|
301
|
+
reveal = nextReveal;
|
|
302
|
+
element.style.setProperty("--ux-reveal", reveal);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
//#endregion
|
|
308
|
+
export { createShader, gatherOf, gatherText, revealOf };
|
package/dist/gl/engine.js
CHANGED
|
@@ -3,6 +3,7 @@ import { approachOf, localOf, presenceOf } from "./stage.js";
|
|
|
3
3
|
import { BASE_PARTICLES } from "./constants.js";
|
|
4
4
|
import { BLOOM_DOWN_FRAG, BLOOM_PREFILTER_FRAG, BLOOM_UP_FRAG, COMPOSITE_FRAG, PARTICLE_FRAG, QUAD_VERT, TRAIL_FRAG } from "./glsl.js";
|
|
5
5
|
import { composeParticleShader } from "./compose.js";
|
|
6
|
+
import { DEFAULT_LOOK } from "./formation.js";
|
|
6
7
|
import { STACKED_QUERY } from "../layout.js";
|
|
7
8
|
//#region src/gl/engine.ts
|
|
8
9
|
const PARTICLES = {
|
|
@@ -61,6 +62,18 @@ function fitEye(stageView, aspect, out) {
|
|
|
61
62
|
out[2] = stageView.target[2] + dz * k;
|
|
62
63
|
return out;
|
|
63
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* The trail's decay and the composite's colour fringe for a frame. Scrolling
|
|
67
|
+
* between formations lengthens the trail and widens the fringe by `boost`, as
|
|
68
|
+
* far as the look's `streak` lets it, and the intro rises through a long trail.
|
|
69
|
+
*/
|
|
70
|
+
function trailOf(look, boost, intro) {
|
|
71
|
+
const speed = boost * look.streak;
|
|
72
|
+
return {
|
|
73
|
+
decay: Math.max(look.trail, speed, (1 - intro) * .75),
|
|
74
|
+
aberration: .012 + speed * .03
|
|
75
|
+
};
|
|
76
|
+
}
|
|
64
77
|
const nextFrame = () => new Promise((resolve) => requestAnimationFrame(resolve));
|
|
65
78
|
/**
|
|
66
79
|
* Waits for the driver to finish compiling without ever blocking the main
|
|
@@ -138,7 +151,10 @@ async function createEngine(canvas, options) {
|
|
|
138
151
|
get: (name) => uniform(particleProgram, `${formation.key}_${name}`)
|
|
139
152
|
}));
|
|
140
153
|
gl.useProgram(particleProgram.program);
|
|
141
|
-
formationShaders.forEach((shader, i) => shader.setup?.(formationUniforms[i]
|
|
154
|
+
formationShaders.forEach((shader, i) => shader.setup?.(formationUniforms[i], {
|
|
155
|
+
index: i,
|
|
156
|
+
root: options.root
|
|
157
|
+
}));
|
|
142
158
|
const vao = gl.createVertexArray();
|
|
143
159
|
gl.bindVertexArray(vao);
|
|
144
160
|
gl.disable(gl.DEPTH_TEST);
|
|
@@ -285,7 +301,8 @@ async function createEngine(canvas, options) {
|
|
|
285
301
|
const blendLook = (stage, key) => {
|
|
286
302
|
const a = Math.min(Math.floor(stage), stageCount - 1);
|
|
287
303
|
const b = Math.min(a + 1, stageCount - 1);
|
|
288
|
-
|
|
304
|
+
const valueOf = (k) => formations[k].look[key] ?? DEFAULT_LOOK[key];
|
|
305
|
+
return lerp(valueOf(a), valueOf(b), smoothstep(0, 1, stage - a));
|
|
289
306
|
};
|
|
290
307
|
const updateCamera = (stage, motion) => {
|
|
291
308
|
const aspect = canvas.width / canvas.height;
|
|
@@ -418,6 +435,7 @@ async function createEngine(canvas, options) {
|
|
|
418
435
|
const shader = formationShaders[k];
|
|
419
436
|
if (!shader.update) continue;
|
|
420
437
|
const frame = {
|
|
438
|
+
index: k,
|
|
421
439
|
stage,
|
|
422
440
|
local: localOf(k, stage, local),
|
|
423
441
|
approach: approachOf(k, stage),
|
|
@@ -425,6 +443,7 @@ async function createEngine(canvas, options) {
|
|
|
425
443
|
time,
|
|
426
444
|
dt,
|
|
427
445
|
motion,
|
|
446
|
+
scrollY: input.scrollY,
|
|
428
447
|
scrolled: input.scrolled
|
|
429
448
|
};
|
|
430
449
|
shader.update(formationUniforms[k], frame);
|
|
@@ -436,7 +455,10 @@ async function createEngine(canvas, options) {
|
|
|
436
455
|
gl.drawArrays(gl.POINTS, 0, particleCount);
|
|
437
456
|
}
|
|
438
457
|
gl.disable(gl.BLEND);
|
|
439
|
-
const decay
|
|
458
|
+
const { decay, aberration } = trailOf({
|
|
459
|
+
trail: blendLook(stage, "trail"),
|
|
460
|
+
streak: blendLook(stage, "streak")
|
|
461
|
+
}, trailBoost, intro);
|
|
440
462
|
const previous = trails[current];
|
|
441
463
|
current = 1 - current;
|
|
442
464
|
const trail = trails[current];
|
|
@@ -446,7 +468,7 @@ async function createEngine(canvas, options) {
|
|
|
446
468
|
bindTexture(1, previous.tex);
|
|
447
469
|
gl.uniform1i(uniform(trailProgram, "uScene"), 0);
|
|
448
470
|
gl.uniform1i(uniform(trailProgram, "uPrevious"), 1);
|
|
449
|
-
gl.uniform1f(uniform(trailProgram, "uDecay"), decay);
|
|
471
|
+
gl.uniform1f(uniform(trailProgram, "uDecay"), motion ? decay : 0);
|
|
450
472
|
drawQuad();
|
|
451
473
|
const threshold = .55;
|
|
452
474
|
const knee = .45;
|
|
@@ -487,7 +509,7 @@ async function createEngine(canvas, options) {
|
|
|
487
509
|
gl.uniform1f(uniform(compositeProgram, "uBloomStrength"), .55 * blendLook(stage, "bloom"));
|
|
488
510
|
gl.uniform1f(uniform(compositeProgram, "uExposure"), blendLook(stage, "exposure"));
|
|
489
511
|
gl.uniform1f(uniform(compositeProgram, "uTime"), time);
|
|
490
|
-
gl.uniform1f(uniform(compositeProgram, "uAberration"),
|
|
512
|
+
gl.uniform1f(uniform(compositeProgram, "uAberration"), aberration);
|
|
491
513
|
gl.uniform1f(uniform(compositeProgram, "uFade"), smoothstep(0, 1, fade));
|
|
492
514
|
gl.uniform3fv(uniform(compositeProgram, "uBackground"), BACKGROUND);
|
|
493
515
|
drawQuad();
|
|
@@ -543,6 +565,7 @@ async function createEngine(canvas, options) {
|
|
|
543
565
|
},
|
|
544
566
|
destroy() {
|
|
545
567
|
canvas.removeEventListener("webglcontextlost", onLost);
|
|
568
|
+
formationShaders.forEach((shader) => shader.teardown?.());
|
|
546
569
|
if (gl.isContextLost()) return;
|
|
547
570
|
for (const p of programs) {
|
|
548
571
|
p.shaders.forEach((s) => gl.deleteShader(s));
|
|
@@ -556,4 +579,4 @@ async function createEngine(canvas, options) {
|
|
|
556
579
|
};
|
|
557
580
|
}
|
|
558
581
|
//#endregion
|
|
559
|
-
export { createEngine, fitEye };
|
|
582
|
+
export { createEngine, fitEye, trailOf };
|
package/dist/gl/formation.js
CHANGED
|
@@ -4,7 +4,8 @@ const DEFAULT_LOOK = {
|
|
|
4
4
|
bloom: 1,
|
|
5
5
|
trail: 0,
|
|
6
6
|
exposure: 1,
|
|
7
|
-
reflect: 0
|
|
7
|
+
reflect: 0,
|
|
8
|
+
streak: 1
|
|
8
9
|
};
|
|
9
10
|
/** Checks a formation and fills in its defaults. Anchor ids become `key:id`. */
|
|
10
11
|
function defineFormation(def) {
|
|
@@ -36,4 +37,4 @@ function mergeViews(base, override) {
|
|
|
36
37
|
};
|
|
37
38
|
}
|
|
38
39
|
//#endregion
|
|
39
|
-
export { defineFormation, mergeViews };
|
|
40
|
+
export { DEFAULT_LOOK, defineFormation, mergeViews };
|
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/gl/types.d.ts
CHANGED
|
@@ -24,6 +24,12 @@ export interface FormationLook {
|
|
|
24
24
|
exposure: number;
|
|
25
25
|
/** Strength of the mirror-floor reflection. */
|
|
26
26
|
reflect: number;
|
|
27
|
+
/**
|
|
28
|
+
* How much scrolling between formations lengthens the trail and widens the
|
|
29
|
+
* colour fringe, 0 (none) to 1. A formation pinned to the page turns it off,
|
|
30
|
+
* so a fast scroll leaves no ghost copies of it behind. Defaults to 1.
|
|
31
|
+
*/
|
|
32
|
+
streak?: number;
|
|
27
33
|
}
|
|
28
34
|
/** Where a formation stands in the scroll. */
|
|
29
35
|
export interface FormationProgress {
|
|
@@ -39,6 +45,19 @@ export interface FormationProgress {
|
|
|
39
45
|
time: number;
|
|
40
46
|
}
|
|
41
47
|
export interface FormationFrame extends FormationProgress {
|
|
48
|
+
/**
|
|
49
|
+
* The formation's place in the scene, which is also its `[data-stage]`
|
|
50
|
+
* section's. The engine always sets it; it's optional so frames written
|
|
51
|
+
* by hand, in tests, still type-check.
|
|
52
|
+
*/
|
|
53
|
+
index?: number;
|
|
54
|
+
/**
|
|
55
|
+
* The page's scroll position, which the scene reads before it writes
|
|
56
|
+
* anything in the frame. Read it here rather than from `window.scrollY`,
|
|
57
|
+
* which forces the browser to restyle the page after those writes. The
|
|
58
|
+
* engine always sets it, like `index`.
|
|
59
|
+
*/
|
|
60
|
+
scrollY?: number;
|
|
42
61
|
/** Seconds since the last frame, whether or not motion is enabled. */
|
|
43
62
|
dt: number;
|
|
44
63
|
motion: boolean;
|
|
@@ -70,10 +89,25 @@ export interface FormationShader {
|
|
|
70
89
|
* [0, 1) across the particles, to split them between the parts of a formation.
|
|
71
90
|
*/
|
|
72
91
|
glsl: string;
|
|
73
|
-
/**
|
|
74
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Uploads uniforms that never change, and starts anything the formation
|
|
94
|
+
* listens to. The particle program is bound. `scene` holds the formation's
|
|
95
|
+
* `index` and the scene's `root`, so its section is
|
|
96
|
+
* `root.querySelectorAll("[data-stage]")[index]`. The engine always passes
|
|
97
|
+
* it; it's optional so calls written by hand, in tests, still type-check.
|
|
98
|
+
*/
|
|
99
|
+
setup?(uniforms: Uniforms, scene?: {
|
|
100
|
+
index: number;
|
|
101
|
+
root: HTMLElement;
|
|
102
|
+
}): void;
|
|
75
103
|
/** Uploads per-frame uniforms. The particle program is bound. */
|
|
76
104
|
update?(uniforms: Uniforms, frame: FormationFrame): void;
|
|
105
|
+
/**
|
|
106
|
+
* Stops what `setup` started, like observers and listeners. The scene
|
|
107
|
+
* calls it when it's destroyed and when it loses its WebGL context, before
|
|
108
|
+
* it loads the shaders again for a restored one.
|
|
109
|
+
*/
|
|
110
|
+
teardown?(): void;
|
|
77
111
|
}
|
|
78
112
|
export interface FormationDef {
|
|
79
113
|
/** Unique within a scene. Letters and digits only: it namespaces GLSL identifiers. */
|
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.2",
|
|
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",
|