@arraypress/effects-astro 0.2.0 → 0.4.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 -0
- package/COMPONENTS.md +71 -1
- package/README.md +6 -3
- package/package.json +1 -1
- package/src/components/BorderSweep.astro +9 -3
- package/src/components/EffectCanvas.astro +20 -2
- package/src/components/Galaxy.astro +40 -0
- package/src/components/GlitchText.astro +69 -0
- package/src/components/LiveGradient.astro +62 -0
- package/src/components/PixelGlitchText.astro +2 -2
- package/src/index.d.ts +6 -1
- package/src/index.js +4 -1
- package/src/runtime/galaxy.d.ts +37 -0
- package/src/runtime/galaxy.js +311 -0
- package/src/runtime/ink.d.ts +4 -0
- package/src/runtime/ink.js +17 -3
- package/src/runtime/live-gradient.d.ts +15 -0
- package/src/runtime/live-gradient.js +40 -0
- package/src/runtime/mount.js +13 -1
- package/src/runtime/near.d.ts +9 -0
- package/src/runtime/near.js +94 -0
- package/src/runtime/paper.d.ts +2 -0
- package/src/runtime/pixel-glitch.d.ts +8 -1
- package/src/runtime/pixel-glitch.js +71 -12
- package/src/runtime/shader-background.d.ts +2 -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
|
+
}
|
package/src/runtime/ink.d.ts
CHANGED
|
@@ -25,6 +25,10 @@ export interface InkConfig {
|
|
|
25
25
|
opacity?: number;
|
|
26
26
|
/** Seconds between drops of ink on their own, so it moves before anyone touches it; 0 for none. They wait while it's off screen or the tab is hidden. Default 0. */
|
|
27
27
|
idle?: number;
|
|
28
|
+
/** Ink for a light ground: the colours given are laid dark on light (dark dye alone would barely show). Default false. */
|
|
29
|
+
dark?: boolean;
|
|
30
|
+
/** Run only while near the screen (true, or a CSS length; default `200px`), handled by `mount`. Default false. */
|
|
31
|
+
lazy?: boolean | string;
|
|
28
32
|
/** Clicks on these are theirs, not the ink's. Default: links, buttons, form fields and `[data-effect-ignore]`. */
|
|
29
33
|
ignore?: string;
|
|
30
34
|
}
|
package/src/runtime/ink.js
CHANGED
|
@@ -34,6 +34,7 @@ import { PALETTES, pickColors } from './palettes.js';
|
|
|
34
34
|
* @property {number} [momentum=0.25] How fast the motion dies away, per second (lower flows on longer).
|
|
35
35
|
* @property {number} [opacity=1] The ink's strongest cover, 0–1.
|
|
36
36
|
* @property {number} [idle=0] Seconds between drops of ink on their own, so it moves before anyone touches it; 0 for none. They wait while the ink is off screen or the tab is hidden.
|
|
37
|
+
* @property {boolean} [dark=false] Ink for a light ground: the colours given are laid dark on light (a pale dye, turned over as it's shown), since the ink's cover follows the dye's brightness and dark dye would barely show.
|
|
37
38
|
* @property {string} [ignore] Clicks on these are theirs, not the ink's (default: links, buttons, form fields, `[data-effect-ignore]`).
|
|
38
39
|
*/
|
|
39
40
|
|
|
@@ -43,12 +44,16 @@ precision highp float;
|
|
|
43
44
|
uniform vec2 uRes;
|
|
44
45
|
uniform sampler2D uDye;
|
|
45
46
|
uniform float uOpacity;
|
|
47
|
+
uniform float uDark;
|
|
46
48
|
void main() {
|
|
47
49
|
vec3 dye = texture2D(uDye, gl_FragCoord.xy / uRes).rgb;
|
|
48
50
|
float ink = max(dye.r, max(dye.g, dye.b));
|
|
49
51
|
float cover = (1.0 - exp(-ink * 2.5)) * uOpacity;
|
|
52
|
+
vec3 colour = dye / max(ink, 1e-4);
|
|
53
|
+
// dark ink: the dye was laid as the colour's opposite; turn it back
|
|
54
|
+
colour = mix(colour, 1.0 - colour, uDark);
|
|
50
55
|
// Premultiplied, as the canvas composites.
|
|
51
|
-
gl_FragColor = vec4(
|
|
56
|
+
gl_FragColor = vec4(colour * cover, cover);
|
|
52
57
|
}
|
|
53
58
|
`;
|
|
54
59
|
|
|
@@ -69,6 +74,14 @@ export function ink(el, config) {
|
|
|
69
74
|
let fluid = null;
|
|
70
75
|
/** @type {ReturnType<typeof stir> | null} */
|
|
71
76
|
let stirrer = null;
|
|
77
|
+
const dark = config.dark === true;
|
|
78
|
+
/**
|
|
79
|
+
* A colour as dye: itself, or its opposite for dark ink (turned back when shown).
|
|
80
|
+
*
|
|
81
|
+
* @param {import('./color.js').Rgb} c The colour.
|
|
82
|
+
* @returns {import('./color.js').Rgb} The dye.
|
|
83
|
+
*/
|
|
84
|
+
const dye = (c) => (dark ? /** @type {import('./color.js').Rgb} */ (c.map((v) => 1 - v)) : c);
|
|
72
85
|
const idle = Math.max(0, config.idle ?? 0);
|
|
73
86
|
// the first drop comes soon after it starts, then one every `idle` seconds of running
|
|
74
87
|
let untilDrop = Math.min(idle, 0.4);
|
|
@@ -83,8 +96,9 @@ export function ink(el, config) {
|
|
|
83
96
|
gl.useProgram(program);
|
|
84
97
|
gl.uniform1i(uniform('uDye'), 3);
|
|
85
98
|
gl.uniform1f(uniform('uOpacity'), config.opacity ?? 1);
|
|
99
|
+
gl.uniform1f(uniform('uDark'), dark ? 1 : 0);
|
|
86
100
|
const s = stir(fluid, el, {
|
|
87
|
-
colors: pickColors(config, [...PALETTES.Peacock.colors]).map(parseColor),
|
|
101
|
+
colors: pickColors(config, [...PALETTES.Peacock.colors]).map(parseColor).map(dye),
|
|
88
102
|
trail: config.trail ?? 0.35,
|
|
89
103
|
drops: config.drops,
|
|
90
104
|
force: config.force,
|
|
@@ -110,7 +124,7 @@ export function ink(el, config) {
|
|
|
110
124
|
});
|
|
111
125
|
if (!stage) return stop;
|
|
112
126
|
|
|
113
|
-
const unwatch = watchColors(el, (next) => stirrer?.setColors(next.colors.map(parseColor)));
|
|
127
|
+
const unwatch = watchColors(el, (next) => stirrer?.setColors(next.colors.map(parseColor).map(dye)));
|
|
114
128
|
return () => {
|
|
115
129
|
stop();
|
|
116
130
|
unwatch();
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PaperConfig } from './paper.js';
|
|
2
|
+
|
|
3
|
+
export interface LiveGradientOptions {
|
|
4
|
+
/** How fast it morphs (Paper's speed; 1 is Paper's own). Default 0.16. */
|
|
5
|
+
speed?: number;
|
|
6
|
+
/** How far the colours bend into each other, 0–1. Default 0.7. */
|
|
7
|
+
distortion?: number;
|
|
8
|
+
/** How much they turn, 0–1. Default 0.3. */
|
|
9
|
+
swirl?: number;
|
|
10
|
+
/** Share of the pixel ratio drawn at. Default 0.5. */
|
|
11
|
+
scale?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The Paper settings (`config`, for `paperShader`) and a still CSS `background` (`fallback`) for a live gradient. Throws with fewer than two colours. */
|
|
15
|
+
export function liveGradient(colors: string[], options?: LiveGradientOptions): { config: PaperConfig; fallback: string };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/live-gradient
|
|
3
|
+
*
|
|
4
|
+
* A live gradient: Paper's mesh gradient in your colours, morphing slowly
|
|
5
|
+
* (subtle by default: a sixth of Paper's speed, a gentle swirl), drawn at
|
|
6
|
+
* half resolution (a soft gradient loses nothing, and grain covers the
|
|
7
|
+
* rest). It comes with the same colours as a still CSS gradient, to show
|
|
8
|
+
* before it starts and without WebGL, so the live one can fade in over it.
|
|
9
|
+
* `<LiveGradient>` is the component; this builds its settings, for pages
|
|
10
|
+
* that write their own HTML (a card list made in a script).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {object} LiveGradientOptions
|
|
15
|
+
* @property {number} [speed=0.16] How fast it morphs (Paper's speed; 1 is Paper's own).
|
|
16
|
+
* @property {number} [distortion=0.7] How far the colours bend into each other, 0–1.
|
|
17
|
+
* @property {number} [swirl=0.3] How much they turn, 0–1.
|
|
18
|
+
* @property {number} [scale=0.5] Share of the pixel ratio drawn at.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The Paper settings and the still CSS gradient for a live gradient.
|
|
23
|
+
*
|
|
24
|
+
* @param {string[]} colors Two to four colours (hex); the last is the ground the others bloom on.
|
|
25
|
+
* @param {LiveGradientOptions} [options] How it moves.
|
|
26
|
+
* @returns {{ config: import('./paper.js').PaperConfig, fallback: string }} `config` for `paperShader`, `fallback` a CSS `background`.
|
|
27
|
+
* @throws {TypeError} With fewer than two colours.
|
|
28
|
+
*/
|
|
29
|
+
export function liveGradient(colors, { speed = 0.16, distortion = 0.7, swirl = 0.3, scale = 0.5 } = {}) {
|
|
30
|
+
if (!Array.isArray(colors) || colors.length < 2) throw new TypeError('A live gradient needs at least two colours');
|
|
31
|
+
const cs = colors.slice(0, 4);
|
|
32
|
+
const ground = cs[cs.length - 1];
|
|
33
|
+
const blooms = cs.slice(0, -1);
|
|
34
|
+
const at = ['20% 18%', '82% 72%', '28% 88%'];
|
|
35
|
+
const fallback = [...blooms.map((c, i) => `radial-gradient(at ${at[i]}, ${c} 0, transparent 60%)`), ground].join(', ');
|
|
36
|
+
return {
|
|
37
|
+
config: { shader: 'mesh-gradient', colors: cs, speed, scale, params: { distortion, swirl, grainMixer: 0, grainOverlay: 0 } },
|
|
38
|
+
fallback,
|
|
39
|
+
};
|
|
40
|
+
}
|
package/src/runtime/mount.js
CHANGED
|
@@ -9,8 +9,14 @@
|
|
|
9
9
|
* an element with its settings as JSON in `data-effect-config`; the setup
|
|
10
10
|
* function is called once per element with those settings, and the
|
|
11
11
|
* function it returns is called when the element leaves the page.
|
|
12
|
+
*
|
|
13
|
+
* An element marked lazy (`data-effect-lazy`, or `lazy` in its settings)
|
|
14
|
+
* runs only while it is near the screen (./near.js): a page can carry many
|
|
15
|
+
* effects and still hold only the WebGL contexts it shows.
|
|
12
16
|
*/
|
|
13
17
|
|
|
18
|
+
import { whenNear, lazyMargin } from './near.js';
|
|
19
|
+
|
|
14
20
|
/** @type {Map<string, (el: HTMLElement, config: any) => (() => void) | void>} */
|
|
15
21
|
const effects = new Map();
|
|
16
22
|
/** For each registered selector, its running elements and how to stop each: one element can carry several effects. */
|
|
@@ -48,7 +54,13 @@ function scan() {
|
|
|
48
54
|
document.querySelectorAll(selector).forEach((node) => {
|
|
49
55
|
const el = /** @type {HTMLElement} */ (node);
|
|
50
56
|
if (live.has(el)) return;
|
|
51
|
-
|
|
57
|
+
const config = readConfig(el);
|
|
58
|
+
const margin = lazyMargin(el, config);
|
|
59
|
+
if (margin) live.set(el, whenNear(el, () => setup(el, config), { margin }));
|
|
60
|
+
else {
|
|
61
|
+
live.set(el, setup(el, config) ?? (() => {}));
|
|
62
|
+
el.setAttribute('data-effect-live', '');
|
|
63
|
+
}
|
|
52
64
|
});
|
|
53
65
|
}
|
|
54
66
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Starts `start` while `el` is within `margin` (default `200px`) of the viewport and stops it when it moves away,
|
|
3
|
+
* again each time it comes back; `data-effect-live` is on the element while it runs. Returns a function that stops
|
|
4
|
+
* watching (and the effect).
|
|
5
|
+
*/
|
|
6
|
+
export function whenNear(el: HTMLElement, start: () => (() => void) | void, options?: { margin?: string }): () => void;
|
|
7
|
+
|
|
8
|
+
/** An element's lazy margin: `data-effect-lazy`, or `lazy` in its config (true for `200px`); null to start at once. */
|
|
9
|
+
export function lazyMargin(el: HTMLElement, config: Record<string, unknown>): string | null;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/near
|
|
3
|
+
*
|
|
4
|
+
* Runs an effect only while its element is near the screen: started as it
|
|
5
|
+
* comes within `margin` of the viewport, stopped (its WebGL context given
|
|
6
|
+
* back) as it leaves. A browser keeps about sixteen WebGL contexts at once
|
|
7
|
+
* and drops the oldest past that, so a page with many effects (a wall of
|
|
8
|
+
* cards, a long list) needs this; `mount` uses it for any element marked
|
|
9
|
+
* `lazy`. While running, the element carries `data-effect-live`, which the
|
|
10
|
+
* stylesheet can fade the canvas in on.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** One observer per margin, shared by every element that uses it. */
|
|
14
|
+
/** @type {Map<string, { observer: IntersectionObserver, handlers: Map<Element, (near: boolean) => void> }>} */
|
|
15
|
+
const observers = new Map();
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The shared observer for a margin, made on first use.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} margin A CSS length for `rootMargin`'s top and bottom (and left and right).
|
|
21
|
+
* @returns {{ observer: IntersectionObserver, handlers: Map<Element, (near: boolean) => void> }} It and its handlers.
|
|
22
|
+
*/
|
|
23
|
+
function observerFor(margin) {
|
|
24
|
+
let entry = observers.get(margin);
|
|
25
|
+
if (!entry) {
|
|
26
|
+
/** @type {Map<Element, (near: boolean) => void>} */
|
|
27
|
+
const handlers = new Map();
|
|
28
|
+
const observer = new IntersectionObserver(
|
|
29
|
+
(changes) => {
|
|
30
|
+
for (const change of changes) handlers.get(change.target)?.(change.isIntersecting);
|
|
31
|
+
},
|
|
32
|
+
{ rootMargin: `${margin} ${margin}` },
|
|
33
|
+
);
|
|
34
|
+
entry = { observer, handlers };
|
|
35
|
+
observers.set(margin, entry);
|
|
36
|
+
}
|
|
37
|
+
return entry;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Starts `start` while `el` is within `margin` of the viewport, and stops it
|
|
42
|
+
* when it moves further away; again each time it comes back.
|
|
43
|
+
*
|
|
44
|
+
* @param {HTMLElement} el The element to watch.
|
|
45
|
+
* @param {() => (() => void) | void} start Starts the effect; returns how to stop it.
|
|
46
|
+
* @param {{ margin?: string }} [options] How near counts as near (CSS length). Default `200px`.
|
|
47
|
+
* @returns {() => void} Stops watching, and stops the effect if it is running.
|
|
48
|
+
*/
|
|
49
|
+
export function whenNear(el, start, { margin = '200px' } = {}) {
|
|
50
|
+
/** @type {(() => void) | null} */
|
|
51
|
+
let stop = null;
|
|
52
|
+
/** Starts the effect, if it isn't running. */
|
|
53
|
+
const on = () => {
|
|
54
|
+
if (stop) return;
|
|
55
|
+
stop = start() ?? (() => {});
|
|
56
|
+
el.setAttribute('data-effect-live', '');
|
|
57
|
+
};
|
|
58
|
+
/** Stops the effect, if it's running. */
|
|
59
|
+
const off = () => {
|
|
60
|
+
if (!stop) return;
|
|
61
|
+
const s = stop;
|
|
62
|
+
stop = null;
|
|
63
|
+
el.removeAttribute('data-effect-live');
|
|
64
|
+
s();
|
|
65
|
+
};
|
|
66
|
+
if (typeof IntersectionObserver === 'undefined') {
|
|
67
|
+
on();
|
|
68
|
+
return off;
|
|
69
|
+
}
|
|
70
|
+
const { observer, handlers } = observerFor(margin);
|
|
71
|
+
handlers.set(el, (near) => (near ? on() : off()));
|
|
72
|
+
observer.observe(el);
|
|
73
|
+
return () => {
|
|
74
|
+
observer.unobserve(el);
|
|
75
|
+
handlers.delete(el);
|
|
76
|
+
off();
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Reads how lazy an element is: its `data-effect-lazy` attribute, or a
|
|
82
|
+
* `lazy` setting in its config (true for the default margin, or a length).
|
|
83
|
+
*
|
|
84
|
+
* @param {HTMLElement} el The element.
|
|
85
|
+
* @param {Record<string, unknown>} config Its settings.
|
|
86
|
+
* @returns {string | null} The margin to start within, or null to start at once.
|
|
87
|
+
*/
|
|
88
|
+
export function lazyMargin(el, config) {
|
|
89
|
+
const attr = el.dataset.effectLazy;
|
|
90
|
+
if (attr !== undefined) return attr || '200px';
|
|
91
|
+
if (config.lazy === true) return '200px';
|
|
92
|
+
if (typeof config.lazy === 'string' && config.lazy) return config.lazy;
|
|
93
|
+
return null;
|
|
94
|
+
}
|
package/src/runtime/paper.d.ts
CHANGED
|
@@ -25,6 +25,8 @@ export interface PaperConfig {
|
|
|
25
25
|
speed?: number;
|
|
26
26
|
/** Share of the pixel ratio drawn at. Default 1. */
|
|
27
27
|
scale?: number;
|
|
28
|
+
/** Run only while near the screen (true, or a CSS length; default `200px`), handled by `mount`. Default false. */
|
|
29
|
+
lazy?: boolean | string;
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
/** A shader's settings: its preset with the config's settings over it. Throws for an unknown shader. */
|
|
@@ -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;
|