@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,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
+ }
@@ -0,0 +1,14 @@
1
+ /** GLSL: `vec3 hash33(vec3 p3)`, three values 0–1 from three inputs (Hash without Sine). */
2
+ export declare const GLSL_HASH33: string;
3
+ /** Hash without Sine, three in and three out (the shader's `hash33`); each value 0–1. */
4
+ export declare function hash33(x: number, y: number, z: number): [number, number, number];
5
+ /** GLSL: `vec2 grad(vec2 p)` and `float gnoise(vec2 p)`, gradient noise with a quintic fade, 0–1. */
6
+ export declare const GLSL_GRADIENT_NOISE: string;
7
+ /** A lattice point's gradient (the shader's `grad`), each part −1 to 1. */
8
+ export declare function grad(x: number, y: number): [number, number];
9
+ /** Gradient noise with a quintic fade (the shader's `gnoise`), 0–1. */
10
+ export declare function gnoise(x: number, y: number): number;
11
+ /** GLSL: `float noiseField(vec2 p, float evo, float wt)`, the warped five-octave field, 0–1 (needs GLSL_GRADIENT_NOISE). */
12
+ export declare const GLSL_NOISE_FIELD: string;
13
+ /** The warped field (the shader's `noiseField`): five octaves of gradient noise, each warped by the sum so far; 0–1. */
14
+ export declare function noiseField(x: number, y: number, evo: number, wt: number): number;
@@ -0,0 +1,169 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/hash-noise
3
+ *
4
+ * Hashing and noise that the GPU and JavaScript compute alike, so an
5
+ * effect's shader and its JS twin (a text still, a test, a frame worked
6
+ * out on the CPU) agree: Dave Hoskins' Hash without Sine, which brings its
7
+ * input under 1 before it multiplies (32-bit and 64-bit floats then stay in
8
+ * step), gradient noise with a quintic fade on it, and the warped
9
+ * five-octave field the friend's sketches share (ASCII noise, the dither
10
+ * field). Each comes as GLSL (ES 1.00) and as a JS function of the same
11
+ * name.
12
+ *
13
+ * Hash without Sine is MIT, © 2014 David Hoskins
14
+ * (https://www.shadertoy.com/view/4djSRW).
15
+ */
16
+
17
+ /**
18
+ * The fractional part.
19
+ *
20
+ * @param {number} n A number.
21
+ * @returns {number} `n − floor(n)`, 0–1.
22
+ */
23
+ const fract = (n) => n - Math.floor(n);
24
+
25
+ /** GLSL: `vec3 hash33(vec3 p3)`, three values 0–1 from three inputs. */
26
+ export const GLSL_HASH33 = `
27
+ vec3 hash33(vec3 p3) {
28
+ p3 = fract(p3 * vec3(0.1031, 0.1030, 0.0973));
29
+ p3 += dot(p3, p3.yxz + 33.33);
30
+ return fract((p3.xxy + p3.yxx) * p3.zyx);
31
+ }
32
+ `;
33
+
34
+ /**
35
+ * Hash without Sine, three in and three out (the shader's `hash33`).
36
+ *
37
+ * @param {number} x The first input.
38
+ * @param {number} y The second.
39
+ * @param {number} z The third.
40
+ * @returns {[number, number, number]} Three values, each 0–1.
41
+ */
42
+ export function hash33(x, y, z) {
43
+ let a = fract(x * 0.1031);
44
+ let b = fract(y * 0.103);
45
+ let c = fract(z * 0.0973);
46
+ const d = a * (b + 33.33) + b * (a + 33.33) + c * (c + 33.33);
47
+ a += d;
48
+ b += d;
49
+ c += d;
50
+ return [fract((a + b) * c), fract((a + a) * b), fract((b + a) * a)];
51
+ }
52
+
53
+ /**
54
+ * GLSL: `vec2 grad(vec2 p)`, a lattice point's gradient (Hash without
55
+ * Sine, −1 to 1), and `float gnoise(vec2 p)`, gradient noise with a
56
+ * quintic fade, 0–1.
57
+ */
58
+ export const GLSL_GRADIENT_NOISE = `
59
+ vec2 grad(vec2 p) {
60
+ vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
61
+ p3 += dot(p3, p3.yzx + 33.33);
62
+ return -1.0 + 2.0 * fract((p3.xx + p3.yz) * p3.zy);
63
+ }
64
+ float gnoise(vec2 p) {
65
+ vec2 i = floor(p);
66
+ vec2 f = p - i;
67
+ vec2 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
68
+ float a = dot(grad(i), f);
69
+ float b = dot(grad(i + vec2(1.0, 0.0)), f - vec2(1.0, 0.0));
70
+ float c = dot(grad(i + vec2(0.0, 1.0)), f - vec2(0.0, 1.0));
71
+ float d = dot(grad(i + vec2(1.0, 1.0)), f - vec2(1.0, 1.0));
72
+ return 0.5 + 0.5 * mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
73
+ }
74
+ `;
75
+
76
+ /**
77
+ * A lattice point's gradient (the shader's `grad`).
78
+ *
79
+ * @param {number} x The point's x.
80
+ * @param {number} y The point's y.
81
+ * @returns {[number, number]} A gradient, each part −1 to 1.
82
+ */
83
+ export function grad(x, y) {
84
+ let a = fract(x * 0.1031);
85
+ let b = fract(y * 0.103);
86
+ let c = fract(x * 0.0973);
87
+ const d = a * (b + 33.33) + b * (c + 33.33) + c * (a + 33.33);
88
+ a += d;
89
+ b += d;
90
+ c += d;
91
+ return [-1 + 2 * fract((a + b) * c), -1 + 2 * fract((a + c) * b)];
92
+ }
93
+
94
+ /**
95
+ * Gradient noise with a quintic fade (the shader's `gnoise`).
96
+ *
97
+ * @param {number} x Where, x.
98
+ * @param {number} y Where, y.
99
+ * @returns {number} 0–1.
100
+ */
101
+ export function gnoise(x, y) {
102
+ const ix = Math.floor(x);
103
+ const iy = Math.floor(y);
104
+ const fx = x - ix;
105
+ const fy = y - iy;
106
+ const ux = fx * fx * fx * (fx * (fx * 6 - 15) + 10);
107
+ const uy = fy * fy * fy * (fy * (fy * 6 - 15) + 10);
108
+ /**
109
+ * A corner's gradient dotted with the offset to it.
110
+ *
111
+ * @param {[number, number]} g The gradient.
112
+ * @param {number} dx Offset x.
113
+ * @param {number} dy Offset y.
114
+ * @returns {number} The dot product.
115
+ */
116
+ const dot = (g, dx, dy) => g[0] * dx + g[1] * dy;
117
+ const a = dot(grad(ix, iy), fx, fy);
118
+ const b = dot(grad(ix + 1, iy), fx - 1, fy);
119
+ const c = dot(grad(ix, iy + 1), fx, fy - 1);
120
+ const d = dot(grad(ix + 1, iy + 1), fx - 1, fy - 1);
121
+ const ab = a + (b - a) * ux;
122
+ const cd = c + (d - c) * ux;
123
+ return 0.5 + 0.5 * (ab + (cd - ab) * uy);
124
+ }
125
+
126
+ /**
127
+ * GLSL: `float noiseField(vec2 p, float evo, float wt)`, five octaves of
128
+ * gradient noise each warped by the sum so far, 0–1. Needs
129
+ * GLSL_GRADIENT_NOISE before it.
130
+ */
131
+ export const GLSL_NOISE_FIELD = `
132
+ float noiseField(vec2 p, float evo, float wt) {
133
+ float warp = gnoise(p + wt * 0.04);
134
+ float v = 0.0;
135
+ float amp = 0.5;
136
+ float freq = 1.0;
137
+ for (int i = 0; i < 5; i++) {
138
+ v += amp * gnoise(p * freq + vec2(warp * 0.3, warp * 0.25) + evo * 0.06);
139
+ amp *= 0.5;
140
+ freq *= 2.07;
141
+ warp = v;
142
+ }
143
+ return v;
144
+ }
145
+ `;
146
+
147
+ /**
148
+ * The warped field (the shader's `noiseField`): five octaves of gradient
149
+ * noise, each warped by the sum so far.
150
+ *
151
+ * @param {number} x Where, x.
152
+ * @param {number} y Where, y.
153
+ * @param {number} evo The morphing clock.
154
+ * @param {number} wt The warp's clock (seconds).
155
+ * @returns {number} 0–1, near 0.5 on average.
156
+ */
157
+ export function noiseField(x, y, evo, wt) {
158
+ let warp = gnoise(x + wt * 0.04, y + wt * 0.04);
159
+ let v = 0;
160
+ let amp = 0.5;
161
+ let freq = 1;
162
+ for (let i = 0; i < 5; i++) {
163
+ v += amp * gnoise(x * freq + warp * 0.3 + evo * 0.06, y * freq + warp * 0.25 + evo * 0.06);
164
+ amp *= 0.5;
165
+ freq *= 2.07;
166
+ warp = v;
167
+ }
168
+ return v;
169
+ }
@@ -0,0 +1,88 @@
1
+ import type { PaletteName } from './palettes.js';
2
+
3
+ export interface KineticGridConfig {
4
+ /** Two CSS hex colours: the wires' and the pulses'. Default the sketch's blue and white. */
5
+ colors?: string[];
6
+ /** A shared palette by name (`PALETTES`, any case), when there are no `colors`: its first colour for the wires, its glint for the pulses. */
7
+ palette?: PaletteName | (string & {});
8
+ /** The ground, a CSS hex colour (opaque, so the canvas covers what is under it); `transparent` leaves it clear. Default '#0a0a0a'. */
9
+ background?: string;
10
+ /** Grid cells across, whatever the width (cells are square). Default 48. */
11
+ cols?: number;
12
+ /** A wire's width, CSS px. Default 1. */
13
+ width?: number;
14
+ /** How bright a wire at rest is, 0–1; stretched wires brighten to full. Default 0.35. */
15
+ rest?: number;
16
+ /** Drops alive at once, 0–6 (each slot lands a new one about every five seconds). Default 4. */
17
+ drops?: number;
18
+ /** How far the ripples push the wires, 0–1.6 (past that they fold). Default 1. */
19
+ strength?: number;
20
+ /** The ripples' scale: wavelength, reach and speed together, 0.5–2. Default 1. */
21
+ size?: number;
22
+ /** Ring pulses alive at once, 0–4 (each slot about every two and a half seconds). Default 3. */
23
+ pulses?: number;
24
+ /** Plays the clock faster or slower; 0 for one still frame. Default 1. */
25
+ speed?: number;
26
+ /** Seeds where and when drops land, so two cards differ. Default 0. */
27
+ offset?: number;
28
+ /** An area kept calm and dim: centre (0–1 from the top left), radius (share of the height), depth (0–1). */
29
+ quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
30
+ }
31
+
32
+ /** The settings' defaults. */
33
+ export declare const KINETIC_DEFAULTS: {
34
+ background: string;
35
+ cols: number;
36
+ width: number;
37
+ rest: number;
38
+ drops: number;
39
+ strength: number;
40
+ size: number;
41
+ pulses: number;
42
+ speed: number;
43
+ offset: number;
44
+ };
45
+
46
+ /** Most drop slots (the shader's array). */
47
+ export declare const MAX_DROPS: number;
48
+ /** Most pulse slots (the shader's array). */
49
+ export declare const MAX_PULSES: number;
50
+
51
+ /** A drop's timing, seconds: a slot lands one every `period`, up to `jitter` late; it lasts `life`, its height falling by e every `decay`, rising over `rise`. */
52
+ export declare const DROP_TIMING: { period: number; jitter: number; life: number; decay: number; rise: number };
53
+
54
+ /** A pulse's timing, seconds: a slot fires every `period`, up to `jitter` late, and a ring lasts `life`. */
55
+ export declare const PULSE_TIMING: { period: number; jitter: number; life: number };
56
+
57
+ /** The ripple's shape at `size` 1, in widths (speeds a second). */
58
+ export declare const WAVE: { speed: number; wavelength: number; sigma: number; spread: number; core: number; amp: number; reach: number };
59
+
60
+ /** Dave Hoskins' Hash without Sine, three in, three out, each 0–1. */
61
+ export declare function hash33(x: number, y: number, z: number): [number, number, number];
62
+
63
+ /** Where (0–1 of the width and height, from the top left) and when (seconds) a slot's drop for a cycle lands. */
64
+ export declare function dropAt(slot: number, cycle: number, offset?: number): { x: number; y: number; start: number };
65
+
66
+ /** A drop's height over its life, 0–1: a quick rise, an exponential fall, eased to nothing by the end. */
67
+ export declare function dropEnvelope(age: number): number;
68
+
69
+ /** The drops alive at a moment (the clock already at its speed): place, age in seconds, and envelope. */
70
+ export declare function dropsAt(time: number, config?: { drops?: number; offset?: number }): { x: number; y: number; age: number; amp: number }[];
71
+
72
+ /** The pulse rings alive at a moment: centre (0–1 of the width and height), radius in widths, brightness 0–1. */
73
+ export declare function pulsesAt(time: number, config?: { pulses?: number; offset?: number; size?: number }): { x: number; y: number; radius: number; glow: number }[];
74
+
75
+ /** One drop's ripple at a distance (widths) and age (seconds): the radial displacement and its slope. */
76
+ export declare function ripple(r: number, age: number, size?: number): [number, number];
77
+
78
+ /** How far the grid is moved at a point (widths from the top left), summed over the live drops: x and y, widths. */
79
+ export declare function displacement(x: number, y: number, time: number, config?: KineticGridConfig, aspect?: number): [number, number];
80
+
81
+ /** The two colours as the shader takes them: the wires' and the pulses'. */
82
+ export declare function gridColors(config: KineticGridConfig): [string, string];
83
+
84
+ /** A CSS `background` of the grid at rest (for before the shader starts and without WebGL); `aspect` is height over width, default 1.25. */
85
+ export declare function gridStill(config?: KineticGridConfig, aspect?: number): string;
86
+
87
+ /** Starts the grid on an element (its first canvas is drawn into). Returns a function that stops it. */
88
+ export declare function kineticGrid(el: HTMLElement, config: KineticGridConfig): () => void;