@arraypress/effects-astro 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +1 -1
- package/COMPONENTS.md +86 -10
- package/package.json +1 -1
- package/src/components/DitherGradient.astro +41 -0
- package/src/components/FlutedGlow.astro +39 -0
- package/src/components/PixelFountain.astro +20 -7
- package/src/components/PixelGlitchText.astro +4 -3
- package/src/components/PixelHorizon.astro +40 -0
- package/src/components/TruchetField.astro +40 -0
- package/src/index.d.ts +10 -1
- package/src/index.js +6 -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/pixel-fountain.d.ts +29 -13
- package/src/runtime/pixel-fountain.js +183 -42
- package/src/runtime/pixel-glitch.d.ts +2 -0
- package/src/runtime/pixel-glitch.js +8 -2
- package/src/runtime/pixel-horizon.d.ts +60 -0
- package/src/runtime/pixel-horizon.js +274 -0
- package/src/runtime/truchet-field.d.ts +60 -0
- package/src/runtime/truchet-field.js +272 -0
|
@@ -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,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;
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/truchet-field
|
|
3
|
+
*
|
|
4
|
+
* A field of Truchet tiles: square tiles each holding two quarter-circle
|
|
5
|
+
* arcs on opposite corners, set one way or the other at random, so the
|
|
6
|
+
* arcs join across the tiles into a meander of loops and S-bends. Now and
|
|
7
|
+
* then a tile turns a quarter (the arcs then join their neighbours the
|
|
8
|
+
* other way), each on its own clock, so the pattern rearranges a few tiles
|
|
9
|
+
* at a time and never all at once. A slow noise brightens and thickens
|
|
10
|
+
* the lines in drifting patches; with `tube` each line is shaded across
|
|
11
|
+
* its width like a rounded tube.
|
|
12
|
+
*
|
|
13
|
+
* Every clock is in seconds and nothing is kept between frames: a tile's
|
|
14
|
+
* way and its turn are a hash of the tile and of how many turns its clock
|
|
15
|
+
* has made, here in GLSL and in plain JavaScript (`truchetTile`) alike.
|
|
16
|
+
* The tiles keep their size in CSS px.
|
|
17
|
+
*
|
|
18
|
+
* Clear where there are no lines (or the `background` colour), so it lays
|
|
19
|
+
* over any ground. A `quiet` area thins the lines away behind copy. Without
|
|
20
|
+
* WebGL nothing is drawn; with prefers-reduced-motion it is one still frame.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { createStage } from './stage.js';
|
|
24
|
+
import { GLSL_GRADIENT_NOISE, GLSL_HASH33, gnoise, hash33 } from './hash-noise.js';
|
|
25
|
+
import { GLSL_OKLAB, parseColor, toOklab, toHex, mixOklab } from './color.js';
|
|
26
|
+
import { GLSL_QUIET, quietUniform } from './glsl.js';
|
|
27
|
+
import { watchColors } from './mount.js';
|
|
28
|
+
import { pickColors } from './palettes.js';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @typedef {object} TruchetFieldConfig
|
|
32
|
+
* @property {string[]} [colors] Two CSS hex colours: the lines' dim one and their bright one. Default a grey and white.
|
|
33
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`: its first and last colours.
|
|
34
|
+
* @property {string} [background] A CSS hex colour to fill behind the lines; none leaves the canvas clear there.
|
|
35
|
+
* @property {number} [tile=26] A tile's side, in CSS px.
|
|
36
|
+
* @property {number} [width=0.26] The lines' width at their brightest, a share of the tile.
|
|
37
|
+
* @property {number} [thin=0.45] How thin the lines get where the noise is low, a share of `width`.
|
|
38
|
+
* @property {boolean} [tube=true] Shade each line across its width, like a rounded tube.
|
|
39
|
+
* @property {number} [period=8] Seconds between one tile's turns, on average.
|
|
40
|
+
* @property {number} [turn=0.6] How long a turn takes, seconds.
|
|
41
|
+
* @property {number} [scale=1.4] The brightness noise's features across the width.
|
|
42
|
+
* @property {[number, number]} [drift=[0.03, 0.015]] How fast the bright patches pan, features a second.
|
|
43
|
+
* @property {number} [seed=0] Which random tiling, 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 settings' defaults, shared by the shader and the JS twin. */
|
|
50
|
+
export const TRUCHET_DEFAULTS = {
|
|
51
|
+
tile: 26,
|
|
52
|
+
width: 0.26,
|
|
53
|
+
thin: 0.45,
|
|
54
|
+
tube: true,
|
|
55
|
+
period: 8,
|
|
56
|
+
turn: 0.6,
|
|
57
|
+
scale: 1.4,
|
|
58
|
+
drift: /** @type {[number, number]} */ ([0.03, 0.015]),
|
|
59
|
+
seed: 0,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** The default colours: a grey and white. */
|
|
63
|
+
const GREYS = ['#5a5a5a', '#f4f3ef'];
|
|
64
|
+
|
|
65
|
+
const FRAG = `
|
|
66
|
+
precision highp float;
|
|
67
|
+
uniform vec2 uRes;
|
|
68
|
+
uniform vec2 uSize;
|
|
69
|
+
uniform float uTile;
|
|
70
|
+
uniform float uTime;
|
|
71
|
+
uniform float uWidth;
|
|
72
|
+
uniform float uThin;
|
|
73
|
+
uniform float uTube;
|
|
74
|
+
uniform float uPeriod;
|
|
75
|
+
uniform float uTurn;
|
|
76
|
+
uniform float uScale;
|
|
77
|
+
uniform vec2 uDrift;
|
|
78
|
+
uniform float uSeed;
|
|
79
|
+
uniform vec3 uDim;
|
|
80
|
+
uniform vec3 uBright;
|
|
81
|
+
uniform vec4 uBg;
|
|
82
|
+
${GLSL_OKLAB}
|
|
83
|
+
${GLSL_QUIET}
|
|
84
|
+
${GLSL_HASH33}
|
|
85
|
+
${GLSL_GRADIENT_NOISE}
|
|
86
|
+
|
|
87
|
+
// A tile's way now (0 or 1) and how far through a quarter turn it is (0–1): its clock runs at its own rate and phase,
|
|
88
|
+
// and each tick picks a way from the hash of the tile and the tick.
|
|
89
|
+
vec2 tileState(vec2 cell, float t) {
|
|
90
|
+
vec3 h = hash33(vec3(cell, uSeed));
|
|
91
|
+
float clock = t / (uPeriod * (0.6 + 0.8 * h.x)) + h.y * 7.0;
|
|
92
|
+
float tick = floor(clock);
|
|
93
|
+
float now = step(0.5, hash33(vec3(cell + tick * 0.618, uSeed + 1.0)).x);
|
|
94
|
+
float before = step(0.5, hash33(vec3(cell + (tick - 1.0) * 0.618, uSeed + 1.0)).x);
|
|
95
|
+
float into = (clock - tick) * uPeriod * (0.6 + 0.8 * h.x) / max(uTurn, 0.001);
|
|
96
|
+
float turning = (1.0 - step(1.0, into)) * abs(now - before);
|
|
97
|
+
return vec2(mix(now, before, turning), turning * into);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
void main() {
|
|
101
|
+
vec2 px = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y);
|
|
102
|
+
vec2 cell = floor(px / uTile);
|
|
103
|
+
vec2 l = px / uTile - cell - 0.5;
|
|
104
|
+
vec2 st = tileState(cell, uTime);
|
|
105
|
+
// Mid-turn, the old way turned by the eased share of a quarter.
|
|
106
|
+
float e = st.y * st.y * (3.0 - 2.0 * st.y) * 1.5707963;
|
|
107
|
+
l = mat2(cos(e), sin(e), -sin(e), cos(e)) * l;
|
|
108
|
+
vec2 k = st.x > 0.5 ? vec2(1.0, -1.0) : vec2(1.0, 1.0);
|
|
109
|
+
float d = min(abs(length(l - 0.5 * k) - 0.5), abs(length(l + 0.5 * k) - 0.5));
|
|
110
|
+
|
|
111
|
+
vec2 c = (cell + 0.5) * uTile / uRes.x;
|
|
112
|
+
float tone = smoothstep(0.32, 0.72, gnoise(c * uScale + vec2(uSeed * 3.1, uSeed * 1.7) + uDrift * uTime));
|
|
113
|
+
vec2 q = vec2(c.x - 0.5, 0.5 * uSize.y / uSize.x - c.y) * uSize.x / uSize.y;
|
|
114
|
+
float open = mix(1.0 - uQuiet.w, 1.0, quietOpen(q));
|
|
115
|
+
float half_ = 0.5 * uWidth * mix(uThin, 1.0, tone) * open;
|
|
116
|
+
float aa = 1.0 / uTile;
|
|
117
|
+
float lit = (1.0 - smoothstep(half_ - aa, half_ + aa, d)) * step(0.004, half_);
|
|
118
|
+
float across = clamp(d / max(half_, 0.001), 0.0, 1.0);
|
|
119
|
+
float shade = mix(1.0, 0.35 + 0.65 * sqrt(1.0 - across * across), uTube);
|
|
120
|
+
vec3 col = fromOklab(mix(uDim, uBright, tone * open)) * shade;
|
|
121
|
+
gl_FragColor = vec4(col * lit + uBg.rgb * uBg.a * (1.0 - lit), lit + uBg.a * (1.0 - lit));
|
|
122
|
+
}
|
|
123
|
+
`;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Hermite smoothing between two edges (GLSL's `smoothstep`).
|
|
127
|
+
*
|
|
128
|
+
* @param {number} e0 Where it starts rising.
|
|
129
|
+
* @param {number} e1 Where it reaches 1.
|
|
130
|
+
* @param {number} x The value.
|
|
131
|
+
* @returns {number} 0–1.
|
|
132
|
+
*/
|
|
133
|
+
const smooth = (e0, e1, x) => {
|
|
134
|
+
const t = Math.min(1, Math.max(0, (x - e0) / (e1 - e0)));
|
|
135
|
+
return t * t * (3 - 2 * t);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* A tile's way and turn at a time (the shader's `tileState`).
|
|
140
|
+
*
|
|
141
|
+
* @param {TruchetFieldConfig} config The settings (`period`, `turn`, `seed`).
|
|
142
|
+
* @param {number} col The tile's column.
|
|
143
|
+
* @param {number} row The tile's row.
|
|
144
|
+
* @param {number} [time=0] Seconds (already at its speed).
|
|
145
|
+
* @returns {{ way: 0 | 1, turning: number }} Its way (which corners its arcs hug) and how far through a quarter turn to it, 0 when still.
|
|
146
|
+
*/
|
|
147
|
+
export function truchetTile(config, col, row, time = 0) {
|
|
148
|
+
const period = config.period ?? TRUCHET_DEFAULTS.period;
|
|
149
|
+
const turn = config.turn ?? TRUCHET_DEFAULTS.turn;
|
|
150
|
+
const seed = config.seed ?? TRUCHET_DEFAULTS.seed;
|
|
151
|
+
const h = hash33(col, row, seed);
|
|
152
|
+
const rate = period * (0.6 + 0.8 * h[0]);
|
|
153
|
+
const clock = time / rate + h[1] * 7;
|
|
154
|
+
const tick = Math.floor(clock);
|
|
155
|
+
const way = (/** @type {number} */ n) => (hash33(col + n * 0.618, row + n * 0.618, seed + 1)[0] >= 0.5 ? 1 : 0);
|
|
156
|
+
const now = way(tick);
|
|
157
|
+
const before = way(tick - 1);
|
|
158
|
+
const into = ((clock - tick) * rate) / Math.max(turn, 0.001);
|
|
159
|
+
const mid = into < 1 && now !== before;
|
|
160
|
+
return { way: /** @type {0 | 1} */ (mid ? before : now), turning: mid ? into : 0 };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* How bright a tile's lines are (the shader's tone), before the quiet area.
|
|
165
|
+
*
|
|
166
|
+
* @param {TruchetFieldConfig} config The settings (`scale`, `drift`, `seed`).
|
|
167
|
+
* @param {number} x The tile's centre, widths from the left.
|
|
168
|
+
* @param {number} y The tile's centre, widths from the top.
|
|
169
|
+
* @param {number} [time=0] Seconds (already at its speed).
|
|
170
|
+
* @returns {number} 0–1.
|
|
171
|
+
*/
|
|
172
|
+
export function truchetTone(config, x, y, time = 0) {
|
|
173
|
+
const o = { ...TRUCHET_DEFAULTS, ...config };
|
|
174
|
+
return smooth(0.32, 0.72, gnoise(x * o.scale + o.seed * 3.1 + o.drift[0] * time, y * o.scale + o.seed * 1.7 + o.drift[1] * time));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The two colours as the shader takes them.
|
|
179
|
+
*
|
|
180
|
+
* @param {TruchetFieldConfig} config The settings.
|
|
181
|
+
* @returns {[string, string]} The dim colour and the bright one.
|
|
182
|
+
*/
|
|
183
|
+
export function truchetColors(config) {
|
|
184
|
+
const colors = pickColors(config, GREYS);
|
|
185
|
+
return [colors[0], colors[colors.length - 1]];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* A CSS `background` that stands in for the field before the shader starts
|
|
190
|
+
* and without WebGL: every tile one way (the arcs then run as rows of
|
|
191
|
+
* S-bends), in the colour halfway from dim to bright, and the quiet area
|
|
192
|
+
* left to the ground.
|
|
193
|
+
*
|
|
194
|
+
* @param {TruchetFieldConfig} config The settings (`colors`/`palette`, `background`, `tile`, `width`, `quiet`).
|
|
195
|
+
* @returns {string} The CSS background.
|
|
196
|
+
*/
|
|
197
|
+
export function truchetStill(config) {
|
|
198
|
+
const [dim, bright] = truchetColors(config).map(parseColor);
|
|
199
|
+
const ground = config.background ?? '#000000';
|
|
200
|
+
const tile = Math.max(4, config.tile ?? TRUCHET_DEFAULTS.tile);
|
|
201
|
+
const w = Math.max(1, ((config.width ?? TRUCHET_DEFAULTS.width) * tile) / 2);
|
|
202
|
+
const r = tile / 2;
|
|
203
|
+
const ink = toHex(mixOklab(dim, bright, 0.5));
|
|
204
|
+
const ring = (/** @type {string} */ at) =>
|
|
205
|
+
`radial-gradient(circle at ${at}, transparent ${(r - w).toFixed(1)}px, ${ink} ${(r - w).toFixed(1)}px ${(r + w).toFixed(1)}px, transparent ${(r + w).toFixed(1)}px) 0 0 / ${tile}px ${tile}px`;
|
|
206
|
+
const pc = (/** @type {number} */ n) => `${Math.round(n * 100)}%`;
|
|
207
|
+
const quiet = config.quiet;
|
|
208
|
+
const qr = pc((quiet?.radius ?? 0.8) * 0.55);
|
|
209
|
+
return [
|
|
210
|
+
quiet ? `radial-gradient(${qr} ${qr} at ${pc(quiet.x ?? 0.5)} ${pc(quiet.y ?? 0.5)}, ${ground} 55%, transparent 100%)` : '',
|
|
211
|
+
ring('0 0'),
|
|
212
|
+
ring('100% 100%'),
|
|
213
|
+
ground,
|
|
214
|
+
]
|
|
215
|
+
.filter(Boolean)
|
|
216
|
+
.join(', ');
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Starts the field on an element: a canvas inside it (its first), at the
|
|
221
|
+
* stage's capped pixel ratio.
|
|
222
|
+
*
|
|
223
|
+
* @param {HTMLElement} el The element.
|
|
224
|
+
* @param {TruchetFieldConfig} config The settings.
|
|
225
|
+
* @returns {() => void} Stops it.
|
|
226
|
+
*/
|
|
227
|
+
export function truchetField(el, config) {
|
|
228
|
+
const o = { ...TRUCHET_DEFAULTS, ...config };
|
|
229
|
+
const speed = config.speed ?? 1;
|
|
230
|
+
const lab = (/** @type {string} */ c) => toOklab(parseColor(c));
|
|
231
|
+
let [dim, bright] = truchetColors(config).map(lab);
|
|
232
|
+
const bg = config.background ? [...parseColor(config.background), 1] : [0, 0, 0, 0];
|
|
233
|
+
const start = performance.now();
|
|
234
|
+
|
|
235
|
+
const { stage, stop } = createStage(el, {
|
|
236
|
+
fragment: FRAG,
|
|
237
|
+
still: speed <= 0,
|
|
238
|
+
setup({ gl, uniform }) {
|
|
239
|
+
gl.uniform1f(uniform('uWidth'), o.width);
|
|
240
|
+
gl.uniform1f(uniform('uThin'), o.thin);
|
|
241
|
+
gl.uniform1f(uniform('uTube'), o.tube ? 1 : 0);
|
|
242
|
+
gl.uniform1f(uniform('uPeriod'), Math.max(0.1, o.period));
|
|
243
|
+
gl.uniform1f(uniform('uTurn'), o.turn);
|
|
244
|
+
gl.uniform1f(uniform('uScale'), o.scale);
|
|
245
|
+
gl.uniform2f(uniform('uDrift'), o.drift[0], o.drift[1]);
|
|
246
|
+
gl.uniform1f(uniform('uSeed'), o.seed);
|
|
247
|
+
gl.uniform4f(uniform('uBg'), bg[0], bg[1], bg[2], bg[3]);
|
|
248
|
+
},
|
|
249
|
+
resize({ gl, uniform, ratio }) {
|
|
250
|
+
const w = Math.max(1, el.clientWidth);
|
|
251
|
+
const h = Math.max(1, el.clientHeight);
|
|
252
|
+
gl.uniform2f(uniform('uSize'), w, h);
|
|
253
|
+
gl.uniform1f(uniform('uTile'), Math.max(4, o.tile) * ratio);
|
|
254
|
+
gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, w / h));
|
|
255
|
+
},
|
|
256
|
+
uniforms({ gl, uniform }, now) {
|
|
257
|
+
gl.uniform1f(uniform('uTime'), ((now - start) / 1000) * Math.max(0, speed));
|
|
258
|
+
gl.uniform3f(uniform('uDim'), dim[0], dim[1], dim[2]);
|
|
259
|
+
gl.uniform3f(uniform('uBright'), bright[0], bright[1], bright[2]);
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
if (!stage) return stop;
|
|
263
|
+
|
|
264
|
+
const unwatch = watchColors(el, (next) => {
|
|
265
|
+
[dim, bright] = truchetColors({ colors: next.colors }).map(lab);
|
|
266
|
+
if (stage.still) stage.redraw();
|
|
267
|
+
});
|
|
268
|
+
return () => {
|
|
269
|
+
stop();
|
|
270
|
+
unwatch();
|
|
271
|
+
};
|
|
272
|
+
}
|