@arraypress/effects-astro 0.10.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 +128 -0
- package/LICENSE +2 -2
- package/package.json +1 -1
- 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 +13 -0
- package/src/index.js +7 -0
- package/src/runtime/ascii-noise.js +6 -107
- 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,408 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/light-leak
|
|
3
|
+
*
|
|
4
|
+
* A soft, slow colour field, a lava lamp or a light leak: value-noise fBm
|
|
5
|
+
* (up to four octaves) under a gentle domain warp, sampled as three fields
|
|
6
|
+
* that each drift on their own slow loop. The first mixes the two darkest
|
|
7
|
+
* colours, the second the two brightest, the third mixes those two, all
|
|
8
|
+
* in Oklab, so the ramp never passes through mud. Over it a very slow
|
|
9
|
+
* breath, the whole frame 3% darker and back over three and a half
|
|
10
|
+
* minutes; never a flash. `contrast` pushes each field toward the ramp's
|
|
11
|
+
* ends so the brightest colour shows.
|
|
12
|
+
*
|
|
13
|
+
* One clock in seconds (`speed` plays it faster or slower; 0 is one still
|
|
14
|
+
* frame). The field is measured in the canvas's shorter side, from its
|
|
15
|
+
* centre, so a 330 px card and a 1280 px hero show blobs of the same
|
|
16
|
+
* share; `offset` moves the sample window so two cards differ. A `quiet`
|
|
17
|
+
* area flattens the field behind copy to one even tone of the ramp
|
|
18
|
+
* (`quietTone`, 0 the darkest colour, 1 the brightest). Opaque by nature;
|
|
19
|
+
* drawn at half the pixel ratio, as nothing in it is sharp, with a fixed
|
|
20
|
+
* dither against banding. The same field is here in plain JavaScript
|
|
21
|
+
* (`leakFields`, `leakColor`), and `lightLeakStill` makes a CSS background
|
|
22
|
+
* from it to show before the shader starts or without WebGL.
|
|
23
|
+
*
|
|
24
|
+
* After `lavalamp_bg`, a Processing sketch shared with the owner
|
|
25
|
+
* (2026-11-20): the noise, the warp, the three drifting fields, their
|
|
26
|
+
* rates and the breath are its; here the colours mix in Oklab rather than
|
|
27
|
+
* RGB, each field is rescaled to span 0–1 and pushed by `contrast` (the
|
|
28
|
+
* sketch's never reached its brightest colour), the breath is half as
|
|
29
|
+
* deep, the clock runs by the second, the units follow the canvas, and its
|
|
30
|
+
* lattice hash is Dave Hoskins' Hash without Sine (MIT, © 2014 David
|
|
31
|
+
* Hoskins), which stays stable at GPU precision and keeps the shader and
|
|
32
|
+
* the JavaScript in step.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import { createStage } from './stage.js';
|
|
36
|
+
import { GLSL_OKLAB, parseColor, toOklab, fromOklab, toHex } from './color.js';
|
|
37
|
+
import { GLSL_QUIET, quietUniform } from './glsl.js';
|
|
38
|
+
import { watchColors } from './mount.js';
|
|
39
|
+
import { pickColors } from './palettes.js';
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @typedef {object} LightLeakConfig
|
|
43
|
+
* @property {string[]} [colors] Four CSS hex colours, darkest first (fewer or more are resampled to four, in Oklab). Default the sketch's maroon ramp.
|
|
44
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`: its three colours and glint, darkest first.
|
|
45
|
+
* @property {number} [scale=1.5] How many blobs span the shorter side (higher is busier).
|
|
46
|
+
* @property {number} [warp=0.3] How far the domain warp bends the field (0 for none).
|
|
47
|
+
* @property {number} [octaves=2] Octaves of noise, 1–4: more adds finer detail.
|
|
48
|
+
* @property {number} [contrast=1.6] How far each field is pushed from its middle toward the ramp's ends (1 is the noise as it is, 0.5–3).
|
|
49
|
+
* @property {[number, number]} [offset=[0, 0]] Where the sample window sits, so two cards differ.
|
|
50
|
+
* @property {number} [drift=0.3] How fast the blobs drift and the warp moves (the sketch's own, made for a long mix behind it,
|
|
51
|
+
* was 0.04: a loop took four and a half minutes, so a card looked still; 0.3 loops in about thirty-five seconds).
|
|
52
|
+
* @property {number} [speed=1] Plays the clock faster or slower; 0 for one still frame.
|
|
53
|
+
* @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
|
|
54
|
+
* An area flattened to one tone behind copy: centre (0–1 from the top left), radius (share of the height), depth (0–1).
|
|
55
|
+
* @property {number} [quietTone=0] The tone the quiet area settles to: 0 the darkest colour, 1 the brightest (for dark copy).
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/** The settings' defaults, shared by the shader and the JavaScript twin. */
|
|
59
|
+
export const LEAK_DEFAULTS = {
|
|
60
|
+
scale: 1.5,
|
|
61
|
+
warp: 0.3,
|
|
62
|
+
octaves: 2,
|
|
63
|
+
contrast: 1.6,
|
|
64
|
+
quietTone: 0,
|
|
65
|
+
drift: 0.3,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** The sketch's maroon ramp around #A40F27, darkest first. */
|
|
69
|
+
export const LEAK_COLORS = ['#0a0306', '#780c1c', '#a40f27', '#d65464'];
|
|
70
|
+
|
|
71
|
+
/** The breath: its depth (a share of the brightness) and its rate (radians a second). */
|
|
72
|
+
const BREATH = 0.03;
|
|
73
|
+
const BREATH_RATE = 0.03;
|
|
74
|
+
/** The share of the pixel ratio it draws at: the field has no hard edges. */
|
|
75
|
+
const RESOLUTION = 0.5;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* GLSL (ES 1.00): `leakHash`, `leakNoise` and `leakFbm(p, octaves)`, the
|
|
79
|
+
* same as `leakHash`, `valueNoise` and `fbm` below.
|
|
80
|
+
*/
|
|
81
|
+
const GLSL_FIELD = `
|
|
82
|
+
float leakHash(vec2 p) {
|
|
83
|
+
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
|
|
84
|
+
p3 += dot(p3, p3.yzx + 33.33);
|
|
85
|
+
return fract((p3.x + p3.y) * p3.z);
|
|
86
|
+
}
|
|
87
|
+
float leakNoise(vec2 p) {
|
|
88
|
+
vec2 i = floor(p);
|
|
89
|
+
vec2 f = p - i;
|
|
90
|
+
f = f * f * (3.0 - 2.0 * f);
|
|
91
|
+
float a = leakHash(i);
|
|
92
|
+
float b = leakHash(i + vec2(1.0, 0.0));
|
|
93
|
+
float c = leakHash(i + vec2(0.0, 1.0));
|
|
94
|
+
float d = leakHash(i + vec2(1.0, 1.0));
|
|
95
|
+
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
|
|
96
|
+
}
|
|
97
|
+
float leakFbm(vec2 p, float octaves) {
|
|
98
|
+
float v = 0.0;
|
|
99
|
+
float amp = 0.5;
|
|
100
|
+
float freq = 1.0;
|
|
101
|
+
for (int i = 0; i < 4; i++) {
|
|
102
|
+
if (float(i) >= octaves) break;
|
|
103
|
+
v += amp * leakNoise(p * freq);
|
|
104
|
+
freq *= 2.0;
|
|
105
|
+
amp *= 0.5;
|
|
106
|
+
}
|
|
107
|
+
return v / (1.0 - amp * 2.0);
|
|
108
|
+
}
|
|
109
|
+
float leakShape(float n, float contrast) {
|
|
110
|
+
return clamp((n - 0.5) * contrast + 0.5, 0.0, 1.0);
|
|
111
|
+
}
|
|
112
|
+
`;
|
|
113
|
+
|
|
114
|
+
const FRAG = `
|
|
115
|
+
precision highp float;
|
|
116
|
+
uniform vec2 uRes;
|
|
117
|
+
uniform float uTime;
|
|
118
|
+
uniform float uDrift;
|
|
119
|
+
uniform float uScale;
|
|
120
|
+
uniform float uWarp;
|
|
121
|
+
uniform float uOctaves;
|
|
122
|
+
uniform float uContrast;
|
|
123
|
+
uniform vec2 uOffset;
|
|
124
|
+
uniform float uTone;
|
|
125
|
+
uniform vec3 uC0;
|
|
126
|
+
uniform vec3 uC1;
|
|
127
|
+
uniform vec3 uC2;
|
|
128
|
+
uniform vec3 uC3;
|
|
129
|
+
${GLSL_OKLAB}
|
|
130
|
+
${GLSL_QUIET}
|
|
131
|
+
${GLSL_FIELD}
|
|
132
|
+
|
|
133
|
+
void main() {
|
|
134
|
+
// From the top left, as the JavaScript twin measures it.
|
|
135
|
+
vec2 px = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y);
|
|
136
|
+
vec2 p = (px - 0.5 * uRes) / min(uRes.x, uRes.y) * uScale + uOffset;
|
|
137
|
+
float t = uTime * uDrift;
|
|
138
|
+
if (uWarp > 0.0) {
|
|
139
|
+
float wx = leakNoise(p * 0.5 + vec2(t * 0.2, 0.0));
|
|
140
|
+
float wy = leakNoise(p * 0.5 + vec2(0.0, t * 0.2));
|
|
141
|
+
p += vec2(wx, wy) * uWarp;
|
|
142
|
+
}
|
|
143
|
+
float n1 = leakShape(leakFbm(p + vec2(0.0, 0.1) + vec2(sin(t * 0.57), cos(t * 0.47)), uOctaves), uContrast);
|
|
144
|
+
float n2 = leakShape(leakFbm(p + vec2(0.1, 0.0) + vec2(cos(t * 0.42), sin(t * 0.72)), uOctaves), uContrast);
|
|
145
|
+
float n3 = leakShape(leakFbm(p + vec2(0.6, 0.2) + vec2(sin(t * 0.27), cos(t * 0.32)), uOctaves), uContrast);
|
|
146
|
+
vec2 q = vec2(px.x - 0.5 * uRes.x, 0.5 * uRes.y - px.y) / uRes.y;
|
|
147
|
+
float open = mix(1.0 - uQuiet.w, 1.0, quietOpen(q));
|
|
148
|
+
n1 = mix(uTone, n1, open);
|
|
149
|
+
n2 = mix(uTone, n2, open);
|
|
150
|
+
n3 = mix(uTone, n3, open);
|
|
151
|
+
vec3 col = fromOklab(mix(mix(uC0, uC1, n1), mix(uC2, uC3, n2), n3));
|
|
152
|
+
col *= 1.0 - ${BREATH.toFixed(3)} * (0.5 - 0.5 * sin(uTime * ${BREATH_RATE.toFixed(3)}));
|
|
153
|
+
col += (leakHash(gl_FragCoord.xy) - 0.5) / 255.0;
|
|
154
|
+
gl_FragColor = vec4(col, 1.0);
|
|
155
|
+
}
|
|
156
|
+
`;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The lattice hash (the shader's `leakHash`): Dave Hoskins' Hash without
|
|
160
|
+
* Sine, which brings its input under 1 before it multiplies, so the GPU's
|
|
161
|
+
* 32-bit floats and these 64-bit ones agree. (The sketch's own hash
|
|
162
|
+
* multiplied values in the hundreds, where the two drift apart.)
|
|
163
|
+
*
|
|
164
|
+
* @param {number} x A lattice point's x.
|
|
165
|
+
* @param {number} y Its y.
|
|
166
|
+
* @returns {number} 0–1.
|
|
167
|
+
*/
|
|
168
|
+
export function leakHash(x, y) {
|
|
169
|
+
const fract = (/** @type {number} */ n) => n - Math.floor(n);
|
|
170
|
+
let a = fract(x * 0.1031);
|
|
171
|
+
let b = fract(y * 0.1031);
|
|
172
|
+
let c = fract(x * 0.1031);
|
|
173
|
+
const d = a * (b + 33.33) + b * (c + 33.33) + c * (a + 33.33);
|
|
174
|
+
a += d;
|
|
175
|
+
b += d;
|
|
176
|
+
c += d;
|
|
177
|
+
return fract((a + b) * c);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Value noise with a cubic Hermite fade (the shader's `leakNoise`).
|
|
182
|
+
*
|
|
183
|
+
* @param {number} x Where, x.
|
|
184
|
+
* @param {number} y Where, y.
|
|
185
|
+
* @returns {number} 0–1.
|
|
186
|
+
*/
|
|
187
|
+
export function valueNoise(x, y) {
|
|
188
|
+
const ix = Math.floor(x);
|
|
189
|
+
const iy = Math.floor(y);
|
|
190
|
+
let fx = x - ix;
|
|
191
|
+
let fy = y - iy;
|
|
192
|
+
fx = fx * fx * (3 - 2 * fx);
|
|
193
|
+
fy = fy * fy * (3 - 2 * fy);
|
|
194
|
+
const a = leakHash(ix, iy);
|
|
195
|
+
const b = leakHash(ix + 1, iy);
|
|
196
|
+
const c = leakHash(ix, iy + 1);
|
|
197
|
+
const d = leakHash(ix + 1, iy + 1);
|
|
198
|
+
const ab = a + (b - a) * fx;
|
|
199
|
+
const cd = c + (d - c) * fx;
|
|
200
|
+
return ab + (cd - ab) * fy;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Fractal value noise (the shader's `leakFbm`): each octave twice the
|
|
205
|
+
* frequency and half the weight of the last, as the sketch sums them,
|
|
206
|
+
* then divided by the weights' total so it spans 0–1 (mean 0.5) whatever
|
|
207
|
+
* the octaves; the sketch's two octaves reached only 0.75, which kept its
|
|
208
|
+
* brightest colour from ever showing.
|
|
209
|
+
*
|
|
210
|
+
* @param {number} x Where, x.
|
|
211
|
+
* @param {number} y Where, y.
|
|
212
|
+
* @param {number} [octaves=2] How many, 1–4.
|
|
213
|
+
* @returns {number} 0–1.
|
|
214
|
+
*/
|
|
215
|
+
export function fbm(x, y, octaves = LEAK_DEFAULTS.octaves) {
|
|
216
|
+
const n = clampOctaves(octaves);
|
|
217
|
+
let v = 0;
|
|
218
|
+
let amp = 0.5;
|
|
219
|
+
let freq = 1;
|
|
220
|
+
for (let i = 0; i < n; i++) {
|
|
221
|
+
v += amp * valueNoise(x * freq, y * freq);
|
|
222
|
+
freq *= 2;
|
|
223
|
+
amp *= 0.5;
|
|
224
|
+
}
|
|
225
|
+
return v / (1 - amp * 2);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* A field pushed from its middle toward 0 and 1 (the shader's `leakShape`).
|
|
230
|
+
*
|
|
231
|
+
* @param {number} n The field, 0–1.
|
|
232
|
+
* @param {number} [contrast=1.6] How hard: 1 leaves it as it is.
|
|
233
|
+
* @returns {number} 0–1.
|
|
234
|
+
*/
|
|
235
|
+
export function leakShape(n, contrast = LEAK_DEFAULTS.contrast) {
|
|
236
|
+
return Math.min(1, Math.max(0, (n - 0.5) * contrast + 0.5));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* An octave count as the shader takes it: a whole number, 1–4.
|
|
241
|
+
*
|
|
242
|
+
* @param {number | undefined} octaves The setting.
|
|
243
|
+
* @returns {number} 1–4.
|
|
244
|
+
*/
|
|
245
|
+
export function clampOctaves(octaves) {
|
|
246
|
+
return Math.min(4, Math.max(1, Math.round(Number.isFinite(octaves) ? /** @type {number} */ (octaves) : LEAK_DEFAULTS.octaves)));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* The four colours as the shader takes them, darkest first: `colors`
|
|
251
|
+
* resampled to four stops (in Oklab), or a palette's three and its glint
|
|
252
|
+
* put in order of lightness, or the sketch's maroon ramp.
|
|
253
|
+
*
|
|
254
|
+
* @param {LightLeakConfig} config The settings.
|
|
255
|
+
* @returns {[string, string, string, string]} CSS hex colours.
|
|
256
|
+
*/
|
|
257
|
+
export function leakColors(config) {
|
|
258
|
+
const picked = pickColors(config, LEAK_COLORS, (palette) =>
|
|
259
|
+
[...palette.colors, palette.glint].sort((a, b) => toOklab(parseColor(a))[0] - toOklab(parseColor(b))[0]),
|
|
260
|
+
);
|
|
261
|
+
if (picked.length === 4) return /** @type {[string, string, string, string]} */ ([...picked]);
|
|
262
|
+
const labs = picked.map((c) => toOklab(parseColor(c)));
|
|
263
|
+
return /** @type {[string, string, string, string]} */ (
|
|
264
|
+
[0, 1, 2, 3].map((i) => {
|
|
265
|
+
const x = (i / 3) * (labs.length - 1);
|
|
266
|
+
const lo = Math.floor(x);
|
|
267
|
+
const hi = Math.min(lo + 1, labs.length - 1);
|
|
268
|
+
const f = x - lo;
|
|
269
|
+
return toHex(fromOklab(/** @type {[number, number, number]} */ (labs[lo].map((v, k) => v + (labs[hi][k] - v) * f))));
|
|
270
|
+
})
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The three fields at a point, as the shader samples them (quiet area
|
|
276
|
+
* and all): what mixes the ramp there.
|
|
277
|
+
*
|
|
278
|
+
* @param {LightLeakConfig} config The settings.
|
|
279
|
+
* @param {number} u Across, 0–1 from the left.
|
|
280
|
+
* @param {number} v Down, 0–1 from the top.
|
|
281
|
+
* @param {number} [aspect=1] The canvas's width over its height.
|
|
282
|
+
* @param {number} [time=0] Seconds, as the shader's clock (already at its speed).
|
|
283
|
+
* @returns {[number, number, number]} n1 (darkest pair), n2 (brightest pair), n3 (between them), each 0–1.
|
|
284
|
+
*/
|
|
285
|
+
export function leakFields(config, u, v, aspect = 1, time = 0) {
|
|
286
|
+
const { scale, warp, contrast, quietTone, drift } = { ...LEAK_DEFAULTS, ...config };
|
|
287
|
+
const octaves = clampOctaves(config.octaves);
|
|
288
|
+
const [ox, oy] = config.offset ?? [0, 0];
|
|
289
|
+
const unit = Math.min(aspect, 1);
|
|
290
|
+
let x = ((u - 0.5) * aspect) / unit * scale + ox;
|
|
291
|
+
let y = (v - 0.5) / unit * scale + oy;
|
|
292
|
+
const t = time * drift;
|
|
293
|
+
if (warp > 0) {
|
|
294
|
+
const wx = valueNoise(x * 0.5 + t * 0.2, y * 0.5);
|
|
295
|
+
const wy = valueNoise(x * 0.5, y * 0.5 + t * 0.2);
|
|
296
|
+
x += wx * warp;
|
|
297
|
+
y += wy * warp;
|
|
298
|
+
}
|
|
299
|
+
const n = /** @type {[number, number, number]} */ ([
|
|
300
|
+
fbm(x + Math.sin(t * 0.57), y + 0.1 + Math.cos(t * 0.47), octaves),
|
|
301
|
+
fbm(x + 0.1 + Math.cos(t * 0.42), y + Math.sin(t * 0.72), octaves),
|
|
302
|
+
fbm(x + 0.6 + Math.sin(t * 0.27), y + 0.2 + Math.cos(t * 0.32), octaves),
|
|
303
|
+
].map((f) => leakShape(f, contrast)));
|
|
304
|
+
const [qx, qy, radius, depth] = quietUniform(config.quiet, aspect);
|
|
305
|
+
if (radius <= 0) return n;
|
|
306
|
+
const d = Math.hypot((u - 0.5) * aspect - qx, 0.5 - v - qy);
|
|
307
|
+
const e = Math.min(1, Math.max(0, (d - radius * 0.06) / (radius - radius * 0.06)));
|
|
308
|
+
const open = 1 - depth + depth * e * e * (3 - 2 * e);
|
|
309
|
+
return /** @type {[number, number, number]} */ (n.map((f) => quietTone + (f - quietTone) * open));
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* The ramp mixed by the three fields, in Oklab (the shader's mix).
|
|
314
|
+
*
|
|
315
|
+
* @param {string[]} colors The four colours, darkest first (`leakColors`).
|
|
316
|
+
* @param {[number, number, number]} fields n1, n2 and n3 (`leakFields`).
|
|
317
|
+
* @returns {string} The colour, CSS hex.
|
|
318
|
+
*/
|
|
319
|
+
export function leakMix(colors, [n1, n2, n3]) {
|
|
320
|
+
const [c0, c1, c2, c3] = colors.map((c) => toOklab(parseColor(c)));
|
|
321
|
+
const mix = (/** @type {number[]} */ a, /** @type {number[]} */ b, /** @type {number} */ t) => a.map((v, i) => v + (b[i] - v) * t);
|
|
322
|
+
return toHex(fromOklab(/** @type {[number, number, number]} */ (mix(mix(c0, c1, n1), mix(c2, c3, n2), n3))));
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* The field's colour at a point: what the shader draws there (bar the
|
|
327
|
+
* breath and the dither).
|
|
328
|
+
*
|
|
329
|
+
* @param {LightLeakConfig} config The settings.
|
|
330
|
+
* @param {number} u Across, 0–1 from the left.
|
|
331
|
+
* @param {number} v Down, 0–1 from the top.
|
|
332
|
+
* @param {number} [aspect=1] The canvas's width over its height.
|
|
333
|
+
* @param {number} [time=0] Seconds, as the shader's clock.
|
|
334
|
+
* @returns {string} CSS hex.
|
|
335
|
+
*/
|
|
336
|
+
export function leakColor(config, u, v, aspect = 1, time = 0) {
|
|
337
|
+
return leakMix(leakColors(config), leakFields(config, u, v, aspect, time));
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* A CSS background close to the field's first frame: soft radial pools of
|
|
342
|
+
* its colour at a three-by-three grid of points over the average of them.
|
|
343
|
+
* For the element's `background`, shown before the shader starts and
|
|
344
|
+
* where there is no WebGL.
|
|
345
|
+
*
|
|
346
|
+
* @param {LightLeakConfig} config The settings (quiet area included).
|
|
347
|
+
* @param {number} [aspect=1] The element's width over its height.
|
|
348
|
+
* @returns {string} A CSS `background` value.
|
|
349
|
+
*/
|
|
350
|
+
export function lightLeakStill(config, aspect = 1) {
|
|
351
|
+
const colors = leakColors(config);
|
|
352
|
+
/** @type {{ x: number, y: number, hex: string }[]} */
|
|
353
|
+
const pools = [];
|
|
354
|
+
for (const y of [1 / 6, 3 / 6, 5 / 6]) for (const x of [1 / 6, 3 / 6, 5 / 6]) pools.push({ x, y, hex: leakMix(colors, leakFields(config, x, y, aspect, 0)) });
|
|
355
|
+
const labs = pools.map((p) => toOklab(parseColor(p.hex)));
|
|
356
|
+
const mean = toHex(fromOklab(/** @type {[number, number, number]} */ ([0, 1, 2].map((k) => labs.reduce((s, l) => s + l[k], 0) / labs.length))));
|
|
357
|
+
const pct = (/** @type {number} */ n) => `${Math.round(n * 1000) / 10}%`;
|
|
358
|
+
return [...pools.map((p) => `radial-gradient(42% 42% at ${pct(p.x)} ${pct(p.y)}, ${p.hex} 0%, ${p.hex}00 100%)`), mean].join(', ');
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Starts the light leak on an element: a canvas inside it (its first),
|
|
363
|
+
* filling it, opaque.
|
|
364
|
+
*
|
|
365
|
+
* @param {HTMLElement} el The element.
|
|
366
|
+
* @param {LightLeakConfig} config The settings.
|
|
367
|
+
* @returns {() => void} Stops it.
|
|
368
|
+
*/
|
|
369
|
+
export function lightLeak(el, config) {
|
|
370
|
+
const o = { ...LEAK_DEFAULTS, ...config };
|
|
371
|
+
const speed = Math.max(0, config.speed ?? 1);
|
|
372
|
+
const labs = (/** @type {string[]} */ colors) => colors.map((c) => toOklab(parseColor(c)));
|
|
373
|
+
let ramp = labs(leakColors(config));
|
|
374
|
+
const start = performance.now();
|
|
375
|
+
|
|
376
|
+
const { stage, stop } = createStage(el, {
|
|
377
|
+
fragment: FRAG,
|
|
378
|
+
alpha: false,
|
|
379
|
+
scale: RESOLUTION,
|
|
380
|
+
still: speed <= 0,
|
|
381
|
+
setup({ gl, uniform }) {
|
|
382
|
+
gl.uniform1f(uniform('uScale'), o.scale);
|
|
383
|
+
gl.uniform1f(uniform('uWarp'), Math.max(0, o.warp));
|
|
384
|
+
gl.uniform1f(uniform('uOctaves'), clampOctaves(config.octaves));
|
|
385
|
+
gl.uniform1f(uniform('uContrast'), Math.max(0, o.contrast));
|
|
386
|
+
gl.uniform2f(uniform('uOffset'), ...(config.offset ?? [0, 0]));
|
|
387
|
+
gl.uniform1f(uniform('uTone'), Math.min(1, Math.max(0, o.quietTone)));
|
|
388
|
+
gl.uniform1f(uniform('uDrift'), Math.max(0, o.drift));
|
|
389
|
+
},
|
|
390
|
+
resize({ gl, uniform, canvas }) {
|
|
391
|
+
gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, canvas.width / canvas.height));
|
|
392
|
+
},
|
|
393
|
+
uniforms({ gl, uniform }, now) {
|
|
394
|
+
gl.uniform1f(uniform('uTime'), ((now - start) / 1000) * speed);
|
|
395
|
+
ramp.forEach((lab, i) => gl.uniform3f(uniform(`uC${i}`), lab[0], lab[1], lab[2]));
|
|
396
|
+
},
|
|
397
|
+
});
|
|
398
|
+
if (!stage) return stop;
|
|
399
|
+
|
|
400
|
+
const unwatch = watchColors(el, (next) => {
|
|
401
|
+
ramp = labs(leakColors({ colors: next.colors }));
|
|
402
|
+
if (stage.still) stage.redraw();
|
|
403
|
+
});
|
|
404
|
+
return () => {
|
|
405
|
+
stop();
|
|
406
|
+
unwatch();
|
|
407
|
+
};
|
|
408
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { PaletteName } from './palettes.js';
|
|
2
|
+
|
|
3
|
+
export interface VoronoiMeshConfig {
|
|
4
|
+
/** Two CSS hex colours: the macro lines' and the inner lines'. Default a pale stone and a dusty rose (`#c1c2bc`, `#a67676`). */
|
|
5
|
+
colors?: string[];
|
|
6
|
+
/** A shared palette by name (`PALETTES`, any case), when there are no `colors`: its glint for the macro lines, its first colour for the inner. */
|
|
7
|
+
palette?: PaletteName | (string & {});
|
|
8
|
+
/** The ground, a CSS hex colour (opaque, so the canvas covers what is behind it); `transparent` draws the lines alone. Default `#350f0e`. */
|
|
9
|
+
background?: string;
|
|
10
|
+
/** Macro cells across the width. Default 9. */
|
|
11
|
+
cells?: number;
|
|
12
|
+
/** Inner seeds per macro cell, 0–8; 0 for the macro tier alone. Default 5. */
|
|
13
|
+
inner?: number;
|
|
14
|
+
/** The macro lines' width in CSS px; the inner lines are 0.7 of it. Default 1. */
|
|
15
|
+
width?: number;
|
|
16
|
+
/** The macro and inner lines' opacity, each 0–1. Default [0.4, 0.22]. */
|
|
17
|
+
opacity?: [number, number];
|
|
18
|
+
/** How far the inner seeds pulse, 0–1: 1 draws them in to the macro seed and out again, 0 holds them still. Default 1. */
|
|
19
|
+
pulse?: number;
|
|
20
|
+
/** How far out the inner seeds push, a share of a cell. Default 0.6. */
|
|
21
|
+
spread?: number;
|
|
22
|
+
/** How far each macro seed rests off its square's centre, 0–1. Default 0.75. */
|
|
23
|
+
jitter?: number;
|
|
24
|
+
/** How wide the macro seeds orbit, 0–1. Default 1. */
|
|
25
|
+
orbit?: number;
|
|
26
|
+
/** The slow driver, 0–1: how much it speeds the clocks, thickens the lines and lifts the macro tier. Default 0.35. */
|
|
27
|
+
breathe?: number;
|
|
28
|
+
/** Plays every clock faster or slower; 0 for one still frame. Default 1. */
|
|
29
|
+
speed?: number;
|
|
30
|
+
/** Where the grid starts, in cells, so two cards differ. Default [0, 0]. */
|
|
31
|
+
offset?: [number, number];
|
|
32
|
+
/** An area kept quiet: centre (0–1 from the top left), radius (share of the height), depth (0–1). */
|
|
33
|
+
quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The settings as the seeds take them, each in cells. */
|
|
37
|
+
export interface SeedOptions {
|
|
38
|
+
/** The furthest a seed rests off its square's centre, along each axis. */
|
|
39
|
+
jitter: number;
|
|
40
|
+
/** The widest it orbits. */
|
|
41
|
+
orbit: number;
|
|
42
|
+
/** Inner seeds per cell. */
|
|
43
|
+
inner: number;
|
|
44
|
+
/** How far out the inner seeds push. */
|
|
45
|
+
spread: number;
|
|
46
|
+
/** How far they pulse, 0–1. */
|
|
47
|
+
pulse: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The settings' defaults, shared by the shader and the JavaScript twins. */
|
|
51
|
+
export declare const MESH_DEFAULTS: {
|
|
52
|
+
background: string;
|
|
53
|
+
cells: number;
|
|
54
|
+
inner: number;
|
|
55
|
+
width: number;
|
|
56
|
+
opacity: [number, number];
|
|
57
|
+
pulse: number;
|
|
58
|
+
spread: number;
|
|
59
|
+
jitter: number;
|
|
60
|
+
orbit: number;
|
|
61
|
+
breathe: number;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** Dave Hoskins' Hash without Sine, three in and three out (the shader's `hash33`); each 0–1. */
|
|
65
|
+
export declare function hash33(x: number, y: number, z: number): [number, number, number];
|
|
66
|
+
|
|
67
|
+
/** The slow driver at a time (seconds, at the effect's speed): 0 to `breathe`. */
|
|
68
|
+
export declare function driveAt(t: number, breathe?: number): number;
|
|
69
|
+
|
|
70
|
+
/** The seeds' clock at a time: the integral of `1 + 2 · driveAt`, in closed form; 0 at 0. */
|
|
71
|
+
export declare function clockAt(t: number, breathe?: number): number;
|
|
72
|
+
|
|
73
|
+
/** The pulse's shape over one cycle (0–1): at rest through the dead zone, a squared sine out to 1 halfway and back. */
|
|
74
|
+
export declare function pulseWave(t: number): number;
|
|
75
|
+
|
|
76
|
+
/** The settings, with defaults, as the seeds take them (in cells). */
|
|
77
|
+
export declare function seedOptions(config?: VoronoiMeshConfig): SeedOptions;
|
|
78
|
+
|
|
79
|
+
/** The macro seed for the grid square at (cx, cy), at a clock (`clockAt`), in cells. */
|
|
80
|
+
export declare function macroSeed(cx: number, cy: number, clock: number, options?: SeedOptions): [number, number];
|
|
81
|
+
|
|
82
|
+
/** A macro cell's inner seeds at a clock, in cells. */
|
|
83
|
+
export declare function innerSeeds(cx: number, cy: number, clock: number, options?: SeedOptions): [number, number][];
|
|
84
|
+
|
|
85
|
+
/** The macro tier at a point (in cells), as the shader finds it: the nearest seed's square, the seed, and the distance to its cell's border. */
|
|
86
|
+
export declare function macroAt(x: number, y: number, clock: number, options?: SeedOptions): { cell: [number, number]; seed: [number, number]; edge: number };
|
|
87
|
+
|
|
88
|
+
/** One frame's macro and inner cells as polygons, in CSS px from the top left. */
|
|
89
|
+
export declare function meshPolygons(config: VoronoiMeshConfig, width: number, height: number, time?: number): { macro: [number, number][][]; inner: [number, number][][] };
|
|
90
|
+
|
|
91
|
+
/** The two line colours as the shader takes them: the macro lines' and the inner lines'. */
|
|
92
|
+
export declare function meshColors(config: VoronoiMeshConfig): [string, string];
|
|
93
|
+
|
|
94
|
+
/** One frame as an SVG's markup: the ground, both tiers' lines and the quiet area. */
|
|
95
|
+
export declare function meshSvg(config: VoronoiMeshConfig, width: number, height: number, time?: number): string;
|
|
96
|
+
|
|
97
|
+
/** One frame as a CSS `background` value (the SVG as a data URL over the ground), for the still shown before the shader starts and without WebGL. */
|
|
98
|
+
export declare function meshStill(config: VoronoiMeshConfig, width: number, height: number, time?: number): string;
|
|
99
|
+
|
|
100
|
+
/** Starts the mesh on an element (its first canvas is drawn into). Returns a function that stops it. */
|
|
101
|
+
export declare function voronoiMesh(el: HTMLElement, config: VoronoiMeshConfig): () => void;
|