@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.
@@ -10,7 +10,10 @@
10
10
  *
11
11
  * With `stroke` set, the text at rest is a hollow outline of that width and
12
12
  * the pixels it breaks into fill the letters solid, so the pointer lights
13
- * the word up as it passes.
13
+ * the word up as it passes. The outline is one shape for the whole word
14
+ * (the solid letters less themselves eroded by the width), so letters that
15
+ * touch or overlap under tight tracking merge rather than each outline
16
+ * crossing the next.
14
17
  *
15
18
  * The element keeps its text (it only turns transparent once the canvas
16
19
  * has drawn), so fit-text, selection and the no-JS look are unchanged.
@@ -21,8 +24,11 @@
21
24
  * `pointer-events: none` (a big wordmark's line box often overlaps links).
22
25
  * Animates only while some cell is hot, and only while the text is on
23
26
  * screen. Off with prefers-reduced-motion and without a hovering pointer.
27
+ * Repainted when the text's own face has loaded (a face used only here can
28
+ * arrive after `document.fonts.ready`).
24
29
  *
25
- * From the SampleGrid (WaveGrid) theme's footer.
30
+ * From the SampleGrid (WaveGrid) theme's footer; the merged outline and the
31
+ * font reload from the Logomarks site's.
26
32
  */
27
33
 
28
34
  import { canHover, prefersReducedMotion } from './device.js';
@@ -38,6 +44,44 @@ import { textNodes } from './text.js';
38
44
  * @property {number} [stroke=0] With a width (CSS px), the text at rest is an outline that the pixels fill.
39
45
  */
40
46
 
47
+ /** The 16 directions the outline's erosion looks in, as unit vectors. */
48
+ const AROUND = Array.from({ length: 16 }, (_, k) => [Math.cos((k / 16) * Math.PI * 2), Math.sin((k / 16) * Math.PI * 2)]);
49
+
50
+ /**
51
+ * The outline of a shape, from its coverage: each pixel's alpha less the
52
+ * smallest alpha within `radius` of it (looked for in 16 directions), so
53
+ * the line runs `radius` px inside the shape's edge and stays smooth where
54
+ * the edge is antialiased. Shapes that touch or overlap give one outline
55
+ * round the lot.
56
+ *
57
+ * @param {Uint8ClampedArray} rgba The shape, RGBA (only alpha is read).
58
+ * @param {number} width Its width, px.
59
+ * @param {number} height Its height, px.
60
+ * @param {number} radius The outline's width, px (rounded, at least 1).
61
+ * @returns {Uint8ClampedArray} The outline's alpha, one byte a pixel.
62
+ */
63
+ export function outlineAlpha(rgba, width, height, radius) {
64
+ const r = Math.max(1, Math.round(radius));
65
+ const steps = AROUND.map(([x, y]) => [Math.round(x * r), Math.round(y * r)]);
66
+ const out = new Uint8ClampedArray(width * height);
67
+ for (let y = 0; y < height; y++) {
68
+ for (let x = 0; x < width; x++) {
69
+ const v = rgba[(y * width + x) * 4 + 3];
70
+ if (!v) continue;
71
+ let low = v;
72
+ for (const [dx, dy] of steps) {
73
+ const nx = x + dx;
74
+ const ny = y + dy;
75
+ const n = nx < 0 || ny < 0 || nx >= width || ny >= height ? 0 : rgba[(ny * width + nx) * 4 + 3];
76
+ if (n < low) low = n;
77
+ if (!low) break;
78
+ }
79
+ out[y * width + x] = v - low;
80
+ }
81
+ }
82
+ return out;
83
+ }
84
+
41
85
  /**
42
86
  * Starts the pixel glitch on an element of text.
43
87
  *
@@ -152,8 +196,8 @@ export function pixelGlitch(el, config) {
152
196
 
153
197
  /**
154
198
  * Draws the text at rest into `crisp`, each glyph where the browser put
155
- * it: solid, or with `stroke` an outline (the stroke is centred on the
156
- * glyph's edge, so it is drawn twice as wide and the inner half cut away).
199
+ * it: solid, or with `stroke` one outline round the whole word (see
200
+ * outlineAlpha), so overlapping letters do not show their crossings.
157
201
  */
