@arraypress/effects-astro 0.9.0 → 0.11.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.
- package/AGENTS.md +1 -1
- package/COMPONENTS.md +160 -0
- package/LICENSE +2 -2
- package/package.json +1 -1
- package/src/components/AsciiNoise.astro +37 -0
- package/src/components/CascadeGrid.astro +41 -0
- package/src/components/DitherField.astro +41 -0
- package/src/components/FlowField.astro +38 -0
- package/src/components/KineticGrid.astro +38 -0
- package/src/components/LightLeak.astro +39 -0
- package/src/components/VoronoiMesh.astro +39 -0
- package/src/index.d.ts +15 -0
- package/src/index.js +8 -0
- package/src/runtime/ascii-noise.d.ts +75 -0
- package/src/runtime/ascii-noise.js +276 -0
- package/src/runtime/cascade-grid.d.ts +112 -0
- package/src/runtime/cascade-grid.js +423 -0
- package/src/runtime/dither-field.d.ts +74 -0
- package/src/runtime/dither-field.js +442 -0
- package/src/runtime/flow-field.d.ts +117 -0
- package/src/runtime/flow-field.js +516 -0
- package/src/runtime/hash-noise.d.ts +14 -0
- package/src/runtime/hash-noise.js +169 -0
- package/src/runtime/kinetic-grid.d.ts +88 -0
- package/src/runtime/kinetic-grid.js +487 -0
- package/src/runtime/light-leak.d.ts +72 -0
- package/src/runtime/light-leak.js +408 -0
- package/src/runtime/voronoi-mesh.d.ts +101 -0
- package/src/runtime/voronoi-mesh.js +610 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/dither-field
|
|
3
|
+
*
|
|
4
|
+
* A field of slow noise drawn in square dots: five octaves of warped
|
|
5
|
+
* gradient noise, pushed hard toward empty or full (`contrast`), its edges
|
|
6
|
+
* an 8×8 Bayer ordered dither that packs tighter toward a solid core, the
|
|
7
|
+
* colour stepped into bands (`steps`) from a dim edge colour to a bright
|
|
8
|
+
* core one, blended in Oklab. Ripples run out from a corner (`origin`):
|
|
9
|
+
* rings where the contrast rises, bent by a slow turbulence into
|
|
10
|
+
* shore-wave shapes.
|
|
11
|
+
*
|
|
12
|
+
* Each dot is one pixel of a small canvas, scaled up with square pixels,
|
|
13
|
+
* so the noise is worked out once a dot rather than once a screen pixel:
|
|
14
|
+
* a card of 3 px dots costs about a fortieth of a full-resolution pass.
|
|
15
|
+
* The field is measured in widths, so a 330 px card and a 1280 px hero
|
|
16
|
+
* show the same shapes; the dots keep their size in CSS px.
|
|
17
|
+
*
|
|
18
|
+
* Every clock is in seconds. The ripple has no history to keep: a ring's
|
|
19
|
+
* strength at distance d is a smooth noise of (t − d / travel), so it is
|
|
20
|
+
* the same at any frame rate and needs nothing from the CPU. The same
|
|
21
|
+
* field is here in plain JavaScript (`ditherDots`), dot for dot.
|
|
22
|
+
*
|
|
23
|
+
* Clear where there are no dots (or the `background` colour), so it lays
|
|
24
|
+
* over any ground. A `quiet` area empties the field behind copy. Without
|
|
25
|
+
* WebGL nothing is drawn; with prefers-reduced-motion it is one still frame.
|
|
26
|
+
*
|
|
27
|
+
* After `ripple_dither_bg`, a Processing sketch shared with the owner
|
|
28
|
+
* (2026-11-20): the field, the dither zones, the ripple and the tunings are
|
|
29
|
+
* its; here the Bayer matrix is worked out in the shader, the ring buffer of
|
|
30
|
+
* past levels is an analytic signal, and the hashes are Dave Hoskins' Hash
|
|
31
|
+
* without Sine (MIT, © 2014 David Hoskins), which keeps the GPU and
|
|
32
|
+
* `ditherDots` in step.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import { createStage } from './stage.js';
|
|
36
|
+
import { GLSL_GRADIENT_NOISE, GLSL_NOISE_FIELD, grad, gnoise, noiseField } from './hash-noise.js';
|
|
37
|
+
import { GLSL_OKLAB, parseColor, toOklab, mixOklab, toHex } from './color.js';
|
|
38
|
+
import { GLSL_QUIET, quietUniform } from './glsl.js';
|
|
39
|
+
import { watchColors } from './mount.js';
|
|
40
|
+
import { pickColors } from './palettes.js';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @typedef {object} DitherFieldConfig
|
|
44
|
+
* @property {string[]} [colors] Two CSS hex colours: the dim edge's and the bright core's. Default a grey and amber.
|
|
45
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`: its first and last colours.
|
|
46
|
+
* @property {string} [background] A CSS hex colour to fill behind the dots; none leaves the canvas clear there.
|
|
47
|
+
* @property {number} [dot=3] A dot's size in CSS px (rounded to whole screen pixels).
|
|
48
|
+
* @property {number} [scale=1.1] How many noise features span the width (higher is busier).
|
|
49
|
+
* @property {number} [contrast=6] How hard the edges are: higher thins the dithered band.
|
|
50
|
+
* @property {number} [brightness=-0.15] Shifts the whole field; lower leaves more of it empty.
|
|
51
|
+
* @property {number} [steps=24] Colour bands across the field; 0 blends smoothly.
|
|
52
|
+
* @property {[number, number]} [drift=[0.02, 0.007]] How fast the field pans, noise features a second, x and y.
|
|
53
|
+
* @property {number} [evolve=0.35] How fast the shapes morph.
|
|
54
|
+
* @property {number} [ripple=1] How strongly the rings lift the field; 0 for none.
|
|
55
|
+
* @property {number} [rings=3] Rings across the width, roughly (higher is tighter).
|
|
56
|
+
* @property {number} [rippleSpeed=0.08] How fast the rings travel, widths a second.
|
|
57
|
+
* @property {[number, number]} [origin=[0.04, 0.3]] Where the rings start, 0–1 from the top left.
|
|
58
|
+
* @property {[number, number]} [offset=[0, 0]] Where the field and the rings start, so two cards differ.
|
|
59
|
+
* @property {number} [speed=1] Plays every clock faster or slower; 0 for one still frame.
|
|
60
|
+
* @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
|
|
61
|
+
* An area kept empty: centre (0–1 from the top left), radius (share of the height), depth (0–1).
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
/** The settings' defaults, shared by the shader and `ditherDots`. */
|
|
65
|
+
export const DITHER_DEFAULTS = {
|
|
66
|
+
dot: 3,
|
|
67
|
+
scale: 1.1,
|
|
68
|
+
contrast: 6,
|
|
69
|
+
brightness: -0.15,
|
|
70
|
+
steps: 24,
|
|
71
|
+
drift: /** @type {[number, number]} */ ([0.02, 0.007]),
|
|
72
|
+
evolve: 0.35,
|
|
73
|
+
ripple: 1,
|
|
74
|
+
rings: 3,
|
|
75
|
+
rippleSpeed: 0.08,
|
|
76
|
+
origin: /** @type {[number, number]} */ ([0.04, 0.3]),
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The sketch's dither zones, on the field's 0–1 value: dots begin at
|
|
81
|
+
* `edge`, pack tighter toward `core`, solid from there. `bias` thickens the
|
|
82
|
+
* dots everywhere and `inner` more so near the core; `fuzz` moves the edge a
|
|
83
|
+
* little per dot so it is not a hard contour. `gain` is how far a full ring
|
|
84
|
+
* lifts the contrast and `lift` the level (so a ring brings dots up even
|
|
85
|
+
* out of the empty ground), `warp` how far the turbulence bends the rings (widths)
|
|
86
|
+
* and `warpScale` its features across the width; rings fade out between
|
|
87
|
+
* `fadeFrom` and `fadeTo` widths from the origin.
|
|
88
|
+
*/
|
|
89
|
+
const ZONES = { edge: 0.1, core: 0.3, bias: 0.2, inner: 0.8, fuzz: 0.02, gain: 8, lift: 0.12, warp: 1, warpScale: 3, fadeFrom: 1, fadeTo: 1.6 };
|
|
90
|
+
|
|
91
|
+
/** The default colours: the sketch's grey edge and amber core. */
|
|
92
|
+
const AMBER = ['#b4b4b4', '#ffbe50'];
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* GLSL (ES 1.00): `float bayer8(vec2 cell)`, the 8×8 Bayer threshold
|
|
96
|
+
* (0–63 over 64) built from the 2×2 one, and `float ripplePulse(float s)`,
|
|
97
|
+
* a smooth 1-D noise, 0–1, that the rings are cut from. `bayer` and
|
|
98
|
+
* `ripplePulse` below are the same in JavaScript.
|
|
99
|
+
*/
|
|
100
|
+
const GLSL_DITHER = `
|
|
101
|
+
float bayer2(vec2 a) { return fract(a.x * 0.5 + a.y * a.y * 0.75); }
|
|
102
|
+
float bayer8(vec2 cell) {
|
|
103
|
+
vec2 a = mod(floor(cell), 8.0);
|
|
104
|
+
vec2 b = floor(a * 0.5);
|
|
105
|
+
vec2 c = floor(a * 0.25);
|
|
106
|
+
return bayer2(a) + bayer2(b) * 0.25 + bayer2(c) * 0.0625;
|
|
107
|
+
}
|
|
108
|
+
float hash11(float p) {
|
|
109
|
+
p = fract(p * 0.1031);
|
|
110
|
+
p *= p + 33.33;
|
|
111
|
+
p *= p + p;
|
|
112
|
+
return fract(p);
|
|
113
|
+
}
|
|
114
|
+
float ripplePulse(float s) {
|
|
115
|
+
float i = floor(s);
|
|
116
|
+
float f = s - i;
|
|
117
|
+
float u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
|
|
118
|
+
return smoothstep(0.2, 0.8, mix(hash11(i), hash11(i + 1.0), u));
|
|
119
|
+
}
|
|
120
|
+
`;
|
|
121
|
+
|
|
122
|
+
const FRAG = `
|
|
123
|
+
precision highp float;
|
|
124
|
+
uniform vec2 uRes;
|
|
125
|
+
uniform vec2 uSize;
|
|
126
|
+
uniform float uTime;
|
|
127
|
+
uniform float uScale;
|
|
128
|
+
uniform float uContrast;
|
|
129
|
+
uniform float uBrightness;
|
|
130
|
+
uniform float uSteps;
|
|
131
|
+
uniform vec2 uDrift;
|
|
132
|
+
uniform float uEvolve;
|
|
133
|
+
uniform vec2 uOffset;
|
|
134
|
+
uniform vec2 uOrigin;
|
|
135
|
+
uniform vec3 uRipple;
|
|
136
|
+
uniform float uPhase;
|
|
137
|
+
uniform vec3 uEdge;
|
|
138
|
+
uniform vec3 uCore;
|
|
139
|
+
uniform vec4 uBg;
|
|
140
|
+
${GLSL_OKLAB}
|
|
141
|
+
${GLSL_QUIET}
|
|
142
|
+
${GLSL_GRADIENT_NOISE}
|
|
143
|
+
${GLSL_NOISE_FIELD}
|
|
144
|
+
${GLSL_DITHER}
|
|
145
|
+
|
|
146
|
+
void main() {
|
|
147
|
+
// One pixel a dot, counted from the top left (the canvas may run a dot past the element).
|
|
148
|
+
vec2 cell = vec2(floor(gl_FragCoord.x), floor(uRes.y - gl_FragCoord.y));
|
|
149
|
+
// In widths, so the shapes keep their size whatever the shape of the target.
|
|
150
|
+
vec2 c = (cell + 0.5) / uSize.x;
|
|
151
|
+
float t = uTime;
|
|
152
|
+
float v = noiseField(c * uScale + uOffset + uDrift * t, uEvolve * t, t);
|
|
153
|
+
|
|
154
|
+
// The rings: a smooth noise of (t - d / travel), d bent by a slow turbulence.
|
|
155
|
+
float ws = gnoise(c * ${ZONES.warpScale.toFixed(1)} + uOffset.yx + uDrift * 0.4 * t) - 0.5;
|
|
156
|
+
float d = distance(c + ws * ${ZONES.warp.toFixed(1)}, uOrigin);
|
|
157
|
+
float s = (t * uRipple.z - d) * uRipple.y + uPhase;
|
|
158
|
+
float band = 0.5 + 0.5 * cos(6.2831853 * s);
|
|
159
|
+
float ring = ripplePulse(s) * band * band * (1.0 - smoothstep(${ZONES.fadeFrom.toFixed(1)}, ${ZONES.fadeTo.toFixed(1)}, d));
|
|
160
|
+
v = clamp((v - 0.5) * (uContrast + ring * uRipple.x) + 0.5 + uBrightness + ring * uRipple.x * ${(ZONES.lift / ZONES.gain).toFixed(4)}, 0.0, 1.0);
|
|
161
|
+
|
|
162
|
+
vec2 q = vec2(c.x - 0.5, (0.5 * uSize.y - (cell.y + 0.5)) / uSize.x) * uSize.x / uSize.y;
|
|
163
|
+
v *= mix(1.0 - uQuiet.w, 1.0, quietOpen(q));
|
|
164
|
+
|
|
165
|
+
// Dots from the edge in, packing tighter toward the core; solid from the core on.
|
|
166
|
+
float near = smoothstep(${ZONES.edge}, ${ZONES.core}, v);
|
|
167
|
+
float dots = step(bayer8(cell), v + ${ZONES.bias} + near * ${ZONES.inner});
|
|
168
|
+
float fuzz = grad(cell + 17.0).x * ${ZONES.fuzz};
|
|
169
|
+
float lit = mix(dots, 1.0, step(${ZONES.core}, v)) * step(${ZONES.edge} + fuzz, v);
|
|
170
|
+
|
|
171
|
+
float tone = uSteps > 0.5 ? floor(v * uSteps + 0.5) / uSteps : v;
|
|
172
|
+
vec3 col = fromOklab(mix(uEdge, uCore, smoothstep(0.35, 0.75, tone)));
|
|
173
|
+
gl_FragColor = vec4(col * lit + uBg.rgb * uBg.a * (1.0 - lit), lit + uBg.a * (1.0 - lit));
|
|
174
|
+
}
|
|
175
|
+
`;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The fractional part.
|
|
179
|
+
*
|
|
180
|
+
* @param {number} n A number.
|
|
181
|
+
* @returns {number} Its part after the point, 0–1.
|
|
182
|
+
*/
|
|
183
|
+
const fract = (n) => n - Math.floor(n);
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Hermite smoothing between two edges (GLSL's `smoothstep`).
|
|
187
|
+
*
|
|
188
|
+
* @param {number} e0 Where it starts rising.
|
|
189
|
+
* @param {number} e1 Where it reaches 1.
|
|
190
|
+
* @param {number} x The value.
|
|
191
|
+
* @returns {number} 0–1.
|
|
192
|
+
*/
|
|
193
|
+
const smooth = (e0, e1, x) => {
|
|
194
|
+
const t = Math.min(1, Math.max(0, (x - e0) / (e1 - e0)));
|
|
195
|
+
return t * t * (3 - 2 * t);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The quintic fade between lattice points.
|
|
200
|
+
*
|
|
201
|
+
* @param {number} f 0–1.
|
|
202
|
+
* @returns {number} 0–1, flat at both ends.
|
|
203
|
+
*/
|
|
204
|
+
const quintic = (f) => f * f * f * (f * (f * 6 - 15) + 10);
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The 8×8 Bayer threshold at a dot (the shader's `bayer8`): the classic
|
|
208
|
+
* ordered-dither matrix, 0/64 to 63/64, built from the 2×2 one at three
|
|
209
|
+
* scales, so neither side needs a texture.
|
|
210
|
+
*
|
|
211
|
+
* @param {number} x The dot's column (any integer; it repeats every 8).
|
|
212
|
+
* @param {number} y The dot's row.
|
|
213
|
+
* @returns {number} The threshold, 0–63/64.
|
|
214
|
+
*/
|
|
215
|
+
export function bayer(x, y) {
|
|
216
|
+
/**
|
|
217
|
+
* The 2×2 matrix (0, 2 / 3, 1 over 4) by a cell's parity.
|
|
218
|
+
*
|
|
219
|
+
* @param {number} a Column.
|
|
220
|
+
* @param {number} b Row.
|
|
221
|
+
* @returns {number} 0, 0.25, 0.5 or 0.75.
|
|
222
|
+
*/
|
|
223
|
+
const two = (a, b) => fract(a * 0.5 + b * b * 0.75);
|
|
224
|
+
const ax = ((Math.floor(x) % 8) + 8) % 8;
|
|
225
|
+
const ay = ((Math.floor(y) % 8) + 8) % 8;
|
|
226
|
+
return two(ax, ay) + two(Math.floor(ax / 2), Math.floor(ay / 2)) * 0.25 + two(Math.floor(ax / 4), Math.floor(ay / 4)) * 0.0625;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* A hash of one number (the shader's `hash11`): Dave Hoskins' Hash without
|
|
231
|
+
* Sine, small multipliers so 32-bit and 64-bit floats agree.
|
|
232
|
+
*
|
|
233
|
+
* @param {number} p The number.
|
|
234
|
+
* @returns {number} 0–1.
|
|
235
|
+
*/
|
|
236
|
+
function hash11(p) {
|
|
237
|
+
let h = fract(p * 0.1031);
|
|
238
|
+
h *= h + 33.33;
|
|
239
|
+
h *= h + h;
|
|
240
|
+
return fract(h);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The signal the rings are cut from (the shader's `ripplePulse`): a smooth
|
|
245
|
+
* 1-D noise, pushed toward 0 and 1 so the rings come as swells and calms.
|
|
246
|
+
*
|
|
247
|
+
* @param {number} s Where along it (one lattice step is one ring's spacing).
|
|
248
|
+
* @returns {number} 0–1.
|
|
249
|
+
*/
|
|
250
|
+
export function ripplePulse(s) {
|
|
251
|
+
const i = Math.floor(s);
|
|
252
|
+
const a = hash11(i);
|
|
253
|
+
return smooth(0.2, 0.8, a + (hash11(i + 1) - a) * quintic(s - i));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* How strong the ring is at a distance from the origin at a time: what a
|
|
258
|
+
* ring buffer of past levels would hold, worked out instead, so it costs
|
|
259
|
+
* nothing a frame and is the same at any frame rate. A ring at distance d
|
|
260
|
+
* now is what left the origin d / rippleSpeed seconds ago: one ring a
|
|
261
|
+
* lattice step of `ripplePulse`, as strong as the pulse is there.
|
|
262
|
+
*
|
|
263
|
+
* @param {number} d Widths from the origin.
|
|
264
|
+
* @param {number} time Seconds (already at its speed).
|
|
265
|
+
* @param {DitherFieldConfig} [config] The settings (`rings`, `rippleSpeed`, `offset`).
|
|
266
|
+
* @returns {number} 0–1, before `ripple` scales it; fading to 0 far out.
|
|
267
|
+
*/
|
|
268
|
+
export function rippleAt(d, time, config = {}) {
|
|
269
|
+
const rings = config.rings ?? DITHER_DEFAULTS.rings;
|
|
270
|
+
const travel = config.rippleSpeed ?? DITHER_DEFAULTS.rippleSpeed;
|
|
271
|
+
const s = (time * travel - d) * rings + phaseOf(config.offset);
|
|
272
|
+
const band = 0.5 + 0.5 * Math.cos(2 * Math.PI * s);
|
|
273
|
+
return ripplePulse(s) * band * band * (1 - smooth(ZONES.fadeFrom, ZONES.fadeTo, d));
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Where on the ring signal an offset starts it, so two cards ripple apart.
|
|
278
|
+
*
|
|
279
|
+
* @param {[number, number] | undefined} offset The field's offset.
|
|
280
|
+
* @returns {number} A phase.
|
|
281
|
+
*/
|
|
282
|
+
function phaseOf(offset) {
|
|
283
|
+
const [x, y] = offset ?? [0, 0];
|
|
284
|
+
return x * 7.31 + y * 3.17;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* One frame dot for dot, as the shader draws it on a target exactly `cols`
|
|
289
|
+
* by `rows` dots: each dot empty (−1) or lit with its tone, 0 the edge
|
|
290
|
+
* colour to 1 the core's.
|
|
291
|
+
*
|
|
292
|
+
* @param {DitherFieldConfig} config The settings (the same as the shader's).
|
|
293
|
+
* @param {number} cols Dots across.
|
|
294
|
+
* @param {number} rows Dots down.
|
|
295
|
+
* @param {number} [time=0] Seconds, as the shader's clock (already at its speed).
|
|
296
|
+
* @returns {number[][]} Each row's dots: −1, or a tone 0–1.
|
|
297
|
+
*/
|
|
298
|
+
export function ditherDots(config, cols, rows, time = 0) {
|
|
299
|
+
const o = { ...DITHER_DEFAULTS, ...config };
|
|
300
|
+
const [ox, oy] = config.offset ?? [0, 0];
|
|
301
|
+
const origin = [o.origin[0], (o.origin[1] * rows) / cols];
|
|
302
|
+
const [qx, qy, radius, depth] = quietUniform(config.quiet, cols / rows);
|
|
303
|
+
const gain = ZONES.gain * Math.max(0, o.ripple);
|
|
304
|
+
return Array.from({ length: rows }, (_, r) =>
|
|
305
|
+
Array.from({ length: cols }, (_, k) => {
|
|
306
|
+
const cx = (k + 0.5) / cols;
|
|
307
|
+
const cy = (r + 0.5) / cols;
|
|
308
|
+
let v = noiseField(cx * o.scale + ox + o.drift[0] * time, cy * o.scale + oy + o.drift[1] * time, o.evolve * time, time);
|
|
309
|
+
const ws = gnoise(cx * ZONES.warpScale + oy + o.drift[0] * 0.4 * time, cy * ZONES.warpScale + ox + o.drift[1] * 0.4 * time) - 0.5;
|
|
310
|
+
const d = Math.hypot(cx + ws * ZONES.warp - origin[0], cy + ws * ZONES.warp - origin[1]);
|
|
311
|
+
const ring = gain > 0 ? rippleAt(d, time, o) * gain : 0;
|
|
312
|
+
v = Math.min(1, Math.max(0, (v - 0.5) * (o.contrast + ring) + 0.5 + o.brightness + ring * Number((ZONES.lift / ZONES.gain).toFixed(4))));
|
|
313
|
+
if (radius > 0) {
|
|
314
|
+
const qd = Math.hypot((cx - 0.5) * (cols / rows) - qx, (0.5 * rows - (r + 0.5)) / rows - qy);
|
|
315
|
+
v *= 1 - depth + depth * smooth(radius * 0.06, radius, qd);
|
|
316
|
+
}
|
|
317
|
+
const near = smooth(ZONES.edge, ZONES.core, v);
|
|
318
|
+
const dots = v + ZONES.bias + near * ZONES.inner >= bayer(k, r) ? 1 : 0;
|
|
319
|
+
const fuzz = grad(k + 17, r + 17)[0] * ZONES.fuzz;
|
|
320
|
+
const lit = (v >= ZONES.core ? 1 : dots) * (v >= ZONES.edge + fuzz ? 1 : 0);
|
|
321
|
+
if (!lit) return -1;
|
|
322
|
+
const tone = o.steps > 0.5 ? Math.floor(v * o.steps + 0.5) / o.steps : v;
|
|
323
|
+
return smooth(0.35, 0.75, tone);
|
|
324
|
+
}),
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* The two colours as the shader takes them.
|
|
330
|
+
*
|
|
331
|
+
* @param {DitherFieldConfig} config The settings.
|
|
332
|
+
* @returns {[string, string]} The dim edge colour and the bright core's.
|
|
333
|
+
*/
|
|
334
|
+
export function ditherColors(config) {
|
|
335
|
+
const colors = pickColors(config, AMBER);
|
|
336
|
+
return [colors[0], colors[colors.length - 1]];
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* A CSS `background` that stands in for the field before the shader starts
|
|
341
|
+
* and without WebGL: the ground, a dim checker of dots (the dither at half
|
|
342
|
+
* strength) under a glow of the core colour around the ring origin and a
|
|
343
|
+
* pool of the edge colour across from it, and the quiet area left to the
|
|
344
|
+
* ground.
|
|
345
|
+
*
|
|
346
|
+
* @param {DitherFieldConfig} config The settings (`colors`/`palette`, `background`, `dot`, `origin`, `quiet`).
|
|
347
|
+
* @returns {string} The CSS background.
|
|
348
|
+
*/
|
|
349
|
+
export function ditherStill(config) {
|
|
350
|
+
const [edge, core] = ditherColors(config).map(parseColor);
|
|
351
|
+
const ground = config.background ?? '#000000';
|
|
352
|
+
const g = parseColor(ground);
|
|
353
|
+
const [x, y] = config.origin ?? DITHER_DEFAULTS.origin;
|
|
354
|
+
const dot = Math.max(1, config.dot ?? DITHER_DEFAULTS.dot);
|
|
355
|
+
const mid = toHex(mixOklab(edge, core, 0.5));
|
|
356
|
+
const pool = toHex(mixOklab(g, edge, 0.85));
|
|
357
|
+
const dots = toHex(mixOklab(g, edge, 0.6));
|
|
358
|
+
const pc = (/** @type {number} */ n) => `${Math.round(n * 100)}%`;
|
|
359
|
+
const quiet = config.quiet;
|
|
360
|
+
const r = pc((quiet?.radius ?? 0.8) * 0.55);
|
|
361
|
+
return [
|
|
362
|
+
quiet ? `radial-gradient(${r} ${r} at ${pc(quiet.x ?? 0.5)} ${pc(quiet.y ?? 0.5)}, ${ground} 55%, transparent 100%)` : '',
|
|
363
|
+
`radial-gradient(70% 55% at ${pc(x)} ${pc(y)}, ${toHex(core)} 0%, ${mid} 30%, transparent 72%)`,
|
|
364
|
+
`radial-gradient(80% 65% at ${pc(1 - x)} ${pc(1 - y)}, ${pool} 0%, ${pool} 25%, transparent 75%)`,
|
|
365
|
+
`repeating-conic-gradient(${dots} 0 25%, transparent 0 50%) 0 0 / ${dot * 2}px ${dot * 2}px`,
|
|
366
|
+
ground,
|
|
367
|
+
]
|
|
368
|
+
.filter(Boolean)
|
|
369
|
+
.join(', ');
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Starts the field on an element: a canvas inside it (its first), one
|
|
374
|
+
* pixel a dot, scaled up to cover it with square pixels.
|
|
375
|
+
*
|
|
376
|
+
* @param {HTMLElement} el The element.
|
|
377
|
+
* @param {DitherFieldConfig} config The settings.
|
|
378
|
+
* @returns {() => void} Stops it.
|
|
379
|
+
*/
|
|
380
|
+
export function ditherField(el, config) {
|
|
381
|
+
const o = { ...DITHER_DEFAULTS, ...config };
|
|
382
|
+
const speed = config.speed ?? 1;
|
|
383
|
+
const lab = (/** @type {string} */ c) => toOklab(parseColor(c));
|
|
384
|
+
let [edge, core] = ditherColors(config).map(lab);
|
|
385
|
+
const bg = config.background ? [...parseColor(config.background), 1] : [0, 0, 0, 0];
|
|
386
|
+
const native = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
|
|
387
|
+
// Whole screen pixels a dot, so the scaled-up pixels are all the same size.
|
|
388
|
+
const dotCss = Math.max(1, Math.round(Math.max(0.5, o.dot) * native)) / native;
|
|
389
|
+
const start = performance.now();
|
|
390
|
+
const canvas = el.querySelector('canvas');
|
|
391
|
+
|
|
392
|
+
const { stage, stop } = createStage(el, {
|
|
393
|
+
fragment: FRAG,
|
|
394
|
+
still: speed <= 0,
|
|
395
|
+
// One pixel a dot already: never lower it further.
|
|
396
|
+
adaptive: false,
|
|
397
|
+
scale: 1 / (dotCss * Math.min(native, 2)),
|
|
398
|
+
setup({ gl, uniform, canvas }) {
|
|
399
|
+
canvas.style.imageRendering = 'pixelated';
|
|
400
|
+
gl.uniform1f(uniform('uScale'), o.scale);
|
|
401
|
+
gl.uniform1f(uniform('uContrast'), o.contrast);
|
|
402
|
+
gl.uniform1f(uniform('uBrightness'), o.brightness);
|
|
403
|
+
gl.uniform1f(uniform('uSteps'), o.steps);
|
|
404
|
+
gl.uniform2f(uniform('uDrift'), o.drift[0], o.drift[1]);
|
|
405
|
+
gl.uniform1f(uniform('uEvolve'), o.evolve);
|
|
406
|
+
gl.uniform2f(uniform('uOffset'), ...(config.offset ?? [0, 0]));
|
|
407
|
+
gl.uniform3f(uniform('uRipple'), ZONES.gain * Math.max(0, o.ripple), o.rings, o.rippleSpeed);
|
|
408
|
+
gl.uniform1f(uniform('uPhase'), phaseOf(config.offset));
|
|
409
|
+
gl.uniform4f(uniform('uBg'), bg[0], bg[1], bg[2], bg[3]);
|
|
410
|
+
},
|
|
411
|
+
resize({ gl, uniform, canvas }) {
|
|
412
|
+
// The element in dots; the canvas rounds up and runs past it (clipped), so every dot is whole.
|
|
413
|
+
const w = Math.max(1, el.clientWidth / dotCss);
|
|
414
|
+
const h = Math.max(1, el.clientHeight / dotCss);
|
|
415
|
+
canvas.width = Math.ceil(w);
|
|
416
|
+
canvas.height = Math.ceil(h);
|
|
417
|
+
canvas.style.width = `${canvas.width * dotCss}px`;
|
|
418
|
+
canvas.style.height = `${canvas.height * dotCss}px`;
|
|
419
|
+
gl.uniform2f(uniform('uSize'), w, h);
|
|
420
|
+
gl.uniform2f(uniform('uOrigin'), o.origin[0], (o.origin[1] * h) / w);
|
|
421
|
+
gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, w / h));
|
|
422
|
+
},
|
|
423
|
+
uniforms({ gl, uniform }, now) {
|
|
424
|
+
gl.uniform1f(uniform('uTime'), ((now - start) / 1000) * Math.max(0, speed));
|
|
425
|
+
gl.uniform3f(uniform('uEdge'), edge[0], edge[1], edge[2]);
|
|
426
|
+
gl.uniform3f(uniform('uCore'), core[0], core[1], core[2]);
|
|
427
|
+
},
|
|
428
|
+
});
|
|
429
|
+
if (!stage) return stop;
|
|
430
|
+
|
|
431
|
+
const unwatch = watchColors(el, (next) => {
|
|
432
|
+
[edge, core] = ditherColors({ colors: next.colors }).map(lab);
|
|
433
|
+
if (stage.still) stage.redraw();
|
|
434
|
+
});
|
|
435
|
+
return () => {
|
|
436
|
+
stop();
|
|
437
|
+
unwatch();
|
|
438
|
+
canvas?.style.removeProperty('width');
|
|
439
|
+
canvas?.style.removeProperty('height');
|
|
440
|
+
canvas?.style.removeProperty('image-rendering');
|
|
441
|
+
};
|
|
442
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { PaletteName } from './palettes.js';
|
|
2
|
+
|
|
3
|
+
export interface FlowFieldConfig {
|
|
4
|
+
/** The trails' colours, CSS hex, blended in Oklab along each trail's life (or by its speed). Default white. */
|
|
5
|
+
colors?: string[];
|
|
6
|
+
/** A shared palette by name (`PALETTES`, any case), when there are no `colors`. */
|
|
7
|
+
palette?: PaletteName | (string & {});
|
|
8
|
+
/** The ground, CSS hex: filled every frame, so the canvas is opaque. Default `#000000`. */
|
|
9
|
+
background?: string;
|
|
10
|
+
/** What picks a trail's colour along `colors`: its age, how fast it travels, or one colour each at random. Default `life`. */
|
|
11
|
+
colorBy?: 'life' | 'speed' | 'random';
|
|
12
|
+
/** Trails per 100 × 100 CSS px, on average. Default 12. */
|
|
13
|
+
density?: number;
|
|
14
|
+
/** How much of its path a trail shows behind it, seconds. Default 0.5. */
|
|
15
|
+
length?: number;
|
|
16
|
+
/** Line width, CSS px. Default 0.7. */
|
|
17
|
+
weight?: number;
|
|
18
|
+
/** A trail head's opacity, 0–1; the tail fades from it. Default 0.75. */
|
|
19
|
+
opacity?: number;
|
|
20
|
+
/** How fast trails travel, CSS px a second, on average. Default 110. */
|
|
21
|
+
pace?: number;
|
|
22
|
+
/** How many noise features span the element (across √(width × height)); higher is busier. Default 2. */
|
|
23
|
+
scale?: number;
|
|
24
|
+
/** How fast the field changes, noise units a second. Default 0.15. */
|
|
25
|
+
evolve?: number;
|
|
26
|
+
/** A trail's average life, seconds (each lives half to one and a half times it). Default 4. */
|
|
27
|
+
life?: number;
|
|
28
|
+
/** How far the slow drives swing density, pace, weight and evolution, 0–1; 0 holds them. Default 0.7. */
|
|
29
|
+
vary?: number;
|
|
30
|
+
/** Plays every clock faster or slower; 0 for one still frame. Default 1. */
|
|
31
|
+
speed?: number;
|
|
32
|
+
/** Which field and which trails: the same seed draws the same. Default 1. */
|
|
33
|
+
seed?: number;
|
|
34
|
+
/** An area with fewer, fainter trails: centre (0–1 from the top left), radius (share of the height), depth (0–1). */
|
|
35
|
+
quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The settings' defaults. */
|
|
39
|
+
export declare const FLOW_DEFAULTS: Readonly<{
|
|
40
|
+
background: string;
|
|
41
|
+
colorBy: 'life' | 'speed' | 'random';
|
|
42
|
+
density: number;
|
|
43
|
+
length: number;
|
|
44
|
+
weight: number;
|
|
45
|
+
opacity: number;
|
|
46
|
+
pace: number;
|
|
47
|
+
scale: number;
|
|
48
|
+
evolve: number;
|
|
49
|
+
life: number;
|
|
50
|
+
vary: number;
|
|
51
|
+
speed: number;
|
|
52
|
+
seed: number;
|
|
53
|
+
}>;
|
|
54
|
+
|
|
55
|
+
/** Simulation ticks a second. */
|
|
56
|
+
export declare const TICKS: number;
|
|
57
|
+
|
|
58
|
+
/** The field's noise in three dimensions (across, down, time), around 0–1. */
|
|
59
|
+
export declare function flowNoise(x: number, y: number, z: number): number;
|
|
60
|
+
|
|
61
|
+
/** The field's heading at a point: the noise times two whole turns, radians. */
|
|
62
|
+
export declare function flowAngle(x: number, y: number, z: number): number;
|
|
63
|
+
|
|
64
|
+
/** One of the slow drives at `t` seconds, 0–1. */
|
|
65
|
+
export declare function driveAt(t: number, channel: number): number;
|
|
66
|
+
|
|
67
|
+
/** How much a quiet area lets through at a point (CSS px): 1 − depth at its centre, 1 at its edge and beyond. */
|
|
68
|
+
export declare function quietFactor(x: number, y: number, width: number, height: number, quiet: FlowFieldConfig['quiet']): number;
|
|
69
|
+
|
|
70
|
+
/** How many trails an element holds on average. */
|
|
71
|
+
export declare function trailCount(width: number, height: number, density?: number): number;
|
|
72
|
+
|
|
73
|
+
/** The colours as strokes take them: steps along `colors`, blended in Oklab; `rgb(…)` strings. */
|
|
74
|
+
export declare function flowRamp(colors: string[], steps?: number): string[];
|
|
75
|
+
|
|
76
|
+
/** A trail's opacity from its age: fading in at birth, out at the end, 0–1. */
|
|
77
|
+
export declare function lifeAlpha(age: number, life: number): number;
|
|
78
|
+
|
|
79
|
+
export interface Trail {
|
|
80
|
+
alive: boolean;
|
|
81
|
+
x: number;
|
|
82
|
+
y: number;
|
|
83
|
+
/** Its last positions, a ring of x, y pairs. */
|
|
84
|
+
path: Float32Array;
|
|
85
|
+
/** The ring's newest pair. */
|
|
86
|
+
head: number;
|
|
87
|
+
age: number;
|
|
88
|
+
life: number;
|
|
89
|
+
/** Its speed, CSS px a second, set at birth. */
|
|
90
|
+
pace: number;
|
|
91
|
+
/** Where it sits along the colours for `colorBy: 'speed'` or `'random'`, 0–1. */
|
|
92
|
+
tone: number;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface Flow {
|
|
96
|
+
trails: Trail[];
|
|
97
|
+
width: number;
|
|
98
|
+
height: number;
|
|
99
|
+
/** Positions a trail keeps. */
|
|
100
|
+
steps: number;
|
|
101
|
+
/** Seconds simulated. */
|
|
102
|
+
time: number;
|
|
103
|
+
/** How many trails it wants now. */
|
|
104
|
+
target: number;
|
|
105
|
+
/** The line width now, CSS px. */
|
|
106
|
+
weight: number;
|
|
107
|
+
/** Steps the simulation on by some seconds, at fixed ticks. */
|
|
108
|
+
advance(seconds: number): void;
|
|
109
|
+
/** Fills the buckets (colour × alpha) with each visible segment (its point count, then x, y pairs); returns how many segments. */
|
|
110
|
+
segments(buckets: number[][]): number;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The simulation without a canvas, run on until settled (`warm` seconds, default 2.5). */
|
|
114
|
+
export declare function createFlow(config: FlowFieldConfig, width: number, height: number, options?: { warm?: number }): Flow;
|
|
115
|
+
|
|
116
|
+
/** Starts the flow field on an element (its first canvas is drawn into). Returns a function that stops it. */
|
|
117
|
+
export declare function flowField(el: HTMLElement, config: FlowFieldConfig): () => void;
|