@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.
- package/AGENTS.md +1 -1
- package/COMPONENTS.md +214 -10
- 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/DitherGradient.astro +41 -0
- package/src/components/FlowField.astro +38 -0
- package/src/components/FlutedGlow.astro +39 -0
- package/src/components/KineticGrid.astro +38 -0
- package/src/components/LightLeak.astro +39 -0
- package/src/components/PixelFountain.astro +20 -7
- package/src/components/PixelGlitchText.astro +4 -3
- package/src/components/PixelHorizon.astro +40 -0
- package/src/components/TruchetField.astro +40 -0
- package/src/components/VoronoiMesh.astro +39 -0
- package/src/index.d.ts +23 -1
- package/src/index.js +13 -1
- package/src/runtime/ascii-noise.js +6 -107
- package/src/runtime/bayer.d.ts +5 -0
- package/src/runtime/bayer.js +48 -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 +416 -0
- package/src/runtime/dither-gradient.d.ts +91 -0
- package/src/runtime/dither-gradient.js +410 -0
- package/src/runtime/flow-field.d.ts +117 -0
- package/src/runtime/flow-field.js +516 -0
- package/src/runtime/fluted-glow.d.ts +79 -0
- package/src/runtime/fluted-glow.js +312 -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/pixel-fountain.d.ts +29 -13
- package/src/runtime/pixel-fountain.js +183 -42
- package/src/runtime/pixel-glitch.d.ts +2 -0
- package/src/runtime/pixel-glitch.js +8 -2
- package/src/runtime/pixel-horizon.d.ts +60 -0
- package/src/runtime/pixel-horizon.js +274 -0
- package/src/runtime/truchet-field.d.ts +60 -0
- package/src/runtime/truchet-field.js +272 -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
|
+
}
|
|
@@ -1,32 +1,36 @@
|
|
|
1
1
|
export interface PixelFountainConfig {
|
|
2
|
-
/** The pixels' colour when they fire; the darker steps they fade through are made from it. Default `#c8ff00
|
|
2
|
+
/** The pixels' colour when they fire; the darker steps they fade through are made from it. With `source="pointer"`, the one ink every block is drawn in. Default `#c8ff00` (from the pointer, the text's colour, `currentColor`). */
|
|
3
3
|
color?: string;
|
|
4
|
-
/** The steps a pixel fades through, first to last (overrides `color
|
|
4
|
+
/** The steps a pixel fades through, first to last (overrides `color`; from the pointer, only the first is used). Default made from `color`. */
|
|
5
5
|
colors?: string[];
|
|
6
6
|
/** A shared palette by name, its colours as the steps, when there are no `colors`. Default none. */
|
|
7
7
|
palette?: string;
|
|
8
8
|
/** A pixel's size, CSS px (a power-up's biggest blocks are two). Default 8. */
|
|
9
9
|
cell?: number;
|
|
10
|
-
/** How far above the edge the pixels can fly, CSS px. Default 240. */
|
|
10
|
+
/** How far above the edge the pixels can fly, CSS px (from the pointer: how far above the host the canvas reaches). Default 240 (160 from the pointer). */
|
|
11
11
|
rise?: number;
|
|
12
|
-
/** Pixels a second trickling up on their own, per 1,000 CSS px of edge; 0 for none. Default 14. */
|
|
12
|
+
/** Pixels a second trickling up on their own, per 1,000 CSS px of edge; 0 for none (always none from the pointer). Default 14. */
|
|
13
13
|
rate?: number;
|
|
14
|
-
/** Pixels a click or tap fires. Default 40. */
|
|
14
|
+
/** Pixels a click or tap fires. Default 40 (24 from the pointer). */
|
|
15
15
|
burst?: number;
|
|
16
|
+
/** From the pointer: blocks fired for every 100 CSS px it moves, at most 12 a move. Default 10. */
|
|
17
|
+
trail?: number;
|
|
18
|
+
/** Whether a mouse click fires a burst (a tap always does). Default true (false from the pointer). */
|
|
19
|
+
press?: boolean;
|
|
16
20
|
/** How hard they are pulled back, 0–2. Default 1. */
|
|
17
21
|
gravity?: number;
|
|
18
22
|
/** How widely they fan out, 0–1. Default 0.5. */
|
|
19
23
|
spread?: number;
|
|
20
24
|
/** How much a wind pushes them sideways, 0–1. Default 0.3. */
|
|
21
25
|
drift?: number;
|
|
22
|
-
/** The longest a pixel lives, seconds. Default 1.4. */
|
|
26
|
+
/** The longest a pixel lives, seconds. Default 1.4 (0.9 from the pointer). */
|
|
23
27
|
life?: number;
|
|
24
|
-
/** The chance a pixel splits into smaller ones rather than shrinking, 0–1. Default 0.4. */
|
|
28
|
+
/** The chance a pixel splits into smaller ones rather than shrinking, 0–1. Default 0.4 (0.2 from the pointer). */
|
|
25
29
|
split?: number;
|
|
26
|
-
/** The chance a pixel flickers out on and off before it goes, 0–1. Default 0.35. */
|
|
30
|
+
/** The chance a pixel flickers out on and off before it goes, 0–1. Default 0.35 (0.5 from the pointer). */
|
|
27
31
|
flicker?: number;
|
|
28
|
-
/** Fire from the host's top edge
|
|
29
|
-
source?: 'edge' | 'text';
|
|
32
|
+
/** Fire from the host's top edge; from the filled pixels of its text; or only from the pointer, where it moves over the host (a block-dust trail in one ink, nothing on its own). Default `edge`. */
|
|
33
|
+
source?: 'edge' | 'text' | 'pointer';
|
|
30
34
|
/** A selector for the element the pixels fire from and the pointer is followed over: the nearest ancestor that matches, else the first on the page. Default the element's positioned parent. */
|
|
31
35
|
host?: string;
|
|
32
36
|
/** Seeds the randomness. Default 1. */
|
|
@@ -61,8 +65,8 @@ export interface Pixel {
|
|
|
61
65
|
wobble: number;
|
|
62
66
|
}
|
|
63
67
|
|
|
64
|
-
/** What fired a pixel: the ambient trickle, the pointer moving, or a press (a power-up). */
|
|
65
|
-
export type EmitKind = 'ambient' | 'move' | 'burst';
|
|
68
|
+
/** What fired a pixel: the ambient trickle, the pointer moving over the edge or letters, or a press (a power-up); from the pointer, its trail and a tap. */
|
|
69
|
+
export type EmitKind = 'ambient' | 'move' | 'burst' | 'trail' | 'tap';
|
|
66
70
|
|
|
67
71
|
/** A fountain's settings: the canvas's size and the effect's physics. */
|
|
68
72
|
export interface FountainOptions {
|
|
@@ -80,6 +84,8 @@ export interface FountainOptions {
|
|
|
80
84
|
life?: number;
|
|
81
85
|
split?: number;
|
|
82
86
|
flicker?: number;
|
|
87
|
+
/** Blocks a pointer's move fires for every 100 CSS px it travels. Default 10. */
|
|
88
|
+
trail?: number;
|
|
83
89
|
seed?: number;
|
|
84
90
|
}
|
|
85
91
|
|
|
@@ -100,6 +106,10 @@ export interface Fountain {
|
|
|
100
106
|
emitEdge(x: number, n: number, kind?: EmitKind): number;
|
|
101
107
|
/** Fires `n` pixels from where the ambient ones fire. */
|
|
102
108
|
emitAmbient(n: number, kind?: EmitKind): number;
|
|
109
|
+
/** The pointer moved to a point: fires `trail` blocks per 100 px travelled (at most `TRAIL_CAP`), spaced along the way it came and ending there; none on the first point after a lift or for no move. Returns how many fired. */
|
|
110
|
+
moveTo(x: number, y: number): number;
|
|
111
|
+
/** Forgets where the pointer was (it left, or lifted). */
|
|
112
|
+
lift(): void;
|
|
103
113
|
/** One step (default 1/60 s): the trickle, then every pixel ages, breaks down, moves and flickers. */
|
|
104
114
|
step(dt?: number): void;
|
|
105
115
|
/** Empties the pool. */
|
|
@@ -112,6 +122,12 @@ export const MAX_PIXELS: number;
|
|
|
112
122
|
/** The simulation's fixed step, seconds (1/60). */
|
|
113
123
|
export const STEP: number;
|
|
114
124
|
|
|
125
|
+
/** The most blocks one pointer move fires (12). */
|
|
126
|
+
export const TRAIL_CAP: number;
|
|
127
|
+
|
|
128
|
+
/** The settings that change with `source: 'pointer'`. */
|
|
129
|
+
export const POINTER_DEFAULTS: { rate: number; life: number; split: number; flicker: number; rise: number; burst: number; trail: number };
|
|
130
|
+
|
|
115
131
|
/** The steps a pixel fades through, made from one colour in Oklab: darker and less saturated each step, turning a little towards olive. */
|
|
116
132
|
export function fadeSteps(color: string, count?: number): string[];
|
|
117
133
|
|
|
@@ -130,5 +146,5 @@ export function inkedCells(rgba: Uint8ClampedArray, width: number, height: numbe
|
|
|
130
146
|
/** Makes a seeded fountain of pixels. */
|
|
131
147
|
export function createFountain(options: FountainOptions): Fountain;
|
|
132
148
|
|
|
133
|
-
/** Starts the pixel fountain on an element placed over its host's top edge. Returns a function that stops it. */
|
|
149
|
+
/** Starts the pixel fountain on an element placed over its host's top edge (over the host and above it, from the text or the pointer). Returns a function that stops it. */
|
|
134
150
|
export function pixelFountain(el: HTMLElement, config: PixelFountainConfig): () => void;
|