@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,423 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/cascade-grid
|
|
3
|
+
*
|
|
4
|
+
* A regular grid of cells, staggered hexagons or square-set dots, that
|
|
5
|
+
* behaves like a scrolling spectrogram: each row is one band of a made-up
|
|
6
|
+
* spectrum, and each column shows that spectrum a little later than the
|
|
7
|
+
* column before it, so whatever rises in one column cascades across the
|
|
8
|
+
* grid as a wave. A cell's level (0–1) swells it from an idle state, a
|
|
9
|
+
* small dim speck, to a reactive one, near the whole cell in the second
|
|
10
|
+
* colour; the low rows run a little busier, as a real spectrum does.
|
|
11
|
+
*
|
|
12
|
+
* The sketches kept a ring buffer of levels, written once a tick and read
|
|
13
|
+
* further back column by column. Here there is no buffer and no state on
|
|
14
|
+
* the CPU: a cell's level is worked out where it is drawn, as smooth
|
|
15
|
+
* noise over (row, time − column × lag), stretched through a window and
|
|
16
|
+
* lifted by the tilt, so it is the same at any frame rate and costs one
|
|
17
|
+
* shader pass. Each cell is an anti-aliased distance field (a pointy-top
|
|
18
|
+
* hexagon or a circle) in its own cell of the lattice; its size, colour
|
|
19
|
+
* (blended in Oklab), outline and opacity follow its level. The same
|
|
20
|
+
* level and lattice are here in plain JavaScript (`cascadeLevel`,
|
|
21
|
+
* `latticeCell`, `cascadeCells`), so a page can draw a still that agrees.
|
|
22
|
+
*
|
|
23
|
+
* Clear between the cells, or the `background` colour. A `quiet` area
|
|
24
|
+
* calms the cells behind copy. Without WebGL nothing is drawn; with
|
|
25
|
+
* prefers-reduced-motion, or `speed: 0`, it is one still frame.
|
|
26
|
+
*
|
|
27
|
+
* After `hex_spectrogram_bg` and `circle_grid_bg`, Processing sketches
|
|
28
|
+
* shared with the owner (2026-11-20): the lattices, the idle-to-reactive
|
|
29
|
+
* blend, the level window and the tilt are theirs; here the levels are
|
|
30
|
+
* analytic and timed in seconds, and the noise's lattice hash is Dave
|
|
31
|
+
* Hoskins' Hash without Sine (MIT, © 2014 David Hoskins), which keeps the
|
|
32
|
+
* GPU and `cascadeLevel` in step.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import { createStage } from './stage.js';
|
|
36
|
+
import { GLSL_GRADIENT_NOISE, gnoise } from './hash-noise.js';
|
|
37
|
+
import { GLSL_OKLAB, mixOklab, parseColor, toHex, toOklab } 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} CascadeGridConfig
|
|
44
|
+
* @property {'hex' | 'dot'} [cell='hex'] The cells: staggered pointy-top hexagons, or dots on a square grid.
|
|
45
|
+
* @property {number} [pitch=16] A cell's width in CSS px.
|
|
46
|
+
* @property {number} [cols] Cells across instead, whatever the width (overrides `pitch`).
|
|
47
|
+
* @property {string[]} [colors] Two CSS hex colours: an idle cell's and a reactive one's. Default the sketch's grey and green.
|
|
48
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`: its last and first colours.
|
|
49
|
+
* @property {string} [background] A CSS hex colour to fill behind the cells; none leaves the canvas clear there.
|
|
50
|
+
* @property {number} [idle] An idle cell's size, a share of the cell (0–1). Default 0.12 for hexagons, 0.14 for dots.
|
|
51
|
+
* @property {number} [reactive] A reactive cell's size, a share of the cell (0–1; 1 is flush). Default 0.92 for hexagons, 0.9 for dots.
|
|
52
|
+
* @property {'solid' | 'outline' | 'core'} [fill] How a cell is drawn: filled, outlined, or outlined around a solid core that grows with its level (the hexagon sketch's look). Default `core` for hexagons, `solid` for dots.
|
|
53
|
+
* @property {[number, number]} [stroke=[0.06, 0.12]] The outline's width, idle and reactive, a share of the cell (0–0.5).
|
|
54
|
+
* @property {[number, number]} [opacity=[0.5, 1]] A cell's opacity, idle and reactive (0–1).
|
|
55
|
+
* @property {number} [lag=0.06] Seconds between one column and the next: the wave crosses `1 / lag` columns a second.
|
|
56
|
+
* @property {number} [evolve=0.6] How fast the spectrum changes, noise units a second.
|
|
57
|
+
* @property {number} [scale=0.12] Noise units a row: higher makes the bands narrower.
|
|
58
|
+
* @property {[number, number]} [range=[0.3, 0.75]] The noise window stretched to 0–1: a lower top makes more cells reactive.
|
|
59
|
+
* @property {number} [tilt=0.35] How much busier the low rows are (0–1): the bottom row gets it all, the middle none.
|
|
60
|
+
* @property {'right' | 'left' | 'up' | 'down'} [direction='right'] Which way the wave travels. Up and down make the columns the spectrum, busier at the left.
|
|
61
|
+
* @property {number | [number, number]} [offset=0] Where the noise starts (a number, or a band and a time shift), so two grids differ.
|
|
62
|
+
* @property {number} [speed=1] Plays the clock faster or slower; 0 for one still frame.
|
|
63
|
+
* @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
|
|
64
|
+
* An area kept calm: centre (0–1 from the top left), radius (share of the height), depth (0–1).
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
/** The settings' defaults, shared by the shader and the JavaScript twins (`idle`, `reactive` and `fill` depend on the cell; see CELL_DEFAULTS). */
|
|
68
|
+
export const CASCADE_DEFAULTS = {
|
|
69
|
+
cell: /** @type {'hex' | 'dot'} */ ('hex'),
|
|
70
|
+
pitch: 16,
|
|
71
|
+
stroke: /** @type {[number, number]} */ ([0.06, 0.12]),
|
|
72
|
+
opacity: /** @type {[number, number]} */ ([0.5, 1]),
|
|
73
|
+
lag: 0.06,
|
|
74
|
+
evolve: 0.6,
|
|
75
|
+
scale: 0.12,
|
|
76
|
+
range: /** @type {[number, number]} */ ([0.3, 0.75]),
|
|
77
|
+
tilt: 0.35,
|
|
78
|
+
direction: /** @type {'right' | 'left' | 'up' | 'down'} */ ('right'),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** Each cell's own defaults: an idle and a reactive size, and how it is drawn. */
|
|
82
|
+
export const CELL_DEFAULTS = {
|
|
83
|
+
hex: { idle: 0.12, reactive: 0.92, fill: /** @type {'solid' | 'outline' | 'core'} */ ('core') },
|
|
84
|
+
dot: { idle: 0.14, reactive: 0.9, fill: /** @type {'solid' | 'outline' | 'core'} */ ('solid') },
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** The default colours: the hexagon sketch's grey (idle) and green (reactive). */
|
|
88
|
+
const SKETCH = ['#c9c9c9', '#97cd3a'];
|
|
89
|
+
|
|
90
|
+
/** A row's height over the pitch: √3⁄2 for staggered hexagons (they tessellate), 1 for dots. */
|
|
91
|
+
const ROW = { hex: Math.sqrt(3) / 2, dot: 1 };
|
|
92
|
+
|
|
93
|
+
/** How `fill` is passed to the shader. */
|
|
94
|
+
const FILLS = { solid: 0, outline: 1, core: 2 };
|
|
95
|
+
|
|
96
|
+
/** Spectrum and cascade axes by direction, as `uDir`: the spectrum index is `dot(xy, (col, row))`, the cascade index `dot(zw, (col, row))`. */
|
|
97
|
+
const DIRECTIONS = {
|
|
98
|
+
right: [0, 1, 1, 0],
|
|
99
|
+
left: [0, 1, -1, 0],
|
|
100
|
+
down: [1, 0, 0, 1],
|
|
101
|
+
up: [1, 0, 0, -1],
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const FRAG = `
|
|
105
|
+
precision highp float;
|
|
106
|
+
uniform vec2 uRes;
|
|
107
|
+
uniform float uPitch;
|
|
108
|
+
uniform float uHex;
|
|
109
|
+
uniform float uTime;
|
|
110
|
+
uniform float uLag;
|
|
111
|
+
uniform float uEvolve;
|
|
112
|
+
uniform float uScale;
|
|
113
|
+
uniform vec2 uRange;
|
|
114
|
+
uniform float uTilt;
|
|
115
|
+
uniform vec2 uOffset;
|
|
116
|
+
uniform vec4 uDir;
|
|
117
|
+
uniform vec2 uSize;
|
|
118
|
+
uniform vec2 uStroke;
|
|
119
|
+
uniform vec2 uAlpha;
|
|
120
|
+
uniform float uFill;
|
|
121
|
+
uniform vec3 uLo;
|
|
122
|
+
uniform vec3 uHi;
|
|
123
|
+
uniform vec4 uBg;
|
|
124
|
+
${GLSL_OKLAB}
|
|
125
|
+
${GLSL_QUIET}
|
|
126
|
+
${GLSL_GRADIENT_NOISE}
|
|
127
|
+
|
|
128
|
+
// A band's level at a moment: two octaves of noise over (band, time), spread
|
|
129
|
+
// to about the sketches' Perlin noise (most of it 0.3-0.7), the window
|
|
130
|
+
// stretched to 0-1, and the tilt lifting the low end (h 0 to 0.5).
|
|
131
|
+
float cascadeLevel(float a, float b, float h, float t) {
|
|
132
|
+
float s = (t - b * uLag) * uEvolve + uOffset.y;
|
|
133
|
+
vec2 p = vec2(a * uScale + uOffset.x, s);
|
|
134
|
+
float n = 0.5 + 2.2 * ((2.0 * gnoise(p) + gnoise(p * 2.03 + 17.0)) / 3.0 - 0.5);
|
|
135
|
+
float v = clamp((n - uRange.x) / (uRange.y - uRange.x), 0.0, 1.0);
|
|
136
|
+
float lift = uTilt * max(0.0, 1.0 - 2.0 * h);
|
|
137
|
+
if (lift > 0.0) v += lift * gnoise(vec2(a * 0.3 + 50.0 + uOffset.x, s * 1.7));
|
|
138
|
+
return clamp(v, 0.0, 1.0);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// A pointy-top hexagon's distance, its apothem r (iq's, turned a quarter).
|
|
142
|
+
float sdHex(vec2 p, float r) {
|
|
143
|
+
const vec3 k = vec3(-0.866025404, 0.5, 0.577350269);
|
|
144
|
+
p = abs(p.yx);
|
|
145
|
+
p -= 2.0 * min(dot(k.xy, p), 0.0) * k.xy;
|
|
146
|
+
p -= vec2(clamp(p.x, -k.z * r, k.z * r), r);
|
|
147
|
+
return length(p) * sign(p.y);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
void main() {
|
|
151
|
+
// x from the left edge, y down from the middle row.
|
|
152
|
+
vec2 px = vec2(gl_FragCoord.x, 0.5 * uRes.y - gl_FragCoord.y);
|
|
153
|
+
float rowH = uPitch * mix(1.0, 0.866025404, uHex);
|
|
154
|
+
// The nearest cell centre, in the two rows either side of the point (one column in each).
|
|
155
|
+
float r0 = floor(px.y / rowH);
|
|
156
|
+
float best = 1e9;
|
|
157
|
+
vec2 cell = vec2(0.0);
|
|
158
|
+
vec2 centre = vec2(0.0);
|
|
159
|
+
for (int i = 0; i < 2; i++) {
|
|
160
|
+
float r = r0 + float(i);
|
|
161
|
+
float shift = uHex * 0.5 * mod(r, 2.0);
|
|
162
|
+
float c = floor(px.x / uPitch - shift);
|
|
163
|
+
vec2 at = vec2((c + 0.5 + shift) * uPitch, r * rowH);
|
|
164
|
+
vec2 d = px - at;
|
|
165
|
+
float dist = dot(d, d);
|
|
166
|
+
if (dist < best) { best = dist; cell = vec2(c, r); centre = at; }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
float a = dot(uDir.xy, cell);
|
|
170
|
+
float b = dot(uDir.zw, cell);
|
|
171
|
+
// How far up the spectrum: from the bottom (rows), or from the left (columns).
|
|
172
|
+
float h = uDir.x > 0.5 ? centre.x / uRes.x : 0.5 - centre.y / uRes.y;
|
|
173
|
+
float v = cascadeLevel(a, b, h, uTime);
|
|
174
|
+
vec2 q = vec2(centre.x - 0.5 * uRes.x, -centre.y) / uRes.y;
|
|
175
|
+
v *= mix(1.0 - uQuiet.w, 1.0, quietOpen(q));
|
|
176
|
+
|
|
177
|
+
float rad = 0.5 * uPitch * mix(uSize.x, uSize.y, v);
|
|
178
|
+
vec2 d = px - centre;
|
|
179
|
+
float sd = uHex > 0.5 ? sdHex(d, rad) : length(d) - rad;
|
|
180
|
+
float w = 0.5 * uPitch * mix(uStroke.x, uStroke.y, v);
|
|
181
|
+
float op = mix(uAlpha.x, uAlpha.y, v);
|
|
182
|
+
vec3 col = fromOklab(mix(uLo, uHi, v));
|
|
183
|
+
// Solid: the shape. Outline and core: a ring on its edge, at least a pixel wide.
|
|
184
|
+
float alpha = op * (uFill < 0.5 ? clamp(0.5 - sd, 0.0, 1.0) : clamp(0.5 - (abs(sd) - max(w, 0.5)), 0.0, 1.0));
|
|
185
|
+
vec3 ink = col * alpha;
|
|
186
|
+
if (uFill > 1.5) {
|
|
187
|
+
// The solid core, grown with the level, over the outline.
|
|
188
|
+
float cd = uHex > 0.5 ? sdHex(d, rad * v) : length(d) - rad * v;
|
|
189
|
+
float core = clamp(0.5 - cd, 0.0, 1.0) * v * uAlpha.y;
|
|
190
|
+
ink = fromOklab(uHi) * core + ink * (1.0 - core);
|
|
191
|
+
alpha = core + alpha * (1.0 - core);
|
|
192
|
+
}
|
|
193
|
+
gl_FragColor = vec4(ink + uBg.rgb * uBg.a * (1.0 - alpha), alpha + uBg.a * (1.0 - alpha));
|
|
194
|
+
}
|
|
195
|
+
`;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The settings with every default filled in, the cell's own included.
|
|
199
|
+
*
|
|
200
|
+
* @param {CascadeGridConfig} config The settings.
|
|
201
|
+
* @returns {Required<Omit<CascadeGridConfig, 'cols' | 'colors' | 'palette' | 'background' | 'quiet' | 'offset'>> & { offset: [number, number] }}
|
|
202
|
+
* The settings the shader takes.
|
|
203
|
+
*/
|
|
204
|
+
export function cascadeOptions(config = {}) {
|
|
205
|
+
const cell = config.cell === 'dot' ? 'dot' : 'hex';
|
|
206
|
+
const own = CELL_DEFAULTS[cell];
|
|
207
|
+
const offset = config.offset ?? 0;
|
|
208
|
+
return {
|
|
209
|
+
...CASCADE_DEFAULTS,
|
|
210
|
+
...own,
|
|
211
|
+
...Object.fromEntries(Object.entries(config).filter(([, v]) => v !== undefined)),
|
|
212
|
+
cell,
|
|
213
|
+
fill: config.fill && config.fill in FILLS ? config.fill : own.fill,
|
|
214
|
+
direction: config.direction && config.direction in DIRECTIONS ? config.direction : 'right',
|
|
215
|
+
offset: typeof offset === 'number' ? [offset * 1.37, offset * 7.31] : [offset[0] ?? 0, offset[1] ?? 0],
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* A band's level at a moment: what the shader draws a cell at, before the
|
|
221
|
+
* quiet area. Moving one column on (`b + 1`) is the same level `lag`
|
|
222
|
+
* seconds later: the cascade.
|
|
223
|
+
*
|
|
224
|
+
* @param {number} a The band: the row (or the column, flowing up or down).
|
|
225
|
+
* @param {number} b The cascade index: the column (or the row).
|
|
226
|
+
* @param {number} h How far up the spectrum the cell is, 0 at the low end and 1 at the high (only below 0.5 is tilted).
|
|
227
|
+
* @param {number} t Seconds, as the shader's clock (already at its speed).
|
|
228
|
+
* @param {CascadeGridConfig} [config] The settings.
|
|
229
|
+
* @returns {number} 0–1.
|
|
230
|
+
*/
|
|
231
|
+
export function cascadeLevel(a, b, h, t, config = {}) {
|
|
232
|
+
const { lag, evolve, scale, range, tilt, offset } = cascadeOptions(config);
|
|
233
|
+
const s = (t - b * lag) * evolve + offset[1];
|
|
234
|
+
const x = a * scale + offset[0];
|
|
235
|
+
const n = 0.5 + 2.2 * ((2 * gnoise(x, s) + gnoise(x * 2.03 + 17, s * 2.03 + 17)) / 3 - 0.5);
|
|
236
|
+
let v = Math.min(1, Math.max(0, (n - range[0]) / (range[1] - range[0])));
|
|
237
|
+
const lift = tilt * Math.max(0, 1 - 2 * h);
|
|
238
|
+
if (lift > 0) v += lift * gnoise(a * 0.3 + 50 + offset[0], s * 1.7);
|
|
239
|
+
return Math.min(1, Math.max(0, v));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The lattice's spacing on a target.
|
|
244
|
+
*
|
|
245
|
+
* @param {CascadeGridConfig} config The settings (`cell`, `pitch`, `cols`).
|
|
246
|
+
* @param {number} width The target's width, in the units wanted back.
|
|
247
|
+
* @param {number} [ratio=1] Pixels per CSS px, for `pitch`.
|
|
248
|
+
* @returns {{ pitch: number, rowHeight: number }} A cell's width and a row's height.
|
|
249
|
+
*/
|
|
250
|
+
export function latticeOf(config, width, ratio = 1) {
|
|
251
|
+
const cell = config.cell === 'dot' ? 'dot' : 'hex';
|
|
252
|
+
const pitch = config.cols ? width / config.cols : (config.pitch ?? CASCADE_DEFAULTS.pitch) * ratio;
|
|
253
|
+
return { pitch, rowHeight: pitch * ROW[cell] };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* The cell a point is in (the shader's lattice): columns from the left
|
|
258
|
+
* edge, rows from the middle row down (negative above it). Hexagon rows
|
|
259
|
+
* are √3⁄2 of the pitch apart, the odd ones shifted half a cell right, so
|
|
260
|
+
* they tessellate; dots sit on a square grid.
|
|
261
|
+
*
|
|
262
|
+
* @param {'hex' | 'dot'} cell The cells.
|
|
263
|
+
* @param {number} x Across, from the left edge.
|
|
264
|
+
* @param {number} y Down, from the middle row's centre line.
|
|
265
|
+
* @param {number} pitch A cell's width.
|
|
266
|
+
* @returns {{ col: number, row: number, x: number, y: number }} The cell, and its centre.
|
|
267
|
+
*/
|
|
268
|
+
export function latticeCell(cell, x, y, pitch) {
|
|
269
|
+
if (cell === 'dot') {
|
|
270
|
+
const col = Math.floor(x / pitch);
|
|
271
|
+
const row = Math.floor(y / pitch + 0.5);
|
|
272
|
+
return { col, row, x: (col + 0.5) * pitch, y: row * pitch };
|
|
273
|
+
}
|
|
274
|
+
const rowH = pitch * ROW.hex;
|
|
275
|
+
const r0 = Math.floor(y / rowH);
|
|
276
|
+
let best = { col: 0, row: 0, x: 0, y: 0 };
|
|
277
|
+
let dist = Infinity;
|
|
278
|
+
for (const row of [r0, r0 + 1]) {
|
|
279
|
+
const shift = 0.5 * (((row % 2) + 2) % 2);
|
|
280
|
+
const col = Math.floor(x / pitch - shift);
|
|
281
|
+
const cx = (col + 0.5 + shift) * pitch;
|
|
282
|
+
const cy = row * rowH;
|
|
283
|
+
const d = (x - cx) ** 2 + (y - cy) ** 2;
|
|
284
|
+
if (d < dist) {
|
|
285
|
+
dist = d;
|
|
286
|
+
best = { col, row, x: cx, y: cy };
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return best;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* One frame, cell by cell: each cell's centre (from the top left) and the
|
|
294
|
+
* level the shader draws it at, the quiet area included.
|
|
295
|
+
*
|
|
296
|
+
* @param {CascadeGridConfig} config The settings (the same as the shader's).
|
|
297
|
+
* @param {number} width The target's width.
|
|
298
|
+
* @param {number} height The target's height.
|
|
299
|
+
* @param {number} [time=0] Seconds, as the shader's clock (already at its speed).
|
|
300
|
+
* @returns {{ col: number, row: number, x: number, y: number, level: number }[]} Every cell whose centre is on the target.
|
|
301
|
+
*/
|
|
302
|
+
export function cascadeCells(config, width, height, time = 0) {
|
|
303
|
+
const o = cascadeOptions(config);
|
|
304
|
+
const { pitch, rowHeight } = latticeOf(config, width);
|
|
305
|
+
const [ax, ay, bx, by] = DIRECTIONS[o.direction];
|
|
306
|
+
const [qx, qy, radius, depth] = quietUniform(config.quiet, width / height);
|
|
307
|
+
const smooth = (/** @type {number} */ e0, /** @type {number} */ e1, /** @type {number} */ x) => {
|
|
308
|
+
const t = Math.min(1, Math.max(0, (x - e0) / (e1 - e0)));
|
|
309
|
+
return t * t * (3 - 2 * t);
|
|
310
|
+
};
|
|
311
|
+
const rows = Math.floor(height / 2 / rowHeight);
|
|
312
|
+
/** @type {{ col: number, row: number, x: number, y: number, level: number }[]} */
|
|
313
|
+
const cells = [];
|
|
314
|
+
for (let row = -rows; row <= rows; row++) {
|
|
315
|
+
const shift = o.cell === 'hex' ? 0.5 * (((row % 2) + 2) % 2) : 0;
|
|
316
|
+
for (let col = -1; (col + 0.5 + shift) * pitch <= width; col++) {
|
|
317
|
+
const x = (col + 0.5 + shift) * pitch;
|
|
318
|
+
if (x < 0) continue;
|
|
319
|
+
const y = row * rowHeight;
|
|
320
|
+
const h = ax > 0 ? x / width : 0.5 - y / height;
|
|
321
|
+
let level = cascadeLevel(ax * col + ay * row, bx * col + by * row, h, time, config);
|
|
322
|
+
if (radius > 0) {
|
|
323
|
+
const d = Math.hypot((x - width / 2) / height - qx, -y / height - qy);
|
|
324
|
+
level *= 1 - depth + depth * smooth(radius * 0.06, radius, d);
|
|
325
|
+
}
|
|
326
|
+
cells.push({ col, row, x, y: y + height / 2, level });
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return cells;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* The two colours as the shader takes them.
|
|
334
|
+
*
|
|
335
|
+
* @param {CascadeGridConfig} config The settings.
|
|
336
|
+
* @returns {[string, string]} An idle cell's colour and a reactive one's (a palette's last and first).
|
|
337
|
+
*/
|
|
338
|
+
export function cascadeColors(config) {
|
|
339
|
+
const colors = pickColors(config, SKETCH, (palette) => [palette.colors[2], palette.colors[0]]);
|
|
340
|
+
return [colors[0], colors[colors.length - 1]];
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* A CSS `background` showing the grid at rest, for before the shader
|
|
345
|
+
* starts and without WebGL: the idle cells as dots (an idle hexagon reads
|
|
346
|
+
* as one) over the `background` colour, in the shader's lattice. Without
|
|
347
|
+
* a width it can only place cells by `pitch`, so with `cols` it is the
|
|
348
|
+
* ground alone.
|
|
349
|
+
*
|
|
350
|
+
* @param {CascadeGridConfig} config The settings.
|
|
351
|
+
* @param {number} [width] The element's width in CSS px, when known (needed with `cols`).
|
|
352
|
+
* @returns {string} A CSS `background` value.
|
|
353
|
+
*/
|
|
354
|
+
export function cascadeStill(config, width) {
|
|
355
|
+
const o = cascadeOptions(config);
|
|
356
|
+
const ground = config.background ?? 'transparent';
|
|
357
|
+
if (config.cols && !width) return ground;
|
|
358
|
+
const { pitch, rowHeight } = latticeOf(config, width ?? 1);
|
|
359
|
+
const [idle] = cascadeColors(config);
|
|
360
|
+
const ink = config.background ? toHex(mixOklab(parseColor(config.background), parseColor(idle), o.opacity[0])) : idle;
|
|
361
|
+
const r = Math.max(0.6, (pitch * Math.max(o.idle, o.stroke[0])) / 2);
|
|
362
|
+
const px = (/** @type {number} */ n) => `${+n.toFixed(3)}px`;
|
|
363
|
+
const dot = `radial-gradient(circle, ${ink} ${px(r)}, transparent ${px(r + 0.6)})`;
|
|
364
|
+
// A percentage places a tile's middle on the element's (the middle row's line); the odd hexagon rows sit a row below, half a cell on.
|
|
365
|
+
if (o.cell === 'dot') return `${dot} 0 50% / ${px(pitch)} ${px(rowHeight)}, ${ground}`;
|
|
366
|
+
const tile = `${px(pitch)} ${px(rowHeight * 2)}`;
|
|
367
|
+
return `${dot} 0 50% / ${tile}, ${dot} ${px(pitch / 2)} calc(50% + ${px(rowHeight)}) / ${tile}, ${ground}`;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Starts the grid on an element: a canvas inside it (its first), filling it.
|
|
372
|
+
*
|
|
373
|
+
* @param {HTMLElement} el The element.
|
|
374
|
+
* @param {CascadeGridConfig} config The settings.
|
|
375
|
+
* @returns {() => void} Stops it.
|
|
376
|
+
*/
|
|
377
|
+
export function cascadeGrid(el, config) {
|
|
378
|
+
const o = cascadeOptions(config);
|
|
379
|
+
const speed = config.speed ?? 1;
|
|
380
|
+
const lab = (/** @type {string} */ c) => toOklab(parseColor(c));
|
|
381
|
+
let [lo, hi] = cascadeColors(config).map(lab);
|
|
382
|
+
const bg = config.background ? [...parseColor(config.background), 1] : [0, 0, 0, 0];
|
|
383
|
+
const start = performance.now();
|
|
384
|
+
|
|
385
|
+
const { stage, stop } = createStage(el, {
|
|
386
|
+
fragment: FRAG,
|
|
387
|
+
still: speed <= 0,
|
|
388
|
+
setup({ gl, uniform }) {
|
|
389
|
+
gl.uniform1f(uniform('uHex'), o.cell === 'hex' ? 1 : 0);
|
|
390
|
+
gl.uniform1f(uniform('uLag'), o.lag);
|
|
391
|
+
gl.uniform1f(uniform('uEvolve'), o.evolve);
|
|
392
|
+
gl.uniform1f(uniform('uScale'), o.scale);
|
|
393
|
+
gl.uniform2f(uniform('uRange'), o.range[0], Math.max(o.range[1], o.range[0] + 1e-3));
|
|
394
|
+
gl.uniform1f(uniform('uTilt'), o.tilt);
|
|
395
|
+
gl.uniform2f(uniform('uOffset'), o.offset[0], o.offset[1]);
|
|
396
|
+
gl.uniform4fv(uniform('uDir'), DIRECTIONS[o.direction]);
|
|
397
|
+
gl.uniform2f(uniform('uSize'), o.idle, o.reactive);
|
|
398
|
+
gl.uniform2f(uniform('uStroke'), o.stroke[0], o.stroke[1]);
|
|
399
|
+
gl.uniform2f(uniform('uAlpha'), o.opacity[0], o.opacity[1]);
|
|
400
|
+
gl.uniform1f(uniform('uFill'), FILLS[o.fill]);
|
|
401
|
+
gl.uniform4f(uniform('uBg'), bg[0], bg[1], bg[2], bg[3]);
|
|
402
|
+
},
|
|
403
|
+
resize({ gl, uniform, canvas, ratio }) {
|
|
404
|
+
gl.uniform1f(uniform('uPitch'), latticeOf(config, canvas.width, ratio).pitch);
|
|
405
|
+
gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, canvas.width / canvas.height));
|
|
406
|
+
},
|
|
407
|
+
uniforms({ gl, uniform }, now) {
|
|
408
|
+
gl.uniform1f(uniform('uTime'), ((now - start) / 1000) * Math.max(0, speed));
|
|
409
|
+
gl.uniform3f(uniform('uLo'), lo[0], lo[1], lo[2]);
|
|
410
|
+
gl.uniform3f(uniform('uHi'), hi[0], hi[1], hi[2]);
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
if (!stage) return stop;
|
|
414
|
+
|
|
415
|
+
const unwatch = watchColors(el, (next) => {
|
|
416
|
+
[lo, hi] = cascadeColors({ colors: next.colors }).map(lab);
|
|
417
|
+
if (stage.still) stage.redraw();
|
|
418
|
+
});
|
|
419
|
+
return () => {
|
|
420
|
+
stop();
|
|
421
|
+
unwatch();
|
|
422
|
+
};
|
|
423
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { PaletteName } from './palettes.js';
|
|
2
|
+
|
|
3
|
+
export interface DitherFieldConfig {
|
|
4
|
+
/** Two CSS hex colours: the dim edge's and the bright core's. Default a grey and amber. */
|
|
5
|
+
colors?: string[];
|
|
6
|
+
/** A shared palette by name (`PALETTES`, any case), when there are no `colors`: its first and last colours. */
|
|
7
|
+
palette?: PaletteName | (string & {});
|
|
8
|
+
/** A CSS hex colour to fill behind the dots; none leaves the canvas clear there. */
|
|
9
|
+
background?: string;
|
|
10
|
+
/** A dot's size in CSS px (rounded to whole screen pixels). Default 3. */
|
|
11
|
+
dot?: number;
|
|
12
|
+
/** How many noise features span the width (higher is busier). Default 1.1. */
|
|
13
|
+
scale?: number;
|
|
14
|
+
/** How hard the edges are: higher thins the dithered band. Default 6. */
|
|
15
|
+
contrast?: number;
|
|
16
|
+
/** Shifts the whole field; lower leaves more of it empty. Default -0.15. */
|
|
17
|
+
brightness?: number;
|
|
18
|
+
/** Colour bands across the field; 0 blends smoothly. Default 24. */
|
|
19
|
+
steps?: number;
|
|
20
|
+
/** How fast the field pans, noise features a second, x and y. Default [0.02, 0.007]. */
|
|
21
|
+
drift?: [number, number];
|
|
22
|
+
/** How fast the shapes morph. Default 0.35. */
|
|
23
|
+
evolve?: number;
|
|
24
|
+
/** How strongly the rings lift the field; 0 for none. Default 1. */
|
|
25
|
+
ripple?: number;
|
|
26
|
+
/** Rings across the width, roughly (higher is tighter). Default 3. */
|
|
27
|
+
rings?: number;
|
|
28
|
+
/** How fast the rings travel, widths a second. Default 0.08. */
|
|
29
|
+
rippleSpeed?: number;
|
|
30
|
+
/** Where the rings start, 0–1 from the top left. Default [0.04, 0.3]. */
|
|
31
|
+
origin?: [number, number];
|
|
32
|
+
/** Where the field and the rings start, so two cards differ. Default [0, 0]. */
|
|
33
|
+
offset?: [number, number];
|
|
34
|
+
/** Plays every clock faster or slower; 0 for one still frame. Default 1. */
|
|
35
|
+
speed?: number;
|
|
36
|
+
/** An area kept empty: centre (0–1 from the top left), radius (share of the height), depth (0–1). */
|
|
37
|
+
quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The settings' defaults, shared by the shader and `ditherDots`. */
|
|
41
|
+
export declare const DITHER_DEFAULTS: {
|
|
42
|
+
dot: number;
|
|
43
|
+
scale: number;
|
|
44
|
+
contrast: number;
|
|
45
|
+
brightness: number;
|
|
46
|
+
steps: number;
|
|
47
|
+
drift: [number, number];
|
|
48
|
+
evolve: number;
|
|
49
|
+
ripple: number;
|
|
50
|
+
rings: number;
|
|
51
|
+
rippleSpeed: number;
|
|
52
|
+
origin: [number, number];
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** The 8×8 Bayer threshold at a dot, 0–63/64 (it repeats every 8 dots). */
|
|
56
|
+
export declare function bayer(x: number, y: number): number;
|
|
57
|
+
|
|
58
|
+
/** The smooth 1-D noise the rings are cut from, 0–1; one lattice step is one ring's spacing. */
|
|
59
|
+
export declare function ripplePulse(s: number): number;
|
|
60
|
+
|
|
61
|
+
/** How strong the ring is `d` widths from the origin at `time` seconds, 0–1 before `ripple` scales it. */
|
|
62
|
+
export declare function rippleAt(d: number, time: number, config?: DitherFieldConfig): number;
|
|
63
|
+
|
|
64
|
+
/** One frame dot for dot, as the shader draws it on a target exactly `cols` by `rows` dots: −1 empty, or a tone 0 (edge) to 1 (core). */
|
|
65
|
+
export declare function ditherDots(config: DitherFieldConfig, cols: number, rows: number, time?: number): number[][];
|
|
66
|
+
|
|
67
|
+
/** The two colours as the shader takes them: the dim edge's and the bright core's. */
|
|
68
|
+
export declare function ditherColors(config: DitherFieldConfig): [string, string];
|
|
69
|
+
|
|
70
|
+
/** A CSS `background` that stands in for the field before the shader starts and without WebGL. */
|
|
71
|
+
export declare function ditherStill(config: DitherFieldConfig): string;
|
|
72
|
+
|
|
73
|
+
/** Starts the field on an element (its first canvas is drawn into, one pixel a dot). Returns a function that stops it. */
|
|
74
|
+
export declare function ditherField(el: HTMLElement, config: DitherFieldConfig): () => void;
|