@arraypress/effects-astro 0.11.0 → 0.13.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 +2 -2
- package/COMPONENTS.md +175 -10
- package/package.json +1 -1
- package/src/components/DitherGradient.astro +41 -0
- package/src/components/FlutedGlow.astro +39 -0
- package/src/components/GlassDrop.astro +40 -0
- package/src/components/GlassStack.astro +41 -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/index.d.ts +19 -1
- package/src/index.js +11 -1
- package/src/runtime/bayer.d.ts +5 -0
- package/src/runtime/bayer.js +48 -0
- package/src/runtime/dither-field.js +7 -33
- package/src/runtime/dither-gradient.d.ts +91 -0
- package/src/runtime/dither-gradient.js +410 -0
- package/src/runtime/fluted-glow.d.ts +79 -0
- package/src/runtime/fluted-glow.js +312 -0
- package/src/runtime/glass-drop-plan.d.ts +59 -0
- package/src/runtime/glass-drop-plan.js +133 -0
- package/src/runtime/glass-drop.d.ts +61 -0
- package/src/runtime/glass-drop.js +331 -0
- package/src/runtime/glass-optics.d.ts +5 -0
- package/src/runtime/glass-optics.js +49 -0
- package/src/runtime/glass-stack.d.ts +65 -0
- package/src/runtime/glass-stack.js +315 -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/slab-runs.d.ts +18 -0
- package/src/runtime/slab-runs.js +112 -0
- package/src/runtime/truchet-field.d.ts +60 -0
- package/src/runtime/truchet-field.js +272 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/pixel-horizon
|
|
3
|
+
*
|
|
4
|
+
* A flat colour filling one side of the element and breaking up into
|
|
5
|
+
* square blocks toward the other: solid near its `side`, then a band where
|
|
6
|
+
* blocks show with a falling chance, then nothing. The frontier sways
|
|
7
|
+
* slowly and wanders along its length, and each block in the band rolls
|
|
8
|
+
* its chance again on its own clock, so blocks wink on and off a few at a
|
|
9
|
+
* time. The blocks deepen from the `edge` colour at the frontier to the
|
|
10
|
+
* `fill` colour on the solid side, blended in Oklab.
|
|
11
|
+
*
|
|
12
|
+
* Each block is one pixel of a small canvas, scaled up with square
|
|
13
|
+
* pixels, so the work is done once a block. Every clock is in seconds and
|
|
14
|
+
* nothing is kept between frames; the same blocks are worked out in plain
|
|
15
|
+
* JavaScript (`horizonBlocks`), block for block.
|
|
16
|
+
*
|
|
17
|
+
* Clear where there are no blocks (or the `background` colour), so it lays
|
|
18
|
+
* over any ground. A `quiet` area clears the blocks behind copy. Without
|
|
19
|
+
* WebGL nothing is drawn; with prefers-reduced-motion it is one still frame.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { createStage } from './stage.js';
|
|
23
|
+
import { GLSL_GRADIENT_NOISE, GLSL_HASH33, gnoise, hash33 } from './hash-noise.js';
|
|
24
|
+
import { GLSL_OKLAB, parseColor, toOklab, toHex } from './color.js';
|
|
25
|
+
import { GLSL_QUIET, quietUniform } from './glsl.js';
|
|
26
|
+
import { watchColors } from './mount.js';
|
|
27
|
+
import { pickColors } from './palettes.js';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {'bottom' | 'left' | 'top' | 'right'} HorizonSide
|
|
31
|
+
*
|
|
32
|
+
* @typedef {object} PixelHorizonConfig
|
|
33
|
+
* @property {string[]} [colors] Two CSS hex colours: the solid `fill` and the `edge` colour the blocks fade to at the frontier (one colour for both). Default an acid green.
|
|
34
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`: its last and first colours.
|
|
35
|
+
* @property {string} [background] A CSS hex colour to fill behind the blocks; none leaves the canvas clear there.
|
|
36
|
+
* @property {HorizonSide} [side='bottom'] Which side the colour fills from.
|
|
37
|
+
* @property {number} [block=14] A block's side, in CSS px (rounded to whole screen pixels).
|
|
38
|
+
* @property {number} [level=0.3] Where the frontier's middle sits, a share of the way from the `side` across.
|
|
39
|
+
* @property {number} [spread=0.3] How deep the broken band is, a share of the way across.
|
|
40
|
+
* @property {number} [sway=0.06] How far the frontier swings, a share of the way across.
|
|
41
|
+
* @property {number} [wander=0.08] How far the frontier wanders along its length, a share of the way across.
|
|
42
|
+
* @property {number} [rate=0.6] How often a block in the band rolls again, times a second.
|
|
43
|
+
* @property {number} [seed=0] Which random blocks, so two cards differ.
|
|
44
|
+
* @property {number} [speed=1] Plays every clock faster or slower; 0 for one still frame.
|
|
45
|
+
* @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
|
|
46
|
+
* An area kept empty: centre (0–1 from the top left), radius (share of the height), depth (0–1).
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/** The sides, in the order the shader numbers them. */
|
|
50
|
+
export const HORIZON_SIDES = /** @type {const} */ (['bottom', 'left', 'top', 'right']);
|
|
51
|
+
|
|
52
|
+
/** The settings' defaults, shared by the shader and `horizonBlocks`. */
|
|
53
|
+
export const HORIZON_DEFAULTS = {
|
|
54
|
+
side: /** @type {HorizonSide} */ ('bottom'),
|
|
55
|
+
block: 14,
|
|
56
|
+
level: 0.3,
|
|
57
|
+
spread: 0.3,
|
|
58
|
+
sway: 0.06,
|
|
59
|
+
wander: 0.08,
|
|
60
|
+
rate: 0.6,
|
|
61
|
+
seed: 0,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** The default colours: an acid green. */
|
|
65
|
+
const ACID = ['#1aff00', '#1aff00'];
|
|
66
|
+
|
|
67
|
+
const FRAG = `
|
|
68
|
+
precision highp float;
|
|
69
|
+
uniform vec2 uRes;
|
|
70
|
+
uniform vec2 uSize;
|
|
71
|
+
uniform float uTime;
|
|
72
|
+
uniform float uSide;
|
|
73
|
+
uniform float uLevel;
|
|
74
|
+
uniform float uSpread;
|
|
75
|
+
uniform float uSway;
|
|
76
|
+
uniform float uWander;
|
|
77
|
+
uniform float uRate;
|
|
78
|
+
uniform float uSeed;
|
|
79
|
+
uniform vec3 uFill;
|
|
80
|
+
uniform vec3 uEdge;
|
|
81
|
+
uniform vec4 uBg;
|
|
82
|
+
${GLSL_OKLAB}
|
|
83
|
+
${GLSL_QUIET}
|
|
84
|
+
${GLSL_HASH33}
|
|
85
|
+
${GLSL_GRADIENT_NOISE}
|
|
86
|
+
|
|
87
|
+
void main() {
|
|
88
|
+
// One pixel a block, counted from the top left.
|
|
89
|
+
vec2 cell = vec2(floor(gl_FragCoord.x), floor(uRes.y - gl_FragCoord.y));
|
|
90
|
+
vec2 f = (cell + 0.5) / uSize;
|
|
91
|
+
// across: 0 at the side the colour fills from, 1 at the far one; along: 0–1 down the frontier.
|
|
92
|
+
float across = uSide < 0.5 ? 1.0 - f.y : uSide < 1.5 ? f.x : uSide < 2.5 ? f.y : 1.0 - f.x;
|
|
93
|
+
float along = uSide < 0.5 || (uSide > 1.5 && uSide < 2.5) ? f.x : f.y;
|
|
94
|
+
float t = uTime;
|
|
95
|
+
float front = uLevel + uSway * sin(6.2831853 * t * 0.05 + uSeed) + uWander * (gnoise(vec2(along * 2.5 + uSeed, t * 0.08)) - 0.5) * 2.0;
|
|
96
|
+
float chance = clamp((front + 0.5 * uSpread - across) / max(uSpread, 0.001), 0.0, 1.0);
|
|
97
|
+
|
|
98
|
+
vec3 h = hash33(vec3(cell, uSeed));
|
|
99
|
+
float tick = floor(t * uRate * (0.6 + 0.8 * h.x) + h.y * 11.0);
|
|
100
|
+
float roll = hash33(vec3(cell + tick * 0.618, uSeed + 2.0)).x;
|
|
101
|
+
float lit = step(roll, chance) * step(0.001, chance);
|
|
102
|
+
|
|
103
|
+
vec2 q = vec2(f.x - 0.5, 0.5 - f.y) * vec2(uSize.x / uSize.y, 1.0);
|
|
104
|
+
lit *= step(0.5, mix(1.0 - uQuiet.w, 1.0, quietOpen(q)));
|
|
105
|
+
|
|
106
|
+
vec3 col = fromOklab(mix(uEdge, uFill, smoothstep(0.0, 1.0, chance)));
|
|
107
|
+
gl_FragColor = vec4(col * lit + uBg.rgb * uBg.a * (1.0 - lit), lit + uBg.a * (1.0 - lit));
|
|
108
|
+
}
|
|
109
|
+
`;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The settings, filled with the defaults, with the side's number.
|
|
113
|
+
*
|
|
114
|
+
* @param {PixelHorizonConfig} config The settings.
|
|
115
|
+
* @returns {typeof HORIZON_DEFAULTS & { sideIndex: number }} The settings.
|
|
116
|
+
*/
|
|
117
|
+
function settings(config) {
|
|
118
|
+
const o = { ...HORIZON_DEFAULTS, ...config };
|
|
119
|
+
return { ...o, sideIndex: Math.max(0, HORIZON_SIDES.indexOf(o.side)) };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* One frame block for block, as the shader draws it on a target exactly
|
|
124
|
+
* `cols` by `rows` blocks: each block's chance (0 beyond the band, 1 on
|
|
125
|
+
* the solid side) and whether it shows.
|
|
126
|
+
*
|
|
127
|
+
* @param {PixelHorizonConfig} config The settings (the same as the shader's).
|
|
128
|
+
* @param {number} cols Blocks across.
|
|
129
|
+
* @param {number} rows Blocks down.
|
|
130
|
+
* @param {number} [time=0] Seconds, as the shader's clock (already at its speed).
|
|
131
|
+
* @returns {{ chance: number, lit: boolean }[][]} Each row's blocks.
|
|
132
|
+
*/
|
|
133
|
+
export function horizonBlocks(config, cols, rows, time = 0) {
|
|
134
|
+
const o = settings(config);
|
|
135
|
+
const [qx, qy, radius, depth] = quietUniform(config.quiet, cols / rows);
|
|
136
|
+
const smooth = (/** @type {number} */ e0, /** @type {number} */ e1, /** @type {number} */ x) => {
|
|
137
|
+
const t = Math.min(1, Math.max(0, (x - e0) / (e1 - e0)));
|
|
138
|
+
return t * t * (3 - 2 * t);
|
|
139
|
+
};
|
|
140
|
+
return Array.from({ length: rows }, (_, r) =>
|
|
141
|
+
Array.from({ length: cols }, (_, k) => {
|
|
142
|
+
const fx = (k + 0.5) / cols;
|
|
143
|
+
const fy = (r + 0.5) / rows;
|
|
144
|
+
const s = o.sideIndex;
|
|
145
|
+
const across = s === 0 ? 1 - fy : s === 1 ? fx : s === 2 ? fy : 1 - fx;
|
|
146
|
+
const along = s === 0 || s === 2 ? fx : fy;
|
|
147
|
+
const front = o.level + o.sway * Math.sin(2 * Math.PI * time * 0.05 + o.seed) + o.wander * (gnoise(along * 2.5 + o.seed, time * 0.08) - 0.5) * 2;
|
|
148
|
+
const chance = Math.min(1, Math.max(0, (front + 0.5 * o.spread - across) / Math.max(o.spread, 0.001)));
|
|
149
|
+
const h = hash33(k, r, o.seed);
|
|
150
|
+
const tick = Math.floor(time * o.rate * (0.6 + 0.8 * h[0]) + h[1] * 11);
|
|
151
|
+
const roll = hash33(k + tick * 0.618, r + tick * 0.618, o.seed + 2)[0];
|
|
152
|
+
let lit = chance >= roll && chance >= 0.001;
|
|
153
|
+
if (radius > 0) {
|
|
154
|
+
const qd = Math.hypot((fx - 0.5) * (cols / rows) - qx, 0.5 - fy - qy);
|
|
155
|
+
if (1 - depth + depth * smooth(radius * 0.06, radius, qd) < 0.5) lit = false;
|
|
156
|
+
}
|
|
157
|
+
return { chance, lit };
|
|
158
|
+
}),
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The two colours as the shader takes them.
|
|
164
|
+
*
|
|
165
|
+
* @param {PixelHorizonConfig} config The settings.
|
|
166
|
+
* @returns {[string, string]} The fill colour and the edge's.
|
|
167
|
+
*/
|
|
168
|
+
export function horizonColors(config) {
|
|
169
|
+
if (config.colors?.length) return [config.colors[0], config.colors[1] ?? config.colors[0]];
|
|
170
|
+
const colors = pickColors(config, ACID);
|
|
171
|
+
return [colors[colors.length - 1], colors[0]];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* A CSS `background` that stands in for the horizon before the shader
|
|
176
|
+
* starts and without WebGL: the solid colour up to the frontier, a
|
|
177
|
+
* two-block checker along it, and the quiet area left to the ground.
|
|
178
|
+
*
|
|
179
|
+
* @param {PixelHorizonConfig} config The settings (`colors`/`palette`, `background`, `side`, `block`, `level`, `spread`, `quiet`).
|
|
180
|
+
* @returns {string} The CSS background.
|
|
181
|
+
*/
|
|
182
|
+
export function horizonStill(config) {
|
|
183
|
+
const o = settings(config);
|
|
184
|
+
const [fill] = horizonColors(config).map((c) => toHex(parseColor(c)));
|
|
185
|
+
const ground = config.background ?? '#000000';
|
|
186
|
+
const b = Math.max(2, o.block);
|
|
187
|
+
const solid = Math.round(Math.max(0, o.level - 0.25 * o.spread) * 100);
|
|
188
|
+
const side = ['top', 'right', 'bottom', 'left'][o.sideIndex];
|
|
189
|
+
// Where the checker row sits: the frontier, measured the way background-position runs.
|
|
190
|
+
const at = o.sideIndex === 0 || o.sideIndex === 1 ? 100 - solid : solid;
|
|
191
|
+
const pos = o.sideIndex % 2 === 0 ? `0 ${at}%` : `${at}% 0`;
|
|
192
|
+
const size = `${b * 2}px ${b * 2}px`;
|
|
193
|
+
const repeat = o.sideIndex % 2 === 0 ? 'repeat-x' : 'repeat-y';
|
|
194
|
+
const pc = (/** @type {number} */ n) => `${Math.round(n * 100)}%`;
|
|
195
|
+
const quiet = config.quiet;
|
|
196
|
+
const r = pc((quiet?.radius ?? 0.8) * 0.55);
|
|
197
|
+
return [
|
|
198
|
+
quiet ? `radial-gradient(${r} ${r} at ${pc(quiet.x ?? 0.5)} ${pc(quiet.y ?? 0.5)}, ${ground} 55%, transparent 100%)` : '',
|
|
199
|
+
`repeating-conic-gradient(${fill} 0 25%, transparent 0 50%) ${pos} / ${size} ${repeat}`,
|
|
200
|
+
`linear-gradient(to ${side}, ${fill} ${solid}%, transparent ${solid}%)`,
|
|
201
|
+
ground,
|
|
202
|
+
]
|
|
203
|
+
.filter(Boolean)
|
|
204
|
+
.join(', ');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Starts the horizon on an element: a canvas inside it (its first), one
|
|
209
|
+
* pixel a block, scaled up to cover it with square pixels.
|
|
210
|
+
*
|
|
211
|
+
* @param {HTMLElement} el The element.
|
|
212
|
+
* @param {PixelHorizonConfig} config The settings.
|
|
213
|
+
* @returns {() => void} Stops it.
|
|
214
|
+
*/
|
|
215
|
+
export function pixelHorizon(el, config) {
|
|
216
|
+
const o = settings(config);
|
|
217
|
+
const speed = config.speed ?? 1;
|
|
218
|
+
const lab = (/** @type {string} */ c) => toOklab(parseColor(c));
|
|
219
|
+
let [fill, edge] = horizonColors(config).map(lab);
|
|
220
|
+
const bg = config.background ? [...parseColor(config.background), 1] : [0, 0, 0, 0];
|
|
221
|
+
const native = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
|
|
222
|
+
// Whole screen pixels a block, so the scaled-up pixels are all the same size.
|
|
223
|
+
const blockCss = Math.max(1, Math.round(Math.max(1, o.block) * native)) / native;
|
|
224
|
+
const start = performance.now();
|
|
225
|
+
const canvas = el.querySelector('canvas');
|
|
226
|
+
|
|
227
|
+
const { stage, stop } = createStage(el, {
|
|
228
|
+
fragment: FRAG,
|
|
229
|
+
still: speed <= 0,
|
|
230
|
+
// One pixel a block already: never lower it further.
|
|
231
|
+
adaptive: false,
|
|
232
|
+
scale: 1 / (blockCss * Math.min(native, 2)),
|
|
233
|
+
setup({ gl, uniform, canvas }) {
|
|
234
|
+
canvas.style.imageRendering = 'pixelated';
|
|
235
|
+
gl.uniform1f(uniform('uSide'), o.sideIndex);
|
|
236
|
+
gl.uniform1f(uniform('uLevel'), o.level);
|
|
237
|
+
gl.uniform1f(uniform('uSpread'), o.spread);
|
|
238
|
+
gl.uniform1f(uniform('uSway'), o.sway);
|
|
239
|
+
gl.uniform1f(uniform('uWander'), o.wander);
|
|
240
|
+
gl.uniform1f(uniform('uRate'), o.rate);
|
|
241
|
+
gl.uniform1f(uniform('uSeed'), o.seed);
|
|
242
|
+
gl.uniform4f(uniform('uBg'), bg[0], bg[1], bg[2], bg[3]);
|
|
243
|
+
},
|
|
244
|
+
resize({ gl, uniform, canvas }) {
|
|
245
|
+
// The element in blocks; the canvas rounds up and runs past it (clipped), so every block is whole.
|
|
246
|
+
const w = Math.max(1, el.clientWidth / blockCss);
|
|
247
|
+
const h = Math.max(1, el.clientHeight / blockCss);
|
|
248
|
+
canvas.width = Math.ceil(w);
|
|
249
|
+
canvas.height = Math.ceil(h);
|
|
250
|
+
canvas.style.width = `${canvas.width * blockCss}px`;
|
|
251
|
+
canvas.style.height = `${canvas.height * blockCss}px`;
|
|
252
|
+
gl.uniform2f(uniform('uSize'), w, h);
|
|
253
|
+
gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, w / h));
|
|
254
|
+
},
|
|
255
|
+
uniforms({ gl, uniform }, now) {
|
|
256
|
+
gl.uniform1f(uniform('uTime'), ((now - start) / 1000) * Math.max(0, speed));
|
|
257
|
+
gl.uniform3f(uniform('uFill'), fill[0], fill[1], fill[2]);
|
|
258
|
+
gl.uniform3f(uniform('uEdge'), edge[0], edge[1], edge[2]);
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
if (!stage) return stop;
|
|
262
|
+
|
|
263
|
+
const unwatch = watchColors(el, (next) => {
|
|
264
|
+
[fill, edge] = horizonColors({ colors: next.colors }).map(lab);
|
|
265
|
+
if (stage.still) stage.redraw();
|
|
266
|
+
});
|
|
267
|
+
return () => {
|
|
268
|
+
stop();
|
|
269
|
+
unwatch();
|
|
270
|
+
canvas?.style.removeProperty('width');
|
|
271
|
+
canvas?.style.removeProperty('height');
|
|
272
|
+
canvas?.style.removeProperty('image-rendering');
|
|
273
|
+
};
|
|
274
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** A stretch of logo across a slice, as columns (both ends included). */
|
|
2
|
+
export interface Run {
|
|
3
|
+
from: number;
|
|
4
|
+
to: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** The runs of each slice (top first, each left to right): where a mask's logo crosses it. `phase` (0–1) is where in its slice each samples, `band` (0–1) how much of it, `threshold` the coverage that counts (0–255), `min` the narrowest run kept. */
|
|
8
|
+
export declare function sliceRuns(
|
|
9
|
+
mask: { alpha: Uint8ClampedArray | Uint8Array; width: number; height: number },
|
|
10
|
+
slices: number,
|
|
11
|
+
options?: { phase?: number; band?: number; threshold?: number; min?: number },
|
|
12
|
+
): Run[][];
|
|
13
|
+
|
|
14
|
+
/** The runs as the shader's RGBA texture (a row per slice, a texel per column): where along its run a column is in red and green (16 bits), 255 in blue, the run's width share in alpha; zero outside runs. */
|
|
15
|
+
export declare function runsTexture(runs: Run[][], width: number): Uint8Array;
|
|
16
|
+
|
|
17
|
+
/** A slice's disc size in the intro, `t` seconds in (0 none to 1 whole; a little over while settling): the bottom first, the top done by `duration`. */
|
|
18
|
+
export declare function slabGrowth(slice: number, slices: number, t: number, duration?: number): number;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/slab-runs
|
|
3
|
+
*
|
|
4
|
+
* GlassStack's slicing: a logo cut into horizontal slices, each slice's
|
|
5
|
+
* stretches of logo (its runs) becoming the glass discs of the stack. Pure,
|
|
6
|
+
* so it runs (and is tested) without a browser: it reads a mask's coverage
|
|
7
|
+
* and writes a small texture the shader reads, a row per slice.
|
|
8
|
+
*
|
|
9
|
+
* A slice samples a band of the logo, not one line, so a small part (a dot,
|
|
10
|
+
* a thin bar) is never lost between slices: `phase` says where in its
|
|
11
|
+
* slice the band sits (0 top, 1 bottom) and `band` how much of the slice it
|
|
12
|
+
* covers (0 a line, 1 all of it).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {object} Run
|
|
17
|
+
* @property {number} from Its first column.
|
|
18
|
+
* @property {number} to Its last column.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The runs of each slice: where the logo crosses it, as column spans.
|
|
23
|
+
*
|
|
24
|
+
* @param {{ alpha: Uint8ClampedArray | Uint8Array, width: number, height: number }} mask The logo's coverage, rows from the top.
|
|
25
|
+
* @param {number} slices How many slices, top to bottom.
|
|
26
|
+
* @param {{ phase?: number, band?: number, threshold?: number, min?: number }} [options] Where each slice samples
|
|
27
|
+
* (`phase`, 0–1, default 0.5), how much of it (`band`, 0–1, default 0.35), the coverage that counts (`threshold`,
|
|
28
|
+
* 0–255, default 110) and the narrowest run kept, in columns (`min`, default 2).
|
|
29
|
+
* @returns {Run[][]} A list of runs per slice, top first, each left to right.
|
|
30
|
+
*/
|
|
31
|
+
export function sliceRuns(mask, slices, options = {}) {
|
|
32
|
+
const { alpha, width, height } = mask;
|
|
33
|
+
const phase = options.phase ?? 0.5;
|
|
34
|
+
const band = options.band ?? 0.35;
|
|
35
|
+
const threshold = options.threshold ?? 110;
|
|
36
|
+
const min = options.min ?? 2;
|
|
37
|
+
const pitch = height / slices;
|
|
38
|
+
/** @type {Run[][]} */
|
|
39
|
+
const out = [];
|
|
40
|
+
for (let i = 0; i < slices; i++) {
|
|
41
|
+
const centre = (i + phase) * pitch;
|
|
42
|
+
const reach = Math.max(0.5, (band * pitch) / 2);
|
|
43
|
+
const top = Math.max(0, Math.floor(centre - reach));
|
|
44
|
+
const bottom = Math.min(height - 1, Math.ceil(centre + reach));
|
|
45
|
+
/** @type {Run[]} */
|
|
46
|
+
const runs = [];
|
|
47
|
+
let from = -1;
|
|
48
|
+
for (let x = 0; x <= width; x++) {
|
|
49
|
+
let on = false;
|
|
50
|
+
if (x < width) for (let y = top; y <= bottom && !on; y++) on = alpha[y * width + x] > threshold;
|
|
51
|
+
if (on && from < 0) from = x;
|
|
52
|
+
if (!on && from >= 0) {
|
|
53
|
+
if (x - from >= min) runs.push({ from, to: x - 1 });
|
|
54
|
+
from = -1;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
out.push(runs);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The runs as the shader's texture: a row per slice (top first), a texel
|
|
64
|
+
* per column. Inside a run, red and green hold where the column is along
|
|
65
|
+
* it (0 its left edge, 1 its right) in sixteen bits, blue is 255, and alpha
|
|
66
|
+
* the run's width as a share of the row; outside, all four are 0.
|
|
67
|
+
*
|
|
68
|
+
* @param {Run[][]} runs The runs per slice.
|
|
69
|
+
* @param {number} width The row's width in columns.
|
|
70
|
+
* @returns {Uint8Array} RGBA bytes, `width × runs.length` texels.
|
|
71
|
+
*/
|
|
72
|
+
export function runsTexture(runs, width) {
|
|
73
|
+
const out = new Uint8Array(width * runs.length * 4);
|
|
74
|
+
runs.forEach((row, i) => {
|
|
75
|
+
for (const { from, to } of row) {
|
|
76
|
+
const span = to - from + 1;
|
|
77
|
+
const share = Math.min(255, Math.round((span / width) * 255));
|
|
78
|
+
for (let x = from; x <= to; x++) {
|
|
79
|
+
const along = Math.round(((x - from + 0.5) / span) * 65535);
|
|
80
|
+
const o = (i * width + x) * 4;
|
|
81
|
+
out[o] = along >> 8;
|
|
82
|
+
out[o + 1] = along & 255;
|
|
83
|
+
out[o + 2] = 255;
|
|
84
|
+
out[o + 3] = share;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* When each slice's disc has grown, in the intro: the bottom first, the
|
|
93
|
+
* top last, each over a share of the whole, eased out with a slight
|
|
94
|
+
* overshoot so the glass settles.
|
|
95
|
+
*
|
|
96
|
+
* @param {number} slice Which slice, 0 the top.
|
|
97
|
+
* @param {number} slices How many.
|
|
98
|
+
* @param {number} t Seconds since the intro began.
|
|
99
|
+
* @param {number} [duration=1.6] Seconds until the top disc has grown.
|
|
100
|
+
* @returns {number} Its size, 0 none to 1 whole (a little over while settling).
|
|
101
|
+
*/
|
|
102
|
+
export function slabGrowth(slice, slices, t, duration = 1.6) {
|
|
103
|
+
if (duration <= 0) return 1;
|
|
104
|
+
const grow = duration * 0.38;
|
|
105
|
+
const from = slices > 1 ? ((slices - 1 - slice) / (slices - 1)) * (duration - grow) : 0;
|
|
106
|
+
const x = (t - from) / grow;
|
|
107
|
+
if (x <= 0) return 0;
|
|
108
|
+
if (x >= 1) return 1;
|
|
109
|
+
// easeOutBack: past 1 a little, back to exactly 1
|
|
110
|
+
const c = 1.4;
|
|
111
|
+
return 1 + (c + 1) * (x - 1) ** 3 + c * (x - 1) ** 2;
|
|
112
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { PaletteName } from './palettes.js';
|
|
2
|
+
|
|
3
|
+
export interface TruchetFieldConfig {
|
|
4
|
+
/** Two CSS hex colours: the lines' dim one and their bright one. Default a grey and white. */
|
|
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 lines; none leaves the canvas clear there. */
|
|
9
|
+
background?: string;
|
|
10
|
+
/** A tile's side, in CSS px. Default 26. */
|
|
11
|
+
tile?: number;
|
|
12
|
+
/** The lines' width at their brightest, a share of the tile. Default 0.26. */
|
|
13
|
+
width?: number;
|
|
14
|
+
/** How thin the lines get where the noise is low, a share of `width`. Default 0.45. */
|
|
15
|
+
thin?: number;
|
|
16
|
+
/** Shade each line across its width, like a rounded tube. Default true. */
|
|
17
|
+
tube?: boolean;
|
|
18
|
+
/** Seconds between one tile's turns, on average. Default 8. */
|
|
19
|
+
period?: number;
|
|
20
|
+
/** How long a turn takes, seconds. Default 0.6. */
|
|
21
|
+
turn?: number;
|
|
22
|
+
/** The brightness noise's features across the width. Default 1.4. */
|
|
23
|
+
scale?: number;
|
|
24
|
+
/** How fast the bright patches pan, features a second. Default [0.03, 0.015]. */
|
|
25
|
+
drift?: [number, number];
|
|
26
|
+
/** Which random tiling, so two cards differ. Default 0. */
|
|
27
|
+
seed?: number;
|
|
28
|
+
/** Plays every clock faster or slower; 0 for one still frame. Default 1. */
|
|
29
|
+
speed?: number;
|
|
30
|
+
/** An area kept empty: centre (0–1 from the top left), radius (share of the height), depth (0–1). */
|
|
31
|
+
quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The settings' defaults, shared by the shader and the JS twin. */
|
|
35
|
+
export declare const TRUCHET_DEFAULTS: {
|
|
36
|
+
tile: number;
|
|
37
|
+
width: number;
|
|
38
|
+
thin: number;
|
|
39
|
+
tube: boolean;
|
|
40
|
+
period: number;
|
|
41
|
+
turn: number;
|
|
42
|
+
scale: number;
|
|
43
|
+
drift: [number, number];
|
|
44
|
+
seed: number;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** A tile's way (which corners its arcs hug) and how far through a quarter turn to it, 0 when still. */
|
|
48
|
+
export declare function truchetTile(config: TruchetFieldConfig, col: number, row: number, time?: number): { way: 0 | 1; turning: number };
|
|
49
|
+
|
|
50
|
+
/** How bright a tile's lines are, 0–1, before the quiet area (its centre in widths from the top left). */
|
|
51
|
+
export declare function truchetTone(config: TruchetFieldConfig, x: number, y: number, time?: number): number;
|
|
52
|
+
|
|
53
|
+
/** The two colours as the shader takes them: the dim one and the bright one. */
|
|
54
|
+
export declare function truchetColors(config: TruchetFieldConfig): [string, string];
|
|
55
|
+
|
|
56
|
+
/** A CSS `background` that stands in for the field before the shader starts and without WebGL. */
|
|
57
|
+
export declare function truchetStill(config: TruchetFieldConfig): string;
|
|
58
|
+
|
|
59
|
+
/** Starts the field on an element (its first canvas). Returns a function that stops it. */
|
|
60
|
+
export declare function truchetField(el: HTMLElement, config: TruchetFieldConfig): () => void;
|