@arraypress/effects-astro 0.6.1 → 0.8.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.
Files changed (41) hide show
  1. package/AGENTS.md +2 -0
  2. package/COMPONENTS.md +33 -0
  3. package/README.md +2 -2
  4. package/package.json +6 -2
  5. package/src/components/EffectCanvas.astro +4 -1
  6. package/src/components/PixelFountain.astro +70 -0
  7. package/src/index.d.ts +4 -1
  8. package/src/index.js +3 -1
  9. package/src/runtime/budget.d.ts +8 -0
  10. package/src/runtime/budget.js +49 -0
  11. package/src/runtime/export/clean-svg.d.ts +21 -0
  12. package/src/runtime/export/clean-svg.js +368 -0
  13. package/src/runtime/export/clock.d.ts +25 -0
  14. package/src/runtime/export/clock.js +122 -0
  15. package/src/runtime/export/fetch.d.ts +14 -0
  16. package/src/runtime/export/fetch.js +121 -0
  17. package/src/runtime/export/fonts.d.ts +30 -0
  18. package/src/runtime/export/fonts.js +202 -0
  19. package/src/runtime/export/format.d.ts +11 -0
  20. package/src/runtime/export/format.js +42 -0
  21. package/src/runtime/export/probe.d.ts +16 -0
  22. package/src/runtime/export/probe.js +89 -0
  23. package/src/runtime/export/raster.d.ts +17 -0
  24. package/src/runtime/export/raster.js +91 -0
  25. package/src/runtime/export/save.d.ts +5 -0
  26. package/src/runtime/export/save.js +41 -0
  27. package/src/runtime/export/snapshot.d.ts +43 -0
  28. package/src/runtime/export/snapshot.js +276 -0
  29. package/src/runtime/export/styles.d.ts +18 -0
  30. package/src/runtime/export/styles.js +152 -0
  31. package/src/runtime/export/svg-attrs.d.ts +8 -0
  32. package/src/runtime/export/svg-attrs.js +274 -0
  33. package/src/runtime/export/svg-text.d.ts +21 -0
  34. package/src/runtime/export/svg-text.js +124 -0
  35. package/src/runtime/export/video.d.ts +45 -0
  36. package/src/runtime/export/video.js +106 -0
  37. package/src/runtime/pixel-fountain.d.ts +134 -0
  38. package/src/runtime/pixel-fountain.js +730 -0
  39. package/src/runtime/pixel-glitch.js +2 -24
  40. package/src/runtime/text.d.ts +6 -0
  41. package/src/runtime/text.js +57 -1
@@ -37,7 +37,7 @@
37
37
  */
38
38
 
39
39
  import { canHover, prefersReducedMotion } from './device.js';
40
- import { textNodes } from './text.js';
40
+ import { textNodes, paintGlyphs } from './text.js';
41
41
 
42
42
  /**
43
43
  * @typedef {object} PixelGlitchConfig
@@ -280,29 +280,7 @@ export function pixelGlitch(el, config) {
280
280
  c2.setTransform(dpr, 0, 0, dpr, 0, 0);
281
281
  c2.clearRect(0, 0, g.w, g.h);
282
282
  c2.fillStyle = g.ink;
283
- c2.font = `${cs.fontStyle} ${cs.fontWeight} ${cs.fontSize} ${cs.fontFamily}`;
284
- c2.textBaseline = 'alphabetic';
285
- const range = document.createRange();
286
- /**
287
- * Draws every glyph where the browser laid it out.
288
- *
289
- * @param {(ch: string, x: number, y: number) => void} glyph Draws one glyph at a baseline position.
290
- */
291
- const eachGlyph = (glyph) => {
292
- for (const text of texts) {
293
- const str = text.textContent ?? '';
294
- for (let i = 0; i < str.length; i++) {
295
- const ch = str[i];
296
- if (!ch.trim()) continue;
297
- range.setStart(text, i);
298
- range.setEnd(text, i + 1);
299
- const r = range.getBoundingClientRect();
300
- // The glyph's box starts at r.top; its baseline sits one font ascent below.
301
- glyph(ch, r.left - origin.x, r.top - origin.y + c2.measureText(ch).fontBoundingBoxAscent);
302
- }
303
- }
304
- };
305
- eachGlyph((ch, x, y) => c2.fillText(ch, x, y));
283
+ paintGlyphs(c2, texts, origin);
306
284
  g.alpha = c2.getImageData(0, 0, crisp.width, crisp.height).data;
