@arraypress/effects-astro 0.10.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.
Files changed (47) hide show
  1. package/AGENTS.md +1 -1
  2. package/COMPONENTS.md +214 -10
  3. package/LICENSE +2 -2
  4. package/package.json +1 -1
  5. package/src/components/CascadeGrid.astro +41 -0
  6. package/src/components/DitherField.astro +41 -0
  7. package/src/components/DitherGradient.astro +41 -0
  8. package/src/components/FlowField.astro +38 -0
  9. package/src/components/FlutedGlow.astro +39 -0
  10. package/src/components/KineticGrid.astro +38 -0
  11. package/src/components/LightLeak.astro +39 -0
  12. package/src/components/PixelFountain.astro +20 -7
  13. package/src/components/PixelGlitchText.astro +4 -3
  14. package/src/components/PixelHorizon.astro +40 -0
  15. package/src/components/TruchetField.astro +40 -0
  16. package/src/components/VoronoiMesh.astro +39 -0
  17. package/src/index.d.ts +23 -1
  18. package/src/index.js +13 -1
  19. package/src/runtime/ascii-noise.js +6 -107
  20. package/src/runtime/bayer.d.ts +5 -0
  21. package/src/runtime/bayer.js +48 -0
  22. package/src/runtime/cascade-grid.d.ts +112 -0
  23. package/src/runtime/cascade-grid.js +423 -0
  24. package/src/runtime/dither-field.d.ts +74 -0
  25. package/src/runtime/dither-field.js +416 -0
  26. package/src/runtime/dither-gradient.d.ts +91 -0
  27. package/src/runtime/dither-gradient.js +410 -0
  28. package/src/runtime/flow-field.d.ts +117 -0
  29. package/src/runtime/flow-field.js +516 -0
  30. package/src/runtime/fluted-glow.d.ts +79 -0
  31. package/src/runtime/fluted-glow.js +312 -0
  32. package/src/runtime/hash-noise.d.ts +14 -0
  33. package/src/runtime/hash-noise.js +169 -0
  34. package/src/runtime/kinetic-grid.d.ts +88 -0
  35. package/src/runtime/kinetic-grid.js +487 -0
  36. package/src/runtime/light-leak.d.ts +72 -0
  37. package/src/runtime/light-leak.js +408 -0
  38. package/src/runtime/pixel-fountain.d.ts +29 -13
  39. package/src/runtime/pixel-fountain.js +183 -42
  40. package/src/runtime/pixel-glitch.d.ts +2 -0
  41. package/src/runtime/pixel-glitch.js +8 -2
  42. package/src/runtime/pixel-horizon.d.ts +60 -0
  43. package/src/runtime/pixel-horizon.js +274 -0
  44. package/src/runtime/truchet-field.d.ts +60 -0
  45. package/src/runtime/truchet-field.js +272 -0
  46. package/src/runtime/voronoi-mesh.d.ts +101 -0
  47. package/src/runtime/voronoi-mesh.js +610 -0
