@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,88 @@
|
|
|
1
|
+
import type { PaletteName } from './palettes.js';
|
|
2
|
+
|
|
3
|
+
export interface KineticGridConfig {
|
|
4
|
+
/** Two CSS hex colours: the wires' and the pulses'. Default the sketch's blue and white. */
|
|
5
|
+
colors?: string[];
|
|
6
|
+
/** A shared palette by name (`PALETTES`, any case), when there are no `colors`: its first colour for the wires, its glint for the pulses. */
|
|
7
|
+
palette?: PaletteName | (string & {});
|
|
8
|
+
/** The ground, a CSS hex colour (opaque, so the canvas covers what is under it); `transparent` leaves it clear. Default '#0a0a0a'. */
|
|
9
|
+
background?: string;
|
|
10
|
+
/** Grid cells across, whatever the width (cells are square). Default 48. */
|
|
11
|
+
cols?: number;
|
|
12
|
+
/** A wire's width, CSS px. Default 1. */
|
|
13
|
+
width?: number;
|
|
14
|
+
/** How bright a wire at rest is, 0–1; stretched wires brighten to full. Default 0.35. */
|
|
15
|
+
rest?: number;
|
|
16
|
+
/** Drops alive at once, 0–6 (each slot lands a new one about every five seconds). Default 4. */
|
|
17
|
+
drops?: number;
|
|
18
|
+
/** How far the ripples push the wires, 0–1.6 (past that they fold). Default 1. */
|
|
19
|
+
strength?: number;
|
|
20
|
+
/** The ripples' scale: wavelength, reach and speed together, 0.5–2. Default 1. */
|
|
21
|
+
size?: number;
|
|
22
|
+
/** Ring pulses alive at once, 0–4 (each slot about every two and a half seconds). Default 3. */
|
|
23
|
+
pulses?: number;
|
|
24
|
+
/** Plays the clock faster or slower; 0 for one still frame. Default 1. */
|
|
25
|
+
speed?: number;
|
|
26
|
+
/** Seeds where and when drops land, so two cards differ. Default 0. */
|
|
27
|
+
offset?: number;
|
|
28
|
+
/** An area kept calm and dim: centre (0–1 from the top left), radius (share of the height), depth (0–1). */
|
|
29
|
+
quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The settings' defaults. */
|
|
33
|
+
export declare const KINETIC_DEFAULTS: {
|
|
34
|
+
background: string;
|
|
35
|
+
cols: number;
|
|
36
|
+
width: number;
|
|
37
|
+
rest: number;
|
|
38
|
+
drops: number;
|
|
39
|
+
strength: number;
|
|
40
|
+
size: number;
|
|
41
|
+
pulses: number;
|
|
42
|
+
speed: number;
|
|
43
|
+
offset: number;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Most drop slots (the shader's array). */
|
|
47
|
+
export declare const MAX_DROPS: number;
|
|
48
|
+
/** Most pulse slots (the shader's array). */
|
|
49
|
+
export declare const MAX_PULSES: number;
|
|
50
|
+
|
|
51
|
+
/** A drop's timing, seconds: a slot lands one every `period`, up to `jitter` late; it lasts `life`, its height falling by e every `decay`, rising over `rise`. */
|
|
52
|
+
export declare const DROP_TIMING: { period: number; jitter: number; life: number; decay: number; rise: number };
|
|
53
|
+
|
|
54
|
+
/** A pulse's timing, seconds: a slot fires every `period`, up to `jitter` late, and a ring lasts `life`. */
|
|
55
|
+
export declare const PULSE_TIMING: { period: number; jitter: number; life: number };
|
|
56
|
+
|
|
57
|
+
/** The ripple's shape at `size` 1, in widths (speeds a second). */
|
|
58
|
+
export declare const WAVE: { speed: number; wavelength: number; sigma: number; spread: number; core: number; amp: number; reach: number };
|
|
59
|
+
|
|
60
|
+
/** Dave Hoskins' Hash without Sine, three in, three out, each 0–1. */
|
|
61
|
+
export declare function hash33(x: number, y: number, z: number): [number, number, number];
|
|
62
|
+
|
|
63
|
+
/** Where (0–1 of the width and height, from the top left) and when (seconds) a slot's drop for a cycle lands. */
|
|
64
|
+
export declare function dropAt(slot: number, cycle: number, offset?: number): { x: number; y: number; start: number };
|
|
65
|
+
|
|
66
|
+
/** A drop's height over its life, 0–1: a quick rise, an exponential fall, eased to nothing by the end. */
|
|
67
|
+
export declare function dropEnvelope(age: number): number;
|
|
68
|
+
|
|
69
|
+
/** The drops alive at a moment (the clock already at its speed): place, age in seconds, and envelope. */
|
|
70
|
+
export declare function dropsAt(time: number, config?: { drops?: number; offset?: number }): { x: number; y: number; age: number; amp: number }[];
|
|
71
|
+
|
|
72
|
+
/** The pulse rings alive at a moment: centre (0–1 of the width and height), radius in widths, brightness 0–1. */
|
|
73
|
+
export declare function pulsesAt(time: number, config?: { pulses?: number; offset?: number; size?: number }): { x: number; y: number; radius: number; glow: number }[];
|
|
74
|
+
|
|
75
|
+
/** One drop's ripple at a distance (widths) and age (seconds): the radial displacement and its slope. */
|
|
76
|
+
export declare function ripple(r: number, age: number, size?: number): [number, number];
|
|
77
|
+
|
|
78
|
+
/** How far the grid is moved at a point (widths from the top left), summed over the live drops: x and y, widths. */
|
|
79
|
+
export declare function displacement(x: number, y: number, time: number, config?: KineticGridConfig, aspect?: number): [number, number];
|
|
80
|
+
|
|
81
|
+
/** The two colours as the shader takes them: the wires' and the pulses'. */
|
|
82
|
+
export declare function gridColors(config: KineticGridConfig): [string, string];
|
|
83
|
+
|
|
84
|
+
/** A CSS `background` of the grid at rest (for before the shader starts and without WebGL); `aspect` is height over width, default 1.25. */
|
|
85
|
+
export declare function gridStill(config?: KineticGridConfig, aspect?: number): string;
|
|
86
|
+
|
|
87
|
+
/** Starts the grid on an element (its first canvas is drawn into). Returns a function that stops it. */
|
|
88
|
+
export declare function kineticGrid(el: HTMLElement, config: KineticGridConfig): () => void;
|
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/kinetic-grid
|
|
3
|
+
*
|
|
4
|
+
* A fine wireframe grid that ripples where drops land. A few drops are
|
|
5
|
+
* alive at once; each sends a damped ring out across the wires, which
|
|
6
|
+
* bunch and spread as it passes and settle behind it, brighter where a
|
|
7
|
+
* wire is stretched. Now and then a thin bright ring sweeps out from a
|
|
8
|
+
* point and lights the wire segments it crosses, so pulses read as dashes
|
|
9
|
+
* running along the lines. On an opaque dark ground by default.
|
|
10
|
+
*
|
|
11
|
+
* Nothing is simulated. Each drop's place and landing time are a seeded
|
|
12
|
+
* function of its slot and its cycle (`dropAt`), so the field is the same
|
|
13
|
+
* at the same second whatever the frame rate, and each drop is a radial
|
|
14
|
+
* wave packet whose displacement and stretch are written out in full
|
|
15
|
+
* (`ripple`): the shader moves each pixel back by the summed waves and
|
|
16
|
+
* draws anti-aliased lines, in CSS px, at the grid's rest place. The drops
|
|
17
|
+
* and pulses alive at a moment (`dropsAt`, `pulsesAt`) are worked out once
|
|
18
|
+
* a frame here and handed to the shader, so the pixel work is a handful
|
|
19
|
+
* of exponentials and nothing else; ten cards can run at once.
|
|
20
|
+
*
|
|
21
|
+
* Its unit is the width, so a card and a hero with the same `cols` look
|
|
22
|
+
* alike. A `quiet` area calms the waves and dims the wires behind copy.
|
|
23
|
+
* Without WebGL nothing is drawn (`gridStill` makes a CSS still of the
|
|
24
|
+
* grid at rest); with prefers-reduced-motion, or `speed: 0`, one still
|
|
25
|
+
* frame with ripples on it.
|
|
26
|
+
*
|
|
27
|
+
* After `grid_bg`, a Processing sketch shared with the owner (2026-11-20):
|
|
28
|
+
* its look (a mass-spring wireframe, raindrop impulses, ring pulses
|
|
29
|
+
* lighting whole wire segments, an additive glow, the blue on near-black)
|
|
30
|
+
* is the sketch's; here the springs are replaced by analytic waves, by
|
|
31
|
+
* the second rather than by the frame. Drops are placed with Dave
|
|
32
|
+
* Hoskins' Hash without Sine (MIT, © 2014 David Hoskins).
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import { createStage } from './stage.js';
|
|
36
|
+
import { hash33 } 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
|
+
/** Hash without Sine (./hash-noise.js), the drops' and pulses' hash. */
|
|
43
|
+
export { hash33 };
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @typedef {object} KineticGridConfig
|
|
47
|
+
* @property {string[]} [colors] Two CSS hex colours: the wires' and the pulses'. Default the sketch's blue and white.
|
|
48
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`: its first colour for the wires, its glint for the pulses.
|
|
49
|
+
* @property {string} [background='#0a0a0a'] The ground, a CSS hex colour (opaque, so the canvas covers what is under it); `transparent` leaves it clear.
|
|
50
|
+
* @property {number} [cols=48] Grid cells across, whatever the width (cells are square).
|
|
51
|
+
* @property {number} [width=1] A wire's width, CSS px.
|
|
52
|
+
* @property {number} [rest=0.35] How bright a wire at rest is, 0–1; stretched wires brighten to full.
|
|
53
|
+
* @property {number} [drops=4] Drops alive at once, 0–6 (each slot lands a new one about every five seconds).
|
|
54
|
+
* @property {number} [strength=1] How far the ripples push the wires, 0–1.6 (past that they fold).
|
|
55
|
+
* @property {number} [size=1] The ripples' scale: wavelength, reach and speed together, 0.5–2.
|
|
56
|
+
* @property {number} [pulses=3] Ring pulses alive at once, 0–4 (each slot about every two and a half seconds).
|
|
57
|
+
* @property {number} [speed=1] Plays the clock faster or slower; 0 for one still frame.
|
|
58
|
+
* @property {number} [offset=0] Seeds where and when drops land, so two cards differ.
|
|
59
|
+
* @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
|
|
60
|
+
* An area kept calm and dim: centre (0–1 from the top left), radius (share of the height), depth (0–1).
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
/** The settings' defaults. */
|
|
64
|
+
export const KINETIC_DEFAULTS = {
|
|
65
|
+
background: '#0a0a0a',
|
|
66
|
+
cols: 48,
|
|
67
|
+
width: 1,
|
|
68
|
+
rest: 0.35,
|
|
69
|
+
drops: 4,
|
|
70
|
+
strength: 1,
|
|
71
|
+
size: 1,
|
|
72
|
+
pulses: 3,
|
|
73
|
+
speed: 1,
|
|
74
|
+
offset: 0,
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** The default colours, the sketch's: a clear blue for the wires, white for the pulses. */
|
|
78
|
+
const SKETCH = ['#3197ff', '#ffffff'];
|
|
79
|
+
|
|
80
|
+
/** Most drop slots (the shader's array). */
|
|
81
|
+
export const MAX_DROPS = 6;
|
|
82
|
+
/** Most pulse slots (the shader's array). */
|
|
83
|
+
export const MAX_PULSES = 4;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* A drop's timing, in seconds: a slot lands a drop every `period`, up to
|
|
87
|
+
* `jitter` late, and it lasts `life`. Its amplitude falls by e every `decay`.
|
|
88
|
+
*/
|
|
89
|
+
export const DROP_TIMING = { period: 5, jitter: 0.8, life: 4.2, decay: 1.8, rise: 0.15 };
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A pulse's timing, in seconds: a slot fires every `period`, up to
|
|
93
|
+
* `jitter` late, and a ring takes `life` to reach its widest and fade.
|
|
94
|
+
*/
|
|
95
|
+
export const PULSE_TIMING = { period: 2.6, jitter: 1.1, life: 1.5 };
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The ripple's shape at `size` 1, in widths: how fast the front travels (a
|
|
99
|
+
* second), the wavelength, the packet's half-width at landing and how fast
|
|
100
|
+
* it widens (a second), the calm core at the centre, the height at
|
|
101
|
+
* `strength` 1, and a pulse ring's widest radius.
|
|
102
|
+
*/
|
|
103
|
+
export const WAVE = { speed: 0.1, wavelength: 0.14, sigma: 0.03, spread: 0.018, core: 0.02, amp: 0.006, reach: 0.17 };
|
|
104
|
+
|
|
105
|
+
/** How much strain lights a wire fully. */
|
|
106
|
+
const FULL_STRAIN = 0.22;
|
|
107
|
+
|
|
108
|
+
/** A pulse ring's half-width, in cells: the wire segments whose middles fall within it light up. */
|
|
109
|
+
const PULSE_BAND = 0.55;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Where and when a slot's drop for a cycle lands.
|
|
113
|
+
*
|
|
114
|
+
* @param {number} slot The slot, 0 up.
|
|
115
|
+
* @param {number} cycle The cycle, any whole number (negative before the clock starts).
|
|
116
|
+
* @param {number} [offset=0] The seed.
|
|
117
|
+
* @returns {{ x: number, y: number, start: number }} Its place, 0–1 of the width and height from the top
|
|
118
|
+
* left (kept off the very edge), and when it lands, seconds.
|
|
119
|
+
*/
|
|
120
|
+
export function dropAt(slot, cycle, offset = 0) {
|
|
121
|
+
const [hx, hy, ht] = hash33(slot + 1.37, cycle + 0.5, offset + 7.11);
|
|
122
|
+
const { period, jitter } = DROP_TIMING;
|
|
123
|
+
// the slots are spread through the period, so drops land one at a time
|
|
124
|
+
const phase = (slot / MAX_DROPS) * period;
|
|
125
|
+
return { x: 0.06 + 0.88 * hx, y: 0.06 + 0.88 * hy, start: cycle * period + phase + ht * jitter };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* A drop's height over its life: a quick rise, then falling by e every
|
|
130
|
+
* `decay` seconds, eased to nothing by the end of its life.
|
|
131
|
+
*
|
|
132
|
+
* @param {number} age Seconds since it landed.
|
|
133
|
+
* @returns {number} 0–1 (0 before it lands and after it ends).
|
|
134
|
+
*/
|
|
135
|
+
export function dropEnvelope(age) {
|
|
136
|
+
const { life, decay, rise } = DROP_TIMING;
|
|
137
|
+
if (age <= 0 || age >= life) return 0;
|
|
138
|
+
const up = smooth(0, rise, age);
|
|
139
|
+
const down = 1 - smooth(life * 0.7, life, age);
|
|
140
|
+
return up * down * Math.exp(-age / decay);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The drops alive at a moment: each slot's current (or last) drop, while
|
|
145
|
+
* it lasts.
|
|
146
|
+
*
|
|
147
|
+
* @param {number} time Seconds on the clock (already at its speed).
|
|
148
|
+
* @param {{ drops?: number, offset?: number }} [config] How many slots, and the seed.
|
|
149
|
+
* @returns {{ x: number, y: number, age: number, amp: number }[]} Each live drop: its place (0–1 of the
|
|
150
|
+
* width and height), its age in seconds, and its envelope (0–1).
|
|
151
|
+
*/
|
|
152
|
+
export function dropsAt(time, config = {}) {
|
|
153
|
+
const slots = clampInt(config.drops ?? KINETIC_DEFAULTS.drops, 0, MAX_DROPS);
|
|
154
|
+
const offset = config.offset ?? 0;
|
|
155
|
+
const { period } = DROP_TIMING;
|
|
156
|
+
const live = [];
|
|
157
|
+
for (let slot = 0; slot < slots; slot++) {
|
|
158
|
+
const phase = (slot / MAX_DROPS) * period;
|
|
159
|
+
const cycle = Math.floor((time - phase) / period);
|
|
160
|
+
// this cycle's drop may not have landed yet (it lands up to `jitter` late): then the last one may still be running
|
|
161
|
+
for (const c of [cycle, cycle - 1]) {
|
|
162
|
+
const d = dropAt(slot, c, offset);
|
|
163
|
+
const amp = dropEnvelope(time - d.start);
|
|
164
|
+
if (amp > 0) {
|
|
165
|
+
live.push({ x: d.x, y: d.y, age: time - d.start, amp });
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return live;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The pulse rings alive at a moment.
|
|
175
|
+
*
|
|
176
|
+
* @param {number} time Seconds on the clock (already at its speed).
|
|
177
|
+
* @param {{ pulses?: number, offset?: number, size?: number }} [config] How many slots, the seed and the scale.
|
|
178
|
+
* @returns {{ x: number, y: number, radius: number, glow: number }[]} Each live ring: its centre (0–1 of
|
|
179
|
+
* the width and height), its radius in widths, and its brightness (0–1).
|
|
180
|
+
*/
|
|
181
|
+
export function pulsesAt(time, config = {}) {
|
|
182
|
+
const slots = clampInt(config.pulses ?? KINETIC_DEFAULTS.pulses, 0, MAX_PULSES);
|
|
183
|
+
const offset = config.offset ?? 0;
|
|
184
|
+
const size = config.size ?? 1;
|
|
185
|
+
const { period, jitter, life } = PULSE_TIMING;
|
|
186
|
+
const live = [];
|
|
187
|
+
for (let slot = 0; slot < slots; slot++) {
|
|
188
|
+
const phase = (slot / MAX_PULSES) * period;
|
|
189
|
+
const cycle = Math.floor((time - phase) / period);
|
|
190
|
+
const [hx, hy, ht] = hash33(slot + 11.7, cycle + 0.5, offset + 3.3);
|
|
191
|
+
const a = (time - (cycle * period + phase + ht * jitter)) / life;
|
|
192
|
+
if (a <= 0 || a >= 1) continue;
|
|
193
|
+
// eased out: fast at first, slowing as it fades
|
|
194
|
+
const radius = WAVE.reach * size * (1 - (1 - a) * (1 - a));
|
|
195
|
+
live.push({ x: 0.05 + 0.9 * hx, y: 0.05 + 0.9 * hy, radius, glow: smooth(0, 0.08, a) * (1 - a) ** 1.5 });
|
|
196
|
+
}
|
|
197
|
+
return live;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* One drop's ripple at a distance from where it landed: a wave packet
|
|
202
|
+
* (a sine under a widening Gaussian) whose front travels out, calm at
|
|
203
|
+
* the very centre. Positive pushes outward.
|
|
204
|
+
*
|
|
205
|
+
* @param {number} r Distance from the drop, widths.
|
|
206
|
+
* @param {number} age Seconds since it landed.
|
|
207
|
+
* @param {number} [size=1] The ripples' scale.
|
|
208
|
+
* @returns {[number, number]} The radial displacement (widths, before the drop's envelope and the
|
|
209
|
+
* strength) and its rate of change with distance (the radial strain).
|
|
210
|
+
*/
|
|
211
|
+
export function ripple(r, age, size = 1) {
|
|
212
|
+
const front = WAVE.speed * size * age;
|
|
213
|
+
const sigma = (WAVE.sigma + WAVE.spread * age) * size;
|
|
214
|
+
const k = (2 * Math.PI) / (WAVE.wavelength * size);
|
|
215
|
+
const eps = WAVE.core * size;
|
|
216
|
+
const s = r - front;
|
|
217
|
+
const g = Math.exp(-(s * s) / (sigma * sigma));
|
|
218
|
+
const e = Math.exp(-(r * r) / (eps * eps));
|
|
219
|
+
const h = 1 - e;
|
|
220
|
+
const sin = Math.sin(k * s);
|
|
221
|
+
const f = sin * g * h;
|
|
222
|
+
const df = g * h * (k * Math.cos(k * s) - ((2 * s) / (sigma * sigma)) * sin) + sin * g * ((2 * r) / (eps * eps)) * e;
|
|
223
|
+
return [f, df];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* How far the grid is moved at a point: every live drop's ripple, summed,
|
|
228
|
+
* as the shader sums them (before any quiet area).
|
|
229
|
+
*
|
|
230
|
+
* @param {number} x Across, widths from the left.
|
|
231
|
+
* @param {number} y Down, widths from the top.
|
|
232
|
+
* @param {number} time Seconds on the clock (already at its speed).
|
|
233
|
+
* @param {KineticGridConfig} [config] The settings.
|
|
234
|
+
* @param {number} [aspect=1] The target's height over its width.
|
|
235
|
+
* @returns {[number, number]} The displacement, widths, x and y (y down).
|
|
236
|
+
*/
|
|
237
|
+
export function displacement(x, y, time, config = {}, aspect = 1) {
|
|
238
|
+
const size = config.size ?? 1;
|
|
239
|
+
const height = WAVE.amp * size * (config.strength ?? 1);
|
|
240
|
+
let dx = 0;
|
|
241
|
+
let dy = 0;
|
|
242
|
+
for (const d of dropsAt(time, config)) {
|
|
243
|
+
const vx = x - d.x;
|
|
244
|
+
const vy = y - d.y * aspect;
|
|
245
|
+
const r = Math.hypot(vx, vy);
|
|
246
|
+
if (r < 1e-9) continue;
|
|
247
|
+
const [f] = ripple(r, d.age, size);
|
|
248
|
+
dx += (vx / r) * f * d.amp * height;
|
|
249
|
+
dy += (vy / r) * f * d.amp * height;
|
|
250
|
+
}
|
|
251
|
+
return [dx, dy];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* The two colours as the shader takes them.
|
|
256
|
+
*
|
|
257
|
+
* @param {KineticGridConfig} config The settings.
|
|
258
|
+
* @returns {[string, string]} The wires' colour and the pulses'.
|
|
259
|
+
*/
|
|
260
|
+
export function gridColors(config) {
|
|
261
|
+
const colors = pickColors(config, SKETCH, (palette) => [palette.colors[0], palette.glint]);
|
|
262
|
+
return [colors[0], colors[1] ?? colors[0]];
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* A CSS background of the grid at rest, for what shows before the shader
|
|
267
|
+
* starts and without WebGL: the ground with the wires at their rest
|
|
268
|
+
* brightness, laid as the shader lays them (square cells, `cols` across,
|
|
269
|
+
* centred down the height).
|
|
270
|
+
*
|
|
271
|
+
* @param {KineticGridConfig} [config] The settings.
|
|
272
|
+
* @param {number} [aspect=1.25] The target's height over its width (a 4:5 card is 1.25).
|
|
273
|
+
* @returns {string} A CSS `background` value.
|
|
274
|
+
*/
|
|
275
|
+
export function gridStill(config = {}, aspect = 1.25) {
|
|
276
|
+
const o = { ...KINETIC_DEFAULTS, ...config };
|
|
277
|
+
const [line] = gridColors(config);
|
|
278
|
+
const ground = !o.background || o.background === 'transparent' ? null : o.background;
|
|
279
|
+
const wire = ground ? toHex(mixOklab(parseColor(ground), parseColor(line), o.rest)) : line;
|
|
280
|
+
const w = `${o.width}px`;
|
|
281
|
+
const cell = `calc(100% / ${o.cols})`;
|
|
282
|
+
const rows = aspect * o.cols;
|
|
283
|
+
// the shader centres the rows: the first line sits half the left-over part of a cell down
|
|
284
|
+
const y = `calc(100% * ${((rows - Math.floor(rows)) / 2 / rows).toFixed(5)} - ${o.width / 2}px)`;
|
|
285
|
+
const across = `linear-gradient(90deg, ${wire} 0 ${w}, transparent ${w}) -${o.width / 2}px 0 / ${cell} 100%`;
|
|
286
|
+
const down = `linear-gradient(${wire} 0 ${w}, transparent ${w}) 0 ${y} / 100% calc(100% / ${rows.toFixed(3)})`;
|
|
287
|
+
return ground ? `${across}, ${down}, ${ground}` : `${across}, ${down}`;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const FRAG = `
|
|
291
|
+
precision highp float;
|
|
292
|
+
uniform vec2 uRes;
|
|
293
|
+
uniform float uCols;
|
|
294
|
+
uniform float uWidth;
|
|
295
|
+
uniform float uRatio;
|
|
296
|
+
uniform float uRest;
|
|
297
|
+
uniform float uHeight;
|
|
298
|
+
uniform vec4 uWave;
|
|
299
|
+
uniform float uCore;
|
|
300
|
+
uniform vec4 uDrops[${MAX_DROPS}];
|
|
301
|
+
uniform vec4 uPulses[${MAX_PULSES}];
|
|
302
|
+
uniform vec3 uLine;
|
|
303
|
+
uniform vec3 uPulse;
|
|
304
|
+
uniform vec4 uBg;
|
|
305
|
+
${GLSL_OKLAB}
|
|
306
|
+
${GLSL_QUIET}
|
|
307
|
+
|
|
308
|
+
// Coverage of a line (or dot) of width w device px at a distance d px from its middle, anti-aliased.
|
|
309
|
+
float cover(float d, float w) {
|
|
310
|
+
return clamp(0.5 * max(w, 1.0) + 0.5 - d, 0.0, 1.0) * min(w, 1.0);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// A pulse ring's light on a wire segment whose middle is m (cells).
|
|
314
|
+
float pulseOn(vec2 m) {
|
|
315
|
+
float p = 0.0;
|
|
316
|
+
for (int i = 0; i < ${MAX_PULSES}; i++) {
|
|
317
|
+
vec4 q = uPulses[i];
|
|
318
|
+
if (q.w <= 0.0) continue;
|
|
319
|
+
float b = max(0.0, 1.0 - abs(length(m - q.xy) - q.z) / ${PULSE_BAND.toFixed(3)});
|
|
320
|
+
p = max(p, b * q.w);
|
|
321
|
+
}
|
|
322
|
+
return p;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
void main() {
|
|
326
|
+
// widths from the top left
|
|
327
|
+
vec2 px = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y);
|
|
328
|
+
vec2 q = px / uRes.x;
|
|
329
|
+
vec2 quv = vec2(px.x - 0.5 * uRes.x, 0.5 * uRes.y - px.y) / uRes.y;
|
|
330
|
+
float calm = mix(1.0 - uQuiet.w, 1.0, quietOpen(quv));
|
|
331
|
+
|
|
332
|
+
// every drop's ripple: the displacement D and its Jacobian J (xx, xy, yy; it is symmetric)
|
|
333
|
+
vec2 D = vec2(0.0);
|
|
334
|
+
vec3 J = vec3(0.0);
|
|
335
|
+
for (int i = 0; i < ${MAX_DROPS}; i++) {
|
|
336
|
+
vec4 d = uDrops[i];
|
|
337
|
+
if (d.w <= 0.0) continue;
|
|
338
|
+
vec2 v = q - d.xy;
|
|
339
|
+
float r = max(length(v), 1e-5);
|
|
340
|
+
float front = uWave.x * d.z;
|
|
341
|
+
float sig = uWave.z + uWave.w * d.z;
|
|
342
|
+
float s = r - front;
|
|
343
|
+
if (abs(s) > 3.2 * sig) continue;
|
|
344
|
+
float g = exp(-(s * s) / (sig * sig));
|
|
345
|
+
float e = exp(-(r * r) / (uCore * uCore));
|
|
346
|
+
float h = 1.0 - e;
|
|
347
|
+
float sn = sin(uWave.y * s);
|
|
348
|
+
float a = d.w * uHeight;
|
|
349
|
+
float f = a * sn * g * h;
|
|
350
|
+
float df = a * (g * h * (uWave.y * cos(uWave.y * s) - 2.0 * s / (sig * sig) * sn) + sn * g * 2.0 * r / (uCore * uCore) * e);
|
|
351
|
+
vec2 u = v / r;
|
|
352
|
+
float fr = f / r;
|
|
353
|
+
D += u * f;
|
|
354
|
+
J += vec3(df * u.x * u.x + fr * (1.0 - u.x * u.x), (df - fr) * u.x * u.y, df * u.y * u.y + fr * (1.0 - u.y * u.y));
|
|
355
|
+
}
|
|
356
|
+
D *= calm;
|
|
357
|
+
J *= calm;
|
|
358
|
+
|
|
359
|
+
// the grid's rest place under this pixel, in cells, the rows centred down the height
|
|
360
|
+
float rows = uRes.y / uRes.x * uCols;
|
|
361
|
+
vec2 X = (q - D) * uCols - vec2(0.0, 0.5 * fract(rows));
|
|
362
|
+
vec2 n = floor(X + 0.5);
|
|
363
|
+
vec2 f = X - n;
|
|
364
|
+
float cell = uRes.x / uCols;
|
|
365
|
+
// distance to the nearest vertical and horizontal wire, device px, through the stretch
|
|
366
|
+
float dV = abs(f.x) * cell / length(vec2(1.0 - J.x, J.y));
|
|
367
|
+
float dH = abs(f.y) * cell / length(vec2(J.y, 1.0 - J.z));
|
|
368
|
+
float cV = cover(dV, uWidth);
|
|
369
|
+
float cH = cover(dH, uWidth);
|
|
370
|
+
// a wire brightens where it is stretched or squeezed along its length
|
|
371
|
+
float tV = clamp((abs(J.z) + 0.5 * abs(J.y)) / ${FULL_STRAIN.toFixed(3)}, 0.0, 1.0);
|
|
372
|
+
float tH = clamp((abs(J.x) + 0.5 * abs(J.y)) / ${FULL_STRAIN.toFixed(3)}, 0.0, 1.0);
|
|
373
|
+
// and where it is carried off its place, so the ring reads all the way round
|
|
374
|
+
float moved = clamp(length(D) * uCols / 0.3, 0.0, 1.0);
|
|
375
|
+
tV = max(tV, 0.7 * moved);
|
|
376
|
+
tH = max(tH, 0.7 * moved);
|
|
377
|
+
float lV = cV * mix(uRest, 1.0, tV);
|
|
378
|
+
float lH = cH * mix(uRest, 1.0, tH);
|
|
379
|
+
float glowW = 3.0 * uRatio;
|
|
380
|
+
float glow = 0.28 * (exp(-dV * dV / (glowW * glowW)) * tV + exp(-dH * dH / (glowW * glowW)) * tH);
|
|
381
|
+
// the knots light while they move
|
|
382
|
+
float knot = cover(length(vec2(dV, dH)), 2.4 * uWidth) * moved;
|
|
383
|
+
float line = 1.0 - (1.0 - lV) * (1.0 - lH);
|
|
384
|
+
line = clamp(line + glow + 0.6 * knot, 0.0, 1.0) * calm;
|
|
385
|
+
|
|
386
|
+
// ring pulses light whole segments: each wire's segment is judged by its middle
|
|
387
|
+
float pV = pulseOn(vec2(n.x, floor(X.y) + 0.5));
|
|
388
|
+
float pH = pulseOn(vec2(floor(X.x) + 0.5, n.y));
|
|
389
|
+
float pulse = clamp(1.4 * (cV * pV + cH * pH) + knot * max(pV, pH), 0.0, 1.0) * calm;
|
|
390
|
+
|
|
391
|
+
vec3 lab = mix(uLine, uPulse, pulse);
|
|
392
|
+
float alpha = max(line, pulse);
|
|
393
|
+
if (uBg.a > 0.0) {
|
|
394
|
+
gl_FragColor = vec4(fromOklab(mix(uBg.rgb, lab, alpha)), 1.0);
|
|
395
|
+
} else {
|
|
396
|
+
gl_FragColor = vec4(fromOklab(lab) * alpha, alpha);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
`;
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Starts the grid on an element: a canvas inside it (its first), filling it.
|
|
403
|
+
*
|
|
404
|
+
* @param {HTMLElement} el The element.
|
|
405
|
+
* @param {KineticGridConfig} config The settings.
|
|
406
|
+
* @returns {() => void} Stops it.
|
|
407
|
+
*/
|
|
408
|
+
export function kineticGrid(el, config) {
|
|
409
|
+
const o = { ...KINETIC_DEFAULTS, ...config };
|
|
410
|
+
const speed = Math.max(0, o.speed);
|
|
411
|
+
const size = Math.max(0.25, o.size);
|
|
412
|
+
const lab = (/** @type {string} */ c) => toOklab(parseColor(c));
|
|
413
|
+
let [line, pulse] = gridColors(config).map(lab);
|
|
414
|
+
const opaque = o.background && o.background !== 'transparent';
|
|
415
|
+
const bg = opaque ? [...lab(o.background), 1] : [0, 0, 0, 0];
|
|
416
|
+
const drops = new Float32Array(MAX_DROPS * 4);
|
|
417
|
+
const pulses = new Float32Array(MAX_PULSES * 4);
|
|
418
|
+
const start = performance.now();
|
|
419
|
+
let aspect = 1;
|
|
420
|
+
|
|
421
|
+
const { stage, stop } = createStage(el, {
|
|
422
|
+
fragment: FRAG,
|
|
423
|
+
still: speed <= 0,
|
|
424
|
+
setup({ gl, uniform }) {
|
|
425
|
+
gl.uniform1f(uniform('uRest'), o.rest);
|
|
426
|
+
gl.uniform1f(uniform('uHeight'), WAVE.amp * size * Math.min(1.6, Math.max(0, o.strength)));
|
|
427
|
+
gl.uniform4f(uniform('uWave'), WAVE.speed * size, (2 * Math.PI) / (WAVE.wavelength * size), WAVE.sigma * size, WAVE.spread * size);
|
|
428
|
+
gl.uniform1f(uniform('uCore'), WAVE.core * size);
|
|
429
|
+
gl.uniform4f(uniform('uBg'), bg[0], bg[1], bg[2], bg[3]);
|
|
430
|
+
},
|
|
431
|
+
resize({ gl, uniform, canvas, ratio }) {
|
|
432
|
+
aspect = canvas.height / canvas.width;
|
|
433
|
+
gl.uniform1f(uniform('uCols'), Math.max(1, o.cols));
|
|
434
|
+
gl.uniform1f(uniform('uWidth'), o.width * ratio);
|
|
435
|
+
gl.uniform1f(uniform('uRatio'), ratio);
|
|
436
|
+
gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, canvas.width / canvas.height));
|
|
437
|
+
},
|
|
438
|
+
uniforms({ gl, uniform }, now) {
|
|
439
|
+
const time = ((now - start) / 1000) * speed;
|
|
440
|
+
drops.fill(0);
|
|
441
|
+
dropsAt(time, o).forEach((d, i) => drops.set([d.x, d.y * aspect, d.age, d.amp], i * 4));
|
|
442
|
+
pulses.fill(0);
|
|
443
|
+
// pulse centres and radii in cells, the rows centred as the grid's are
|
|
444
|
+
const shift = 0.5 * (aspect * o.cols - Math.floor(aspect * o.cols));
|
|
445
|
+
pulsesAt(time, { ...o, size }).forEach((p, i) => pulses.set([p.x * o.cols, p.y * aspect * o.cols - shift, p.radius * o.cols, p.glow], i * 4));
|
|
446
|
+
gl.uniform4fv(uniform('uDrops[0]'), drops);
|
|
447
|
+
gl.uniform4fv(uniform('uPulses[0]'), pulses);
|
|
448
|
+
gl.uniform3f(uniform('uLine'), line[0], line[1], line[2]);
|
|
449
|
+
gl.uniform3f(uniform('uPulse'), pulse[0], pulse[1], pulse[2]);
|
|
450
|
+
},
|
|
451
|
+
});
|
|
452
|
+
if (!stage) return stop;
|
|
453
|
+
|
|
454
|
+
const unwatch = watchColors(el, (next) => {
|
|
455
|
+
[line, pulse] = gridColors({ colors: next.glint ? [next.colors[0], next.glint] : next.colors }).map(lab);
|
|
456
|
+
if (stage.still) stage.redraw();
|
|
457
|
+
});
|
|
458
|
+
return () => {
|
|
459
|
+
stop();
|
|
460
|
+
unwatch();
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Hermite smoothstep.
|
|
466
|
+
*
|
|
467
|
+
* @param {number} e0 Where it starts.
|
|
468
|
+
* @param {number} e1 Where it ends.
|
|
469
|
+
* @param {number} x The value.
|
|
470
|
+
* @returns {number} 0–1.
|
|
471
|
+
*/
|
|
472
|
+
function smooth(e0, e1, x) {
|
|
473
|
+
const t = Math.min(1, Math.max(0, (x - e0) / (e1 - e0)));
|
|
474
|
+
return t * t * (3 - 2 * t);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* A whole number within bounds.
|
|
479
|
+
*
|
|
480
|
+
* @param {number} n The number.
|
|
481
|
+
* @param {number} lo The least.
|
|
482
|
+
* @param {number} hi The most.
|
|
483
|
+
* @returns {number} It, rounded and clamped.
|
|
484
|
+
*/
|
|
485
|
+
function clampInt(n, lo, hi) {
|
|
486
|
+
return Math.min(hi, Math.max(lo, Math.round(n)));
|
|
487
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { PaletteName } from './palettes.js';
|
|
2
|
+
|
|
3
|
+
export interface LightLeakConfig {
|
|
4
|
+
/** Four CSS hex colours, darkest first (fewer or more are resampled to four, in Oklab). Default the sketch's maroon ramp. */
|
|
5
|
+
colors?: string[];
|
|
6
|
+
/** A shared palette by name (`PALETTES`, any case), when there are no `colors`: its three colours and glint, darkest first. */
|
|
7
|
+
palette?: PaletteName | (string & {});
|
|
8
|
+
/** How many blobs span the shorter side (higher is busier). Default 1.5. */
|
|
9
|
+
scale?: number;
|
|
10
|
+
/** How far the domain warp bends the field (0 for none). Default 0.3. */
|
|
11
|
+
warp?: number;
|
|
12
|
+
/** Octaves of noise, 1–4: more adds finer detail. Default 2. */
|
|
13
|
+
octaves?: number;
|
|
14
|
+
/** How far each field is pushed from its middle toward the ramp's ends (1 is the noise as it is), 0.5–3. Default 1.6. */
|
|
15
|
+
contrast?: number;
|
|
16
|
+
/** Where the sample window sits, so two cards differ. Default [0, 0]. */
|
|
17
|
+
offset?: [number, number];
|
|
18
|
+
/** Plays the clock faster or slower; 0 for one still frame. Default 1. */
|
|
19
|
+
speed?: number;
|
|
20
|
+
/** How fast the blobs drift and the warp moves (the sketch's own was 0.04, a four-and-a-half-minute loop). Default 0.3. */
|
|
21
|
+
drift?: number;
|
|
22
|
+
/** An area flattened to one tone behind copy: centre (0–1 from the top left), radius (share of the height), depth (0–1). */
|
|
23
|
+
quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
|
|
24
|
+
/** The tone the quiet area settles to: 0 the darkest colour, 1 the brightest (for dark copy). Default 0. */
|
|
25
|
+
quietTone?: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The settings' defaults, shared by the shader and the JavaScript twin. */
|
|
29
|
+
export declare const LEAK_DEFAULTS: {
|
|
30
|
+
scale: number;
|
|
31
|
+
warp: number;
|
|
32
|
+
octaves: number;
|
|
33
|
+
contrast: number;
|
|
34
|
+
quietTone: number;
|
|
35
|
+
drift: number;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** The sketch's maroon ramp around #A40F27, darkest first. */
|
|
39
|
+
export declare const LEAK_COLORS: string[];
|
|
40
|
+
|
|
41
|
+
/** The lattice hash (Dave Hoskins' Hash without Sine), 0–1; the shader's own. */
|
|
42
|
+
export declare function leakHash(x: number, y: number): number;
|
|
43
|
+
|
|
44
|
+
/** Value noise with a cubic Hermite fade, 0–1. */
|
|
45
|
+
export declare function valueNoise(x: number, y: number): number;
|
|
46
|
+
|
|
47
|
+
/** Fractal value noise as the sketch sums it, divided by the weights' total so it spans 0–1 at any octave count. */
|
|
48
|
+
export declare function fbm(x: number, y: number, octaves?: number): number;
|
|
49
|
+
|
|
50
|
+
/** A field pushed from its middle toward 0 and 1; `contrast` 1 leaves it as it is. Default 1.6. */
|
|
51
|
+
export declare function leakShape(n: number, contrast?: number): number;
|
|
52
|
+
|
|
53
|
+
/** An octave count as the shader takes it: a whole number, 1–4. */
|
|
54
|
+
export declare function clampOctaves(octaves: number | undefined): number;
|
|
55
|
+
|
|
56
|
+
/** The four colours as the shader takes them, darkest first. */
|
|
57
|
+
export declare function leakColors(config: LightLeakConfig): [string, string, string, string];
|
|
58
|
+
|
|
59
|
+
/** The three fields at a point (u, v 0–1 from the top left), as the shader samples them, quiet area and all. */
|
|
60
|
+
export declare function leakFields(config: LightLeakConfig, u: number, v: number, aspect?: number, time?: number): [number, number, number];
|
|
61
|
+
|
|
62
|
+
/** The four colours mixed by the three fields, in Oklab: CSS hex. */
|
|
63
|
+
export declare function leakMix(colors: string[], fields: [number, number, number]): string;
|
|
64
|
+
|
|
65
|
+
/** The field's colour at a point (u, v 0–1 from the top left): what the shader draws there, bar the breath and dither. */
|
|
66
|
+
export declare function leakColor(config: LightLeakConfig, u: number, v: number, aspect?: number, time?: number): string;
|
|
67
|
+
|
|
68
|
+
/** A CSS background close to the field's first frame, for before the shader starts and where there is no WebGL. */
|
|
69
|
+
export declare function lightLeakStill(config: LightLeakConfig, aspect?: number): string;
|
|
70
|
+
|
|
71
|
+
/** Starts the light leak on an element (its first canvas is drawn into, opaque). Returns a function that stops it. */
|
|
72
|
+
export declare function lightLeak(el: HTMLElement, config: LightLeakConfig): () => void;
|