307
285
  if (STROKE > 0) {
308
286
  // The pixels still sample the solid letters (g.alpha); what shows at rest is their outline, in the ink.
@@ -9,3 +9,9 @@ export function splitLetters(el: HTMLElement): HTMLElement[];
9
9
 
10
10
  /** Puts letters split by `splitLetters` back as plain text. */
11
11
  export function joinLetters(el: HTMLElement): void;
12
+
13
+ /** Every non-empty text node inside `root`, at any depth, except those inside `skip`. */
14
+ export function textNodesWithin(root: Element, skip?: Element | null): Text[];
15
+
16
+ /** Draws text nodes into a 2D context (CSS px) glyph by glyph, each where the browser laid it out and in its own element's font, `origin` being the viewport point the context's 0, 0 stands for. */
17
+ export function paintGlyphs(ctx: CanvasRenderingContext2D, texts: Text[], origin: { x: number; y: number }): void;
@@ -6,7 +6,8 @@
6
6
  * all, whose box is the text's own extent rather than the element's, and
7
7
  * its letters, each in a span of its own, for effects that move or restyle
8
8
  * them one by one. Split letters stay real text: selectable, found by
9
- * search and read as words.
9
+ * search and read as words. For effects that redraw text into a canvas,
10
+ * `paintGlyphs` draws each glyph where the browser laid it out.
10
11
  */
11
12
 
12
13
  /** Marks a letter's span. */
@@ -88,3 +89,58 @@ export function joinLetters(el) {
88
89
  el.querySelectorAll(`[${LETTER}]`).forEach((span) => span.replaceWith(span.textContent ?? ''));
89
90
  el.normalize();
90
91
  }
92
+
93
+ /**
94
+ * Every non-empty text node inside an element, at any depth, except those
95
+ * inside `skip` (an effect's own element placed in the text).
96
+ *
97
+ * @param {Element} root The element.
98
+ * @param {Element | null} [skip] An element whose text is left out.
99
+ * @returns {Text[]} The text nodes, in document order.
100
+ */
101
+ export function textNodesWithin(root, skip = null) {
102
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
103
+ /** @type {Text[]} */
104
+ const texts = [];
105
+ while (walker.nextNode()) {
106
+ const node = /** @type {Text} */ (walker.currentNode);
107
+ if (!node.textContent?.trim()) continue;
108
+ if (skip && skip.contains(node)) continue;
109
+ texts.push(node);
110
+ }
111
+ return texts;
112
+ }
113
+
114
+ /**
115
+ * Draws text into a 2D context glyph by glyph, each where the browser laid
116
+ * it out (one Range a character), so letter-spacing, kerning and wrapping
117
+ * match the CSS. Each glyph takes its own element's font; the context's
118
+ * fill style is the ink.
119
+ *
120
+ * @param {CanvasRenderingContext2D} ctx The context, in CSS px.
121
+ * @param {Text[]} texts The text nodes to draw.
122
+ * @param {{ x: number, y: number }} origin The viewport point the context's 0, 0 stands for.
123
+ */
124
+ export function paintGlyphs(ctx, texts, origin) {
125
+ const range = document.createRange();
126
+ /** @type {Element | null} */
127
+ let parent = null;
128
+ ctx.textBaseline = 'alphabetic';
129
+ for (const text of texts) {
130
+ if (text.parentElement && text.parentElement !== parent) {
131
+ parent = text.parentElement;
132
+ const cs = getComputedStyle(parent);
133
+ ctx.font = `${cs.fontStyle} ${cs.fontWeight} ${cs.fontSize} ${cs.fontFamily}`;
134
+ }
135
+ const str = text.textContent ?? '';
136
+ for (let i = 0; i < str.length; i++) {
137
+ const ch = str[i];
138
+ if (!ch.trim()) continue;
139
+ range.setStart(text, i);
140
+ range.setEnd(text, i + 1);
141
+ const r = range.getBoundingClientRect();
142
+ // The glyph's box starts at r.top; its baseline sits one font ascent below.
143
+ ctx.fillText(ch, r.left - origin.x, r.top - origin.y + ctx.measureText(ch).fontBoundingBoxAscent);
144
+ }
145
+ }
146
+ }