@arraypress/effects-astro 0.3.0 → 0.5.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 +6 -2
- package/COMPONENTS.md +80 -1
- package/README.md +9 -4
- package/package.json +5 -2
- package/src/components/BorderSweep.astro +9 -3
- package/src/components/Galaxy.astro +40 -0
- package/src/components/GlitchText.astro +69 -0
- package/src/components/PixelGlitchText.astro +2 -2
- package/src/components/Scene.astro +89 -0
- package/src/index.d.ts +6 -2
- package/src/index.js +4 -2
- package/src/runtime/galaxy.d.ts +37 -0
- package/src/runtime/galaxy.js +311 -0
- package/src/runtime/pixel-glitch.d.ts +8 -1
- package/src/runtime/pixel-glitch.js +71 -12
- package/src/runtime/scene.d.ts +58 -0
- package/src/runtime/scene.js +80 -0
- package/src/styles/border-sweep.css +28 -1
- package/src/styles/glitch-text.css +169 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/galaxy
|
|
3
|
+
*
|
|
4
|
+
* Deep space in a palette's colours: soft nebula clouds with darker dust
|
|
5
|
+
* lanes through them, fine glitter that shimmers as a whole, and stars
|
|
6
|
+
* that twinkle each on its own beat (the brightest with a little cross of
|
|
7
|
+
* light), all drifting slowly.
|
|
8
|
+
*
|
|
9
|
+
* The nebula is worked out once, small (400 × 225), from tileable value
|
|
10
|
+
* noise: five octaves for the clouds, three ridged ones for the dust, and a
|
|
11
|
+
* broad field that shifts the colours along the ramp so they wander. Each
|
|
12
|
+
* frame draws it scaled up to cover (it is soft anyway) at a slowly
|
|
13
|
+
* drifting offset, the glitter over it at full resolution, then the stars
|
|
14
|
+
* from one pre-drawn sprite per tint. The ramp runs from near-black
|
|
15
|
+
* through the palette to a pale highlight, blended in Oklab; new colours
|
|
16
|
+
* (`setColors`) only remap the worked-out field.
|
|
17
|
+
*
|
|
18
|
+
* Canvas 2D, opaque. One still frame with prefers-reduced-motion.
|
|
19
|
+
*
|
|
20
|
+
* From the Logomarks studio's showcase.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { createCanvas2D } from './canvas2d.js';
|
|
24
|
+
import { mixOklab, parseColor } from './color.js';
|
|
25
|
+
import { watchColors } from './mount.js';
|
|
26
|
+
import { pickColors } from './palettes.js';
|
|
27
|
+
import { seededRandom } from './random.js';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {object} GalaxyConfig
|
|
31
|
+
* @property {string[]} [colors] The nebula's colours, deep to bright, blended in Oklab.
|
|
32
|
+
* @property {string} [palette] A shared palette by name, when there are no `colors`.
|
|
33
|
+
* @property {number} [speed=1] How fast it drifts and twinkles; 0 holds it still.
|
|
34
|
+
* @property {number} [stars=1] How many stars, as a share of the usual.
|
|
35
|
+
* @property {number} [glitter=1] How bright the glitter is, as a share of the usual.
|
|
36
|
+
* @property {number} [seed=7] Which sky: the same seed draws the same nebula and stars.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/** The colours without `colors` or `palette`: indigo, cornflower, violet and cyan. */
|
|
40
|
+
export const COLORS = ['#3a2bd9', '#5c7cff', '#b45cff', '#5cecff'];
|
|
41
|
+
|
|
42
|
+
/** The worked-out nebula's size, px: small, since it is drawn scaled up and soft. */
|
|
43
|
+
export const NEBULA_WIDTH = 400;
|
|
44
|
+
export const NEBULA_HEIGHT = 225;
|
|
45
|
+
|
|
46
|
+
/** The ramp's top: a pale, faintly violet white. */
|
|
47
|
+
const PALE = /** @type {import('./color.js').Rgb} */ ([245 / 255, 240 / 255, 1]);
|
|
48
|
+
|
|
49
|
+
/** Star tints besides the palette's own: white, a violet white and a blue white. */
|
|
50
|
+
const WHITES = ['#ffffff', '#f2e9ff', '#e6f6ff'];
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Tileable value noise on an n × n lattice of random values, eased
|
|
54
|
+
* between (smoothstep), wrapping at 0–1 in both directions.
|
|
55
|
+
*
|
|
56
|
+
* @param {number} n The lattice's size, cells a side.
|
|
57
|
+
* @param {() => number} rnd Random values, 0–1.
|
|
58
|
+
* @returns {(x: number, y: number) => number} The noise at (x, y), 0–1, for x and y in 0–1.
|
|
59
|
+
*/
|
|
60
|
+
function lattice(n, rnd) {
|
|
61
|
+
const v = Float32Array.from({ length: n * n }, () => rnd());
|
|
62
|
+
/**
|
|
63
|
+
* A lattice value, wrapped.
|
|
64
|
+
*
|
|
65
|
+
* @param {number} i Across.
|
|
66
|
+
* @param {number} j Down.
|
|
67
|
+
* @returns {number} The value there.
|
|
68
|
+
*/
|
|
69
|
+
const at = (i, j) => v[(((j % n) + n) % n) * n + (((i % n) + n) % n)];
|
|
70
|
+
return (x, y) => {
|
|
71
|
+
const fx = x * n;
|
|
72
|
+
const fy = y * n;
|
|
73
|
+
const x0 = Math.floor(fx);
|
|
74
|
+
const y0 = Math.floor(fy);
|
|
75
|
+
const tx = fx - x0;
|
|
76
|
+
const ty = fy - y0;
|
|
77
|
+
const sx = tx * tx * (3 - 2 * tx);
|
|
78
|
+
const sy = ty * ty * (3 - 2 * ty);
|
|
79
|
+
const a = at(x0, y0) + (at(x0 + 1, y0) - at(x0, y0)) * sx;
|
|
80
|
+
const b = at(x0, y0 + 1) + (at(x0 + 1, y0 + 1) - at(x0, y0 + 1)) * sx;
|
|
81
|
+
return a + (b - a) * sy;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The nebula as places on the colour ramp: clouds of layered noise, cut
|
|
87
|
+
* by ridged dust lanes, nudged along the ramp by a broad second field so
|
|
88
|
+
* the colours wander. Tileable.
|
|
89
|
+
*
|
|
90
|
+
* @param {number} width Across, px.
|
|
91
|
+
* @param {number} height Down, px.
|
|
92
|
+
* @param {number} [seed=7] The same seed gives the same nebula.
|
|
93
|
+
* @returns {Float32Array} One value a pixel, 0 (dark) to 1 (the pale top), row by row.
|
|
94
|
+
*/
|
|
95
|
+
export function nebulaField(width, height, seed = 7) {
|
|
96
|
+
const rnd = seededRandom(seed);
|
|
97
|
+
const octaves = [3, 6, 12, 24, 48].map((n) => lattice(n, rnd));
|
|
98
|
+
const dust = [5, 10, 20].map((n) => lattice(n, rnd));
|
|
99
|
+
const hue = lattice(2, rnd);
|
|
100
|
+
const out = new Float32Array(width * height);
|
|
101
|
+
for (let y = 0; y < height; y++) {
|
|
102
|
+
for (let x = 0; x < width; x++) {
|
|
103
|
+
const u = x / width;
|
|
104
|
+
const v = y / height;
|
|
105
|
+
let n = 0;
|
|
106
|
+
let amp = 0.5;
|
|
107
|
+
for (const o of octaves) {
|
|
108
|
+
n += o(u, v) * amp;
|
|
109
|
+
amp *= 0.5;
|
|
110
|
+
}
|
|
111
|
+
n /= 0.97;
|
|
112
|
+
// Ridged dust lanes: dark filaments through the clouds.
|
|
113
|
+
let r = 0;
|
|
114
|
+
for (const o of dust) r += 1 - Math.abs(o(u, v) * 2 - 1);
|
|
115
|
+
r /= dust.length;
|
|
116
|
+
let t = Math.min(1, Math.max(0, (n - 0.3) * 2.3));
|
|
117
|
+
t = Math.max(0, t * (1 - Math.max(0, r - 0.72) * 2.2));
|
|
118
|
+
out[y * width + x] = Math.min(1, Math.max(0, t * 0.8 + (hue(u, v) - 0.5) * 0.6));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The colour ramp the nebula is painted along: near-black (the first
|
|
126
|
+
* colour, darkened), the colours in order, then a pale highlight, each
|
|
127
|
+
* stretch blended in Oklab.
|
|
128
|
+
*
|
|
129
|
+
* @param {string[]} colors The colours, as CSS hex.
|
|
130
|
+
* @param {number} [steps=256] How many entries.
|
|
131
|
+
* @returns {Uint8ClampedArray} `steps` RGB triples, 0–255.
|
|
132
|
+
* @throws {TypeError} When a colour is not hex.
|
|
133
|
+
*/
|
|
134
|
+
export function galaxyRamp(colors, steps = 256) {
|
|
135
|
+
const cs = (colors.length ? colors : COLORS).map(parseColor);
|
|
136
|
+
const stops = [mixOklab(cs[0], [0, 0, 0], 0.62), ...cs, PALE];
|
|
137
|
+
const out = new Uint8ClampedArray(steps * 3);
|
|
138
|
+
for (let s = 0; s < steps; s++) {
|
|
139
|
+
const k = (s / (steps - 1)) * (stops.length - 1);
|
|
140
|
+
const i = Math.min(stops.length - 2, Math.floor(k));
|
|
141
|
+
const rgb = mixOklab(stops[i], stops[i + 1], k - i);
|
|
142
|
+
for (let ch = 0; ch < 3; ch++) out[s * 3 + ch] = Math.round(rgb[ch] * 255);
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Paints a nebula field along a ramp.
|
|
149
|
+
*
|
|
150
|
+
* @param {Float32Array} field From nebulaField.
|
|
151
|
+
* @param {Uint8ClampedArray} ramp From galaxyRamp.
|
|
152
|
+
* @returns {Uint8ClampedArray} RGBA, opaque, one pixel per value of the field.
|
|
153
|
+
*/
|
|
154
|
+
export function nebulaPixels(field, ramp) {
|
|
155
|
+
const last = ramp.length / 3 - 1;
|
|
156
|
+
const out = new Uint8ClampedArray(field.length * 4);
|
|
157
|
+
for (let p = 0; p < field.length; p++) {
|
|
158
|
+
const i = Math.round(field[p] * last) * 3;
|
|
159
|
+
out[p * 4] = ramp[i];
|
|
160
|
+
out[p * 4 + 1] = ramp[i + 1];
|
|
161
|
+
out[p * 4 + 2] = ramp[i + 2];
|
|
162
|
+
out[p * 4 + 3] = 255;
|
|
163
|
+
}
|
|
164
|
+
return out;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* A star's glow, drawn once: a radial gradient in the tint, solid at the
|
|
169
|
+
* centre and gone at the edge.
|
|
170
|
+
*
|
|
171
|
+
* @param {string} tint CSS hex.
|
|
172
|
+
* @returns {HTMLCanvasElement} 64 × 64.
|
|
173
|
+
*/
|
|
174
|
+
function starSprite(tint) {
|
|
175
|
+
const [r, g, b] = parseColor(tint).map((v) => Math.round(v * 255));
|
|
176
|
+
const sprite = document.createElement('canvas');
|
|
177
|
+
sprite.width = sprite.height = 64;
|
|
178
|
+
const s = /** @type {CanvasRenderingContext2D} */ (sprite.getContext('2d'));
|
|
179
|
+
const glow = s.createRadialGradient(32, 32, 0, 32, 32, 32);
|
|
180
|
+
glow.addColorStop(0, `rgb(${r} ${g} ${b})`);
|
|
181
|
+
glow.addColorStop(0.25, `rgb(${r} ${g} ${b} / 0.67)`);
|
|
182
|
+
glow.addColorStop(1, `rgb(${r} ${g} ${b} / 0)`);
|
|
183
|
+
s.fillStyle = glow;
|
|
184
|
+
s.fillRect(0, 0, 64, 64);
|
|
185
|
+
return sprite;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Starts the galaxy on an element (its first canvas is drawn into).
|
|
190
|
+
*
|
|
191
|
+
* @param {HTMLElement} el The element.
|
|
192
|
+
* @param {GalaxyConfig} config The settings.
|
|
193
|
+
* @returns {() => void} Stops it.
|
|
194
|
+
*/
|
|
195
|
+
export function galaxy(el, config) {
|
|
196
|
+
const SPEED = config.speed ?? 1;
|
|
197
|
+
const STARS = Math.max(0, config.stars ?? 1);
|
|
198
|
+
const GLITTER = Math.max(0, config.glitter ?? 1);
|
|
199
|
+
const SEED = config.seed ?? 7;
|
|
200
|
+
let colors = pickColors(config, COLORS);
|
|
201
|
+
|
|
202
|
+
const field = nebulaField(NEBULA_WIDTH, NEBULA_HEIGHT, SEED);
|
|
203
|
+
const nebula = document.createElement('canvas');
|
|
204
|
+
nebula.width = NEBULA_WIDTH;
|
|
205
|
+
nebula.height = NEBULA_HEIGHT;
|
|
206
|
+
const nctx = nebula.getContext('2d');
|
|
207
|
+
/** @type {HTMLCanvasElement[]} */
|
|
208
|
+
let sprites = [];
|
|
209
|
+
|
|
210
|
+
/** Paints the nebula and the star sprites in the current colours. */
|
|
211
|
+
const paint = () => {
|
|
212
|
+
nctx?.putImageData(new ImageData(nebulaPixels(field, galaxyRamp(colors)), NEBULA_WIDTH, NEBULA_HEIGHT), 0, 0);
|
|
213
|
+
sprites = [...WHITES, ...colors].map(starSprite);
|
|
214
|
+
};
|
|
215
|
+
paint();
|
|
216
|
+
|
|
217
|
+
/** @type {HTMLCanvasElement | null} */
|
|
218
|
+
let glitter = null;
|
|
219
|
+
/** @type {{ x: number, y: number, r: number, phase: number, rate: number, cross: boolean, tint: number }[]} */
|
|
220
|
+
let stars = [];
|
|
221
|
+
let dpr = 1;
|
|
222
|
+
const start = performance.now();
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Scatters the glitter and stars over the new size, the same way for
|
|
226
|
+
* the same seed and size.
|
|
227
|
+
*
|
|
228
|
+
* @param {import('./canvas2d.js').Canvas2D} c The canvas.
|
|
229
|
+
*/
|
|
230
|
+
const resize = (c) => {
|
|
231
|
+
dpr = c.canvas.width / c.width;
|
|
232
|
+
const rnd = seededRandom(SEED * 31 + 1);
|
|
233
|
+
// Glitter: fine bright speckle at device resolution.
|
|
234
|
+
glitter = document.createElement('canvas');
|
|
235
|
+
glitter.width = c.canvas.width;
|
|
236
|
+
glitter.height = c.canvas.height;
|
|
237
|
+
const g = glitter.getContext('2d');
|
|
238
|
+
if (g) {
|
|
239
|
+
for (let k = 0; k < (glitter.width * glitter.height) / 70; k++) {
|
|
240
|
+
const x = rnd() * glitter.width;
|
|
241
|
+
const y = rnd() * glitter.height;
|
|
242
|
+
g.fillStyle = `rgb(255 255 255 / ${0.2 + rnd() ** 2.5 * 0.8})`;
|
|
243
|
+
const size = rnd() < 0.9 ? dpr : dpr * 1.6;
|
|
244
|
+
g.fillRect(x, y, size, size);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
stars = Array.from({ length: Math.round(((c.width * c.height) / 9000) * STARS) }, () => ({
|
|
248
|
+
x: rnd() * c.width,
|
|
249
|
+
y: rnd() * c.height,
|
|
250
|
+
r: 0.6 + rnd() ** 4 * 3.4,
|
|
251
|
+
phase: rnd() * Math.PI * 2,
|
|
252
|
+
rate: 0.6 + rnd() * 1.8,
|
|
253
|
+
cross: rnd() < 0.12,
|
|
254
|
+
tint: Math.floor(rnd() * 64),
|
|
255
|
+
}));
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* One frame: the nebula drifting, the glitter shimmering over it, the
|
|
260
|
+
* stars twinkling.
|
|
261
|
+
*
|
|
262
|
+
* @param {import('./canvas2d.js').Canvas2D} c The canvas.
|
|
263
|
+
* @param {number} now The time, ms.
|
|
264
|
+
*/
|
|
265
|
+
const draw = (c, now) => {
|
|
266
|
+
const { ctx, width: w, height: h } = c;
|
|
267
|
+
const t = c.still ? 0 : ((now - start) / 1000) * SPEED;
|
|
268
|
+
// The nebula, scaled to cover with a margin to drift through.
|
|
269
|
+
const k = Math.max(w / NEBULA_WIDTH, h / NEBULA_HEIGHT) * 1.12;
|
|
270
|
+
const nw = NEBULA_WIDTH * k;
|
|
271
|
+
const nh = NEBULA_HEIGHT * k;
|
|
272
|
+
const ox = -(nw - w) / 2 + Math.sin(t * 0.05) * (nw - w) * 0.4;
|
|
273
|
+
const oy = -(nh - h) / 2 + Math.cos(t * 0.04) * (nh - h) * 0.4;
|
|
274
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
275
|
+
ctx.globalAlpha = 1;
|
|
276
|
+
ctx.imageSmoothingEnabled = true;
|
|
277
|
+
ctx.imageSmoothingQuality = 'high';
|
|
278
|
+
ctx.drawImage(nebula, ox, oy, nw, nh);
|
|
279
|
+
ctx.globalCompositeOperation = 'lighter';
|
|
280
|
+
if (glitter && GLITTER > 0) {
|
|
281
|
+
ctx.globalAlpha = Math.min(1, (0.75 + Math.sin(t * 1.7) * 0.12) * GLITTER);
|
|
282
|
+
ctx.drawImage(glitter, 0, 0, w, h);
|
|
283
|
+
}
|
|
284
|
+
const hair = 1 / dpr;
|
|
285
|
+
for (const s of stars) {
|
|
286
|
+
const a = 0.35 + 0.65 * (0.5 + 0.5 * Math.sin(s.phase + t * s.rate));
|
|
287
|
+
ctx.globalAlpha = a;
|
|
288
|
+
ctx.drawImage(sprites[s.tint % sprites.length], s.x - s.r * 4, s.y - s.r * 4, s.r * 8, s.r * 8);
|
|
289
|
+
if (s.cross) {
|
|
290
|
+
ctx.fillStyle = '#ffffff';
|
|
291
|
+
const arm = s.r * 9 * a;
|
|
292
|
+
ctx.fillRect(s.x - arm, s.y - hair / 2, arm * 2, hair);
|
|
293
|
+
ctx.fillRect(s.x - hair / 2, s.y - arm, hair, arm * 2);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
ctx.globalAlpha = 1;
|
|
297
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const { c, stop } = createCanvas2D(el, { draw, resize });
|
|
301
|
+
if (!c) return stop;
|
|
302
|
+
const unwatch = watchColors(el, (next) => {
|
|
303
|
+
colors = next.colors;
|
|
304
|
+
paint();
|
|
305
|
+
if (c.still) draw(c, performance.now());
|
|
306
|
+
});
|
|
307
|
+
return () => {
|
|
308
|
+
stop();
|
|
309
|
+
unwatch();
|
|
310
|
+
};
|
|
311
|
+
}
|
|
@@ -9,9 +9,16 @@ export interface PixelGlitchConfig {
|
|
|
9
9
|
block?: number;
|
|
10
10
|
/** How far the hottest rows slip sideways, CSS px. Default 16. */
|
|
11
11
|
shift?: number;
|
|
12
|
-
/** With a width (CSS px), the text at rest is an outline that the pixels fill. Default 0 (solid). */
|
|
12
|
+
/** With a width (CSS px), the text at rest is an outline that the pixels fill: one outline round the word, so overlapping letters merge. Default 0 (solid). */
|
|
13
13
|
stroke?: number;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* The outline of a shape from its RGBA coverage: each pixel's alpha less the
|
|
18
|
+
* smallest within `radius` px of it, so the line runs inside the edge and
|
|
19
|
+
* touching shapes share one outline. One alpha byte a pixel.
|
|
20
|
+
*/
|
|
21
|
+
export function outlineAlpha(rgba: Uint8ClampedArray, width: number, height: number, radius: number): Uint8ClampedArray;
|
|
22
|
+
|
|
16
23
|
/** Starts the pixel glitch on an element of text. Returns a function that stops it. */
|
|
17
24
|
export function pixelGlitch(el: HTMLElement, config: PixelGlitchConfig): () => void;
|
|
@@ -10,7 +10,10 @@
|
|
|
10
10
|
*
|
|
11
11
|
* With `stroke` set, the text at rest is a hollow outline of that width and
|
|
12
12
|
* the pixels it breaks into fill the letters solid, so the pointer lights
|
|
13
|
-
* the word up as it passes.
|
|
13
|
+
* the word up as it passes. The outline is one shape for the whole word
|
|
14
|
+
* (the solid letters less themselves eroded by the width), so letters that
|
|
15
|
+
* touch or overlap under tight tracking merge rather than each outline
|
|
16
|
+
* crossing the next.
|
|
14
17
|
*
|
|
15
18
|
* The element keeps its text (it only turns transparent once the canvas
|
|
16
19
|
* has drawn), so fit-text, selection and the no-JS look are unchanged.
|
|
@@ -21,8 +24,11 @@
|
|
|
21
24
|
* `pointer-events: none` (a big wordmark's line box often overlaps links).
|
|
22
25
|
* Animates only while some cell is hot, and only while the text is on
|
|
23
26
|
* screen. Off with prefers-reduced-motion and without a hovering pointer.
|
|
27
|
+
* Repainted when the text's own face has loaded (a face used only here can
|
|
28
|
+
* arrive after `document.fonts.ready`).
|
|
24
29
|
*
|
|
25
|
-
* From the SampleGrid (WaveGrid) theme's footer
|
|
30
|
+
* From the SampleGrid (WaveGrid) theme's footer; the merged outline and the
|
|
31
|
+
* font reload from the Logomarks site's.
|
|
26
32
|
*/
|
|
27
33
|
|
|
28
34
|
import { canHover, prefersReducedMotion } from './device.js';
|
|
@@ -38,6 +44,44 @@ import { textNodes } from './text.js';
|
|
|
38
44
|
* @property {number} [stroke=0] With a width (CSS px), the text at rest is an outline that the pixels fill.
|
|
39
45
|
*/
|
|
40
46
|
|
|
47
|
+
/** The 16 directions the outline's erosion looks in, as unit vectors. */
|
|
48
|
+
const AROUND = Array.from({ length: 16 }, (_, k) => [Math.cos((k / 16) * Math.PI * 2), Math.sin((k / 16) * Math.PI * 2)]);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The outline of a shape, from its coverage: each pixel's alpha less the
|
|
52
|
+
* smallest alpha within `radius` of it (looked for in 16 directions), so
|
|
53
|
+
* the line runs `radius` px inside the shape's edge and stays smooth where
|
|
54
|
+
* the edge is antialiased. Shapes that touch or overlap give one outline
|
|
55
|
+
* round the lot.
|
|
56
|
+
*
|
|
57
|
+
* @param {Uint8ClampedArray} rgba The shape, RGBA (only alpha is read).
|
|
58
|
+
* @param {number} width Its width, px.
|
|
59
|
+
* @param {number} height Its height, px.
|
|
60
|
+
* @param {number} radius The outline's width, px (rounded, at least 1).
|
|
61
|
+
* @returns {Uint8ClampedArray} The outline's alpha, one byte a pixel.
|
|
62
|
+
*/
|
|
63
|
+
export function outlineAlpha(rgba, width, height, radius) {
|
|
64
|
+
const r = Math.max(1, Math.round(radius));
|
|
65
|
+
const steps = AROUND.map(([x, y]) => [Math.round(x * r), Math.round(y * r)]);
|
|
66
|
+
const out = new Uint8ClampedArray(width * height);
|
|
67
|
+
for (let y = 0; y < height; y++) {
|
|
68
|
+
for (let x = 0; x < width; x++) {
|
|
69
|
+
const v = rgba[(y * width + x) * 4 + 3];
|
|
70
|
+
if (!v) continue;
|
|
71
|
+
let low = v;
|
|
72
|
+
for (const [dx, dy] of steps) {
|
|
73
|
+
const nx = x + dx;
|
|
74
|
+
const ny = y + dy;
|
|
75
|
+
const n = nx < 0 || ny < 0 || nx >= width || ny >= height ? 0 : rgba[(ny * width + nx) * 4 + 3];
|
|
76
|
+
if (n < low) low = n;
|
|
77
|
+
if (!low) break;
|
|
78
|
+
}
|
|
79
|
+
out[y * width + x] = v - low;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
|
|
41
85
|
/**
|
|
42
86
|
* Starts the pixel glitch on an element of text.
|
|
43
87
|
*
|
|
@@ -152,8 +196,8 @@ export function pixelGlitch(el, config) {
|
|
|
152
196
|
|
|
153
197
|
/**
|
|
154
198
|
* Draws the text at rest into `crisp`, each glyph where the browser put
|
|
155
|
-
* it: solid, or with `stroke`
|
|
156
|
-
*
|
|
199
|
+
* it: solid, or with `stroke` one outline round the whole word (see
|
|
200
|
+
* outlineAlpha), so overlapping letters do not show their crossings.
|
|
157
201
|
*/
|
|
158
202
|
const paint = () => {
|
|
159
203
|
const texts = textNodes(el);
|
|
@@ -182,8 +226,6 @@ export function pixelGlitch(el, config) {
|
|
|
182
226
|
c2.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
183
227
|
c2.clearRect(0, 0, g.w, g.h);
|
|
184
228
|
c2.fillStyle = g.ink;
|
|
185
|
-
c2.strokeStyle = g.ink;
|
|
186
|
-
c2.lineJoin = 'round';
|
|
187
229
|
c2.font = `${cs.fontStyle} ${cs.fontWeight} ${cs.fontSize} ${cs.fontFamily}`;
|
|
188
230
|
c2.textBaseline = 'alphabetic';
|
|
189
231
|
const range = document.createRange();
|
|
@@ -209,12 +251,16 @@ export function pixelGlitch(el, config) {
|
|
|
209
251
|
eachGlyph((ch, x, y) => c2.fillText(ch, x, y));
|
|
210
252
|
g.alpha = c2.getImageData(0, 0, crisp.width, crisp.height).data;
|
|
211
253
|
if (STROKE > 0) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
254
|
+
// The pixels still sample the solid letters (g.alpha); what shows at rest is their outline, in the ink.
|
|
255
|
+
const line = outlineAlpha(g.alpha, crisp.width, crisp.height, STROKE * dpr);
|
|
256
|
+
const img = c2.createImageData(crisp.width, crisp.height);
|
|
257
|
+
for (let i = 0; i < line.length; i++) img.data[i * 4 + 3] = line[i];
|
|
258
|
+
c2.putImageData(img, 0, 0);
|
|
259
|
+
c2.setTransform(1, 0, 0, 1, 0, 0);
|
|
260
|
+
c2.globalCompositeOperation = 'source-in';
|
|
261
|
+
c2.fillRect(0, 0, crisp.width, crisp.height);
|
|
217
262
|
c2.globalCompositeOperation = 'source-over';
|
|
263
|
+
c2.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
218
264
|
}
|
|
219
265
|
g.cols = Math.ceil(g.w / CELL);
|
|
220
266
|
g.rows = Math.ceil(g.h / CELL);
|
|
@@ -274,10 +320,23 @@ export function pixelGlitch(el, config) {
|
|
|
274
320
|
g.visible = entry.isIntersecting;
|
|
275
321
|
});
|
|
276
322
|
watcher.observe(el);
|
|
277
|
-
|
|
323
|
+
// Repaint once the fonts are in, and once the text's own face is (it may load after fonts.ready).
|
|
324
|
+
let stopped = false;
|
|
325
|
+
/** Repaints, unless stopped meanwhile. */
|
|
326
|
+
const repaint = () => {
|
|
327
|
+
if (!stopped) paint();
|
|
328
|
+
};
|
|
329
|
+
document.fonts?.ready.then(repaint).catch(() => {});
|
|
330
|
+
const face = getComputedStyle(el);
|
|
331
|
+
document.fonts
|
|
332
|
+
?.load(`${face.fontStyle} ${face.fontWeight} ${face.fontSize} ${face.fontFamily}`, el.textContent ?? '')
|
|
333
|
+
.then(repaint)
|
|
334
|
+
.catch(() => {});
|
|
278
335
|
paint();
|
|
279
336
|
|
|
280
337
|
return () => {
|
|
338
|
+
stopped = true;
|
|
339
|
+
cancelAnimationFrame(pending);
|
|
281
340
|
document.removeEventListener('pointermove', move);
|
|
282
341
|
resizer.disconnect();
|
|
283
342
|
watcher.disconnect();
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { SceneOptions } from '@arraypress/shaders';
|
|
2
|
+
import type { PaletteName } from './palettes.js';
|
|
3
|
+
|
|
4
|
+
/** A scene's name: one of `@arraypress/shaders`' scenes. */
|
|
5
|
+
export type SceneName =
|
|
6
|
+
| 'marble'
|
|
7
|
+
| 'frosted'
|
|
8
|
+
| 'ribs'
|
|
9
|
+
| 'tiles'
|
|
10
|
+
| 'blob'
|
|
11
|
+
| 'chrome'
|
|
12
|
+
| 'glitter'
|
|
13
|
+
| 'foil'
|
|
14
|
+
| 'ribbon'
|
|
15
|
+
| 'spiral'
|
|
16
|
+
| 'swirl'
|
|
17
|
+
| 'arches'
|
|
18
|
+
| 'spheres'
|
|
19
|
+
| 'spheres2'
|
|
20
|
+
| 'strands'
|
|
21
|
+
| 'rings'
|
|
22
|
+
| 'ink-water'
|
|
23
|
+
| 'topo'
|
|
24
|
+
| 'oil-bubbles'
|
|
25
|
+
| 'melting-hologram';
|
|
26
|
+
|
|
27
|
+
/** A `<Scene>`'s settings, as its element carries them. */
|
|
28
|
+
export interface SceneConfig {
|
|
29
|
+
/** The scene. */
|
|
30
|
+
name: SceneName;
|
|
31
|
+
/** A named palette (PALETTES). */
|
|
32
|
+
palette?: PaletteName | (string & {});
|
|
33
|
+
/** One to three colours (hex), over the palette's. */
|
|
34
|
+
colors?: string[];
|
|
35
|
+
/** The highlight colour (hex). */
|
|
36
|
+
glint?: string;
|
|
37
|
+
/** The scene's own settings by key. */
|
|
38
|
+
params?: Record<string, number>;
|
|
39
|
+
speed?: number;
|
|
40
|
+
hue?: number;
|
|
41
|
+
sat?: number;
|
|
42
|
+
exposure?: number;
|
|
43
|
+
tint?: string;
|
|
44
|
+
tintAmount?: number;
|
|
45
|
+
zoom?: number;
|
|
46
|
+
angle?: number;
|
|
47
|
+
grain?: number;
|
|
48
|
+
fps?: number;
|
|
49
|
+
scale?: number;
|
|
50
|
+
/** The component's `lazy`: hold a context only while near the screen (true, or a CSS length). Default true. */
|
|
51
|
+
near?: boolean | string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A `<Scene>`'s settings as `@arraypress/shaders`' options: the palette found by name, `near` as `lazy`. */
|
|
55
|
+
export function sceneSettings(config: SceneConfig): { name: SceneName; options: SceneOptions };
|
|
56
|
+
|
|
57
|
+
/** Starts a scene on an element's canvas; returns a function that stops it. */
|
|
58
|
+
export function scene(el: HTMLElement, config: SceneConfig): () => void;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/scene
|
|
3
|
+
*
|
|
4
|
+
* A scene from `@arraypress/shaders` on an effect's canvas: liquid marble,
|
|
5
|
+
* frosted glass, glossy tiles, chrome, glitter, holographic foil, ink in
|
|
6
|
+
* water and the rest, recoloured by a palette and shaped by the scene's own
|
|
7
|
+
* settings. `<Scene>` is the component.
|
|
8
|
+
*
|
|
9
|
+
* The scene is loaded by name, each in its own chunk, so a page ships only
|
|
10
|
+
* the shaders it shows. Its laziness is the package's own, not mount()'s:
|
|
11
|
+
* it holds a WebGL context only while near the screen and gives it back
|
|
12
|
+
* four seconds after it leaves, on a fresh canvas, and it caps its frame
|
|
13
|
+
* rate, adapts its resolution, pauses off screen and draws one still frame
|
|
14
|
+
* with reduced motion. `setColors` recolours it live.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { scene as run, LOOK } from '@arraypress/shaders/core';
|
|
18
|
+
import { load } from '@arraypress/shaders/load';
|
|
19
|
+
import { findPalette } from './palettes.js';
|
|
20
|
+
import { watchColors } from './mount.js';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {import('./scene.js').SceneConfig} SceneConfig
|
|
24
|
+
* @typedef {import('@arraypress/shaders').SceneOptions} SceneOptions
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** The settings a `<Scene>` hands to its scene as they are: the look, the colours, the params, the frame rate and resolution. */
|
|
28
|
+
const OPTIONS = /** @type {(keyof SceneConfig)[]} */ ([...Object.keys(LOOK), 'colors', 'glint', 'params', 'fps', 'scale']);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A `<Scene>`'s settings as the package's options: its palette found by
|
|
32
|
+
* name (an unknown one is warned about and the scene's own colours kept),
|
|
33
|
+
* `near` as `lazy`, and the rest as they are.
|
|
34
|
+
*
|
|
35
|
+
* @param {SceneConfig} config The component's settings.
|
|
36
|
+
* @returns {{ name: SceneConfig['name'], options: SceneOptions }} The scene and its options.
|
|
37
|
+
*/
|
|
38
|
+
export function sceneSettings(config) {
|
|
39
|
+
const { name, palette, near = true } = config;
|
|
40
|
+
// Only the scene's own options: anything else a component is handed (Astro's `slot`, say) is not the scene's.
|
|
41
|
+
/** @type {Record<string, unknown>} */
|
|
42
|
+
const options = Object.fromEntries(OPTIONS.filter((k) => config[k] !== undefined).map((k) => [k, config[k]]));
|
|
43
|
+
if (palette) {
|
|
44
|
+
const found = findPalette(palette);
|
|
45
|
+
if (found) options.palette = found;
|
|
46
|
+
else console.warn('[effects-astro]', `No palette "${palette}"`);
|
|
47
|
+
}
|
|
48
|
+
options.lazy = near;
|
|
49
|
+
return { name, options: /** @type {SceneOptions} */ (options) };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Starts a scene on an element's canvas.
|
|
54
|
+
*
|
|
55
|
+
* @param {HTMLElement} el The element (its first canvas is drawn on).
|
|
56
|
+
* @param {SceneConfig} config The scene and its settings.
|
|
57
|
+
* @returns {() => void} Stops it.
|
|
58
|
+
*/
|
|
59
|
+
export function scene(el, config) {
|
|
60
|
+
const canvas = el.querySelector('canvas');
|
|
61
|
+
if (!canvas) return () => {};
|
|
62
|
+
const { name, options } = sceneSettings(config);
|
|
63
|
+
let stopped = false;
|
|
64
|
+
/** @type {import('@arraypress/shaders').SceneHandle | null} */
|
|
65
|
+
let handle = null;
|
|
66
|
+
let unwatch = () => {};
|
|
67
|
+
load(name)
|
|
68
|
+
.then((def) => {
|
|
69
|
+
if (stopped) return;
|
|
70
|
+
const h = run(canvas, def, options);
|
|
71
|
+
handle = h;
|
|
72
|
+
unwatch = watchColors(el, (next) => h.set({ colors: next.colors.slice(0, 3), ...(next.glint ? { glint: next.glint } : {}) }));
|
|
73
|
+
})
|
|
74
|
+
.catch((error) => console.warn('[effects-astro]', error?.message ?? error));
|
|
75
|
+
return () => {
|
|
76
|
+
stopped = true;
|
|
77
|
+
unwatch();
|
|
78
|
+
handle?.stop();
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -15,7 +15,13 @@
|
|
|
15
15
|
* a pill reads as a stall). Colour and speed from `--sweep-color` and
|
|
16
16
|
* `--sweep-speed`. With reduced motion the ring stays, still.
|
|
17
17
|
*
|
|
18
|
-
*
|
|
18
|
+
* `sweep-ring--two` sends two lights round, chasing each other half a lap
|
|
19
|
+
* apart. `sweep-ring--inset` lifts the ring over the element's own
|
|
20
|
+
* content (z-index 3, inside its edge), for cards that clip their
|
|
21
|
+
* contents (`overflow: hidden`) and fill themselves with a picture or a
|
|
22
|
+
* canvas that would otherwise cover it.
|
|
23
|
+
*
|
|
24
|
+
* From the ArrayPress theme; the two lights from the Logomarks site.
|
|
19
25
|
* ───────────────────────────────────────────────────────────────────── */
|
|
20
26
|
|
|
21
27
|
@property --sweep-angle {
|
|
@@ -63,6 +69,27 @@
|
|
|
63
69
|
--sweep-speed: 1.8s;
|
|
64
70
|
}
|
|
65
71
|
|
|
72
|
+
/* Two lights, half a lap apart, each with a faint lead-in. */
|
|
73
|
+
.sweep-ring--two::after {
|
|
74
|
+
background: conic-gradient(
|
|
75
|
+
from var(--sweep-angle),
|
|
76
|
+
transparent 0%,
|
|
77
|
+
color-mix(in srgb, var(--sweep-color, currentColor) 25%, transparent) 14%,
|
|
78
|
+
var(--sweep-color, currentColor) 24%,
|
|
79
|
+
transparent 30%,
|
|
80
|
+
transparent 50%,
|
|
81
|
+
color-mix(in srgb, var(--sweep-color, currentColor) 25%, transparent) 64%,
|
|
82
|
+
var(--sweep-color, currentColor) 74%,
|
|
83
|
+
transparent 80%
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/* Inside the edge and over the content, for cards that clip and fill themselves. */
|
|
88
|
+
.sweep-ring--inset::after {
|
|
89
|
+
inset: 0;
|
|
90
|
+
z-index: 3;
|
|
91
|
+
}
|
|
92
|
+
|
|
66
93
|
@keyframes effect-border-sweep {
|
|
67
94
|
to {
|
|
68
95
|
--sweep-angle: 360deg;
|