158
202
  const paint = () => {
159
203
  const texts = textNodes(el);
@@ -182,8 +226,6 @@ export function pixelGlitch(el, config) {
182
226
  c2.setTransform(dpr, 0, 0, dpr, 0, 0);
183
227
  c2.clearRect(0, 0, g.w, g.h);
184
228
  c2.fillStyle = g.ink;
185
- c2.strokeStyle = g.ink;
186
- c2.lineJoin = 'round';
187
229
  c2.font = `${cs.fontStyle} ${cs.fontWeight} ${cs.fontSize} ${cs.fontFamily}`;
188
230
  c2.textBaseline = 'alphabetic';
189
231
  const range = document.createRange();
@@ -209,12 +251,16 @@ export function pixelGlitch(el, config) {
209
251
  eachGlyph((ch, x, y) => c2.fillText(ch, x, y));
210
252
  g.alpha = c2.getImageData(0, 0, crisp.width, crisp.height).data;
211
253
  if (STROKE > 0) {
212
- c2.clearRect(0, 0, g.w, g.h);
213
- c2.lineWidth = STROKE * 2;
214
- eachGlyph((ch, x, y) => c2.strokeText(ch, x, y));
215
- c2.globalCompositeOperation = 'destination-out';
216
- eachGlyph((ch, x, y) => c2.fillText(ch, x, y));
254
+ // The pixels still sample the solid letters (g.alpha); what shows at rest is their outline, in the ink.
255
+ const line = outlineAlpha(g.alpha, crisp.width, crisp.height, STROKE * dpr);
256
+ const img = c2.createImageData(crisp.width, crisp.height);
257
+ for (let i = 0; i < line.length; i++) img.data[i * 4 + 3] = line[i];
258
+ c2.putImageData(img, 0, 0);
259
+ c2.setTransform(1, 0, 0, 1, 0, 0);
260
+ c2.globalCompositeOperation = 'source-in';
261
+ c2.fillRect(0, 0, crisp.width, crisp.height);
217
262
  c2.globalCompositeOperation = 'source-over';
263
+ c2.setTransform(dpr, 0, 0, dpr, 0, 0);
218
264
  }
219
265
  g.cols = Math.ceil(g.w / CELL);
220
266
  g.rows = Math.ceil(g.h / CELL);
@@ -274,10 +320,23 @@ export function pixelGlitch(el, config) {
274
320
  g.visible = entry.isIntersecting;
275
321
  });
276
322
  watcher.observe(el);
277
- document.fonts?.ready.then(paint).catch(() => {});
323
+ // Repaint once the fonts are in, and once the text's own face is (it may load after fonts.ready).
324
+ let stopped = false;
325
+ /** Repaints, unless stopped meanwhile. */
326
+ const repaint = () => {
327
+ if (!stopped) paint();
328
+ };
329
+ document.fonts?.ready.then(repaint).catch(() => {});
330
+ const face = getComputedStyle(el);
331
+ document.fonts
332
+ ?.load(`${face.fontStyle} ${face.fontWeight} ${face.fontSize} ${face.fontFamily}`, el.textContent ?? '')
333
+ .then(repaint)
334
+ .catch(() => {});
278
335
  paint();
279
336
 
280
337
  return () => {
338
+ stopped = true;
339
+ cancelAnimationFrame(pending);
281
340
  document.removeEventListener('pointermove', move);
282
341
  resizer.disconnect();
283
342
  watcher.disconnect();
@@ -21,6 +21,8 @@ export interface ShaderBackgroundConfig {
21
21
  frame?: number;
22
22
  /** Lean towards the pointer, on devices that hover. Default false. */
23
23
  interactive?: boolean;
24
+ /** Run only while near the screen (true, or a CSS length; default `200px`), handled by `mount`. Default false. */
25
+ lazy?: boolean | string;
24
26
  }
25
27
 
26
28
  /** Every shader background's fragment shader, by name. */
@@ -15,7 +15,13 @@
15
15
  * a pill reads as a stall). Colour and speed from `--sweep-color` and
16
16
  * `--sweep-speed`. With reduced motion the ring stays, still.
17
17
  *
18
- * From the ArrayPress theme.
18
+ * `sweep-ring--two` sends two lights round, chasing each other half a lap
19
+ * apart. `sweep-ring--inset` lifts the ring over the element's own
20
+ * content (z-index 3, inside its edge), for cards that clip their
21
+ * contents (`overflow: hidden`) and fill themselves with a picture or a
22
+ * canvas that would otherwise cover it.
23
+ *
24
+ * From the ArrayPress theme; the two lights from the Logomarks site.
19
25
  * ───────────────────────────────────────────────────────────────────── */
20
26
 
21
27
  @property --sweep-angle {
@@ -63,6 +69,27 @@
63
69
  --sweep-speed: 1.8s;
64
70
  }
65
71
 
72
+ /* Two lights, half a lap apart, each with a faint lead-in. */
73
+ .sweep-ring--two::after {
74
+ background: conic-gradient(
75
+ from var(--sweep-angle),
76
+ transparent 0%,
77
+ color-mix(in srgb, var(--sweep-color, currentColor) 25%, transparent) 14%,
78
+ var(--sweep-color, currentColor) 24%,
79
+ transparent 30%,
80
+ transparent 50%,
81
+ color-mix(in srgb, var(--sweep-color, currentColor) 25%, transparent) 64%,
82
+ var(--sweep-color, currentColor) 74%,
83
+ transparent 80%
84
+ );
85
+ }
86
+
87
+ /* Inside the edge and over the content, for cards that clip and fill themselves. */
88
+ .sweep-ring--inset::after {
89
+ inset: 0;
90
+ z-index: 3;
91
+ }
92
+
66
93
  @keyframes effect-border-sweep {
67
94
  to {
68
95
  --sweep-angle: 360deg;
@@ -0,0 +1,169 @@
1
+ /* ─────────────────────────────────────────────────────────────────────
2
+ * @arraypress/effects-astro/glitch-text.css
3
+ *
4
+ * A headline that glitches now and then: two coloured copies of it, laid
5
+ * over the real one, flash on for a few frames near the end of each
6
+ * cycle, each sliced to a different band (clip-path) and shifted
7
+ * sideways, while the real text nudges with them. Mostly it sits still.
8
+ * Livelier under the pointer, on the text or a link or button round it.
9
+ *
10
+ * Markup: the real text first, then the two copies, hidden from assistive
11
+ * technology and from focus:
12
+ *
13
+ * <h2 class="glitch-text" style="--glitch-delay: -1.7s">
14
+ * <span>Make it yours</span>
15
+ * <span class="glitch-text-a" aria-hidden="true" inert>Make it yours</span>
16
+ * <span class="glitch-text-b" aria-hidden="true" inert>Make it yours</span>
17
+ * </h2>
18
+ *
19
+ * Set from custom properties: `--glitch-every` (a cycle, default 5s),
20
+ * `--glitch-hover` (a cycle under the pointer, 1.6s), `--glitch-delay`
21
+ * (where in its cycle this one is, so several glitch on their own beats),
22
+ * `--glitch-a` and `--glitch-b` (the copies' colours, pink and blue) and
23
+ * `--glitch-blend` (how they blend: multiply on a light ground, screen on
24
+ * a dark one). With reduced motion it is the plain text, still.
25
+ *
26
+ * CSS only. From the Logomarks site's Custom card.
27
+ * ───────────────────────────────────────────────────────────────────── */
28
+
29
+ .glitch-text {
30
+ position: relative;
31
+ display: block;
32
+ }
33
+
34
+ :is(span, a, em, strong, b, i, small).glitch-text {
35
+ display: inline-block;
36
+ }
37
+
38
+ .glitch-text > * {
39
+ display: block;
40
+ animation: effect-glitch-text-main var(--glitch-every, 5s) steps(1, end) infinite;
41
+ animation-delay: var(--glitch-delay, 0s);
42
+ }
43
+
44
+ .glitch-text > .glitch-text-a,
45
+ .glitch-text > .glitch-text-b {
46
+ position: absolute;
47
+ inset: 0;
48
+ pointer-events: none;
49
+ user-select: none;
50
+ opacity: 0;
51
+ mix-blend-mode: var(--glitch-blend, multiply);
52
+ animation-name: effect-glitch-text-a;
53
+ }
54
+
55
+ .glitch-text > .glitch-text-a,
56
+ .glitch-text > .glitch-text-a * {
57
+ color: var(--glitch-a, #ff3ea5);
58
+ -webkit-text-fill-color: currentColor;
59
+ }
60
+
61
+ .glitch-text > .glitch-text-b,
62
+ .glitch-text > .glitch-text-b * {
63
+ color: var(--glitch-b, #2e4bff);
64
+ -webkit-text-fill-color: currentColor;
65
+ }
66
+
67
+ .glitch-text > .glitch-text-b {
68
+ animation-name: effect-glitch-text-b;
69
+ }
70
+
71
+ .glitch-text:hover > *,
72
+ :is(a, button):is(:hover, :focus-visible) .glitch-text > * {
73
+ animation-duration: var(--glitch-hover, 1.6s);
74
+ }
75
+
76
+ /* Mostly hidden; a burst near the end of each cycle: a few frames of offset slices. */
77
+ @keyframes effect-glitch-text-a {
78
+ 0%,
79
+ 88% {
80
+ opacity: 0;
81
+ clip-path: inset(0 0 100% 0);
82
+ transform: none;
83
+ }
84
+
85
+ 89% {
86
+ opacity: 1;
87
+ clip-path: inset(12% 0 64% 0);
88
+ transform: translateX(-3px);
89
+ }
90
+
91
+ 91% {
92
+ clip-path: inset(58% 0 22% 0);
93
+ transform: translateX(4px);
94
+ }
95
+
96
+ 93% {
97
+ clip-path: inset(30% 0 48% 0);
98
+ transform: translateX(-2px);
99
+ }
100
+
101
+ 95% {
102
+ clip-path: inset(78% 0 6% 0);
103
+ transform: translateX(2px);
104
+ }
105
+
106
+ 96%,
107
+ 100% {
108
+ opacity: 0;
109
+ clip-path: inset(0 0 100% 0);
110
+ transform: none;
111
+ }
112
+ }
113
+
114
+ /* The second copy a frame behind the first, on other bands. */
115
+ @keyframes effect-glitch-text-b {
116
+ 0%,
117
+ 89% {
118
+ opacity: 0;
119
+ clip-path: inset(0 0 100% 0);
120
+ transform: none;
121
+ }
122
+
123
+ 90% {
124
+ opacity: 1;
125
+ clip-path: inset(44% 0 36% 0);
126
+ transform: translateX(3px);
127
+ }
128
+
129
+ 92% {
130
+ clip-path: inset(6% 0 76% 0);
131
+ transform: translateX(-4px);
132
+ }
133
+
134
+ 94% {
135
+ clip-path: inset(66% 0 14% 0);
136
+ transform: translateX(2px);
137
+ }
138
+
139
+ 96%,
140
+ 100% {
141
+ opacity: 0;
142
+ clip-path: inset(0 0 100% 0);
143
+ transform: none;
144
+ }
145
+ }
146
+
147
+ /* The real text nudges with them. */
148
+ @keyframes effect-glitch-text-main {
149
+ 0%,
150
+ 88.9%,
151
+ 96%,
152
+ 100% {
153
+ transform: none;
154
+ }
155
+
156
+ 89% {
157
+ transform: translateX(1px);
158
+ }
159
+
160
+ 92% {
161
+ transform: translateX(-1px);
162
+ }
163
+ }
164
+
165
+ @media (prefers-reduced-motion: reduce) {
166
+ .glitch-text > * {
167
+ animation: none !important;
168
+ }
169
+ }