@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,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
|
+
}
|