@arraypress/effects-astro 0.11.0 → 0.12.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.
@@ -0,0 +1,312 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/fluted-glow
3
+ *
4
+ * A soft light seen through reeded glass: behind upright flutes, a glowing
5
+ * ring, a round orb or a light rising from the bottom, ramped from the
6
+ * ground through a deep and a mid colour to a hot one (in Oklab), breathing
7
+ * and drifting slowly. Each flute shows a narrow slice of what is behind it
8
+ * stretched across its width, so the light breaks into vertical strips,
9
+ * with a faint shadow and highlight at each flute's seams and a fine grain
10
+ * over all of it. Unlike `<FlutedGlass>` (slanted flutes over pointer-led
11
+ * blooms) it is made to sit behind copy: no pointer, the light framing a
12
+ * quiet middle, the copy sharp in front of the glass.
13
+ *
14
+ * Every clock is in seconds and nothing is kept between frames; the light
15
+ * and the flutes' slicing are here in plain JavaScript too (`glowAt`,
16
+ * `fluteSample`).
17
+ *
18
+ * Opaque: it paints its `background` (default near-black) everywhere.
19
+ * Without WebGL nothing is drawn; with prefers-reduced-motion it is one
20
+ * still frame.
21
+ */
22
+
23
+ import { createStage } from './stage.js';
24
+ import { GLSL_HASH33 } from './hash-noise.js';
25
+ import { GLSL_OKLAB, parseColor, toOklab, toHex, mixOklab } from './color.js';
26
+ import { GLSL_QUIET, quietUniform } from './glsl.js';
27
+ import { watchColors } from './mount.js';
28
+ import { pickColors } from './palettes.js';
29
+
30
+ /**
31
+ * @typedef {'ring' | 'orb' | 'rise'} GlowShape
32
+ *
33
+ * @typedef {object} FlutedGlowConfig
34
+ * @property {string[]} [colors] Three CSS hex colours, the light from its edge in: deep, mid and hot. Default a violet ring's.
35
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`.
36
+ * @property {string} [background='#08070c'] The ground, painted everywhere.
37
+ * @property {GlowShape} [shape='ring'] The light behind the glass.
38
+ * @property {number} [flutes=14] Flutes across the width.
39
+ * @property {number} [squeeze=0.35] How much of what is behind a flute it shows across its width (lower stretches more).
40
+ * @property {number} [size=0.3] The ring's radius or the orb's, a share of the height; how high the rise reaches.
41
+ * @property {number} [thickness=0.09] The ring's soft width, a share of the height.
42
+ * @property {number} [glow=1] How bright the light is.
43
+ * @property {number} [breathe=0.06] How far the light swells and shrinks, a share of its size.
44
+ * @property {number} [drift=0.04] How far its centre wanders, a share of the height.
45
+ * @property {number} [rate=0.05] How fast it breathes and wanders, cycles a second.
46
+ * @property {number} [seams=0.35] How strong the flutes' shadows and highlights are; 0 for none.
47
+ * @property {number} [grain=0.05] Fine grain over all of it; 0 for none.
48
+ * @property {[number, number]} [centre=[0.5, 0.5]] The ring's or orb's centre, 0–1 from the top left.
49
+ * @property {number} [seed=0] Where its wandering starts, so two cards differ.
50
+ * @property {number} [speed=1] Plays every clock faster or slower; 0 for one still frame.
51
+ * @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
52
+ * An area dimmed: centre (0–1 from the top left), radius (share of the height), depth (0–1).
53
+ */
54
+
55
+ /** The shapes, in the order the shader numbers them. */
56
+ export const GLOW_SHAPES = /** @type {const} */ (['ring', 'orb', 'rise']);
57
+
58
+ /** The settings' defaults, shared by the shader and the JS twin. */
59
+ export const GLOW_DEFAULTS = {
60
+ background: '#08070c',
61
+ shape: /** @type {GlowShape} */ ('ring'),
62
+ flutes: 14,
63
+ squeeze: 0.35,
64
+ size: 0.3,
65
+ thickness: 0.09,
66
+ glow: 1,
67
+ breathe: 0.06,
68
+ drift: 0.04,
69
+ rate: 0.05,
70
+ seams: 0.35,
71
+ grain: 0.05,
72
+ centre: /** @type {[number, number]} */ ([0.5, 0.5]),
73
+ seed: 0,
74
+ };
75
+
76
+ /** The default colours: a violet ring's. */
77
+ const VIOLET = ['#3a1a7a', '#8f7cff', '#f2eeff'];
78
+
79
+ /**
80
+ * GLSL: `float glowAt(vec2 p, float t)`, how bright the light behind the
81
+ * glass is at a point (heights from the top left), 0–1 and beyond at its
82
+ * hottest. `glowAt` below is the same in JavaScript.
83
+ */
84
+ const GLSL_GLOW = `
85
+ uniform float uShape;
86
+ uniform float uSizeL;
87
+ uniform float uThick;
88
+ uniform float uGlow;
89
+ uniform float uBreathe;
90
+ uniform float uDrift;
91
+ uniform float uRate;
92
+ uniform vec2 uCentre;
93
+ uniform float uSeed;
94
+ float glowAt(vec2 p, float t) {
95
+ float ph = 6.2831853 * t * uRate;
96
+ float grow = 1.0 + uBreathe * sin(ph + uSeed);
97
+ vec2 c = uCentre + uDrift * vec2(sin(ph * 0.7 + uSeed * 1.3), cos(ph * 0.53 + uSeed * 0.7));
98
+ if (uShape < 0.5) {
99
+ float d = (length(p - c) - uSizeL * grow) / max(uThick, 0.01);
100
+ return uGlow * exp(-d * d);
101
+ }
102
+ if (uShape < 1.5) {
103
+ float d = length(p - c) / max(uSizeL * grow, 0.01);
104
+ return uGlow * exp(-d * d * 1.6);
105
+ }
106
+ float top = uCentre.y * 2.0 - uSizeL * grow;
107
+ float sway = 0.06 * sin(p.x * 5.0 + ph + uSeed);
108
+ return uGlow * smoothstep(top - 0.25, top + 0.55, p.y + sway);
109
+ }
110
+ `;
111
+
112
+ const FRAG = `
113
+ precision highp float;
114
+ uniform vec2 uRes;
115
+ uniform float uTime;
116
+ uniform float uFlutes;
117
+ uniform float uSqueeze;
118
+ uniform float uSeams;
119
+ uniform float uGrain;
120
+ uniform vec3 uBg;
121
+ uniform vec3 uRamp[3];
122
+ ${GLSL_OKLAB}
123
+ ${GLSL_QUIET}
124
+ ${GLSL_HASH33}
125
+ ${GLSL_GLOW}
126
+
127
+ void main() {
128
+ vec2 px = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y);
129
+ float aspect = uRes.x / uRes.y;
130
+ // Heights from the top left; the flutes measured in widths.
131
+ vec2 p = px / uRes.y;
132
+ float fw = 1.0 / uFlutes;
133
+ float fx = px.x / uRes.x;
134
+ float idx = floor(fx / fw);
135
+ float local = (fx / fw - idx) * 2.0 - 1.0;
136
+ float sx = ((idx + 0.5) * fw + local * 0.5 * fw * uSqueeze) * aspect;
137
+ float i = glowAt(vec2(sx, p.y), uTime);
138
+
139
+ vec2 q = vec2(p.x - 0.5 * aspect, 0.5 - p.y);
140
+ i *= mix(1.0 - uQuiet.w, 1.0, quietOpen(q));
141
+
142
+ // The ramp: ground, deep, mid, hot, by how bright.
143
+ vec3 lab = mix(uBg, uRamp[0], smoothstep(0.0, 0.35, i));
144
+ lab = mix(lab, uRamp[1], smoothstep(0.3, 0.75, i));
145
+ lab = mix(lab, uRamp[2], smoothstep(0.8, 1.2, i));
146
+ vec3 col = fromOklab(lab);
147
+
148
+ // Each flute a little darker toward its seams, a highlight on its left seam, a shadow on its right.
149
+ float seam = 1.0 - uSeams * 0.35 * (1.0 - cos(local * 1.5707963));
150
+ seam += uSeams * 0.25 * smoothstep(-0.86, -1.0, local) * (0.3 + i);
151
+ seam -= uSeams * 0.3 * smoothstep(0.86, 1.0, local);
152
+ col *= seam;
153
+ col += (hash33(vec3(px, floor(uTime * 24.0))).x - 0.5) * uGrain;
154
+ gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
155
+ }
156
+ `;
157
+
158
+ /**
159
+ * The settings, filled with the defaults, with the shape's number.
160
+ *
161
+ * @param {FlutedGlowConfig} config The settings.
162
+ * @returns {typeof GLOW_DEFAULTS & { shapeIndex: number, speed: number }} The settings.
163
+ */
164
+ function settings(config) {
165
+ const o = { ...GLOW_DEFAULTS, speed: 1, ...config };
166
+ return { ...o, shapeIndex: Math.max(0, GLOW_SHAPES.indexOf(o.shape)) };
167
+ }
168
+
169
+ /**
170
+ * How bright the light behind the glass is at a point (the shader's
171
+ * `glowAt`), before the flutes and the quiet area.
172
+ *
173
+ * @param {FlutedGlowConfig} config The settings.
174
+ * @param {number} x Heights from the left.
175
+ * @param {number} y Heights from the top.
176
+ * @param {number} aspect Width over height (the centre is a share of each).
177
+ * @param {number} [time=0] Seconds (already at its speed).
178
+ * @returns {number} 0 and up; about `glow` at its hottest.
179
+ */
180
+ export function glowAt(config, x, y, aspect, time = 0) {
181
+ const o = settings(config);
182
+ const ph = 2 * Math.PI * time * o.rate;
183
+ const grow = 1 + o.breathe * Math.sin(ph + o.seed);
184
+ const cx = o.centre[0] * aspect + o.drift * Math.sin(ph * 0.7 + o.seed * 1.3);
185
+ const cy = o.centre[1] + o.drift * Math.cos(ph * 0.53 + o.seed * 0.7);
186
+ if (o.shapeIndex === 0) {
187
+ const d = (Math.hypot(x - cx, y - cy) - o.size * grow) / Math.max(o.thickness, 0.01);
188
+ return o.glow * Math.exp(-d * d);
189
+ }
190
+ if (o.shapeIndex === 1) {
191
+ const d = Math.hypot(x - cx, y - cy) / Math.max(o.size * grow, 0.01);
192
+ return o.glow * Math.exp(-d * d * 1.6);
193
+ }
194
+ const top = o.centre[1] * 2 - o.size * grow;
195
+ const sway = 0.06 * Math.sin(x * 5 + ph + o.seed);
196
+ const t = Math.min(1, Math.max(0, (y + sway - (top - 0.25)) / 0.8));
197
+ return o.glow * t * t * (3 - 2 * t);
198
+ }
199
+
200
+ /**
201
+ * Where behind the glass a point looks (the shader's slicing): each flute
202
+ * shows a slice `squeeze` of its own width, stretched across it.
203
+ *
204
+ * @param {number} fx The point, a share of the width (0–1).
205
+ * @param {number} flutes Flutes across the width.
206
+ * @param {number} squeeze The share of a flute's width it shows.
207
+ * @returns {{ x: number, local: number }} The share of the width it shows, and where in its flute it is (−1 to 1).
208
+ */
209
+ export function fluteSample(fx, flutes, squeeze) {
210
+ const fw = 1 / flutes;
211
+ const idx = Math.floor(fx / fw);
212
+ const local = (fx / fw - idx) * 2 - 1;
213
+ return { x: (idx + 0.5) * fw + local * 0.5 * fw * squeeze, local };
214
+ }
215
+
216
+ /**
217
+ * The light's three colours as the shader takes them (fewer repeat).
218
+ *
219
+ * @param {FlutedGlowConfig} config The settings.
220
+ * @returns {[string, string, string]} Deep, mid and hot.
221
+ */
222
+ export function glowColors(config) {
223
+ const c = pickColors(config, VIOLET);
224
+ return [c[0], c[Math.min(1, c.length - 1)], c[c.length - 1]];
225
+ }
226
+
227
+ /**
228
+ * A CSS `background` that stands in for the light before the shader starts
229
+ * and without WebGL: the ring, orb or rise as a CSS gradient, under upright
230
+ * stripes for the flutes.
231
+ *
232
+ * @param {FlutedGlowConfig} config The settings (`colors`/`palette`, `background`, `shape`, `flutes`, `size`, `thickness`, `centre`).
233
+ * @param {number} [aspect=0.8] The element's width over its height, to size the ring and orb as the shader does (a 4:5 card by default).
234
+ * @returns {string} The CSS background.
235
+ */
236
+ export function glowStill(config, aspect = 0.8) {
237
+ const o = settings(config);
238
+ // A circle's stops run along the ray to the farthest corner; the shader measures in heights.
239
+ const [cx, cy] = o.centre;
240
+ const ray = Math.hypot(Math.max(cx, 1 - cx) * aspect, Math.max(cy, 1 - cy));
241
+ const [deep, mid, hot] = glowColors(config).map((c) => toHex(parseColor(c)));
242
+ const ground = o.background;
243
+ const fade = toHex(mixOklab(parseColor(ground), parseColor(deep), 0.5));
244
+ const at = `${Math.round(o.centre[0] * 100)}% ${Math.round(o.centre[1] * 100)}%`;
245
+ const r = Math.round((o.size / ray) * 100);
246
+ const w = Math.round((o.thickness / ray) * 100);
247
+ const flute = `repeating-linear-gradient(90deg, rgba(255,255,255,0.06) 0, transparent ${(100 / o.flutes) * 0.12}%, transparent ${(100 / o.flutes) * 0.8}%, rgba(0,0,0,0.25) ${100 / o.flutes}%)`;
248
+ const light =
249
+ o.shapeIndex === 0
250
+ ? `radial-gradient(circle at ${at}, ${ground} ${Math.max(0, r - 2 * w)}%, ${deep} ${r - w}%, ${hot} ${r}%, ${mid} ${r + w * 0.6}%, ${fade} ${r + w * 1.4}%, ${ground} ${r + w * 2.4}%)`
251
+ : o.shapeIndex === 1
252
+ ? `radial-gradient(circle at ${at}, ${hot} 0%, ${mid} ${Math.round(r * 0.5)}%, ${deep} ${r}%, ${ground} ${Math.round(r * 1.6)}%)`
253
+ : `linear-gradient(to top, ${hot} 0%, ${mid} ${Math.round(o.size * 60)}%, ${deep} ${Math.round(o.size * 100)}%, ${ground} ${Math.round(o.size * 160)}%)`;
254
+ return [flute, light, ground].join(', ');
255
+ }
256
+
257
+ /**
258
+ * Starts the light on an element: a canvas inside it (its first), at the
259
+ * stage's capped pixel ratio.
260
+ *
261
+ * @param {HTMLElement} el The element.
262
+ * @param {FlutedGlowConfig} config The settings.
263
+ * @returns {() => void} Stops it.
264
+ */
265
+ export function flutedGlow(el, config) {
266
+ const o = settings(config);
267
+ const lab = (/** @type {string} */ c) => toOklab(parseColor(c));
268
+ let ramp = glowColors(config).map(lab);
269
+ const ground = lab(o.background);
270
+ const start = performance.now();
271
+
272
+ const { stage, stop } = createStage(el, {
273
+ fragment: FRAG,
274
+ alpha: false,
275
+ still: o.speed <= 0,
276
+ setup({ gl, uniform }) {
277
+ gl.uniform1f(uniform('uFlutes'), Math.max(1, o.flutes));
278
+ gl.uniform1f(uniform('uSqueeze'), o.squeeze);
279
+ gl.uniform1f(uniform('uSeams'), o.seams);
280
+ gl.uniform1f(uniform('uGrain'), o.grain);
281
+ gl.uniform3f(uniform('uBg'), ground[0], ground[1], ground[2]);
282
+ gl.uniform1f(uniform('uShape'), o.shapeIndex);
283
+ gl.uniform1f(uniform('uSizeL'), o.size);
284
+ gl.uniform1f(uniform('uThick'), o.thickness);
285
+ gl.uniform1f(uniform('uGlow'), o.glow);
286
+ gl.uniform1f(uniform('uBreathe'), o.breathe);
287
+ gl.uniform1f(uniform('uDrift'), o.drift);
288
+ gl.uniform1f(uniform('uRate'), o.rate);
289
+ gl.uniform1f(uniform('uSeed'), o.seed);
290
+ },
291
+ resize({ gl, uniform }) {
292
+ const w = Math.max(1, el.clientWidth);
293
+ const h = Math.max(1, el.clientHeight);
294
+ gl.uniform2f(uniform('uCentre'), o.centre[0] * (w / h), o.centre[1]);
295
+ gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, w / h));
296
+ },
297
+ uniforms({ gl, uniform }, now) {
298
+ gl.uniform1f(uniform('uTime'), ((now - start) / 1000) * Math.max(0, o.speed));
299
+ gl.uniform3fv(uniform('uRamp[0]'), new Float32Array(ramp.flat()));
300
+ },
301
+ });
302
+ if (!stage) return stop;
303
+
304
+ const unwatch = watchColors(el, (next) => {
305
+ ramp = glowColors({ colors: next.colors }).map(lab);
306
+ if (stage.still) stage.redraw();
307
+ });
308
+ return () => {
309
+ stop();
310
+ unwatch();
311
+ };
312
+ }
@@ -1,32 +1,36 @@
1
1
  export interface PixelFountainConfig {
2
- /** The pixels' colour when they fire; the darker steps they fade through are made from it. Default `#c8ff00`. */
2
+ /** The pixels' colour when they fire; the darker steps they fade through are made from it. With `source="pointer"`, the one ink every block is drawn in. Default `#c8ff00` (from the pointer, the text's colour, `currentColor`). */
3
3
  color?: string;
4
- /** The steps a pixel fades through, first to last (overrides `color`). Default made from `color`. */
4
+ /** The steps a pixel fades through, first to last (overrides `color`; from the pointer, only the first is used). Default made from `color`. */
5
5
  colors?: string[];
6
6
  /** A shared palette by name, its colours as the steps, when there are no `colors`. Default none. */
7
7
  palette?: string;
8
8
  /** A pixel's size, CSS px (a power-up's biggest blocks are two). Default 8. */
9
9
  cell?: number;
10
- /** How far above the edge the pixels can fly, CSS px. Default 240. */
10
+ /** How far above the edge the pixels can fly, CSS px (from the pointer: how far above the host the canvas reaches). Default 240 (160 from the pointer). */
11
11
  rise?: number;
12
- /** Pixels a second trickling up on their own, per 1,000 CSS px of edge; 0 for none. Default 14. */
12
+ /** Pixels a second trickling up on their own, per 1,000 CSS px of edge; 0 for none (always none from the pointer). Default 14. */
13
13
  rate?: number;
14
- /** Pixels a click or tap fires. Default 40. */
14
+ /** Pixels a click or tap fires. Default 40 (24 from the pointer). */
15
15
  burst?: number;
16
+ /** From the pointer: blocks fired for every 100 CSS px it moves, at most 12 a move. Default 10. */
17
+ trail?: number;
18
+ /** Whether a mouse click fires a burst (a tap always does). Default true (false from the pointer). */
19
+ press?: boolean;
16
20
  /** How hard they are pulled back, 0–2. Default 1. */
17
21
  gravity?: number;
18
22
  /** How widely they fan out, 0–1. Default 0.5. */
19
23
  spread?: number;
20
24
  /** How much a wind pushes them sideways, 0–1. Default 0.3. */
21
25
  drift?: number;
22
- /** The longest a pixel lives, seconds. Default 1.4. */
26
+ /** The longest a pixel lives, seconds. Default 1.4 (0.9 from the pointer). */
23
27
  life?: number;
24
- /** The chance a pixel splits into smaller ones rather than shrinking, 0–1. Default 0.4. */
28
+ /** The chance a pixel splits into smaller ones rather than shrinking, 0–1. Default 0.4 (0.2 from the pointer). */
25
29
  split?: number;
26
- /** The chance a pixel flickers out on and off before it goes, 0–1. Default 0.35. */
30
+ /** The chance a pixel flickers out on and off before it goes, 0–1. Default 0.35 (0.5 from the pointer). */
27
31
  flicker?: number;
28
- /** Fire from the host's top edge, or from the filled pixels of its text. Default `edge`. */
29
- source?: 'edge' | 'text';
32
+ /** Fire from the host's top edge; from the filled pixels of its text; or only from the pointer, where it moves over the host (a block-dust trail in one ink, nothing on its own). Default `edge`. */
33
+ source?: 'edge' | 'text' | 'pointer';
30
34
  /** A selector for the element the pixels fire from and the pointer is followed over: the nearest ancestor that matches, else the first on the page. Default the element's positioned parent. */
31
35
  host?: string;
32
36
  /** Seeds the randomness. Default 1. */
@@ -61,8 +65,8 @@ export interface Pixel {
61
65
  wobble: number;
62
66
  }
63
67
 
64
- /** What fired a pixel: the ambient trickle, the pointer moving, or a press (a power-up). */
65
- export type EmitKind = 'ambient' | 'move' | 'burst';
68
+ /** What fired a pixel: the ambient trickle, the pointer moving over the edge or letters, or a press (a power-up); from the pointer, its trail and a tap. */
69
+ export type EmitKind = 'ambient' | 'move' | 'burst' | 'trail' | 'tap';
66
70
 
67
71
  /** A fountain's settings: the canvas's size and the effect's physics. */
68
72
  export interface FountainOptions {
@@ -80,6 +84,8 @@ export interface FountainOptions {
80
84
  life?: number;
81
85
  split?: number;
82
86
  flicker?: number;
87
+ /** Blocks a pointer's move fires for every 100 CSS px it travels. Default 10. */
88
+ trail?: number;
83
89
  seed?: number;
84
90
  }
85
91
 
@@ -100,6 +106,10 @@ export interface Fountain {
100
106
  emitEdge(x: number, n: number, kind?: EmitKind): number;
101
107
  /** Fires `n` pixels from where the ambient ones fire. */
102
108
  emitAmbient(n: number, kind?: EmitKind): number;
109
+ /** The pointer moved to a point: fires `trail` blocks per 100 px travelled (at most `TRAIL_CAP`), spaced along the way it came and ending there; none on the first point after a lift or for no move. Returns how many fired. */
110
+ moveTo(x: number, y: number): number;
111
+ /** Forgets where the pointer was (it left, or lifted). */
112
+ lift(): void;
103
113
  /** One step (default 1/60 s): the trickle, then every pixel ages, breaks down, moves and flickers. */
104
114
  step(dt?: number): void;
105
115
  /** Empties the pool. */
@@ -112,6 +122,12 @@ export const MAX_PIXELS: number;
112
122
  /** The simulation's fixed step, seconds (1/60). */
113
123
  export const STEP: number;
114
124
 
125
+ /** The most blocks one pointer move fires (12). */
126
+ export const TRAIL_CAP: number;
127
+
128
+ /** The settings that change with `source: 'pointer'`. */
129
+ export const POINTER_DEFAULTS: { rate: number; life: number; split: number; flicker: number; rise: number; burst: number; trail: number };
130
+
115
131
  /** The steps a pixel fades through, made from one colour in Oklab: darker and less saturated each step, turning a little towards olive. */
116
132
  export function fadeSteps(color: string, count?: number): string[];
117
133
 
@@ -130,5 +146,5 @@ export function inkedCells(rgba: Uint8ClampedArray, width: number, height: numbe
130
146
  /** Makes a seeded fountain of pixels. */
131
147
  export function createFountain(options: FountainOptions): Fountain;
132
148
 
133
- /** Starts the pixel fountain on an element placed over its host's top edge. Returns a function that stops it. */
149
+ /** Starts the pixel fountain on an element placed over its host's top edge (over the host and above it, from the text or the pointer). Returns a function that stops it. */
134
150
  export function pixelFountain(el: HTMLElement, config: PixelFountainConfig): () => void;