@arraypress/effects-astro 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +1 -1
- package/COMPONENTS.md +86 -10
- package/package.json +1 -1
- package/src/components/DitherGradient.astro +41 -0
- package/src/components/FlutedGlow.astro +39 -0
- package/src/components/PixelFountain.astro +20 -7
- package/src/components/PixelGlitchText.astro +4 -3
- package/src/components/PixelHorizon.astro +40 -0
- package/src/components/TruchetField.astro +40 -0
- package/src/index.d.ts +10 -1
- package/src/index.js +6 -1
- package/src/runtime/bayer.d.ts +5 -0
- package/src/runtime/bayer.js +48 -0
- package/src/runtime/dither-field.js +7 -33
- package/src/runtime/dither-gradient.d.ts +91 -0
- package/src/runtime/dither-gradient.js +410 -0
- package/src/runtime/fluted-glow.d.ts +79 -0
- package/src/runtime/fluted-glow.js +312 -0
- package/src/runtime/pixel-fountain.d.ts +29 -13
- package/src/runtime/pixel-fountain.js +183 -42
- package/src/runtime/pixel-glitch.d.ts +2 -0
- package/src/runtime/pixel-glitch.js +8 -2
- package/src/runtime/pixel-horizon.d.ts +60 -0
- package/src/runtime/pixel-horizon.js +274 -0
- package/src/runtime/truchet-field.d.ts +60 -0
- package/src/runtime/truchet-field.js +272 -0
|
@@ -15,13 +15,23 @@
|
|
|
15
15
|
* over the host fires a stream from under it, and a click or tap fires a
|
|
16
16
|
* power-up: a bigger burst whose first frames burn white.
|
|
17
17
|
*
|
|
18
|
+
* With `source: 'pointer'` nothing fires on its own: blocks fire only where
|
|
19
|
+
* the pointer is, as it moves over the host, a few for every stretch it
|
|
20
|
+
* travels (none while it rests or once it leaves). They pop out and up with
|
|
21
|
+
* a little spread and a kick along the pointer's way, arc under gravity and
|
|
22
|
+
* break down within about a second: a block-dust trail off the cursor, in
|
|
23
|
+
* one solid ink (the text's colour unless `color` is set), with a few
|
|
24
|
+
* chunky blocks among the small ones. A tap fires a burst where it lands;
|
|
25
|
+
* a click does too only with `press`.
|
|
26
|
+
*
|
|
18
27
|
* The simulation (`createFountain`) is pure and seeded: it runs in fixed
|
|
19
28
|
* 60 Hz steps, so the same seed and the same emits give the same pixels
|
|
20
29
|
* whatever the frame rate. At most MAX_PIXELS live at once, in a pool that
|
|
21
30
|
* allocates nothing while it runs. Canvas 2D in device pixels, every
|
|
22
31
|
* rectangle on whole pixels, so the blocks stay crisp at any pixel ratio.
|
|
23
32
|
* Drawn only while near the screen and while the tab shows (the shared
|
|
24
|
-
* loop); with prefers-reduced-motion, a still scatter of pixels
|
|
33
|
+
* loop); with prefers-reduced-motion, a still scatter of pixels (nothing
|
|
34
|
+
* at all from the pointer).
|
|
25
35
|
*/
|
|
26
36
|
|
|
27
37
|
import { createCanvas2D } from './canvas2d.js';
|
|
@@ -34,20 +44,22 @@ import { textNodesWithin, paintGlyphs } from './text.js';
|
|
|
34
44
|
|
|
35
45
|
/**
|
|
36
46
|
* @typedef {object} PixelFountainConfig
|
|
37
|
-
* @property {string} [color='#c8ff00'] The pixels' colour when they fire; the darker steps they fade through are made from it.
|
|
38
|
-
* @property {string[]} [colors] The steps a pixel fades through, first to last (overrides `color
|
|
47
|
+
* @property {string} [color='#c8ff00'] The pixels' colour when they fire; the darker steps they fade through are made from it. With `source: 'pointer'`, the one ink every block is drawn in (default the text's colour, `currentColor`).
|
|
48
|
+
* @property {string[]} [colors] The steps a pixel fades through, first to last (overrides `color`; from the pointer, only the first is used).
|
|
39
49
|
* @property {string} [palette] A shared palette by name, its colours as the steps, when there are no `colors`.
|
|
40
50
|
* @property {number} [cell=8] A pixel's size, CSS px (a power-up's biggest blocks are two).
|
|
41
|
-
* @property {number} [rise=240] How far above the edge the pixels can fly, CSS px.
|
|
42
|
-
* @property {number} [rate=14] Pixels a second trickling up on their own, per 1,000 CSS px of edge; 0 for none.
|
|
43
|
-
* @property {number} [burst=40] Pixels a click or tap fires.
|
|
51
|
+
* @property {number} [rise=240] How far above the edge the pixels can fly, CSS px (160 from the pointer: how far above the host the canvas reaches, and about how high a tap's burst flies).
|
|
52
|
+
* @property {number} [rate=14] Pixels a second trickling up on their own, per 1,000 CSS px of edge; 0 for none (always none from the pointer).
|
|
53
|
+
* @property {number} [burst=40] Pixels a click or tap fires (24 from the pointer).
|
|
54
|
+
* @property {number} [trail=10] From the pointer: blocks fired for every 100 CSS px it moves (at most 12 a move).
|
|
55
|
+
* @property {boolean} [press] Whether a mouse click fires a burst: default true, false from the pointer (where a tap still does).
|
|
44
56
|
* @property {number} [gravity=1] How hard they are pulled back, 0–2.
|
|
45
57
|
* @property {number} [spread=0.5] How widely they fan out, 0–1.
|
|
46
58
|
* @property {number} [drift=0.3] How much a wind pushes them sideways, 0–1.
|
|
47
|
-
* @property {number} [life=1.4] The longest a pixel lives, seconds.
|
|
48
|
-
* @property {number} [split=0.4] The chance a pixel splits into smaller ones rather than shrinking, 0–1.
|
|
49
|
-
* @property {number} [flicker=0.35] The chance a pixel flickers out on and off before it goes, 0–1.
|
|
50
|
-
* @property {'edge' | 'text'} [source='edge'] Fire from the host's top edge,
|
|
59
|
+
* @property {number} [life=1.4] The longest a pixel lives, seconds (0.9 from the pointer).
|
|
60
|
+
* @property {number} [split=0.4] The chance a pixel splits into smaller ones rather than shrinking, 0–1 (0.2 from the pointer).
|
|
61
|
+
* @property {number} [flicker=0.35] The chance a pixel flickers out on and off before it goes, 0–1 (0.5 from the pointer).
|
|
62
|
+
* @property {'edge' | 'text' | 'pointer'} [source='edge'] Fire from the host's top edge, from the filled pixels of its text, or only from the pointer as it moves over the host.
|
|
51
63
|
* @property {string} [host] A selector for the element the pixels fire from and the pointer is followed over: the nearest ancestor that matches, else the first on the page. Default the element's positioned parent.
|
|
52
64
|
* @property {number} [seed=1] Seeds the randomness.
|
|
53
65
|
*/
|
|
@@ -68,10 +80,28 @@ const LAST_TIER = TIER_CELLS.length - 1;
|
|
|
68
80
|
const GRAVITY = 1000;
|
|
69
81
|
|
|
70
82
|
/** How hard each kind of emit throws, a share of the throw that reaches 0.62 of `rise`. */
|
|
71
|
-
const POWER = { ambient: [0.5, 0.35], move: [0.62, 0.3], burst: [0.7, 0.6] };
|
|
83
|
+
const POWER = { ambient: [0.5, 0.35], move: [0.62, 0.3], burst: [0.7, 0.6], trail: [0.15, 0.7], tap: [0.4, 0.55] };
|
|
72
84
|
|
|
73
85
|
/** How far into its flight a fired pixel may already be, seconds: a burst comes out as a spurt, not one slab. */
|
|
74
|
-
const LEAD = { ambient: 0, move: 0.06, burst: 0.3 };
|
|
86
|
+
const LEAD = { ambient: 0, move: 0.06, burst: 0.3, trail: 0, tap: 0.05 };
|
|
87
|
+
|
|
88
|
+
/** The size a pixel fires at, by what fired it: a double block below the first share of a roll, a block below the second, else a half. */
|
|
89
|
+
const TIERS = { ambient: [0, 0.7], move: [0.15, 1], burst: [0.4, 1], trail: [0.2, 0.7], tap: [0.35, 0.85] };
|
|
90
|
+
|
|
91
|
+
/** How far a pointer's block may lean from straight up, radians at `spread` 1 (a tap's twice as far). */
|
|
92
|
+
const FAN = 1.1;
|
|
93
|
+
|
|
94
|
+
/** The share of the pointer's way (CSS px a move) a trail block is kicked along, as CSS px/s. */
|
|
95
|
+
const KICK = 7;
|
|
96
|
+
|
|
97
|
+
/** The most kick a trail block takes along the pointer's way, CSS px/s. */
|
|
98
|
+
const MAX_KICK = 260;
|
|
99
|
+
|
|
100
|
+
/** The most blocks one pointer move fires. */
|
|
101
|
+
export const TRAIL_CAP = 12;
|
|
102
|
+
|
|
103
|
+
/** The settings that change with `source: 'pointer'`: no trickle, a short life, fewer splits, more flicker, a lower canvas, a smaller tap. */
|
|
104
|
+
export const POINTER_DEFAULTS = { rate: 0, life: 0.9, split: 0.2, flicker: 0.5, rise: 160, burst: 24, trail: 10 };
|
|
75
105
|
|
|
76
106
|
/** The wind at `drift` 1, CSS px/s² (a slow swing either way). */
|
|
77
107
|
const WIND = 70;
|
|
@@ -120,11 +150,14 @@ const HOT_MIX = 0.6;
|
|
|
120
150
|
* @property {number} [life=1.4] The longest life, seconds.
|
|
121
151
|
* @property {number} [split=0.4] The chance of a split at each break, 0–1.
|
|
122
152
|
* @property {number} [flicker=0.35] The chance of flickering out, 0–1.
|
|
153
|
+
* @property {number} [trail=10] Blocks a pointer's move fires for every 100 CSS px it travels.
|
|
123
154
|
* @property {number} [seed=1] The seed.
|
|
124
155
|
*/
|
|
125
156
|
|
|
126
157
|
/**
|
|
127
|
-
* @typedef {'ambient' | 'move' | 'burst'} EmitKind
|
|
158
|
+
* @typedef {'ambient' | 'move' | 'burst' | 'trail' | 'tap'} EmitKind
|
|
159
|
+
* What fired a pixel: the trickle, the pointer moving over the edge or the
|
|
160
|
+
* letters, a press (a power-up); or, from the pointer, its trail and a tap.
|
|
128
161
|
*/
|
|
129
162
|
|
|
130
163
|
/**
|
|
@@ -263,6 +296,8 @@ export function inkedCells(rgba, width, height, cell, floor = height) {
|
|
|
263
296
|
* emitNear: (x: number, y: number, n: number, kind?: EmitKind, radius?: number) => number,
|
|
264
297
|
* emitEdge: (x: number, n: number, kind?: EmitKind) => number,
|
|
265
298
|
* emitAmbient: (n: number, kind?: EmitKind) => number,
|
|
299
|
+
* moveTo: (x: number, y: number) => number,
|
|
300
|
+
* lift: () => void,
|
|
266
301
|
* step: (dt?: number) => void,
|
|
267
302
|
* clear: () => void,
|
|
268
303
|
* }} The fountain: `pixels[0…count)` are alive.
|
|
@@ -277,6 +312,7 @@ export function createFountain(options) {
|
|
|
277
312
|
const LIFE = options.life ?? 1.4;
|
|
278
313
|
const SPLIT = options.split ?? 0.4;
|
|
279
314
|
const FLICKER = options.flicker ?? 0.35;
|
|
315
|
+
const TRAIL = options.trail ?? 10;
|
|
280
316
|
const random = seededRandom(options.seed ?? 1);
|
|
281
317
|
// The throw that reaches 0.62 of `rise` at the nominal pull, whatever `gravity` is: gravity changes the arc, not the throw.
|
|
282
318
|
const THROW = Math.sqrt(2 * GRAVITY * RISE * 0.62);
|
|
@@ -291,6 +327,10 @@ export function createFountain(options) {
|
|
|
291
327
|
/** @type {Float32Array | null} */
|
|
292
328
|
let origins = null;
|
|
293
329
|
let topCount = 0;
|
|
330
|
+
// The pointer's last place (NaN while it is away), and the share of a block its moves owe.
|
|
331
|
+
let px = NaN;
|
|
332
|
+
let py = NaN;
|
|
333
|
+
let carry = 0;
|
|
294
334
|
|
|
295
335
|
/** @type {Pixel[]} */
|
|
296
336
|
const pixels = Array.from({ length: MAX_PIXELS }, () => ({
|
|
@@ -334,24 +374,36 @@ export function createFountain(options) {
|
|
|
334
374
|
* @param {number} x Centre, across, CSS px.
|
|
335
375
|
* @param {number} y Centre, down, CSS px.
|
|
336
376
|
* @param {EmitKind} kind What fired it.
|
|
377
|
+
* @param {number} [kx=0] A kick across, CSS px/s (the pointer's way, for its trail).
|
|
378
|
+
* @param {number} [ky=0] A kick down, CSS px/s.
|
|
337
379
|
* @returns {boolean} False when the pool is full.
|
|
338
380
|
*/
|
|
339
|
-
const spawn = (x, y, kind) => {
|
|
381
|
+
const spawn = (x, y, kind, kx = 0, ky = 0) => {
|
|
340
382
|
if (count >= MAX_PIXELS) return false;
|
|
341
383
|
const p = pixels[count++];
|
|
342
384
|
const [base, range] = POWER[kind];
|
|
343
385
|
const speed = THROW * (base + random() * range);
|
|
344
|
-
const
|
|
386
|
+
const pointer = kind === 'trail' || kind === 'tap';
|
|
345
387
|
p.x = x;
|
|
346
388
|
p.y = y;
|
|
347
|
-
|
|
348
|
-
|
|
389
|
+
if (pointer) {
|
|
390
|
+
// Out and up, leaning either way by `spread`, and carried a little along the pointer's way.
|
|
391
|
+
const lean = (random() * 2 - 1) * SPREAD * FAN * (kind === 'tap' ? 2 : 1);
|
|
392
|
+
p.vx = Math.sin(lean) * speed + kx;
|
|
393
|
+
p.vy = -Math.cos(lean) * speed + ky;
|
|
394
|
+
} else {
|
|
395
|
+
const fan = kind === 'burst' ? 2 : 1;
|
|
396
|
+
p.vx = (random() * 2 - 1) * SPREAD * fan * speed * 0.5;
|
|
397
|
+
p.vy = -speed;
|
|
398
|
+
}
|
|
349
399
|
p.age = 0;
|
|
350
400
|
// Gone before it lands: a flight's length at most, so each runs through its colours in the air.
|
|
351
|
-
|
|
401
|
+
// The pointer's blocks are thrown from mid-air, so they only live their life (and fall past where they started).
|
|
402
|
+
const flight = G > 0 && !pointer ? (2 * speed) / G : Infinity;
|
|
352
403
|
p.life = Math.min(LIFE * (0.6 + random() * 0.4), flight * (0.85 + random() * 0.2));
|
|
353
404
|
const roll = random();
|
|
354
|
-
|
|
405
|
+
const [big, block] = TIERS[kind];
|
|
406
|
+
p.tier = roll < big ? 0 : roll < block ? 1 : 2;
|
|
355
407
|
schedule(p);
|
|
356
408
|
p.hot = kind === 'burst';
|
|
357
409
|
p.flicker = random() < FLICKER ? 0.45 + random() * 0.3 : 2;
|
|
@@ -453,7 +505,7 @@ export function createFountain(options) {
|
|
|
453
505
|
topCount = tops;
|
|
454
506
|
},
|
|
455
507
|
emit(x, y, n, kind = 'burst') {
|
|
456
|
-
const jitter = (kind === 'burst' ? 2.5 : kind === 'move' ? 1 : 0) * CELL;
|
|
508
|
+
const jitter = (kind === 'burst' ? 2.5 : kind === 'move' || kind === 'tap' ? 1 : 0) * CELL;
|
|
457
509
|
let made = 0;
|
|
458
510
|
for (let i = 0; i < n; i++) {
|
|
459
511
|
if (!spawn(x + (random() * 2 - 1) * jitter, y, kind)) break;
|
|
@@ -490,6 +542,40 @@ export function createFountain(options) {
|
|
|
490
542
|
}
|
|
491
543
|
return made;
|
|
492
544
|
},
|
|
545
|
+
moveTo(x, y) {
|
|
546
|
+
const was = [px, py];
|
|
547
|
+
px = x;
|
|
548
|
+
py = y;
|
|
549
|
+
// The first place after a lift only marks where the pointer is.
|
|
550
|
+
if (!Number.isFinite(was[0])) return 0;
|
|
551
|
+
const dx = x - was[0];
|
|
552
|
+
const dy = y - was[1];
|
|
553
|
+
const d = Math.hypot(dx, dy);
|
|
554
|
+
if (!d) return 0;
|
|
555
|
+
carry += (d * TRAIL) / 100;
|
|
556
|
+
// A hair under a whole block counts: ten moves of a tenth must make one.
|
|
557
|
+
const owed = Math.floor(carry + 1e-9);
|
|
558
|
+
const n = Math.min(TRAIL_CAP, owed);
|
|
559
|
+
// What the cap cut off is dropped, not saved up for later moves.
|
|
560
|
+
carry = Math.max(0, carry - owed);
|
|
561
|
+
const kick = Math.min(MAX_KICK, d * KICK) / d;
|
|
562
|
+
const jitter = CELL;
|
|
563
|
+
let made = 0;
|
|
564
|
+
for (let i = 0; i < n; i++) {
|
|
565
|
+
// Spaced along the stretch just travelled, ending at the pointer, so a quick sweep leaves an even trail.
|
|
566
|
+
const t = (i + random()) / n;
|
|
567
|
+
const x0 = was[0] + dx * t + (random() * 2 - 1) * jitter;
|
|
568
|
+
const y0 = was[1] + dy * t + (random() * 2 - 1) * jitter;
|
|
569
|
+
if (!spawn(x0, y0, 'trail', dx * kick, dy * kick * 0.5)) break;
|
|
570
|
+
made++;
|
|
571
|
+
}
|
|
572
|
+
return made;
|
|
573
|
+
},
|
|
574
|
+
lift() {
|
|
575
|
+
px = NaN;
|
|
576
|
+
py = NaN;
|
|
577
|
+
carry = 0;
|
|
578
|
+
},
|
|
493
579
|
step(dt = STEP) {
|
|
494
580
|
time += dt;
|
|
495
581
|
owed += ((RATE * width) / 1000) * dt;
|
|
@@ -552,25 +638,39 @@ function findHost(el, selector) {
|
|
|
552
638
|
return /** @type {HTMLElement} */ (el.offsetParent ?? el.parentElement ?? el);
|
|
553
639
|
}
|
|
554
640
|
|
|
641
|
+
/**
|
|
642
|
+
* Whether a colour asks for the text's own (none, or `currentColor`).
|
|
643
|
+
*
|
|
644
|
+
* @param {string | undefined} color The colour given.
|
|
645
|
+
* @returns {boolean} True to read the element's CSS colour.
|
|
646
|
+
*/
|
|
647
|
+
const isCurrent = (color) => !color || color.toLowerCase() === 'currentcolor';
|
|
648
|
+
|
|
555
649
|
/**
|
|
556
650
|
* Starts the pixel fountain on an element (its first canvas is drawn
|
|
557
651
|
* into). The element should be positioned over its host's top edge (or,
|
|
558
|
-
* with `source: 'text'`, over the host and the space above
|
|
559
|
-
* bottom on the host's bottom edge: the component does this.
|
|
652
|
+
* with `source: 'text'` or `'pointer'`, over the host and the space above
|
|
653
|
+
* it), with its bottom on the host's bottom edge: the component does this.
|
|
560
654
|
*
|
|
561
655
|
* @param {HTMLElement} el The element.
|
|
562
656
|
* @param {PixelFountainConfig} config The settings.
|
|
563
657
|
* @returns {() => void} Stops it.
|
|
564
658
|
*/
|
|
565
659
|
export function pixelFountain(el, config) {
|
|
566
|
-
const
|
|
567
|
-
|
|
568
|
-
const
|
|
569
|
-
const
|
|
570
|
-
const
|
|
571
|
-
|
|
572
|
-
|
|
660
|
+
const POINTER = config.source === 'pointer';
|
|
661
|
+
// From the pointer: its own defaults, and never a trickle.
|
|
662
|
+
const settings = POINTER ? { ...POINTER_DEFAULTS, ...config, rate: 0 } : config;
|
|
663
|
+
const CELL = Math.max(2, settings.cell ?? 8);
|
|
664
|
+
const BURST = settings.burst ?? 40;
|
|
665
|
+
const PRESS = settings.press ?? !POINTER;
|
|
666
|
+
const TEXT = settings.source === 'text';
|
|
667
|
+
const host = findHost(el, settings.host);
|
|
668
|
+
const fountain = createFountain({ ...settings, cell: CELL, width: 1, height: 1 });
|
|
669
|
+
|
|
670
|
+
// The ramp's steps; from the pointer, one ink (empty for the text's colour).
|
|
671
|
+
let steps = POINTER ? pickColors(settings, isCurrent(settings.color) ? [] : [/** @type {string} */ (settings.color)]) : pickColors(settings, fadeSteps(settings.color ?? '#c8ff00'));
|
|
573
672
|
let hot = '';
|
|
673
|
+
let ink = '';
|
|
574
674
|
/**
|
|
575
675
|
* Takes a new set of steps, and the hot colour from the first.
|
|
576
676
|
*
|
|
@@ -578,17 +678,24 @@ export function pixelFountain(el, config) {
|
|
|
578
678
|
*/
|
|
579
679
|
const recolor = (next) => {
|
|
580
680
|
steps = next;
|
|
681
|
+
readInk();
|
|
581
682
|
try {
|
|
582
683
|
hot = toHex(mixOklab(parseColor(steps[0]), [1, 1, 1], HOT_MIX));
|
|
583
684
|
} catch {
|
|
584
685
|
hot = steps[0];
|
|
585
686
|
}
|
|
586
687
|
};
|
|
688
|
+
/** The pointer's one ink: the first step, or the element's CSS colour. */
|
|
689
|
+
const readInk = () => {
|
|
690
|
+
if (POINTER) ink = isCurrent(steps[0]) ? getComputedStyle(el).color : steps[0];
|
|
691
|
+
};
|
|
587
692
|
recolor(steps);
|
|
588
693
|
const unwatch = watchColors(el, ({ colors }) => recolor(colors));
|
|
589
694
|
|
|
590
695
|
let acc = 0;
|
|
591
696
|
let stopped = false;
|
|
697
|
+
/** Whether the last frame drew anything (an empty pool is cleared once, then left). */
|
|
698
|
+
let drawn = true;
|
|
592
699
|
/** @type {import('./canvas2d.js').Canvas2D | null} */
|
|
593
700
|
let canvas2d = null;
|
|
594
701
|
|
|
@@ -614,25 +721,31 @@ export function pixelFountain(el, config) {
|
|
|
614
721
|
|
|
615
722
|
/**
|
|
616
723
|
* With reduced motion: a still scatter, the fountain run on its own
|
|
617
|
-
* for a moment from its seed and held.
|
|
724
|
+
* for a moment from its seed and held (nothing from the pointer).
|
|
618
725
|
*
|
|
619
726
|
* @param {import('./canvas2d.js').Canvas2D} c The canvas.
|
|
620
727
|
*/
|
|
621
728
|
const scatter = (c) => {
|
|
622
729
|
fountain.clear();
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
730
|
+
if (!POINTER) {
|
|
731
|
+
for (let i = 0; i < 90; i++) {
|
|
732
|
+
fountain.step();
|
|
733
|
+
if (i % 5 === 0) fountain.emitAmbient(1, 'move');
|
|
734
|
+
}
|
|
626
735
|
}
|
|
736
|
+
drawn = true;
|
|
627
737
|
render(c);
|
|
628
738
|
};
|
|
629
739
|
|
|
630
740
|
/**
|
|
631
|
-
* Draws the live pixels, each on its own grid in device pixels
|
|
741
|
+
* Draws the live pixels, each on its own grid in device pixels: through
|
|
742
|
+
* the ramp, or from the pointer all in its one ink.
|
|
632
743
|
*
|
|
633
744
|
* @param {import('./canvas2d.js').Canvas2D} c The canvas.
|
|
634
745
|
*/
|
|
635
746
|
const render = (c) => {
|
|
747
|
+
if (!fountain.count && !drawn) return;
|
|
748
|
+
drawn = fountain.count > 0;
|
|
636
749
|
const { ctx, dpr } = c;
|
|
637
750
|
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
638
751
|
ctx.clearRect(0, 0, c.canvas.width, c.canvas.height);
|
|
@@ -640,12 +753,15 @@ export function pixelFountain(el, config) {
|
|
|
640
753
|
const floor = c.canvas.height;
|
|
641
754
|
const n = steps.length;
|
|
642
755
|
let fill = '';
|
|
756
|
+
if (POINTER) ctx.fillStyle = fill = ink;
|
|
643
757
|
for (let i = 0; i < fountain.count; i++) {
|
|
644
758
|
const p = fountain.pixels[i];
|
|
645
759
|
if (!p.on) continue;
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
760
|
+
if (!POINTER) {
|
|
761
|
+
const shade = shadeAt(p.age / p.life, n, p.hot);
|
|
762
|
+
const color = shade < 0 ? hot : steps[shade];
|
|
763
|
+
if (color !== fill) ctx.fillStyle = fill = color;
|
|
764
|
+
}
|
|
649
765
|
const size = sizes[p.tier];
|
|
650
766
|
const [x, y] = pixelRect(p.x * dpr, p.y * dpr, size, floor);
|
|
651
767
|
ctx.fillRect(x, y, size, size);
|
|
@@ -656,6 +772,8 @@ export function pixelFountain(el, config) {
|
|
|
656
772
|
resize(c) {
|
|
657
773
|
canvas2d = c;
|
|
658
774
|
fountain.resize(c.width, c.height);
|
|
775
|
+
readInk();
|
|
776
|
+
drawn = true;
|
|
659
777
|
if (TEXT) readText();
|
|
660
778
|
else if (c.still) scatter(c);
|
|
661
779
|
},
|
|
@@ -679,6 +797,18 @@ export function pixelFountain(el, config) {
|
|
|
679
797
|
let last = /** @type {{ x: number, y: number } | null} */ (null);
|
|
680
798
|
let travel = 0;
|
|
681
799
|
|
|
800
|
+
/**
|
|
801
|
+
* A point in the canvas's CSS px.
|
|
802
|
+
*
|
|
803
|
+
* @param {number} clientX Viewport x.
|
|
804
|
+
* @param {number} clientY Viewport y.
|
|
805
|
+
* @returns {[number, number]} Across and down the canvas.
|
|
806
|
+
*/
|
|
807
|
+
const toCanvas = (clientX, clientY) => {
|
|
808
|
+
const box = c.canvas.getBoundingClientRect();
|
|
809
|
+
return [clientX - box.left, clientY - box.top];
|
|
810
|
+
};
|
|
811
|
+
|
|
682
812
|
/**
|
|
683
813
|
* Fires pixels from under a point.
|
|
684
814
|
*
|
|
@@ -688,14 +818,22 @@ export function pixelFountain(el, config) {
|
|
|
688
818
|
* @param {EmitKind} kind What fired them.
|
|
689
819
|
*/
|
|
690
820
|
const fireAt = (clientX, clientY, n, kind) => {
|
|
691
|
-
const
|
|
692
|
-
|
|
693
|
-
if (TEXT) fountain.emitNear(x,
|
|
821
|
+
const [x, y] = toCanvas(clientX, clientY);
|
|
822
|
+
if (POINTER) fountain.emit(x, y, n, 'tap');
|
|
823
|
+
else if (TEXT) fountain.emitNear(x, y, n, kind, kind === 'burst' ? 110 : 60);
|
|
694
824
|
else fountain.emitEdge(x, n, kind);
|
|
695
825
|
};
|
|
696
826
|
|
|
697
827
|
/** @param {PointerEvent} e The move. */
|
|
698
828
|
const move = (e) => {
|
|
829
|
+
if (POINTER) {
|
|
830
|
+
// Only over the host's own box (a child may overflow it).
|
|
831
|
+
const box = host.getBoundingClientRect();
|
|
832
|
+
if (e.clientX < box.left || e.clientX > box.right || e.clientY < box.top || e.clientY > box.bottom) return fountain.lift();
|
|
833
|
+
const [x, y] = toCanvas(e.clientX, e.clientY);
|
|
834
|
+
fountain.moveTo(x, y);
|
|
835
|
+
return;
|
|
836
|
+
}
|
|
699
837
|
if (last) travel += Math.hypot(e.clientX - last.x, e.clientY - last.y);
|
|
700
838
|
last = { x: e.clientX, y: e.clientY };
|
|
701
839
|
const n = Math.min(8, Math.floor(travel / 10));
|
|
@@ -703,13 +841,16 @@ export function pixelFountain(el, config) {
|
|
|
703
841
|
travel -= n * 10;
|
|
704
842
|
fireAt(e.clientX, e.clientY, n, 'move');
|
|
705
843
|
};
|
|
706
|
-
/** Forgets the pointer's path when it leaves the host. */
|
|
844
|
+
/** Forgets the pointer's path when it leaves the host (and picks up the text's colour again as it comes back). */
|
|
707
845
|
const leave = () => {
|
|
708
846
|
last = null;
|
|
709
847
|
travel = 0;
|
|
848
|
+
fountain.lift();
|
|
849
|
+
readInk();
|
|
710
850
|
};
|
|
711
|
-
/** @param {PointerEvent} e The press
|
|
851
|
+
/** @param {PointerEvent} e The press: a tap always fires, a click only with `press`. */
|
|
712
852
|
const press = (e) => {
|
|
853
|
+
if (!PRESS && e.pointerType === 'mouse') return;
|
|
713
854
|
if (!pressAt(host, e)) return;
|
|
714
855
|
fireAt(e.clientX, e.clientY, BURST, 'burst');
|
|
715
856
|
};
|
|
@@ -15,6 +15,8 @@ export interface PixelGlitchConfig {
|
|
|
15
15
|
tear?: 'cell' | 'row';
|
|
16
16
|
/** A colour for a copy of the blocks, nudged 3px right behind them (an accent under the break). Default none. */
|
|
17
17
|
ghost?: string;
|
|
18
|
+
/** Break into pixels under the pointer. False only draws the text at rest (with `stroke`, its merged outline), everywhere, with reduced motion and on touch screens too: the same look, the pointer left to something else. Default true. */
|
|
19
|
+
glitch?: boolean;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
/**
|
|
@@ -29,6 +29,9 @@
|
|
|
29
29
|
* `pointer-events: none` (a big wordmark's line box often overlaps links).
|
|
30
30
|
* Animates only while some cell is hot, and only while the text is on
|
|
31
31
|
* screen. Off with prefers-reduced-motion and without a hovering pointer.
|
|
32
|
+
* With `glitch: false` it never breaks: the text (with `stroke`, its merged
|
|
33
|
+
* outline) is only drawn at rest, everywhere, for a wordmark that should
|
|
34
|
+
* look the same but leave the pointer to something else.
|
|
32
35
|
* Repainted when the text's own face has loaded (a face used only here can
|
|
33
36
|
* arrive after `document.fonts.ready`).
|
|
34
37
|
*
|
|
@@ -49,6 +52,7 @@ import { textNodes, paintGlyphs } from './text.js';
|
|
|
49
52
|
* @property {number} [stroke=0] With a width (CSS px), the text at rest is an outline that the pixels fill.
|
|
50
53
|
* @property {'cell' | 'row'} [tear='cell'] What slips: each hot cell on its own, or a whole row at once.
|
|
51
54
|
* @property {string} [ghost] A colour for a copy of the blocks, nudged right behind them (none by default).
|
|
55
|
+
* @property {boolean} [glitch=true] Break into pixels under the pointer; false only draws the text at rest (the merged outline with `stroke`).
|
|
52
56
|
*/
|
|
53
57
|
|
|
54
58
|
/** How far the ghost sits right of its block, CSS px. */
|
|
@@ -134,7 +138,9 @@ export function outlineAlpha(rgba, width, height, radius) {
|
|
|
134
138
|
* @returns {() => void} Stops it.
|
|
135
139
|
*/
|
|
136
140
|
export function pixelGlitch(el, config) {
|
|
137
|
-
|
|
141
|
+
const GLITCH = config.glitch !== false;
|
|
142
|
+
// Drawing the text at rest only is not motion, and needs no hover.
|
|
143
|
+
if (GLITCH && (prefersReducedMotion() || !canHover())) return () => {};
|
|
138
144
|
const CELL = config.cell ?? 14;
|
|
139
145
|
const REACH = config.reach ?? 120;
|
|
140
146
|
const DECAY = config.decay ?? 0.9;
|
|
@@ -339,7 +345,7 @@ export function pixelGlitch(el, config) {
|
|
|
339
345
|
}
|
|
340
346
|
heatAt(x, y);
|
|
341
347
|
};
|
|
342
|
-
document.addEventListener('pointermove', move, { passive: true });
|
|
348
|
+
if (GLITCH) document.addEventListener('pointermove', move, { passive: true });
|
|
343
349
|
|
|
344
350
|
// Repaint when the text changes size (fit-text, fonts loading).
|
|
345
351
|
let pending = 0;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { PaletteName } from './palettes.js';
|
|
2
|
+
|
|
3
|
+
/** Which side the colour fills from. */
|
|
4
|
+
export type HorizonSide = 'bottom' | 'left' | 'top' | 'right';
|
|
5
|
+
|
|
6
|
+
export interface PixelHorizonConfig {
|
|
7
|
+
/** Two CSS hex colours: the solid fill and the edge colour the blocks fade to at the frontier (one colour for both). Default an acid green. */
|
|
8
|
+
colors?: string[];
|
|
9
|
+
/** A shared palette by name (`PALETTES`, any case), when there are no `colors`: its last and first colours. */
|
|
10
|
+
palette?: PaletteName | (string & {});
|
|
11
|
+
/** A CSS hex colour to fill behind the blocks; none leaves the canvas clear there. */
|
|
12
|
+
background?: string;
|
|
13
|
+
/** Which side the colour fills from. Default 'bottom'. */
|
|
14
|
+
side?: HorizonSide;
|
|
15
|
+
/** A block's side, in CSS px (rounded to whole screen pixels). Default 14. */
|
|
16
|
+
block?: number;
|
|
17
|
+
/** Where the frontier's middle sits, a share of the way from the side across. Default 0.3. */
|
|
18
|
+
level?: number;
|
|
19
|
+
/** How deep the broken band is, a share of the way across. Default 0.3. */
|
|
20
|
+
spread?: number;
|
|
21
|
+
/** How far the frontier swings, a share of the way across. Default 0.06. */
|
|
22
|
+
sway?: number;
|
|
23
|
+
/** How far the frontier wanders along its length, a share of the way across. Default 0.08. */
|
|
24
|
+
wander?: number;
|
|
25
|
+
/** How often a block in the band rolls again, times a second. Default 0.6. */
|
|
26
|
+
rate?: number;
|
|
27
|
+
/** Which random blocks, so two cards differ. Default 0. */
|
|
28
|
+
seed?: number;
|
|
29
|
+
/** Plays every clock faster or slower; 0 for one still frame. Default 1. */
|
|
30
|
+
speed?: number;
|
|
31
|
+
/** An area kept empty: centre (0–1 from the top left), radius (share of the height), depth (0–1). */
|
|
32
|
+
quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The sides, in the order the shader numbers them. */
|
|
36
|
+
export declare const HORIZON_SIDES: readonly ['bottom', 'left', 'top', 'right'];
|
|
37
|
+
|
|
38
|
+
/** The settings' defaults, shared by the shader and `horizonBlocks`. */
|
|
39
|
+
export declare const HORIZON_DEFAULTS: {
|
|
40
|
+
side: HorizonSide;
|
|
41
|
+
block: number;
|
|
42
|
+
level: number;
|
|
43
|
+
spread: number;
|
|
44
|
+
sway: number;
|
|
45
|
+
wander: number;
|
|
46
|
+
rate: number;
|
|
47
|
+
seed: number;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** One frame block for block on a target exactly `cols` by `rows` blocks: each block's chance and whether it shows. */
|
|
51
|
+
export declare function horizonBlocks(config: PixelHorizonConfig, cols: number, rows: number, time?: number): { chance: number; lit: boolean }[][];
|
|
52
|
+
|
|
53
|
+
/** The two colours as the shader takes them: the fill and the edge's. */
|
|
54
|
+
export declare function horizonColors(config: PixelHorizonConfig): [string, string];
|
|
55
|
+
|
|
56
|
+
/** A CSS `background` that stands in for the horizon before the shader starts and without WebGL. */
|
|
57
|
+
export declare function horizonStill(config: PixelHorizonConfig): string;
|
|
58
|
+
|
|
59
|
+
/** Starts the horizon on an element (its first canvas, one pixel a block). Returns a function that stops it. */
|
|
60
|
+
export declare function pixelHorizon(el: HTMLElement, config: PixelHorizonConfig): () => void;
|