@@ -0,0 +1,516 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/flow-field
3
+ *
4
+ * A Perlin-style flow field with hundreds of short, fine trails riding
5
+ * it: each trail is born somewhere at random, follows the field (its
6
+ * heading is the noise there, turned up to two whole turns, so trails
7
+ * gather into streams and swing round in loops), fades in, lives a few
8
+ * seconds and fades out, its tail thinning as it goes. The field itself
9
+ * evolves slowly, and two slow signals drift the look: one how many
10
+ * trails there are, how fast new ones travel and how fast the field
11
+ * changes; the other how heavy the lines are.
12
+ *
13
+ * The simulation (`createFlow`) is plain JavaScript and steps at a fixed
14
+ * 60 ticks a second whatever the frame rate, with every clock in seconds;
15
+ * it is seeded (`seed`), so two cards differ and the same card is the
16
+ * same on every load. Trail counts follow the element's area (`density`
17
+ * is trails per 100 × 100 CSS px) and the field's features follow its
18
+ * size (`scale` across √(width × height)), so a card and a hero look
19
+ * alike. It starts settled: the trails are already full, so the first
20
+ * frame (and the still frame with prefers-reduced-motion or `speed: 0`)
21
+ * is a field in motion, not an empty one.
22
+ *
23
+ * Canvas 2D, opaque: each frame fills the `background` and draws every
24
+ * trail's tail over it, segment by segment, each fainter than the last,
25
+ * so trails fade into the ground colour exactly (no smear). Segments are
26
+ * grouped by colour and alpha so a frame is a few dozen strokes, not
27
+ * thousands. Colours blend in Oklab, along each trail's life or by its
28
+ * speed. A `quiet` area spawns fewer trails and dims those passing
29
+ * through, behind a centred lockup. Canvas 2D rather than WebGL: fine
30
+ * antialiased hairlines come free, and a wall of cards takes no WebGL
31
+ * contexts from the effects beside it.
32
+ *
33
+ * After `perlin_field_bg`, a Processing sketch shared with the owner
34
+ * (2026-11-20): the heading as noise × 4π, the tail of thirty steps drawn
35
+ * every third, fading from the head, and the two slow drives are its;
36
+ * here the field is sampled smoothly rather than on a 40px grid, trails
37
+ * live and die rather than run on, and everything is by the second.
38
+ */
39
+
40
+ import { createCanvas2D } from './canvas2d.js';
41
+ import { mixOklab, parseColor } from './color.js';
42
+ import { noiseGrad } from './curl.js';
43
+ import { quietUniform } from './glsl.js';
44
+ import { watchColors } from './mount.js';
45
+ import { pickColors } from './palettes.js';
46
+ import { seededRandom } from './random.js';
47
+
48
+ /**
49
+ * @typedef {object} FlowFieldConfig
50
+ * @property {string[]} [colors] The trails' colours, CSS hex, blended in Oklab along each trail's life (or by its speed). Default white.
51
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`.
52
+ * @property {string} [background='#000000'] The ground, CSS hex: filled every frame, so the canvas is opaque.
53
+ * @property {'life' | 'speed' | 'random'} [colorBy='life'] What picks a trail's colour along `colors`: its age, how fast it travels, or one colour each at random.
54
+ * @property {number} [density=12] Trails per 100 × 100 CSS px, on average.
55
+ * @property {number} [length=0.5] How much of its path a trail shows behind it, seconds.
56
+ * @property {number} [weight=0.7] Line width, CSS px.
57
+ * @property {number} [opacity=0.75] A trail head's opacity, 0–1; the tail fades from it.
58
+ * @property {number} [pace=110] How fast trails travel, CSS px a second, on average.
59
+ * @property {number} [scale=2] How many noise features span the element (across √(width × height)); higher is busier.
60
+ * @property {number} [evolve=0.15] How fast the field changes, noise units a second.
61
+ * @property {number} [life=4] A trail's average life, seconds (each lives half to one and a half times it).
62
+ * @property {number} [vary=0.7] How far the slow drives swing density, pace, weight and evolution, 0–1; 0 holds them.
63
+ * @property {number} [speed=1] Plays every clock faster or slower; 0 for one still frame.
64
+ * @property {number} [seed=1] Which field and which trails: the same seed draws the same.
65
+ * @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
66
+ * An area with fewer, fainter trails: centre (0–1 from the top left), radius (share of the height), depth (0–1).
67
+ */
68
+
69
+ /** The settings' defaults. */
70
+ export const FLOW_DEFAULTS = Object.freeze({
71
+ background: '#000000',
72
+ colorBy: /** @type {'life' | 'speed' | 'random'} */ ('life'),
73
+ density: 12,
74
+ length: 0.5,
75
+ weight: 0.7,
76
+ opacity: 0.75,
77
+ pace: 110,
78
+ scale: 2,
79
+ evolve: 0.15,
80
+ life: 4,
81
+ vary: 0.7,
82
+ speed: 1,
83
+ seed: 1,
84
+ });
85
+
86
+ /** The default trail colour: white, as the sketch drew. */
87
+ const WHITE = ['#ffffff'];
88
+
89
+ /** Simulation ticks a second: the sketch's frame rate, now fixed. */
90
+ export const TICKS = 60;
91
+
92
+ /** One tick, seconds. */
93
+ const TICK = 1 / TICKS;
94
+
95
+ /** How many segments a tail is drawn in (the sketch: thirty steps, every third). */
96
+ const SEGMENTS = 10;
97
+
98
+ /** Alpha steps segments are grouped by. */
99
+ const ALPHA_LEVELS = 10;
100
+
101
+ /** Colour steps along `colors` segments are grouped by (one colour needs one). */
102
+ const COLOR_LEVELS = 10;
103
+
104
+ /** Seconds a trail takes to fade in, and to fade out. */
105
+ const FADE_IN = 0.4;
106
+ const FADE_OUT = 0.7;
107
+
108
+ /** How fast the two slow drives wander, noise units a second (the sketch's `driveSpeed`). */
109
+ const DRIVE_RATE = 0.12;
110
+
111
+ /** How long a new canvas is run before its first frame, seconds, so it starts settled. */
112
+ const WARM = 2.5;
113
+
114
+ /** Scratch for one noise sample. */
115
+ const sample = new Float64Array(3);
116
+
117
+ /**
118
+ * The field's noise in three dimensions: value noise across, blended
119
+ * smoothly between independent slices in time, its spread kept even
120
+ * between slices (a plain blend of two would go flat halfway).
121
+ *
122
+ * @param {number} x Across, noise units.
123
+ * @param {number} y Down, noise units.
124
+ * @param {number} z Time, noise units.
125
+ * @returns {number} Around 0–1, centred on 0.5.
126
+ */
127
+ export function flowNoise(x, y, z) {
128
+ const iz = Math.floor(z);
129
+ const f = z - iz;
130
+ const u = f * f * (3 - 2 * f);
131
+ const a = noiseGrad(x + iz * 31.7, y + iz * 17.3, sample)[0];
132
+ const b = noiseGrad(x + (iz + 1) * 31.7, y + (iz + 1) * 17.3, sample)[0];
133
+ const spread = Math.sqrt((1 - u) * (1 - u) + u * u);
134
+ return 0.5 + (a + (b - a) * u - 0.5) / spread;
135
+ }
136
+
137
+ /**
138
+ * The field's heading at a point, as the sketch turned it: the noise
139
+ * times two whole turns.
140
+ *
141
+ * @param {number} x Across, noise units.
142
+ * @param {number} y Down, noise units.
143
+ * @param {number} z Time, noise units.
144
+ * @returns {number} Radians.
145
+ */
146
+ export function flowAngle(x, y, z) {
147
+ return flowNoise(x, y, z) * Math.PI * 4;
148
+ }
149
+
150
+ /**
151
+ * One of the slow drives: a signal that wanders 0–1 over tens of
152
+ * seconds, stretched (as the sketch stretched its noise) so it reaches
153
+ * both ends.
154
+ *
155
+ * @param {number} t Seconds.
156
+ * @param {number} channel Which drive (each is independent).
157
+ * @returns {number} 0–1.
158
+ */
159
+ export function driveAt(t, channel) {
160
+ const v = noiseGrad(t * DRIVE_RATE + channel * 57.31, channel * 13.7 + 0.5, sample)[0];
161
+ return Math.min(1, Math.max(0, (v - 0.2) / 0.6));
162
+ }
163
+
164
+ /**
165
+ * How much a quiet area lets through at a point: the GLSL quiet's
166
+ * falloff (`quietOpen` in glsl.js), in CSS px.
167
+ *
168
+ * @param {number} x Across, CSS px from the left.
169
+ * @param {number} y Down, CSS px from the top.
170
+ * @param {number} width The element's width, CSS px.
171
+ * @param {number} height Its height, CSS px.
172
+ * @param {FlowFieldConfig['quiet']} quiet The area, or none.
173
+ * @returns {number} 1 − depth at the area's centre, rising to 1 at its edge and beyond; 1 without one.
174
+ */
175
+ export function quietFactor(x, y, width, height, quiet) {
176
+ const [qx, qy, radius, depth] = quietUniform(quiet, width / height);
177
+ if (radius <= 0) return 1;
178
+ const d = Math.hypot((x - width / 2) / height - qx, (height / 2 - y) / height - qy);
179
+ const t = Math.min(1, Math.max(0, (d - radius * 0.06) / (radius * 0.94)));
180
+ return 1 - depth + depth * t * t * (3 - 2 * t);
181
+ }
182
+
183
+ /**
184
+ * How many trails an element holds on average.
185
+ *
186
+ * @param {number} width CSS px.
187
+ * @param {number} height CSS px.
188
+ * @param {number} [density=12] Trails per 100 × 100 CSS px.
189
+ * @returns {number} A whole number, at least 1 for any area.
190
+ */
191
+ export function trailCount(width, height, density = FLOW_DEFAULTS.density) {
192
+ return Math.max(width > 0 && height > 0 && density > 0 ? 1 : 0, Math.round(((width * height) / 10000) * Math.max(0, density)));
193
+ }
194
+
195
+ /**
196
+ * The colours as strokes take them: steps along `colors`, blended in
197
+ * Oklab from the first to the last.
198
+ *
199
+ * @param {string[]} colors CSS hex colours, at least one.
200
+ * @param {number} [steps=10] How many (one colour gives one).
201
+ * @returns {string[]} `rgb(…)` strings.
202
+ * @throws {TypeError} When a colour is not hex.
203
+ */
204
+ export function flowRamp(colors, steps = COLOR_LEVELS) {
205
+ const cs = colors.map(parseColor);
206
+ const css = (/** @type {number[]} */ c) => `rgb(${c.map((v) => Math.round(Math.min(1, Math.max(0, v)) * 255)).join(' ')})`;
207
+ if (cs.length < 2) return [css(cs[0] ?? [1, 1, 1])];
208
+ return Array.from({ length: steps }, (_, s) => {
209
+ const k = (s / (steps - 1)) * (cs.length - 1);
210
+ const i = Math.min(cs.length - 2, Math.floor(k));
211
+ return css(mixOklab(cs[i], cs[i + 1], k - i));
212
+ });
213
+ }
214
+
215
+ /**
216
+ * A trail's opacity from its age: fading in at birth, out at the end.
217
+ *
218
+ * @param {number} age Seconds since it was born.
219
+ * @param {number} life Seconds it lives.
220
+ * @returns {number} 0–1.
221
+ */
222
+ export function lifeAlpha(age, life) {
223
+ return Math.max(0, Math.min(1, age / FADE_IN, (life - age) / FADE_OUT));
224
+ }
225
+
226
+ /**
227
+ * @typedef {object} Trail
228
+ * @property {boolean} alive Whether it is drawn and moving.
229
+ * @property {number} x Its head, CSS px.
230
+ * @property {number} y Its head, CSS px.
231
+ * @property {Float32Array} path Its last positions, a ring of x, y pairs.
232
+ * @property {number} head The ring's newest pair.
233
+ * @property {number} age Seconds since birth.
234
+ * @property {number} life Seconds it lives.
235
+ * @property {number} pace Its speed, CSS px a second, set at birth.
236
+ * @property {number} tone Where it sits along the colours for `colorBy: 'speed'` or `'random'`, 0–1.
237
+ */
238
+
239
+ /**
240
+ * @typedef {object} Flow
241
+ * @property {Trail[]} trails The pool, every trail it may hold.
242
+ * @property {number} width CSS px.
243
+ * @property {number} height CSS px.
244
+ * @property {number} steps Positions a trail keeps.
245
+ * @property {number} time Seconds simulated.
246
+ * @property {number} target How many trails it wants now.
247
+ * @property {number} weight The line width now, CSS px.
248
+ * @property {(seconds: number) => void} advance Steps the simulation on by some seconds (at fixed ticks; a remainder carries over).
249
+ * @property {(buckets: number[][]) => number} segments Fills `buckets[colour × ALPHA_LEVELS + alpha − 1]` with each segment's points; returns how many segments.
250
+ */
251
+
252
+ /**
253
+ * The simulation, without a canvas: a pool of trails following the
254
+ * field over an area, stepped at fixed ticks and run on until settled.
255
+ *
256
+ * @param {FlowFieldConfig} config The settings.
257
+ * @param {number} width The area's width, CSS px.
258
+ * @param {number} height Its height, CSS px.
259
+ * @param {{ warm?: number }} [options] `warm`: seconds to run before the first frame (default 2.5).
260
+ * @returns {Flow} The simulation.
261
+ */
262
+ export function createFlow(config, width, height, options = {}) {
263
+ const o = { ...FLOW_DEFAULTS, ...config };
264
+ const rnd = seededRandom(o.seed);
265
+ const vary = Math.min(1, Math.max(0, o.vary));
266
+ const steps = Math.max(2, Math.round(o.length * TICKS) + 1);
267
+ const per = (steps - 1) / Math.min(SEGMENTS, steps - 1);
268
+ const unit = o.scale / Math.sqrt(Math.max(1, width * height));
269
+ const ox = rnd() * 4096;
270
+ const oy = rnd() * 4096;
271
+ const drift = rnd() * 512;
272
+ const base = trailCount(width, height, o.density);
273
+ const margin = 0.06 * Math.sqrt(width * height);
274
+ const paceLo = o.pace * (1 - 0.8 * vary);
275
+ const paceHi = o.pace * (1 + 0.8 * vary);
276
+ let z = rnd() * 256;
277
+ let carry = 0;
278
+
279
+ /** @type {Trail[]} */
280
+ const trails = Array.from({ length: Math.ceil(base * (1 + 0.7 * vary)) }, () => ({
281
+ alive: false,
282
+ x: 0,
283
+ y: 0,
284
+ path: new Float32Array(steps * 2),
285
+ head: 0,
286
+ age: 0,
287
+ life: 1,
288
+ pace: o.pace,
289
+ tone: 0,
290
+ }));
291
+
292
+ /** @type {Flow} */
293
+ const flow = { trails, width, height, steps, time: 0, target: base, weight: o.weight, advance, segments };
294
+
295
+ /**
296
+ * The drives now, as factors around 1.
297
+ *
298
+ * @returns {{ density: number, pace: number, evolve: number, weight: number }} The factors.
299
+ */
300
+ function drives() {
301
+ const wide = driveAt(flow.time + drift, 0) - 0.5;
302
+ const low = driveAt(flow.time + drift, 1) - 0.5;
303
+ return {
304
+ density: 1 + vary * wide * 1.4,
305
+ pace: 1 + vary * wide * 1.6,
306
+ evolve: 1 + vary * wide * 1.6,
307
+ weight: 1 + vary * low * 1.2,
308
+ };
309
+ }
310
+
311
+ /**
312
+ * Gives a trail a new life at a random place, fewer in a quiet area.
313
+ *
314
+ * @param {Trail} p The trail.
315
+ * @param {number} pace Its speed, CSS px a second.
316
+ */
317
+ function spawn(p, pace) {
318
+ let x = 0;
319
+ let y = 0;
320
+ for (let tries = 0; tries < 6; tries++) {
321
+ x = -margin + rnd() * (width + 2 * margin);
322
+ y = -margin + rnd() * (height + 2 * margin);
323
+ if (rnd() < quietFactor(x, y, width, height, o.quiet)) break;
324
+ }
325
+ p.alive = true;
326
+ p.x = x;
327
+ p.y = y;
328
+ p.path.fill(0);
329
+ for (let i = 0; i < steps; i++) {
330
+ p.path[i * 2] = x;
331
+ p.path[i * 2 + 1] = y;
332
+ }
333
+ p.head = 0;
334
+ p.age = 0;
335
+ p.life = o.life * (0.5 + rnd());
336
+ p.pace = pace * (0.85 + rnd() * 0.3);
337
+ p.tone = o.colorBy === 'speed' ? Math.min(1, Math.max(0, (p.pace - paceLo) / Math.max(1e-6, paceHi - paceLo))) : rnd();
338
+ }
339
+
340
+ /** One tick: the drives move, trails are born and retired toward the count wanted, each trail steps along the field. */
341
+ function tick() {
342
+ flow.time += TICK;
343
+ const d = drives();
344
+ z += o.evolve * d.evolve * TICK;
345
+ flow.target = Math.min(trails.length, Math.round(base * d.density));
346
+ flow.weight = o.weight * d.weight;
347
+ // Births refill the count wanted over about half a second, not all at once.
348
+ let births = Math.max(1, Math.ceil(trails.length * TICK * 2));
349
+ for (let i = 0; i < trails.length; i++) {
350
+ const p = trails[i];
351
+ if (!p.alive) {
352
+ if (i < flow.target && births > 0) {
353
+ spawn(p, o.pace * d.pace);
354
+ births--;
355
+ }
356
+ continue;
357
+ }
358
+ p.age += TICK;
359
+ if (p.age >= p.life) {
360
+ p.alive = false;
361
+ continue;
362
+ }
363
+ // Past the count wanted, or off the edge: fade out early (the sketch's death fade).
364
+ const out = p.x < -margin || p.y < -margin || p.x > width + margin || p.y > height + margin;
365
+ if ((i >= flow.target || out) && p.life - p.age > FADE_OUT) p.life = p.age + FADE_OUT;
366
+ const a = flowAngle(p.x * unit + ox, p.y * unit + oy, z);
367
+ p.x += Math.cos(a) * p.pace * TICK;
368
+ p.y += Math.sin(a) * p.pace * TICK;
369
+ p.head = (p.head + 1) % steps;
370
+ p.path[p.head * 2] = p.x;
371
+ p.path[p.head * 2 + 1] = p.y;
372
+ }
373
+ }
374
+
375
+ /**
376
+ * Steps the simulation on.
377
+ *
378
+ * @param {number} seconds How long, seconds (a remainder under a tick carries over).
379
+ */
380
+ function advance(seconds) {
381
+ carry += Math.max(0, seconds);
382
+ while (carry >= TICK) {
383
+ carry -= TICK;
384
+ tick();
385
+ }
386
+ }
387
+
388
+ /**
389
+ * Lays every visible segment into buckets by colour and alpha, for a
390
+ * stroke each: a tail is `SEGMENTS` pieces from the head back, each
391
+ * fainter (the sketch's fade), times the trail's life fade and the
392
+ * quiet area at the piece's middle.
393
+ *
394
+ * @param {number[][]} buckets One list of points a bucket (`COLOR_LEVELS × ALPHA_LEVELS`), emptied first; each segment is its point count, then x, y pairs.
395
+ * @returns {number} How many segments were laid.
396
+ */
397
+ function segments(buckets) {
398
+ for (const b of buckets) b.length = 0;
399
+ const tones = buckets.length / ALPHA_LEVELS;
400
+ const pieces = Math.round((steps - 1) / per);
401
+ let laid = 0;
402
+ for (const p of trails) {
403
+ if (!p.alive) continue;
404
+ const fade = lifeAlpha(p.age, p.life);
405
+ if (fade <= 0) continue;
406
+ const tone = o.colorBy === 'life' ? p.age / p.life : p.tone;
407
+ const ci = tones > 1 ? Math.min(tones - 1, Math.floor(tone * tones)) : 0;
408
+ for (let k = 0; k < pieces; k++) {
409
+ const from = Math.round(k * per);
410
+ const to = Math.round((k + 1) * per);
411
+ const mid = (p.head - ((from + to) >> 1) + steps) % steps;
412
+ const q = o.quiet ? quietFactor(p.path[mid * 2], p.path[mid * 2 + 1], width, height, o.quiet) : 1;
413
+ const level = Math.round((1 - from / (steps - 1)) * fade * q * ALPHA_LEVELS);
414
+ if (level <= 0) continue;
415
+ const out = buckets[ci * ALPHA_LEVELS + Math.min(ALPHA_LEVELS, level) - 1];
416
+ out.push(to - from + 1);
417
+ for (let s = from; s <= to; s++) {
418
+ const at = (p.head - s + steps) % steps;
419
+ out.push(p.path[at * 2], p.path[at * 2 + 1]);
420
+ }
421
+ laid++;
422
+ }
423
+ }
424
+ return laid;
425
+ }
426
+
427
+ // Born at random ages and run on, so the first frame is already a field in motion.
428
+ const start = drives();
429
+ for (let i = 0; i < Math.min(trails.length, base); i++) {
430
+ spawn(trails[i], o.pace * start.pace);
431
+ trails[i].age = rnd() * trails[i].life * 0.8;
432
+ }
433
+ advance(options.warm ?? WARM);
434
+ return flow;
435
+ }
436
+
437
+ /**
438
+ * Starts the flow field on an element (its first canvas is drawn into,
439
+ * or a 2D one in its place when the host made it for WebGL).
440
+ *
441
+ * @param {HTMLElement} el The element.
442
+ * @param {FlowFieldConfig} config The settings.
443
+ * @returns {() => void} Stops it.
444
+ */
445
+ export function flowField(el, config) {
446
+ const o = { ...FLOW_DEFAULTS, ...config };
447
+ const speed = Math.max(0, o.speed);
448
+ const opacity = Math.min(1, Math.max(0, o.opacity));
449
+ let ramp = flowRamp(pickColors(config, WHITE));
450
+ /** @type {number[][]} */
451
+ let buckets = [];
452
+ /** @type {Flow | null} */
453
+ let flow = null;
454
+ let fresh = true;
455
+
456
+ /**
457
+ * A new simulation for the new size (the same seed, so the same field).
458
+ *
459
+ * @param {import('./canvas2d.js').Canvas2D} c The canvas.
460
+ */
461
+ const resize = (c) => {
462
+ flow = createFlow(config, c.width, c.height);
463
+ buckets = Array.from({ length: ramp.length * ALPHA_LEVELS }, () => []);
464
+ fresh = true;
465
+ };
466
+
467
+ /**
468
+ * One frame: the simulation stepped on, the ground filled, the tails
469
+ * stroked a bucket at a time. Held after one frame when `speed` is 0.
470
+ *
471
+ * @param {import('./canvas2d.js').Canvas2D} c The canvas.
472
+ * @param {number} _now The time, ms.
473
+ * @param {number} dt Seconds since the last frame.
474
+ */
475
+ const draw = (c, _now, dt) => {
476
+ if (!flow || (!fresh && (c.still || speed === 0))) return;
477
+ fresh = false;
478
+ if (!c.still) flow.advance(dt * speed);
479
+ const { ctx } = c;
480
+ ctx.globalAlpha = 1;
481
+ ctx.fillStyle = o.background;
482
+ ctx.fillRect(0, 0, c.width, c.height);
483
+ flow.segments(buckets);
484
+ ctx.lineWidth = flow.weight;
485
+ ctx.lineCap = 'butt';
486
+ ctx.lineJoin = 'round';
487
+ for (let b = 0; b < buckets.length; b++) {
488
+ const pts = buckets[b];
489
+ if (!pts.length) continue;
490
+ ctx.strokeStyle = ramp[Math.floor(b / ALPHA_LEVELS)];
491
+ ctx.globalAlpha = (((b % ALPHA_LEVELS) + 1) / ALPHA_LEVELS) * opacity;
492
+ ctx.beginPath();
493
+ for (let i = 0; i < pts.length; ) {
494
+ const n = pts[i++];
495
+ ctx.moveTo(pts[i], pts[i + 1]);
496
+ for (let s = 1; s < n; s++) ctx.lineTo(pts[i + s * 2], pts[i + s * 2 + 1]);
497
+ i += n * 2;
498
+ }
499
+ ctx.stroke();
500
+ }
501
+ ctx.globalAlpha = 1;
502
+ };
503
+
504
+ const { c, stop } = createCanvas2D(el, { draw, resize });
505
+ if (!c) return stop;
506
+ const unwatch = watchColors(el, (next) => {
507
+ ramp = flowRamp(next.colors);
508
+ buckets = Array.from({ length: ramp.length * ALPHA_LEVELS }, () => []);
509
+ fresh = true;
510
+ draw(c, performance.now(), 0);
511
+ });
512
+ return () => {
513
+ stop();
514
+ unwatch();
515
+ };
516
+ }
@@ -0,0 +1,79 @@
1
+ import type { PaletteName } from './palettes.js';
2
+
3
+ /** The light behind the glass. */
4
+ export type GlowShape = 'ring' | 'orb' | 'rise';
5
+
6
+ export interface FlutedGlowConfig {
7
+ /** Three CSS hex colours, the light from its edge in: deep, mid and hot. Default a violet ring's. */
8
+ colors?: string[];
9
+ /** A shared palette by name (`PALETTES`, any case), when there are no `colors`. */
10
+ palette?: PaletteName | (string & {});
11
+ /** The ground, painted everywhere. Default '#08070c'. */
12
+ background?: string;
13
+ /** The light behind the glass. Default 'ring'. */
14
+ shape?: GlowShape;
15
+ /** Flutes across the width. Default 14. */
16
+ flutes?: number;
17
+ /** How much of what is behind a flute it shows across its width (lower stretches more). Default 0.35. */
18
+ squeeze?: number;
19
+ /** The ring's radius or the orb's, a share of the height; how high the rise reaches. Default 0.3. */
20
+ size?: number;
21
+ /** The ring's soft width, a share of the height. Default 0.09. */
22
+ thickness?: number;
23
+ /** How bright the light is. Default 1. */
24
+ glow?: number;
25
+ /** How far the light swells and shrinks, a share of its size. Default 0.06. */
26
+ breathe?: number;
27
+ /** How far its centre wanders, a share of the height. Default 0.04. */
28
+ drift?: number;
29
+ /** How fast it breathes and wanders, cycles a second. Default 0.05. */
30
+ rate?: number;
31
+ /** How strong the flutes' shadows and highlights are; 0 for none. Default 0.35. */
32
+ seams?: number;
33
+ /** Fine grain over all of it; 0 for none. Default 0.05. */
34
+ grain?: number;
35
+ /** The ring's or orb's centre, 0–1 from the top left. Default [0.5, 0.5]. */
36
+ centre?: [number, number];
37
+ /** Where its wandering starts, so two cards differ. Default 0. */
38
+ seed?: number;
39
+ /** Plays every clock faster or slower; 0 for one still frame. Default 1. */
40
+ speed?: number;
41
+ /** An area dimmed: centre (0–1 from the top left), radius (share of the height), depth (0–1). */
42
+ quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
43
+ }
44
+
45
+ /** The shapes, in the order the shader numbers them. */
46
+ export declare const GLOW_SHAPES: readonly ['ring', 'orb', 'rise'];
47
+
48
+ /** The settings' defaults, shared by the shader and the JS twin. */
49
+ export declare const GLOW_DEFAULTS: {
50
+ background: string;
51
+ shape: GlowShape;
52
+ flutes: number;
53
+ squeeze: number;
54
+ size: number;
55
+ thickness: number;
56
+ glow: number;
57
+ breathe: number;
58
+ drift: number;
59
+ rate: number;
60
+ seams: number;
61
+ grain: number;
62
+ centre: [number, number];
63
+ seed: number;
64
+ };
65
+
66
+ /** How bright the light behind the glass is at a point (heights from the top left; `aspect` width over height), before the flutes and the quiet area. */
67
+ export declare function glowAt(config: FlutedGlowConfig, x: number, y: number, aspect: number, time?: number): number;
68
+
69
+ /** Where behind the glass a point looks: the share of the width it shows, and where in its flute it is (−1 to 1). */
70
+ export declare function fluteSample(fx: number, flutes: number, squeeze: number): { x: number; local: number };
71
+
72
+ /** The light's three colours as the shader takes them: deep, mid and hot. */
73
+ export declare function glowColors(config: FlutedGlowConfig): [string, string, string];
74
+
75
+ /** A CSS `background` that stands in for the light before the shader starts and without WebGL (`aspect`, width over height, default 0.8). */
76
+ export declare function glowStill(config: FlutedGlowConfig, aspect?: number): string;
77
+
78
+ /** Starts the light on an element (its first canvas). Returns a function that stops it. */
79
+ export declare function flutedGlow(el: HTMLElement, config: FlutedGlowConfig): () => void;