@arraypress/effects-astro 0.4.0 → 0.6.1
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 -4
- package/COMPONENTS.md +40 -1
- package/README.md +21 -180
- package/package.json +5 -2
- package/src/components/PixelGlitchText.astro +5 -3
- package/src/components/Scene.astro +89 -0
- package/src/index.d.ts +8 -5
- package/src/index.js +6 -5
- package/src/runtime/canvas2d.d.ts +10 -1
- package/src/runtime/canvas2d.js +44 -9
- package/src/runtime/galaxy.d.ts +22 -7
- package/src/runtime/galaxy.js +169 -71
- package/src/runtime/paper.js +53 -2
- package/src/runtime/pixel-glitch.d.ts +7 -0
- package/src/runtime/pixel-glitch.js +57 -3
- package/src/runtime/random.d.ts +12 -0
- package/src/runtime/random.js +43 -2
- package/src/runtime/scene.d.ts +58 -0
- package/src/runtime/scene.js +80 -0
package/src/runtime/canvas2d.js
CHANGED
|
@@ -6,6 +6,12 @@
|
|
|
6
6
|
* frames from the page's one loop while it is near the screen, one still
|
|
7
7
|
* frame with prefers-reduced-motion (redrawn on resize), and a grain tile
|
|
8
8
|
* for texturing strokes.
|
|
9
|
+
*
|
|
10
|
+
* A canvas holds one kind of context for life, so when the element's
|
|
11
|
+
* canvas has already been claimed for WebGL (a host that makes one canvas
|
|
12
|
+
* for whichever effect it shows), a fresh canvas takes its place, with
|
|
13
|
+
* its attributes, and the host's goes back when the effect stops. An
|
|
14
|
+
* element with no canvas gets one.
|
|
9
15
|
*/
|
|
10
16
|
|
|
11
17
|
import { prefersReducedMotion } from './device.js';
|
|
@@ -17,11 +23,37 @@ import { animate } from './loop.js';
|
|
|
17
23
|
* @property {HTMLCanvasElement} canvas The canvas.
|
|
18
24
|
* @property {number} width The element's width, CSS px.
|
|
19
25
|
* @property {number} height The element's height, CSS px.
|
|
26
|
+
* @property {number} dpr The canvas's pixels per CSS px (the device's, up to 2).
|
|
20
27
|
* @property {boolean} still True with prefers-reduced-motion.
|
|
21
28
|
*/
|
|
22
29
|
|
|
23
30
|
/**
|
|
24
|
-
*
|
|
31
|
+
* The element's canvas, ready for 2D drawing: its first canvas, or a
|
|
32
|
+
* fresh one in its place when that one is already claimed for another
|
|
33
|
+
* kind of context (WebGL), or a fresh one added when it has none.
|
|
34
|
+
*
|
|
35
|
+
* @param {HTMLElement} el The element.
|
|
36
|
+
* @returns {{ canvas: HTMLCanvasElement, ctx: CanvasRenderingContext2D | null, release: () => void }} The canvas, its context and a function that puts the element's own canvas back.
|
|
37
|
+
*/
|
|
38
|
+
export function claimCanvas(el) {
|
|
39
|
+
const found = el.querySelector('canvas');
|
|
40
|
+
const ctx = found?.getContext('2d', { alpha: true });
|
|
41
|
+
if (found && ctx) return { canvas: found, ctx, release: () => {} };
|
|
42
|
+
const canvas = document.createElement('canvas');
|
|
43
|
+
if (found) {
|
|
44
|
+
for (const { name, value } of found.attributes) canvas.setAttribute(name, value);
|
|
45
|
+
found.replaceWith(canvas);
|
|
46
|
+
} else el.append(canvas);
|
|
47
|
+
return {
|
|
48
|
+
canvas,
|
|
49
|
+
ctx: canvas.getContext('2d', { alpha: true }),
|
|
50
|
+
release: () => (found ? canvas.replaceWith(found) : canvas.remove()),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Starts a Canvas 2D effect on an element (its first canvas is drawn into,
|
|
56
|
+
* or one in its place: see claimCanvas).
|
|
25
57
|
*
|
|
26
58
|
* @param {HTMLElement} el The element.
|
|
27
59
|
* @param {object} options The effect.
|
|
@@ -30,20 +62,22 @@ import { animate } from './loop.js';
|
|
|
30
62
|
* @returns {{ c: Canvas2D | null, stop: () => void }} The canvas (null without a 2D context) and a function that stops it.
|
|
31
63
|
*/
|
|
32
64
|
export function createCanvas2D(el, options) {
|
|
33
|
-
const canvas = el
|
|
34
|
-
|
|
35
|
-
|
|
65
|
+
const { canvas, ctx, release } = claimCanvas(el);
|
|
66
|
+
if (!ctx) {
|
|
67
|
+
release();
|
|
68
|
+
return { c: null, stop: () => {} };
|
|
69
|
+
}
|
|
36
70
|
/** @type {Canvas2D} */
|
|
37
|
-
const c = { ctx, canvas, width: 0, height: 0, still: prefersReducedMotion() };
|
|
71
|
+
const c = { ctx, canvas, width: 0, height: 0, dpr: 1, still: prefersReducedMotion() };
|
|
38
72
|
|
|
39
73
|
/** Sizes the canvas to the element. */
|
|
40
74
|
const size = () => {
|
|
41
|
-
|
|
75
|
+
c.dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
42
76
|
c.width = Math.max(1, el.clientWidth);
|
|
43
77
|
c.height = Math.max(1, el.clientHeight);
|
|
44
|
-
canvas.width = Math.floor(c.width * dpr);
|
|
45
|
-
canvas.height = Math.floor(c.height * dpr);
|
|
46
|
-
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
78
|
+
canvas.width = Math.floor(c.width * c.dpr);
|
|
79
|
+
canvas.height = Math.floor(c.height * c.dpr);
|
|
80
|
+
ctx.setTransform(c.dpr, 0, 0, c.dpr, 0, 0);
|
|
47
81
|
options.resize?.(c);
|
|
48
82
|
if (c.still) options.draw(c, performance.now(), 0);
|
|
49
83
|
};
|
|
@@ -56,6 +90,7 @@ export function createCanvas2D(el, options) {
|
|
|
56
90
|
stop: () => {
|
|
57
91
|
stopLoop();
|
|
58
92
|
resizer.disconnect();
|
|
93
|
+
release();
|
|
59
94
|
},
|
|
60
95
|
};
|
|
61
96
|
}
|
package/src/runtime/galaxy.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { PaletteName } from './palettes.js';
|
|
2
|
+
import type { RandomName } from './random.js';
|
|
2
3
|
|
|
3
4
|
export interface GalaxyConfig {
|
|
4
5
|
/** The nebula's colours, deep to bright (two to five read best), blended in Oklab from near-black up to a pale highlight. Default indigo, cornflower, violet and cyan. */
|
|
@@ -13,6 +14,17 @@ export interface GalaxyConfig {
|
|
|
13
14
|
glitter?: number;
|
|
14
15
|
/** Which sky, 1–999: the same seed draws the same nebula and stars. Default 7. */
|
|
15
16
|
seed?: number;
|
|
17
|
+
/** The glitter and stars' own seed. Default `seed × 31 + 1`. */
|
|
18
|
+
starSeed?: number;
|
|
19
|
+
/**
|
|
20
|
+
* The generator. `mulberry32` scatters the glitter and stars afresh from `starSeed` on each resize;
|
|
21
|
+
* `park-miller` (the Logomarks studio's first galaxy) scatters them from one stream that runs on across
|
|
22
|
+
* resizes. With `seed: 7`, `starSeed: 31` and `blend: 'srgb'`, `park-miller` draws that sky to the pixel.
|
|
23
|
+
* Default `mulberry32`.
|
|
24
|
+
*/
|
|
25
|
+
rng?: RandomName;
|
|
26
|
+
/** How the ramp blends its colours: in Oklab, or plainly in sRGB (painted exactly along its stops, the first colour's bytes × 0.18 for its near-black). Default `oklab`. */
|
|
27
|
+
blend?: 'oklab' | 'srgb';
|
|
16
28
|
/** Run only while near the screen (true, or a CSS length; default `200px`), handled by `mount`. Default false. */
|
|
17
29
|
lazy?: boolean | string;
|
|
18
30
|
}
|
|
@@ -24,14 +36,17 @@ export const COLORS: string[];
|
|
|
24
36
|
export const NEBULA_WIDTH: number;
|
|
25
37
|
export const NEBULA_HEIGHT: number;
|
|
26
38
|
|
|
27
|
-
/** The nebula as places on the colour ramp, 0–1, one a pixel row by row: layered noise clouds cut by ridged dust lanes. Tileable; the same seed
|
|
28
|
-
export function nebulaField(width: number, height: number, seed?: number):
|
|
39
|
+
/** The nebula as places on the colour ramp, 0–1, one a pixel row by row: layered noise clouds cut by ridged dust lanes. Tileable; the same seed and generator (default `mulberry32`) give the same field. */
|
|
40
|
+
export function nebulaField(width: number, height: number, seed?: number, rng?: RandomName): Float64Array;
|
|
29
41
|
|
|
30
|
-
/** The ramp
|
|
31
|
-
export function
|
|
42
|
+
/** The ramp's stops (the first colour darkened to near-black, the colours, the pale top) as RGB triples, 0–255, unrounded. Darkened in Oklab, or for `srgb` by scaling its bytes by 0.18. Throws a TypeError for a colour that is not hex. */
|
|
43
|
+
export function galaxyStops(colors: string[], blend?: 'oklab' | 'srgb'): Float64Array;
|
|
32
44
|
|
|
33
|
-
/**
|
|
34
|
-
export function
|
|
45
|
+
/** The ramp the nebula is painted along (the first colour darkened to near-black, the colours, a pale top), blended in Oklab (default) or sRGB: `steps` (default 256) RGB triples. Throws a TypeError for a colour that is not hex. */
|
|
46
|
+
export function galaxyRamp(colors: string[], steps?: number, blend?: 'oklab' | 'srgb'): Uint8ClampedArray;
|
|
35
47
|
|
|
36
|
-
/**
|
|
48
|
+
/** Paints a nebula field along a ramp: opaque RGBA, a pixel per value. Each value takes the nearest entry, or with `smooth` the linear blend of the two either side (for galaxyStops with `srgb`, which it paints exactly). */
|
|
49
|
+
export function nebulaPixels(field: ArrayLike<number>, ramp: ArrayLike<number>, smooth?: boolean): Uint8ClampedArray;
|
|
50
|
+
|
|
51
|
+
/** Starts the galaxy on an element (its first canvas is drawn into, or a 2D one in its place when the host made it for WebGL). Returns a function that stops it. */
|
|
37
52
|
export function galaxy(el: HTMLElement, config: GalaxyConfig): () => void;
|
package/src/runtime/galaxy.js
CHANGED
|
@@ -10,21 +10,29 @@
|
|
|
10
10
|
* noise: five octaves for the clouds, three ridged ones for the dust, and a
|
|
11
11
|
* broad field that shifts the colours along the ramp so they wander. Each
|
|
12
12
|
* frame draws it scaled up to cover (it is soft anyway) at a slowly
|
|
13
|
-
* drifting offset, the glitter
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* (`setColors`) only remap the worked-out field.
|
|
13
|
+
* drifting offset, then the glitter and the stars over it, all in device
|
|
14
|
+
* pixels. The ramp runs from near-black through the palette to a pale
|
|
15
|
+
* highlight, blended in Oklab (or plainly in sRGB, `blend: 'srgb'`); new
|
|
16
|
+
* colours (`setColors`) only remap the worked-out field.
|
|
17
17
|
*
|
|
18
|
-
*
|
|
18
|
+
* The sky comes from `seed` (the nebula) and `starSeed` (the glitter and
|
|
19
|
+
* stars), by mulberry32, scattered afresh from `starSeed` on each resize.
|
|
20
|
+
* `rng: 'park-miller'` uses the Park–Miller generator instead, as the
|
|
21
|
+
* Logomarks studio's first galaxy did, with its glitter and stars from one
|
|
22
|
+
* stream that runs on across resizes; with `seed: 7`, `starSeed: 31` and
|
|
23
|
+
* `blend: 'srgb'` that is its sky to the pixel.
|
|
24
|
+
*
|
|
25
|
+
* Canvas 2D, opaque. One still frame with prefers-reduced-motion. A host
|
|
26
|
+
* canvas already claimed for WebGL is swapped for a 2D one (canvas2d.js).
|
|
19
27
|
*
|
|
20
28
|
* From the Logomarks studio's showcase.
|
|
21
29
|
*/
|
|
22
30
|
|
|
23
31
|
import { createCanvas2D } from './canvas2d.js';
|
|
24
|
-
import { mixOklab, parseColor } from './color.js';
|
|
32
|
+
import { mixOklab, parseColor, toHex } from './color.js';
|
|
25
33
|
import { watchColors } from './mount.js';
|
|
26
34
|
import { pickColors } from './palettes.js';
|
|
27
|
-
import {
|
|
35
|
+
import { randomFrom } from './random.js';
|
|
28
36
|
|
|
29
37
|
/**
|
|
30
38
|
* @typedef {object} GalaxyConfig
|
|
@@ -34,6 +42,9 @@ import { seededRandom } from './random.js';
|
|
|
34
42
|
* @property {number} [stars=1] How many stars, as a share of the usual.
|
|
35
43
|
* @property {number} [glitter=1] How bright the glitter is, as a share of the usual.
|
|
36
44
|
* @property {number} [seed=7] Which sky: the same seed draws the same nebula and stars.
|
|
45
|
+
* @property {number} [starSeed] The glitter and stars' own seed. Default `seed × 31 + 1`.
|
|
46
|
+
* @property {import('./random.js').RandomName} [rng='mulberry32'] The generator: `park-miller` for the studio's first sky (its scatter runs on across resizes).
|
|
47
|
+
* @property {'oklab' | 'srgb'} [blend='oklab'] How the ramp blends its colours: in Oklab, or plainly in sRGB (and painted exactly along its stops).
|
|
37
48
|
*/
|
|
38
49
|
|
|
39
50
|
/** The colours without `colors` or `palette`: indigo, cornflower, violet and cyan. */
|
|
@@ -43,8 +54,17 @@ export const COLORS = ['#3a2bd9', '#5c7cff', '#b45cff', '#5cecff'];
|
|
|
43
54
|
export const NEBULA_WIDTH = 400;
|
|
44
55
|
export const NEBULA_HEIGHT = 225;
|
|
45
56
|
|
|
46
|
-
/** The ramp's top: a pale, faintly violet white. */
|
|
47
|
-
const
|
|
57
|
+
/** The ramp's top: a pale, faintly violet white, as bytes. */
|
|
58
|
+
const PALE_BYTES = [245, 240, 255];
|
|
59
|
+
|
|
60
|
+
/** The ramp's top, 0–1. */
|
|
61
|
+
const PALE = /** @type {import('./color.js').Rgb} */ (PALE_BYTES.map((v) => v / 255));
|
|
62
|
+
|
|
63
|
+
/** The near-black the ramp starts from: the first colour mixed this far to black in Oklab… */
|
|
64
|
+
const DEEP_OKLAB = 0.62;
|
|
65
|
+
|
|
66
|
+
/** …or, blended in sRGB, its bytes scaled by this. */
|
|
67
|
+
const DEEP_SRGB = 0.18;
|
|
48
68
|
|
|
49
69
|
/** Star tints besides the palette's own: white, a violet white and a blue white. */
|
|
50
70
|
const WHITES = ['#ffffff', '#f2e9ff', '#e6f6ff'];
|
|
@@ -90,14 +110,15 @@ function lattice(n, rnd) {
|
|
|
90
110
|
* @param {number} width Across, px.
|
|
91
111
|
* @param {number} height Down, px.
|
|
92
112
|
* @param {number} [seed=7] The same seed gives the same nebula.
|
|
93
|
-
* @
|
|
113
|
+
* @param {import('./random.js').RandomName} [rng='mulberry32'] The generator.
|
|
114
|
+
* @returns {Float64Array} One value a pixel, 0 (dark) to 1 (the pale top), row by row.
|
|
94
115
|
*/
|
|
95
|
-
export function nebulaField(width, height, seed = 7) {
|
|
96
|
-
const rnd =
|
|
116
|
+
export function nebulaField(width, height, seed = 7, rng = 'mulberry32') {
|
|
117
|
+
const rnd = randomFrom(rng, seed);
|
|
97
118
|
const octaves = [3, 6, 12, 24, 48].map((n) => lattice(n, rnd));
|
|
98
119
|
const dust = [5, 10, 20].map((n) => lattice(n, rnd));
|
|
99
120
|
const hue = lattice(2, rnd);
|
|
100
|
-
const out = new
|
|
121
|
+
const out = new Float64Array(width * height);
|
|
101
122
|
for (let y = 0; y < height; y++) {
|
|
102
123
|
for (let x = 0; x < width; x++) {
|
|
103
124
|
const u = x / width;
|
|
@@ -121,20 +142,54 @@ export function nebulaField(width, height, seed = 7) {
|
|
|
121
142
|
return out;
|
|
122
143
|
}
|
|
123
144
|
|
|
145
|
+
/**
|
|
146
|
+
* The colours as channels, the default four when there are none.
|
|
147
|
+
*
|
|
148
|
+
* @param {string[]} colors The colours, as CSS hex.
|
|
149
|
+
* @returns {import('./color.js').Rgb[]} Their channels, 0–1.
|
|
150
|
+
* @throws {TypeError} When a colour is not hex.
|
|
151
|
+
*/
|
|
152
|
+
function parsed(colors) {
|
|
153
|
+
return (colors.length ? colors : COLORS).map(parseColor);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The ramp's stops: near-black (the first colour darkened), the colours
|
|
158
|
+
* in order, then the pale top. Darkened in Oklab, or, for `srgb`, by
|
|
159
|
+
* scaling the colour's bytes (unrounded, as the studio's first ramp did).
|
|
160
|
+
*
|
|
161
|
+
* @param {string[]} colors The colours, as CSS hex; the default four when empty.
|
|
162
|
+
* @param {'oklab' | 'srgb'} [blend='oklab'] How the ramp blends.
|
|
163
|
+
* @returns {Float64Array} One RGB triple a stop, 0–255, unrounded.
|
|
164
|
+
* @throws {TypeError} When a colour is not hex.
|
|
165
|
+
*/
|
|
166
|
+
export function galaxyStops(colors, blend = 'oklab') {
|
|
167
|
+
const cs = parsed(colors);
|
|
168
|
+
const bytes = cs.map((c) => c.map((v) => Math.round(v * 255)));
|
|
169
|
+
const deep = blend === 'srgb' ? bytes[0].map((v) => v * DEEP_SRGB) : mixOklab(cs[0], [0, 0, 0], DEEP_OKLAB).map((v) => v * 255);
|
|
170
|
+
return Float64Array.from([deep, ...bytes, PALE_BYTES].flat());
|
|
171
|
+
}
|
|
172
|
+
|
|
124
173
|
/**
|
|
125
174
|
* The colour ramp the nebula is painted along: near-black (the first
|
|
126
175
|
* colour, darkened), the colours in order, then a pale highlight, each
|
|
127
|
-
* stretch blended in Oklab.
|
|
176
|
+
* stretch blended in Oklab (or plainly in sRGB).
|
|
128
177
|
*
|
|
129
178
|
* @param {string[]} colors The colours, as CSS hex.
|
|
130
179
|
* @param {number} [steps=256] How many entries.
|
|
180
|
+
* @param {'oklab' | 'srgb'} [blend='oklab'] How the stretches blend.
|
|
131
181
|
* @returns {Uint8ClampedArray} `steps` RGB triples, 0–255.
|
|
132
182
|
* @throws {TypeError} When a colour is not hex.
|
|
133
183
|
*/
|
|
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];
|
|
184
|
+
export function galaxyRamp(colors, steps = 256, blend = 'oklab') {
|
|
137
185
|
const out = new Uint8ClampedArray(steps * 3);
|
|
186
|
+
if (blend === 'srgb') {
|
|
187
|
+
const stops = galaxyStops(colors, 'srgb');
|
|
188
|
+
for (let s = 0; s < steps; s++) along(stops, s / (steps - 1), out, s * 3);
|
|
189
|
+
return out;
|
|
190
|
+
}
|
|
191
|
+
const cs = parsed(colors);
|
|
192
|
+
const stops = [mixOklab(cs[0], [0, 0, 0], DEEP_OKLAB), ...cs, PALE];
|
|
138
193
|
for (let s = 0; s < steps; s++) {
|
|
139
194
|
const k = (s / (steps - 1)) * (stops.length - 1);
|
|
140
195
|
const i = Math.min(stops.length - 2, Math.floor(k));
|
|
@@ -145,48 +200,74 @@ export function galaxyRamp(colors, steps = 256) {
|
|
|
145
200
|
}
|
|
146
201
|
|
|
147
202
|
/**
|
|
148
|
-
*
|
|
203
|
+
* Writes the colour a place along some stops comes to, blended linearly
|
|
204
|
+
* between the two either side of it.
|
|
149
205
|
*
|
|
150
|
-
* @param {
|
|
151
|
-
* @param {
|
|
206
|
+
* @param {ArrayLike<number>} stops RGB triples.
|
|
207
|
+
* @param {number} t Where along them, 0–1.
|
|
208
|
+
* @param {Uint8ClampedArray} out Where to write it (rounded as it is stored).
|
|
209
|
+
* @param {number} at The index of its red.
|
|
210
|
+
*/
|
|
211
|
+
function along(stops, t, out, at) {
|
|
212
|
+
const n = stops.length / 3;
|
|
213
|
+
const k = t * (n - 1);
|
|
214
|
+
const i = Math.min(n - 2, Math.floor(k));
|
|
215
|
+
const f = k - i;
|
|
216
|
+
for (let ch = 0; ch < 3; ch++) {
|
|
217
|
+
const a = stops[i * 3 + ch];
|
|
218
|
+
out[at + ch] = a + (stops[(i + 1) * 3 + ch] - a) * f;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Paints a nebula field along a ramp: each value takes the nearest entry,
|
|
224
|
+
* or, `smooth`, the blend of the two either side of it (for a ramp given
|
|
225
|
+
* only at its stops, such as galaxyStops with `srgb`, which paints it
|
|
226
|
+
* exactly).
|
|
227
|
+
*
|
|
228
|
+
* @param {ArrayLike<number>} field From nebulaField.
|
|
229
|
+
* @param {ArrayLike<number>} ramp From galaxyRamp (or galaxyStops, `smooth`).
|
|
230
|
+
* @param {boolean} [smooth=false] Blend between entries rather than take the nearest.
|
|
152
231
|
* @returns {Uint8ClampedArray} RGBA, opaque, one pixel per value of the field.
|
|
153
232
|
*/
|
|
154
|
-
export function nebulaPixels(field, ramp) {
|
|
233
|
+
export function nebulaPixels(field, ramp, smooth = false) {
|
|
155
234
|
const last = ramp.length / 3 - 1;
|
|
156
235
|
const out = new Uint8ClampedArray(field.length * 4);
|
|
157
236
|
for (let p = 0; p < field.length; p++) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
237
|
+
if (smooth) along(ramp, field[p], out, p * 4);
|
|
238
|
+
else {
|
|
239
|
+
const i = Math.round(field[p] * last) * 3;
|
|
240
|
+
out[p * 4] = ramp[i];
|
|
241
|
+
out[p * 4 + 1] = ramp[i + 1];
|
|
242
|
+
out[p * 4 + 2] = ramp[i + 2];
|
|
243
|
+
}
|
|
162
244
|
out[p * 4 + 3] = 255;
|
|
163
245
|
}
|
|
164
246
|
return out;
|
|
165
247
|
}
|
|
166
248
|
|
|
167
249
|
/**
|
|
168
|
-
*
|
|
169
|
-
* centre
|
|
250
|
+
* Paints a star's glow: a radial gradient in the tint, solid at the
|
|
251
|
+
* centre, two thirds at a quarter of the way out, gone at the edge.
|
|
170
252
|
*
|
|
171
|
-
* @param {
|
|
172
|
-
* @
|
|
253
|
+
* @param {CanvasRenderingContext2D} ctx The context, in device px.
|
|
254
|
+
* @param {number} x Its centre across.
|
|
255
|
+
* @param {number} y Its centre down.
|
|
256
|
+
* @param {number} radius The glow's radius.
|
|
257
|
+
* @param {string} tint `#rrggbb`.
|
|
173
258
|
*/
|
|
174
|
-
function
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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;
|
|
259
|
+
function starGlow(ctx, x, y, radius, tint) {
|
|
260
|
+
const glow = ctx.createRadialGradient(x, y, 0, x, y, radius);
|
|
261
|
+
glow.addColorStop(0, tint);
|
|
262
|
+
glow.addColorStop(0.25, `${tint}aa`);
|
|
263
|
+
glow.addColorStop(1, `${tint}00`);
|
|
264
|
+
ctx.fillStyle = glow;
|
|
265
|
+
ctx.fillRect(x - radius, y - radius, radius * 2, radius * 2);
|
|
186
266
|
}
|
|
187
267
|
|
|
188
268
|
/**
|
|
189
|
-
* Starts the galaxy on an element (its first canvas is drawn into
|
|
269
|
+
* Starts the galaxy on an element (its first canvas is drawn into, or a
|
|
270
|
+
* 2D one in its place when the host made it for WebGL).
|
|
190
271
|
*
|
|
191
272
|
* @param {HTMLElement} el The element.
|
|
192
273
|
* @param {GalaxyConfig} config The settings.
|
|
@@ -197,74 +278,90 @@ export function galaxy(el, config) {
|
|
|
197
278
|
const STARS = Math.max(0, config.stars ?? 1);
|
|
198
279
|
const GLITTER = Math.max(0, config.glitter ?? 1);
|
|
199
280
|
const SEED = config.seed ?? 7;
|
|
281
|
+
const STAR_SEED = config.starSeed ?? SEED * 31 + 1;
|
|
282
|
+
const RNG = config.rng ?? 'mulberry32';
|
|
283
|
+
const BLEND = config.blend === 'srgb' ? 'srgb' : 'oklab';
|
|
200
284
|
let colors = pickColors(config, COLORS);
|
|
201
285
|
|
|
202
|
-
const field = nebulaField(NEBULA_WIDTH, NEBULA_HEIGHT, SEED);
|
|
286
|
+
const field = nebulaField(NEBULA_WIDTH, NEBULA_HEIGHT, SEED, RNG);
|
|
203
287
|
const nebula = document.createElement('canvas');
|
|
204
288
|
nebula.width = NEBULA_WIDTH;
|
|
205
289
|
nebula.height = NEBULA_HEIGHT;
|
|
206
290
|
const nctx = nebula.getContext('2d');
|
|
207
|
-
/** @type {
|
|
208
|
-
let
|
|
291
|
+
/** @type {string[]} */
|
|
292
|
+
let tints = [];
|
|
209
293
|
|
|
210
|
-
/** Paints the nebula and the star
|
|
294
|
+
/** Paints the nebula and picks the star tints in the current colours. */
|
|
211
295
|
const paint = () => {
|
|
212
|
-
|
|
213
|
-
|
|
296
|
+
const pixels = BLEND === 'srgb' ? nebulaPixels(field, galaxyStops(colors, 'srgb'), true) : nebulaPixels(field, galaxyRamp(colors));
|
|
297
|
+
nctx?.putImageData(new ImageData(pixels, NEBULA_WIDTH, NEBULA_HEIGHT), 0, 0);
|
|
298
|
+
tints = [...WHITES, ...colors].map((c) => toHex(parseColor(c)));
|
|
214
299
|
};
|
|
215
300
|
paint();
|
|
216
301
|
|
|
217
302
|
/** @type {HTMLCanvasElement | null} */
|
|
218
303
|
let glitter = null;
|
|
304
|
+
/** Each star in device px; `tint` is a 0–1 pick from the tints, so new colours keep it. */
|
|
219
305
|
/** @type {{ x: number, y: number, r: number, phase: number, rate: number, cross: boolean, tint: number }[]} */
|
|
220
306
|
let stars = [];
|
|
221
|
-
|
|
307
|
+
// Park–Miller's scatter is one stream that runs on across resizes; mulberry32's starts afresh each time.
|
|
308
|
+
const running = RNG === 'park-miller' ? randomFrom(RNG, STAR_SEED) : null;
|
|
222
309
|
const start = performance.now();
|
|
223
310
|
|
|
224
311
|
/**
|
|
225
|
-
* Scatters the glitter and stars over the new size,
|
|
226
|
-
* the same seed and size.
|
|
312
|
+
* Scatters the glitter and stars over the new size, in device px.
|
|
227
313
|
*
|
|
228
314
|
* @param {import('./canvas2d.js').Canvas2D} c The canvas.
|
|
229
315
|
*/
|
|
230
316
|
const resize = (c) => {
|
|
231
|
-
|
|
232
|
-
|
|
317
|
+
// Nothing to scatter over while the element has no size (and no numbers spent).
|
|
318
|
+
if (!el.clientWidth || !el.clientHeight) {
|
|
319
|
+
glitter = null;
|
|
320
|
+
stars = [];
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const { dpr } = c;
|
|
324
|
+
const w = c.canvas.width;
|
|
325
|
+
const h = c.canvas.height;
|
|
326
|
+
const rnd = running ?? randomFrom(RNG, STAR_SEED);
|
|
233
327
|
// Glitter: fine bright speckle at device resolution.
|
|
234
328
|
glitter = document.createElement('canvas');
|
|
235
|
-
glitter.width =
|
|
236
|
-
glitter.height =
|
|
329
|
+
glitter.width = w;
|
|
330
|
+
glitter.height = h;
|
|
237
331
|
const g = glitter.getContext('2d');
|
|
238
332
|
if (g) {
|
|
239
|
-
for (let k = 0; k < (
|
|
240
|
-
const x = rnd() *
|
|
241
|
-
const y = rnd() *
|
|
333
|
+
for (let k = 0; k < (w * h) / 70; k++) {
|
|
334
|
+
const x = rnd() * w;
|
|
335
|
+
const y = rnd() * h;
|
|
242
336
|
g.fillStyle = `rgb(255 255 255 / ${0.2 + rnd() ** 2.5 * 0.8})`;
|
|
243
337
|
const size = rnd() < 0.9 ? dpr : dpr * 1.6;
|
|
244
338
|
g.fillRect(x, y, size, size);
|
|
245
339
|
}
|
|
246
340
|
}
|
|
247
|
-
stars = Array.from({ length: Math.round(((
|
|
248
|
-
x: rnd() *
|
|
249
|
-
y: rnd() *
|
|
250
|
-
r: 0.6 + rnd() ** 4 * 3.4,
|
|
341
|
+
stars = Array.from({ length: Math.round(((w * h) / (dpr * dpr * 9000)) * STARS) }, () => ({
|
|
342
|
+
x: rnd() * w,
|
|
343
|
+
y: rnd() * h,
|
|
344
|
+
r: (0.6 + rnd() ** 4 * 3.4) * dpr,
|
|
251
345
|
phase: rnd() * Math.PI * 2,
|
|
252
346
|
rate: 0.6 + rnd() * 1.8,
|
|
253
347
|
cross: rnd() < 0.12,
|
|
254
|
-
tint:
|
|
348
|
+
tint: rnd(),
|
|
255
349
|
}));
|
|
256
350
|
};
|
|
257
351
|
|
|
258
352
|
/**
|
|
259
|
-
* One frame: the nebula drifting, the glitter shimmering
|
|
260
|
-
* stars twinkling.
|
|
353
|
+
* One frame, in device px: the nebula drifting, the glitter shimmering
|
|
354
|
+
* over it, the stars twinkling.
|
|
261
355
|
*
|
|
262
356
|
* @param {import('./canvas2d.js').Canvas2D} c The canvas.
|
|
263
357
|
* @param {number} now The time, ms.
|
|
264
358
|
*/
|
|
265
359
|
const draw = (c, now) => {
|
|
266
|
-
const { ctx,
|
|
360
|
+
const { ctx, dpr } = c;
|
|
361
|
+
const w = c.canvas.width;
|
|
362
|
+
const h = c.canvas.height;
|
|
267
363
|
const t = c.still ? 0 : ((now - start) / 1000) * SPEED;
|
|
364
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
268
365
|
// The nebula, scaled to cover with a margin to drift through.
|
|
269
366
|
const k = Math.max(w / NEBULA_WIDTH, h / NEBULA_HEIGHT) * 1.12;
|
|
270
367
|
const nw = NEBULA_WIDTH * k;
|
|
@@ -279,22 +376,23 @@ export function galaxy(el, config) {
|
|
|
279
376
|
ctx.globalCompositeOperation = 'lighter';
|
|
280
377
|
if (glitter && GLITTER > 0) {
|
|
281
378
|
ctx.globalAlpha = Math.min(1, (0.75 + Math.sin(t * 1.7) * 0.12) * GLITTER);
|
|
282
|
-
ctx.drawImage(glitter, 0, 0
|
|
379
|
+
ctx.drawImage(glitter, 0, 0);
|
|
283
380
|
}
|
|
284
|
-
const hair = 1 / dpr;
|
|
285
381
|
for (const s of stars) {
|
|
286
382
|
const a = 0.35 + 0.65 * (0.5 + 0.5 * Math.sin(s.phase + t * s.rate));
|
|
287
383
|
ctx.globalAlpha = a;
|
|
288
|
-
ctx
|
|
384
|
+
starGlow(ctx, s.x, s.y, s.r * 4, tints[Math.floor(s.tint * tints.length)]);
|
|
289
385
|
if (s.cross) {
|
|
386
|
+
// A cross of light, a CSS px thick.
|
|
290
387
|
ctx.fillStyle = '#ffffff';
|
|
291
388
|
const arm = s.r * 9 * a;
|
|
292
|
-
ctx.fillRect(s.x - arm, s.y -
|
|
293
|
-
ctx.fillRect(s.x -
|
|
389
|
+
ctx.fillRect(s.x - arm, s.y - dpr * 0.5, arm * 2, dpr);
|
|
390
|
+
ctx.fillRect(s.x - dpr * 0.5, s.y - arm, dpr, arm * 2);
|
|
294
391
|
}
|
|
295
392
|
}
|
|
296
393
|
ctx.globalAlpha = 1;
|
|
297
394
|
ctx.globalCompositeOperation = 'source-over';
|
|
395
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
298
396
|
};
|
|
299
397
|
|
|
300
398
|
const { c, stop } = createCanvas2D(el, { draw, resize });
|
package/src/runtime/paper.js
CHANGED
|
@@ -43,6 +43,45 @@ import { PRESETS, ENUMS, RENAMES, FITS } from '../vendor/paper/presets.js';
|
|
|
43
43
|
* @property {number} [scale=1] Share of the pixel ratio drawn at.
|
|
44
44
|
*/
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Each shader's module, loaded on first use, in its own chunk. The paths
|
|
48
|
+
* are literals, not built from the name: a bundler gives a literal path its
|
|
49
|
+
* own chunk, but leaves a template path in a package as it is (Vite's
|
|
50
|
+
* dynamic-import-vars skips node_modules), and the built site 404s on it.
|
|
51
|
+
*/
|
|
52
|
+
const LOADERS = {
|
|
53
|
+
'color-panels': () => import('../vendor/paper/shaders/color-panels.js'),
|
|
54
|
+
dithering: () => import('../vendor/paper/shaders/dithering.js'),
|
|
55
|
+
'dot-grid': () => import('../vendor/paper/shaders/dot-grid.js'),
|
|
56
|
+
'dot-orbit': () => import('../vendor/paper/shaders/dot-orbit.js'),
|
|
57
|
+
'fluted-glass': () => import('../vendor/paper/shaders/fluted-glass.js'),
|
|
58
|
+
'gem-smoke': () => import('../vendor/paper/shaders/gem-smoke.js'),
|
|
59
|
+
'god-rays': () => import('../vendor/paper/shaders/god-rays.js'),
|
|
60
|
+
'grain-gradient': () => import('../vendor/paper/shaders/grain-gradient.js'),
|
|
61
|
+
'halftone-cmyk': () => import('../vendor/paper/shaders/halftone-cmyk.js'),
|
|
62
|
+
'halftone-dots': () => import('../vendor/paper/shaders/halftone-dots.js'),
|
|
63
|
+
heatmap: () => import('../vendor/paper/shaders/heatmap.js'),
|
|
64
|
+
'image-dithering': () => import('../vendor/paper/shaders/image-dithering.js'),
|
|
65
|
+
'lens-distortion': () => import('../vendor/paper/shaders/lens-distortion.js'),
|
|
66
|
+
'liquid-metal': () => import('../vendor/paper/shaders/liquid-metal.js'),
|
|
67
|
+
'mesh-gradient': () => import('../vendor/paper/shaders/mesh-gradient.js'),
|
|
68
|
+
metaballs: () => import('../vendor/paper/shaders/metaballs.js'),
|
|
69
|
+
'neuro-noise': () => import('../vendor/paper/shaders/neuro-noise.js'),
|
|
70
|
+
'paper-texture': () => import('../vendor/paper/shaders/paper-texture.js'),
|
|
71
|
+
'perlin-noise': () => import('../vendor/paper/shaders/perlin-noise.js'),
|
|
72
|
+
'pulsing-border': () => import('../vendor/paper/shaders/pulsing-border.js'),
|
|
73
|
+
'simplex-noise': () => import('../vendor/paper/shaders/simplex-noise.js'),
|
|
74
|
+
'smoke-ring': () => import('../vendor/paper/shaders/smoke-ring.js'),
|
|
75
|
+
spiral: () => import('../vendor/paper/shaders/spiral.js'),
|
|
76
|
+
'static-mesh-gradient': () => import('../vendor/paper/shaders/static-mesh-gradient.js'),
|
|
77
|
+
'static-radial-gradient': () => import('../vendor/paper/shaders/static-radial-gradient.js'),
|
|
78
|
+
swirl: () => import('../vendor/paper/shaders/swirl.js'),
|
|
79
|
+
voronoi: () => import('../vendor/paper/shaders/voronoi.js'),
|
|
80
|
+
warp: () => import('../vendor/paper/shaders/warp.js'),
|
|
81
|
+
water: () => import('../vendor/paper/shaders/water.js'),
|
|
82
|
+
waves: () => import('../vendor/paper/shaders/waves.js'),
|
|
83
|
+
};
|
|
84
|
+
|
|
46
85
|
/** The shaders that sample their picture blurred (mip levels), as Paper's React components set up. */
|
|
47
86
|
const MIPMAPPED = new Set(['fluted-glass', 'gem-smoke', 'heatmap', 'liquid-metal', 'lens-distortion', 'paper-texture', 'water']);
|
|
48
87
|
|
|
@@ -60,6 +99,19 @@ const HOST = new Set(['speed', 'frame']);
|
|
|
60
99
|
*/
|
|
61
100
|
const camel = (name) => name.replace(/-(\w)/g, (_, c) => c.toUpperCase());
|
|
62
101
|
|
|
102
|
+
/**
|
|
103
|
+
* A shader's module, by name.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} name The shader (`mesh-gradient`, `gem-smoke`…).
|
|
106
|
+
* @returns {Promise<Record<string, any>>} Its module: the fragment shader and any picture helper.
|
|
107
|
+
* @throws {TypeError} When there is no shader by that name.
|
|
108
|
+
*/
|
|
109
|
+
async function load(name) {
|
|
110
|
+
const loader = Object.hasOwn(LOADERS, name) ? LOADERS[/** @type {keyof typeof LOADERS} */ (name)] : undefined;
|
|
111
|
+
if (!loader) throw new TypeError(`No Paper shader "${name}" (there are ${Object.keys(LOADERS).join(', ')})`);
|
|
112
|
+
return loader();
|
|
113
|
+
}
|
|
114
|
+
|
|
63
115
|
/**
|
|
64
116
|
* The shader's settings: its preset (the named one or its first), with the
|
|
65
117
|
* config's colours and settings over it.
|
|
@@ -202,8 +254,7 @@ export function paperShader(el, config) {
|
|
|
202
254
|
* @returns {Promise<() => void>} Stops it.
|
|
203
255
|
*/
|
|
204
256
|
async function start(el, config) {
|
|
205
|
-
|
|
206
|
-
const module = await import(`../vendor/paper/shaders/${config.shader}.js`);
|
|
257
|
+
const module = await load(config.shader);
|
|
207
258
|
const fragment = module[`${camel(config.shader)}FragmentShader`];
|
|
208
259
|
if (!fragment) throw new Error(`No fragment shader in Paper's "${config.shader}"`);
|
|
209
260
|
let settings = paperSettings(config);
|
|
@@ -11,6 +11,10 @@ export interface PixelGlitchConfig {
|
|
|
11
11
|
shift?: number;
|
|
12
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
|
+
/** What slips: `cell`, each hot cell on its own; `row`, a whole row at once, now and then, by its hottest cell (up to half of `shift` either way). Default `cell`. */
|
|
15
|
+
tear?: 'cell' | 'row';
|
|
16
|
+
/** A colour for a copy of the blocks, nudged 3px right behind them (an accent under the break). Default none. */
|
|
17
|
+
ghost?: string;
|
|
14
18
|
}
|
|
15
19
|
|
|
16
20
|
/**
|
|
@@ -20,5 +24,8 @@ export interface PixelGlitchConfig {
|
|
|
20
24
|
*/
|
|
21
25
|
export function outlineAlpha(rgba: Uint8ClampedArray, width: number, height: number, radius: number): Uint8ClampedArray;
|
|
22
26
|
|
|
27
|
+
/** How far each row of a heat grid tears this frame (whole CSS px): a row whose hottest cell is over 0.45 tears on about a third of frames, by up to half of `shift` either way, scaled by that heat. */
|
|
28
|
+
export function rowTears(heat: Float32Array, cols: number, rows: number, shift: number, random?: () => number): Float32Array;
|
|
29
|
+
|
|
23
30
|
/** Starts the pixel glitch on an element of text. Returns a function that stops it. */
|
|
24
31
|
export function pixelGlitch(el: HTMLElement, config: PixelGlitchConfig): () => void;
|