@arraypress/effects-astro 0.12.0 → 0.14.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.
@@ -0,0 +1,355 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/fluted-bokeh
3
+ *
4
+ * Coloured lights out of focus, seen through reeded glass: soft discs of
5
+ * colour drift slowly behind a pane of upright flutes (`mode: 'reeds'`) or
6
+ * a wall of square glass blocks (`mode: 'blocks'`). Each flute shows a
7
+ * narrow slice of what is behind it stretched across its width, red, green
8
+ * and blue each squeezed by a little more or less, so every seam carries a
9
+ * thin rainbow; what is behind is smeared a little upright, as light is
10
+ * through reeds, and each flute has a faint highlight on one seam and a
11
+ * shadow on the other. A block bends what is behind it toward its own
12
+ * centre, both ways, between dark joints.
13
+ *
14
+ * A sibling of `<FlutedGlow>` (one ring, orb or rise behind upright flutes)
15
+ * and `<FlutedGlass>` (slanted flutes over pointer-led blooms): several
16
+ * lights, dispersion at the seams, glass blocks, and no pointer, made to
17
+ * sit behind copy.
18
+ *
19
+ * Every clock is in seconds and nothing is kept between frames: the lights
20
+ * and the glass's slicing are here in plain JavaScript too (`bokehLights`,
21
+ * `bokehAt`, `reedSample`), so a still and a test see what the shader draws.
22
+ *
23
+ * Opaque: it paints its `background` (default near-black) everywhere.
24
+ * Without WebGL nothing is drawn; with prefers-reduced-motion it is one
25
+ * still frame.
26
+ */
27
+
28
+ import { createStage } from './stage.js';
29
+ import { GLSL_HASH33, hash33 } from './hash-noise.js';
30
+ import { parseColor, toHex, mixOklab } from './color.js';
31
+ import { GLSL_QUIET, quietUniform } from './glsl.js';
32
+ import { watchColors } from './mount.js';
33
+ import { pickColors } from './palettes.js';
34
+
35
+ /**
36
+ * @typedef {'reeds' | 'blocks'} BokehGlass
37
+ *
38
+ * @typedef {object} FlutedBokehConfig
39
+ * @property {string[]} [colors] Two to four CSS hex colours for the lights. Default coral, amber and ice blue.
40
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`.
41
+ * @property {string} [background='#050608'] The ground, painted everywhere.
42
+ * @property {BokehGlass} [mode='reeds'] Upright flutes, or a wall of square glass blocks.
43
+ * @property {number} [flutes=28] Flutes across the width (blocks across, for `blocks`).
44
+ * @property {number} [squeeze=0.3] How much of what is behind a flute it shows across its width (lower stretches more).
45
+ * @property {number} [dispersion=0.35] How far red and blue part at the seams; 0 for none.
46
+ * @property {number} [streak=0.06] How far the light smears upright, a share of the height; 0 for none.
47
+ * @property {number} [lights=5] Lights behind the glass (1–8).
48
+ * @property {number} [size=0.22] The lights' radius, a share of the height.
49
+ * @property {number} [glow=1] How bright the lights are.
50
+ * @property {number} [drift=0.08] How far the lights wander, a share of the height.
51
+ * @property {number} [rate=0.04] How fast they wander, cycles a second.
52
+ * @property {number} [seams=0.3] How strong the seams' highlights and shadows (or the blocks' joints) are; 0 for none.
53
+ * @property {number} [grain=0.04] Fine grain over all of it; 0 for none.
54
+ * @property {number} [seed=0] Where the lights sit and start wandering, so two cards differ.
55
+ * @property {number} [speed=1] Plays every clock faster or slower; 0 for one still frame.
56
+ * @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
57
+ * An area dimmed: centre (0–1 from the top left), radius (share of the height), depth (0–1).
58
+ */
59
+
60
+ /** The glass, in the order the shader numbers it. */
61
+ export const BOKEH_GLASS = /** @type {const} */ (['reeds', 'blocks']);
62
+
63
+ /** The most lights the shader draws. */
64
+ export const MAX_LIGHTS = 8;
65
+
66
+ /** The settings' defaults, shared by the shader and the JS twin. */
67
+ export const BOKEH_DEFAULTS = {
68
+ background: '#050608',
69
+ mode: /** @type {BokehGlass} */ ('reeds'),
70
+ flutes: 28,
71
+ squeeze: 0.3,
72
+ dispersion: 0.35,
73
+ streak: 0.06,
74
+ lights: 5,
75
+ size: 0.22,
76
+ glow: 1,
77
+ drift: 0.08,
78
+ rate: 0.04,
79
+ seams: 0.3,
80
+ grain: 0.04,
81
+ seed: 0,
82
+ };
83
+
84
+ /** The default colours: coral, amber and ice blue, after the owner's fluted-glass references. */
85
+ const STAGE = ['#ff4a2a', '#ffb347', '#4fc3ff'];
86
+
87
+ /**
88
+ * The settings, filled with the defaults, with the glass's number.
89
+ *
90
+ * @param {FlutedBokehConfig} config The settings.
91
+ * @returns {typeof BOKEH_DEFAULTS & { modeIndex: number, speed: number, count: number }} The settings.
92
+ */
93
+ function settings(config) {
94
+ const o = { ...BOKEH_DEFAULTS, speed: 1, ...config };
95
+ return { ...o, modeIndex: Math.max(0, BOKEH_GLASS.indexOf(o.mode)), count: Math.max(1, Math.min(MAX_LIGHTS, Math.round(o.lights))) };
96
+ }
97
+
98
+ /**
99
+ * The lights: where each sits (shares of the width and height), how big
100
+ * it is (a share of `size`), its colour (an index into the colours) and
101
+ * its own phase, all from the seed.
102
+ *
103
+ * @param {FlutedBokehConfig} config The settings.
104
+ * @returns {{ x: number, y: number, r: number, color: number, phase: number }[]} The lights.
105
+ */
106
+ export function bokehLights(config) {
107
+ const o = settings(config);
108
+ const n = pickColors(config, STAGE).length;
109
+ return Array.from({ length: o.count }, (_, i) => {
110
+ const h = hash33(i * 7.13 + o.seed, o.seed * 3.1 + 1.7, i * 1.31);
111
+ const g = hash33(i * 2.71 + 4.2, o.seed + 9.3, i * 5.9 + o.seed);
112
+ return { x: 0.08 + 0.84 * h[0], y: 0.1 + 0.8 * h[1], r: 0.55 + 0.9 * h[2], color: i % n, phase: g[0] * 6.2831853 };
113
+ });
114
+ }
115
+
116
+ /**
117
+ * GLSL for the lights: `vec3 bokehAt(vec2 s, float t)`, the light behind
118
+ * the glass at a point (shares of the width and height), the same sum as
119
+ * `bokehAt` below.
120
+ */
121
+ const GLSL_LIGHTS = `
122
+ uniform vec4 uLights[${MAX_LIGHTS}];
123
+ uniform vec3 uLightCol[${MAX_LIGHTS}];
124
+ uniform float uCount;
125
+ uniform float uSize;
126
+ uniform float uGlow;
127
+ uniform float uDrift;
128
+ uniform float uRate;
129
+ uniform float uAspect;
130
+ vec3 bokehAt(vec2 s, float t) {
131
+ vec3 c = vec3(0.0);
132
+ for (int i = 0; i < ${MAX_LIGHTS}; i++) {
133
+ if (float(i) >= uCount) break;
134
+ vec4 l = uLights[i];
135
+ float ph = 6.2831853 * t * uRate + l.w;
136
+ vec2 at = l.xy + uDrift * vec2(sin(ph * 0.9) / uAspect, cos(ph * 0.71));
137
+ vec2 d = vec2((s.x - at.x) * uAspect, s.y - at.y);
138
+ float r = uSize * l.z;
139
+ c += uLightCol[i] * exp(-dot(d, d) / (r * r) * 1.4);
140
+ }
141
+ return c * uGlow;
142
+ }
143
+ `;
144
+
145
+ const FRAG = `
146
+ precision highp float;
147
+ uniform vec2 uRes;
148
+ uniform float uTime;
149
+ uniform float uMode;
150
+ uniform float uFlutes;
151
+ uniform float uSqueeze;
152
+ uniform float uDisp;
153
+ uniform float uStreak;
154
+ uniform float uSeams;
155
+ uniform float uGrain;
156
+ uniform vec3 uBg;
157
+ ${GLSL_QUIET}
158
+ ${GLSL_HASH33}
159
+ ${GLSL_LIGHTS}
160
+
161
+ // Where in its cell a share sits (−1 to 1) and the cell's centre, for a cell count across.
162
+ vec2 cellOf(float x, float n) {
163
+ float w = 1.0 / n;
164
+ float i = floor(x / w);
165
+ return vec2((x / w - i) * 2.0 - 1.0, (i + 0.5) * w);
166
+ }
167
+
168
+ vec3 seen(vec2 s, float t) {
169
+ // the light smeared upright a little: three taps
170
+ return (bokehAt(s, t) * 0.5 + bokehAt(s + vec2(0.0, uStreak), t) * 0.25 + bokehAt(s - vec2(0.0, uStreak), t) * 0.25);
171
+ }
172
+
173
+ void main() {
174
+ vec2 px = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y);
175
+ vec2 s = px / uRes;
176
+ vec2 cx = cellOf(s.x, uFlutes);
177
+ float hw = 0.5 / uFlutes;
178
+ vec3 col;
179
+ float seam;
180
+ if (uMode < 0.5) {
181
+ // each flute shows a squeezed slice about its centre; red and blue squeezed a little differently
182
+ float sr = cx.y + cx.x * hw * uSqueeze * (1.0 + uDisp);
183
+ float sg = cx.y + cx.x * hw * uSqueeze;
184
+ float sb = cx.y + cx.x * hw * uSqueeze * (1.0 - uDisp);
185
+ col = vec3(seen(vec2(sr, s.y), uTime).r, seen(vec2(sg, s.y), uTime).g, seen(vec2(sb, s.y), uTime).b);
186
+ seam = 1.0 - uSeams * 0.35 * (1.0 - cos(cx.x * 1.5707963));
187
+ seam += uSeams * 0.3 * smoothstep(-0.86, -1.0, cx.x);
188
+ seam -= uSeams * 0.35 * smoothstep(0.86, 1.0, cx.x);
189
+ } else {
190
+ // a glass block bends what is behind it toward its own centre, both ways, between dark joints
191
+ float rows = max(1.0, floor(uFlutes * uRes.y / uRes.x + 0.5));
192
+ vec2 cy = cellOf(s.y, rows);
193
+ float hy = 0.5 / rows;
194
+ vec2 k = vec2(cx.x, cy.x);
195
+ vec2 b = vec2(cx.y, cy.y) + k * vec2(hw, hy) * uSqueeze * 1.6;
196
+ vec2 off = k * vec2(hw, hy) * uSqueeze * uDisp * 0.8;
197
+ col = vec3(seen(b + off, uTime).r, seen(b, uTime).g, seen(b - off, uTime).b);
198
+ float edge = max(abs(k.x), abs(k.y));
199
+ seam = 1.0 - uSeams * 2.2 * smoothstep(0.82, 0.97, edge);
200
+ seam += uSeams * 0.35 * smoothstep(0.7, 0.9, -k.x - k.y) * (1.0 - smoothstep(0.9, 0.97, edge));
201
+ }
202
+ col = uBg + col * max(seam, 0.0);
203
+ vec2 q = vec2((s.x - 0.5) * uRes.x / uRes.y, 0.5 - s.y);
204
+ col = mix(uBg, col, mix(1.0 - uQuiet.w, 1.0, quietOpen(q)));
205
+ col += (hash33(vec3(px, floor(uTime * 24.0))).x - 0.5) * uGrain;
206
+ gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
207
+ }
208
+ `;
209
+
210
+ /**
211
+ * The light behind the glass at a point (the shader's `bokehAt`), before
212
+ * the glass: red, green and blue, 0 and up.
213
+ *
214
+ * @param {FlutedBokehConfig} config The settings.
215
+ * @param {number} x A share of the width.
216
+ * @param {number} y A share of the height.
217
+ * @param {number} aspect Width over height.
218
+ * @param {number} [time=0] Seconds (already at its speed).
219
+ * @returns {[number, number, number]} The light.
220
+ */
221
+ export function bokehAt(config, x, y, aspect, time = 0) {
222
+ const o = settings(config);
223
+ const cols = bokehColors(config).map((c) => parseColor(c));
224
+ const c = [0, 0, 0];
225
+ for (const l of bokehLights(config)) {
226
+ const ph = 2 * Math.PI * time * o.rate + l.phase;
227
+ const ax = l.x + (o.drift * Math.sin(ph * 0.9)) / aspect;
228
+ const ay = l.y + o.drift * Math.cos(ph * 0.71);
229
+ const dx = (x - ax) * aspect;
230
+ const dy = y - ay;
231
+ const r = o.size * l.r;
232
+ const k = Math.exp(((-(dx * dx + dy * dy)) / (r * r)) * 1.4) * o.glow;
233
+ const col = cols[l.color];
234
+ for (let j = 0; j < 3; j++) c[j] += col[j] * k;
235
+ }
236
+ return /** @type {[number, number, number]} */ (c);
237
+ }
238
+
239
+ /**
240
+ * Where behind the reeds a point looks (the shader's slicing, the green
241
+ * channel): each flute shows a slice `squeeze` of its own width, stretched
242
+ * across it.
243
+ *
244
+ * @param {number} x The point, a share of the width (0–1).
245
+ * @param {number} flutes Flutes across the width.
246
+ * @param {number} squeeze The share of a flute's width it shows.
247
+ * @returns {{ x: number, local: number }} The share of the width it shows, and where in its flute it is (−1 to 1).
248
+ */
249
+ export function reedSample(x, flutes, squeeze) {
250
+ const w = 1 / flutes;
251
+ const i = Math.floor(x / w);
252
+ const local = (x / w - i) * 2 - 1;
253
+ return { x: (i + 0.5) * w + local * 0.5 * w * squeeze, local };
254
+ }
255
+
256
+ /**
257
+ * The lights' colours as the shader takes them: two to four.
258
+ *
259
+ * @param {FlutedBokehConfig} config The settings.
260
+ * @returns {string[]} The colours.
261
+ */
262
+ export function bokehColors(config) {
263
+ return pickColors(config, STAGE).slice(0, 4);
264
+ }
265
+
266
+ /**
267
+ * A CSS `background` that stands in for the lights before the shader
268
+ * starts and without WebGL: the lights as soft radial gradients, under
269
+ * upright stripes for the reeds or a grid for the blocks.
270
+ *
271
+ * @param {FlutedBokehConfig} config The settings (`colors`/`palette`, `background`, `mode`, `flutes`, `lights`, `size`, `seed`).
272
+ * @param {number} [aspect=0.8] The element's width over its height (a 4:5 card by default).
273
+ * @returns {string} The CSS background.
274
+ */
275
+ export function bokehStill(config, aspect = 0.8) {
276
+ const o = settings(config);
277
+ const cols = bokehColors(config);
278
+ const ground = toHex(parseColor(o.background));
279
+ const lights = bokehLights(config).map((l) => {
280
+ const c = toHex(mixOklab(parseColor(ground), parseColor(cols[l.color]), 0.85));
281
+ const rx = Math.round(((o.size * l.r) / aspect) * 100);
282
+ const ry = Math.round(o.size * l.r * 100);
283
+ return `radial-gradient(${rx}% ${ry}% at ${Math.round(l.x * 100)}% ${Math.round(l.y * 100)}%, ${c} 0%, transparent 100%)`;
284
+ });
285
+ const w = 100 / Math.max(1, o.flutes);
286
+ const reeds = `repeating-linear-gradient(90deg, rgba(255,255,255,0.07) 0, transparent ${(w * 0.15).toFixed(2)}%, transparent ${(w * 0.8).toFixed(2)}%, rgba(0,0,0,0.3) ${w.toFixed(2)}%)`;
287
+ const glass =
288
+ o.modeIndex === 1
289
+ ? [reeds, `repeating-linear-gradient(180deg, transparent 0, transparent ${(w * aspect * 0.9).toFixed(2)}%, rgba(0,0,0,0.45) ${(w * aspect).toFixed(2)}%)`]
290
+ : [reeds];
291
+ return [...glass, ...lights, ground].join(', ');
292
+ }
293
+
294
+ /**
295
+ * Starts the lights and the glass on an element: a canvas inside it (its
296
+ * first), at the stage's capped pixel ratio.
297
+ *
298
+ * @param {HTMLElement} el The element.
299
+ * @param {FlutedBokehConfig} config The settings.
300
+ * @returns {() => void} Stops it.
301
+ */
302
+ export function flutedBokeh(el, config) {
303
+ const o = settings(config);
304
+ const lights = bokehLights(config);
305
+ const rgb = (/** @type {string[]} */ cs) => lights.map((l) => parseColor(cs[l.color % cs.length]));
306
+ let colours = rgb(bokehColors(config));
307
+ const ground = parseColor(o.background);
308
+ const start = performance.now();
309
+ const pack = new Float32Array(MAX_LIGHTS * 4);
310
+ lights.forEach((l, i) => pack.set([l.x, l.y, l.r, l.phase], i * 4));
311
+
312
+ const { stage, stop } = createStage(el, {
313
+ fragment: FRAG,
314
+ alpha: false,
315
+ still: o.speed <= 0,
316
+ setup({ gl, uniform }) {
317
+ gl.uniform1f(uniform('uMode'), o.modeIndex);
318
+ gl.uniform1f(uniform('uFlutes'), Math.max(1, o.flutes));
319
+ gl.uniform1f(uniform('uSqueeze'), o.squeeze);
320
+ gl.uniform1f(uniform('uDisp'), o.dispersion);
321
+ gl.uniform1f(uniform('uStreak'), o.streak);
322
+ gl.uniform1f(uniform('uSeams'), o.seams);
323
+ gl.uniform1f(uniform('uGrain'), o.grain);
324
+ gl.uniform1f(uniform('uCount'), lights.length);
325
+ gl.uniform1f(uniform('uSize'), o.size);
326
+ gl.uniform1f(uniform('uGlow'), o.glow);
327
+ gl.uniform1f(uniform('uDrift'), o.drift);
328
+ gl.uniform1f(uniform('uRate'), o.rate);
329
+ gl.uniform3f(uniform('uBg'), ground[0], ground[1], ground[2]);
330
+ gl.uniform4fv(uniform('uLights[0]'), pack);
331
+ },
332
+ resize({ gl, uniform }) {
333
+ const w = Math.max(1, el.clientWidth);
334
+ const h = Math.max(1, el.clientHeight);
335
+ gl.uniform1f(uniform('uAspect'), w / h);
336
+ gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, w / h));
337
+ },
338
+ uniforms({ gl, uniform }, now) {
339
+ gl.uniform1f(uniform('uTime'), ((now - start) / 1000) * Math.max(0, o.speed));
340
+ const flat = new Float32Array(MAX_LIGHTS * 3);
341
+ colours.forEach((c, i) => flat.set(c, i * 3));
342
+ gl.uniform3fv(uniform('uLightCol[0]'), flat);
343
+ },
344
+ });
345
+ if (!stage) return stop;
346
+
347
+ const unwatch = watchColors(el, (next) => {
348
+ colours = rgb(bokehColors({ colors: next.colors }));
349
+ if (stage.still) stage.redraw();
350
+ });
351
+ return () => {
352
+ stop();
353
+ unwatch();
354
+ };
355
+ }
@@ -0,0 +1,59 @@
1
+ /** The sequence's parts, in seconds at speed 1: the drop forming, spreading into its shape, and melting away. */
2
+ export declare const DROP_PHASES: Readonly<{ form: number; spread: number; melt: number }>;
3
+
4
+ /** The drop's radius when it lands, a share of the tile's edge. */
5
+ export declare const DROP_SIZE: number;
6
+
7
+ export interface DropLayout {
8
+ /** The glass's middle, each 0–1 from the top left. */
9
+ centre: { x: number; y: number };
10
+ /** The tile's edge, a share of the element's height. */
11
+ tile: number;
12
+ /** The mark's box under the glass, a share of the height. */
13
+ mark: number;
14
+ /** The mark's box at rest, a share of the height. */
15
+ rest: number;
16
+ }
17
+
18
+ export interface DropState {
19
+ /** How far the drop has formed, 0–1. */
20
+ drop: number;
21
+ /** From the drop (0) to the tile or mark (1); it overshoots a little as the glass settles. */
22
+ morph: number;
23
+ /** How much the glass's edge still ripples, 0–1. */
24
+ wobble: number;
25
+ /** Seconds since the drop landed, or −1 before. */
26
+ ripple: number;
27
+ /** How far the mark has risen under the glass (or, as the logo's own glass melts, come through flat), 0–1. */
28
+ markIn: number;
29
+ /** How much glass there is, 1 whole to 0 melted. */
30
+ glass: number;
31
+ /** The mark's size, 1 under the glass to `restScale` at rest. */
32
+ scale: number;
33
+ /** Whether the sequence has finished and nothing more moves. */
34
+ done: boolean;
35
+ }
36
+
37
+ /** The settings the plan reads. */
38
+ export interface DropPlanConfig {
39
+ area?: { x: number; y: number; width: number; height: number };
40
+ size?: number;
41
+ markScale?: number;
42
+ restScale?: number;
43
+ hold?: number;
44
+ loopDelay?: number;
45
+ loop?: boolean;
46
+ rest?: 'flat' | 'glass';
47
+ delay?: number;
48
+ speed?: number;
49
+ form?: 'tile' | 'mark';
50
+ }
51
+
52
+ /** Where the glass and the mark sit in an element (`aspect`, its width over its height): a square tile `size` of the area's shorter side, the mark's box `markScale` of it, `restScale` times that at rest. */
53
+ export declare function dropLayout(config: DropPlanConfig, aspect: number): DropLayout;
54
+
55
+ /** The cycle's length in seconds at speed 1: the sequence, then the rest before it plays again (none without `loop`). */
56
+ export declare function dropCycle(config: DropPlanConfig): number;
57
+
58
+ /** What the sequence is doing `seconds` after it started (`Infinity`: the final state). */
59
+ export declare function dropAt(seconds: number, config: DropPlanConfig): DropState;
@@ -0,0 +1,133 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/glass-drop-plan
3
+ *
4
+ * GlassDrop's plan, apart from its shader so it can be tested and so a page
5
+ * can lay a still of the resting mark exactly where the effect leaves it:
6
+ * where the glass and the mark sit (`dropLayout`), and what the sequence
7
+ * is doing at a moment (`dropAt`): a drop of liquid glass forms, spreads
8
+ * into a tile (or into the mark's own shape) with the mark rising under it,
9
+ * holds, then melts away and leaves the mark flat on the ground. On a loop,
10
+ * each round starts from the resting mark, which fades as the drop forms, so
11
+ * a page can show the resting mark as the effect's still and the effect
12
+ * takes over from it without a jump.
13
+ */
14
+
15
+ /** The sequence's parts, in seconds at speed 1: the drop forming, spreading into its shape, and melting away. */
16
+ export const DROP_PHASES = Object.freeze({ form: 0.7, spread: 1.3, melt: 1.1 });
17
+
18
+ /** The drop's radius when it lands, a share of the tile's edge. */
19
+ export const DROP_SIZE = 0.17;
20
+
21
+ /** The settings the plan reads, with their defaults. */
22
+ const DEFAULTS = { size: 0.7, markScale: 0.62, restScale: 1, hold: 2.4, loopDelay: 1.6, loop: false, rest: 'flat', delay: 0, speed: 1 };
23
+
24
+ /**
25
+ * @typedef {object} DropLayout
26
+ * @property {{ x: number, y: number }} centre The glass's middle, each 0–1 from the top left.
27
+ * @property {number} tile The tile's edge, a share of the element's height.
28
+ * @property {number} mark The mark's box under the glass, a share of the height.
29
+ * @property {number} rest The mark's box at rest, a share of the height.
30
+ */
31
+
32
+ /**
33
+ * Where the glass and the mark sit in an element: the tile is a square
34
+ * `size` of the area's shorter side, centred in it; the mark's box is
35
+ * `markScale` of the tile under the glass, and `restScale` times that once
36
+ * the glass has gone. A square box holds the whole image (an SVG logo's
37
+ * viewBox), so a still drawn in the same box lines up exactly.
38
+ *
39
+ * @param {{ area?: { x: number, y: number, width: number, height: number }, size?: number, markScale?: number, restScale?: number }} config The settings.
40
+ * @param {number} aspect The element's width over its height.
41
+ * @returns {DropLayout} The layout.
42
+ */
43
+ export function dropLayout(config, aspect) {
44
+ const area = config.area ?? { x: 0, y: 0, width: 1, height: 1 };
45
+ const size = config.size ?? DEFAULTS.size;
46
+ const markScale = config.markScale ?? DEFAULTS.markScale;
47
+ const restScale = config.restScale ?? DEFAULTS.restScale;
48
+ const tile = Math.min(area.width * aspect, area.height) * size;
49
+ return {
50
+ centre: { x: area.x + area.width / 2, y: area.y + area.height / 2 },
51
+ tile,
52
+ mark: tile * markScale,
53
+ rest: tile * markScale * restScale,
54
+ };
55
+ }
56
+
57
+ /**
58
+ * @typedef {object} DropState
59
+ * @property {number} drop How far the drop has formed, 0–1 (its radius is `drop × DROP_SIZE` of the tile).
60
+ * @property {number} morph From the drop (0) to the tile or mark (1); it overshoots a little as the glass settles.
61
+ * @property {number} wobble How much the glass's edge still ripples, 0–1.
62
+ * @property {number} ripple Seconds since the drop landed (the rings on the ground), or −1 before.
63
+ * @property {number} markIn How far the mark has risen under the glass, 0–1.
64
+ * @property {number} glass How much glass there is, 1 whole to 0 melted.
65
+ * @property {number} scale The mark's size, 1 under the glass to `restScale` at rest.
66
+ * @property {boolean} done Whether the sequence has finished and nothing more moves.
67
+ */
68
+
69
+ /** Hermite ease, 0–1. */
70
+ const ease = (x) => {
71
+ const t = Math.min(1, Math.max(0, x));
72
+ return t * t * (3 - 2 * t);
73
+ };
74
+
75
+ /**
76
+ * The cycle's length in seconds at speed 1: the sequence, then the rest
77
+ * before it plays again (none when it does not loop).
78
+ *
79
+ * @param {{ hold?: number, loopDelay?: number, loop?: boolean, rest?: 'flat' | 'glass' }} config The settings.
80
+ * @returns {number} Seconds.
81
+ */
82
+ export function dropCycle(config) {
83
+ const hold = config.hold ?? DEFAULTS.hold;
84
+ if ((config.rest ?? DEFAULTS.rest) === 'glass') return DROP_PHASES.form + DROP_PHASES.spread;
85
+ const end = DROP_PHASES.form + DROP_PHASES.spread + hold + DROP_PHASES.melt;
86
+ return (config.loop ?? DEFAULTS.loop) ? end + (config.loopDelay ?? DEFAULTS.loopDelay) : end;
87
+ }
88
+
89
+ /**
90
+ * What the sequence is doing at a moment. `Infinity` gives the final state:
91
+ * the mark flat at its rest size (or, with `rest: 'glass'`, the glass held).
92
+ *
93
+ * @param {number} seconds Seconds since it started, before `delay` and `speed` are applied.
94
+ * @param {{ hold?: number, loopDelay?: number, loop?: boolean, rest?: 'flat' | 'glass', delay?: number, speed?: number, restScale?: number, form?: 'tile' | 'mark' }} config The settings.
95
+ * @returns {DropState} The state.
96
+ */
97
+ export function dropAt(seconds, config) {
98
+ const hold = config.hold ?? DEFAULTS.hold;
99
+ const restScale = config.restScale ?? DEFAULTS.restScale;
100
+ const keep = (config.rest ?? DEFAULTS.rest) === 'glass';
101
+ const looping = !keep && (config.loop ?? DEFAULTS.loop);
102
+ const speed = Math.max(0.05, config.speed ?? DEFAULTS.speed);
103
+ let t = (seconds - (config.delay ?? DEFAULTS.delay)) * speed;
104
+ const cycle = dropCycle(config);
105
+ if (!Number.isFinite(t)) t = keep ? cycle : cycle + hold + DROP_PHASES.melt + 1;
106
+ else if (looping && t > 0) t %= cycle;
107
+ const { form, spread, melt } = DROP_PHASES;
108
+ const glassOnly = (config.form ?? 'tile') === 'mark';
109
+
110
+ const drop = t <= 0 ? 0 : t >= form ? 1 : 1 - (1 - t / form) ** 3;
111
+ const u = (t - form) / spread;
112
+ // A damped spring from the drop to the shape: past it a little, then settling, landing on exactly 1.
113
+ const spring = u <= 0 ? 0 : 1 - Math.exp(-5 * u) * Math.cos(9 * u);
114
+ const morph = u <= 0 ? 0 : u >= 1 ? 1 : spring + (1 - spring) * ease((u - 0.75) / 0.25);
115
+ const wobble = t <= 0 ? 0 : u <= 0 ? Math.min(1, t / form) * 0.6 : u >= 1 ? 0 : Math.exp(-4 * u);
116
+ const ripple = t >= form ? t - form : -1;
117
+ const markIn = glassOnly ? 0 : ease((u - 0.2) / 0.6);
118
+ const v = keep ? 0 : ease((t - form - spread - hold) / melt);
119
+ const end = form + spread + (keep ? 0 : hold + melt);
120
+ // On a loop every round starts from where the last one rested: the flat mark, fading as the new drop forms.
121
+ if (looping && t < form)
122
+ return { drop, morph, wobble, ripple, markIn: 1 - drop, glass: 1, scale: restScale, done: false };
123
+ return {
124
+ drop,
125
+ morph,
126
+ wobble,
127
+ ripple,
128
+ markIn: glassOnly ? v : markIn,
129
+ glass: 1 - v,
130
+ scale: 1 + (restScale - 1) * v,
131
+ done: !looping && t >= end + 2,
132
+ };
133
+ }
@@ -0,0 +1,61 @@
1
+ export interface GlassDropConfig {
2
+ /** An image (an SVG logo) to make the glass of. */
3
+ src?: string;
4
+ /** A character or word instead of an image. */
5
+ text?: string;
6
+ /** Its CSS font, without the size. Default: the page's, bold. */
7
+ font?: string;
8
+ /** Where the glass sits, each 0–1 from the top left. Default all of the element. */
9
+ area?: { x: number; y: number; width: number; height: number };
10
+ /** What the drop becomes: `tile`, a glass tile with the logo under it, or `mark`, the logo's own shape in glass. Default `tile`. */
11
+ form?: 'tile' | 'mark';
12
+ /** The tile's edge, a share of the area's shorter side, 0.2–1. Default 0.7. */
13
+ size?: number;
14
+ /** The logo's size under the glass, a share of the tile, 0.2–1. Default 0.62. */
15
+ markScale?: number;
16
+ /** The logo's size once the glass has melted, times its size under the glass, 0.5–2. Default 1. */
17
+ restScale?: number;
18
+ /** The tile's corner radius, a share of its edge, 0–0.5. Default 0.24. */
19
+ radius?: number;
20
+ /** The ground. Default `#000000`. */
21
+ background?: string;
22
+ /** The logo's colour. Default `#ffffff`. */
23
+ color?: string;
24
+ /** The tile's surface under the logo. Default `#0a1020`. */
25
+ plate?: string;
26
+ /** The studio light the glass mirrors. Default `#ffffff`. */
27
+ light?: string;
28
+ /** How hard the glass bends what is under it, 0–2. Default 1. */
29
+ depth?: number;
30
+ /** How far the colours part at the rim, 0–2; 0 for none. Default 1. */
31
+ dispersion?: number;
32
+ /** How wide the rounded rim is, a share of the tile (or the logo), 0.02–0.3. Default 0.1. */
33
+ bevel?: number;
34
+ /** The lip and the studio's reflections, 0–2. Default 1. */
35
+ highlights?: number;
36
+ /** Light focused at the glass's edge and onto the ground, 0–2. Default 1. */
37
+ caustics?: number;
38
+ /** A soft glow round the glass, 0–1. Default 0.5. */
39
+ bloom?: number;
40
+ /** The rings the drop sends across the ground, 0–2; 0 for none. Default 1. */
41
+ ripples?: number;
42
+ /** With `form: 'mark'`: how strongly the glass takes the logo's colour, 0–1. Default 0.88. */
43
+ tint?: number;
44
+ /** Where the light comes from, degrees, 0–360 (0 right, 90 top). Default 125. */
45
+ lightAngle?: number;
46
+ /** How fast the sequence plays, 0.25–3. Default 1. */
47
+ speed?: number;
48
+ /** Seconds to wait before it starts, 0–10. Default 0. */
49
+ delay?: number;
50
+ /** Seconds the glass holds before it melts, 0–10. Default 2.4. */
51
+ hold?: number;
52
+ /** Play it again after a rest; each round starts from the resting logo, which fades as the new drop forms. Default false. */
53
+ loop?: boolean;
54
+ /** With `loop`: seconds the logo rests before the next drop, 0–10. Default 1.6. */
55
+ loopDelay?: number;
56
+ /** How it ends: `flat`, the glass melted and the logo left on the ground, or `glass`, the glass held. Default `flat`. */
57
+ rest?: 'flat' | 'glass';
58
+ }
59
+
60
+ /** Starts the glass drop on an element (its first canvas). Returns a function that stops it. */
61
+ export declare function glassDrop(el: HTMLElement, config: GlassDropConfig): () => void;