@vfx-js/effects 1.2.0 → 1.3.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 (136) hide show
  1. package/README.md +2 -0
  2. package/lib/cjs/_figma-common.d.ts +8 -0
  3. package/lib/cjs/_figma-common.js +46 -0
  4. package/lib/cjs/_figma-common.js.map +1 -0
  5. package/lib/cjs/ascii.d.ts +137 -0
  6. package/lib/cjs/ascii.js +482 -0
  7. package/lib/cjs/ascii.js.map +1 -0
  8. package/lib/cjs/chromatic.d.ts +15 -0
  9. package/lib/cjs/chromatic.js +70 -0
  10. package/lib/cjs/chromatic.js.map +1 -0
  11. package/lib/cjs/colored-edges.d.ts +23 -0
  12. package/lib/cjs/colored-edges.js +88 -0
  13. package/lib/cjs/colored-edges.js.map +1 -0
  14. package/lib/cjs/dither.d.ts +24 -0
  15. package/lib/cjs/dither.js +120 -0
  16. package/lib/cjs/dither.js.map +1 -0
  17. package/lib/cjs/duotone.d.ts +17 -0
  18. package/lib/cjs/duotone.js +62 -0
  19. package/lib/cjs/duotone.js.map +1 -0
  20. package/lib/cjs/glitch.d.ts +13 -0
  21. package/lib/cjs/glitch.js +108 -0
  22. package/lib/cjs/glitch.js.map +1 -0
  23. package/lib/cjs/gradient-map.d.ts +25 -0
  24. package/lib/cjs/gradient-map.js +154 -0
  25. package/lib/cjs/gradient-map.js.map +1 -0
  26. package/lib/cjs/hue-shift.d.ts +11 -0
  27. package/lib/cjs/hue-shift.js +65 -0
  28. package/lib/cjs/hue-shift.js.map +1 -0
  29. package/lib/cjs/index.d.ts +20 -0
  30. package/lib/cjs/index.js +43 -1
  31. package/lib/cjs/index.js.map +1 -1
  32. package/lib/cjs/matrix.d.ts +135 -0
  33. package/lib/cjs/matrix.js +593 -0
  34. package/lib/cjs/matrix.js.map +1 -0
  35. package/lib/cjs/package.json +1 -1
  36. package/lib/cjs/pattern-refraction.d.ts +31 -0
  37. package/lib/cjs/pattern-refraction.js +190 -0
  38. package/lib/cjs/pattern-refraction.js.map +1 -0
  39. package/lib/cjs/pixel-stretch.d.ts +21 -0
  40. package/lib/cjs/pixel-stretch.js +78 -0
  41. package/lib/cjs/pixel-stretch.js.map +1 -0
  42. package/lib/cjs/rainbow.d.ts +13 -0
  43. package/lib/cjs/rainbow.js +79 -0
  44. package/lib/cjs/rainbow.js.map +1 -0
  45. package/lib/cjs/rgb-glitch.d.ts +13 -0
  46. package/lib/cjs/rgb-glitch.js +79 -0
  47. package/lib/cjs/rgb-glitch.js.map +1 -0
  48. package/lib/cjs/rgb-shift.d.ts +13 -0
  49. package/lib/cjs/rgb-shift.js +76 -0
  50. package/lib/cjs/rgb-shift.js.map +1 -0
  51. package/lib/cjs/sinewave.d.ts +17 -0
  52. package/lib/cjs/sinewave.js +76 -0
  53. package/lib/cjs/sinewave.js.map +1 -0
  54. package/lib/cjs/slice-shift.d.ts +21 -0
  55. package/lib/cjs/slice-shift.js +75 -0
  56. package/lib/cjs/slice-shift.js.map +1 -0
  57. package/lib/cjs/tile-pixelate.d.ts +28 -0
  58. package/lib/cjs/tile-pixelate.js +168 -0
  59. package/lib/cjs/tile-pixelate.js.map +1 -0
  60. package/lib/cjs/tritone.d.ts +18 -0
  61. package/lib/cjs/tritone.js +67 -0
  62. package/lib/cjs/tritone.js.map +1 -0
  63. package/lib/cjs/vignette.d.ts +15 -0
  64. package/lib/cjs/vignette.js +59 -0
  65. package/lib/cjs/vignette.js.map +1 -0
  66. package/lib/cjs/warp.d.ts +22 -0
  67. package/lib/cjs/warp.js +92 -0
  68. package/lib/cjs/warp.js.map +1 -0
  69. package/lib/esm/_figma-common.d.ts +8 -0
  70. package/lib/esm/_figma-common.js +42 -0
  71. package/lib/esm/_figma-common.js.map +1 -0
  72. package/lib/esm/ascii.d.ts +137 -0
  73. package/lib/esm/ascii.js +478 -0
  74. package/lib/esm/ascii.js.map +1 -0
  75. package/lib/esm/chromatic.d.ts +15 -0
  76. package/lib/esm/chromatic.js +66 -0
  77. package/lib/esm/chromatic.js.map +1 -0
  78. package/lib/esm/colored-edges.d.ts +23 -0
  79. package/lib/esm/colored-edges.js +84 -0
  80. package/lib/esm/colored-edges.js.map +1 -0
  81. package/lib/esm/dither.d.ts +24 -0
  82. package/lib/esm/dither.js +116 -0
  83. package/lib/esm/dither.js.map +1 -0
  84. package/lib/esm/duotone.d.ts +17 -0
  85. package/lib/esm/duotone.js +58 -0
  86. package/lib/esm/duotone.js.map +1 -0
  87. package/lib/esm/glitch.d.ts +13 -0
  88. package/lib/esm/glitch.js +104 -0
  89. package/lib/esm/glitch.js.map +1 -0
  90. package/lib/esm/gradient-map.d.ts +25 -0
  91. package/lib/esm/gradient-map.js +150 -0
  92. package/lib/esm/gradient-map.js.map +1 -0
  93. package/lib/esm/hue-shift.d.ts +11 -0
  94. package/lib/esm/hue-shift.js +61 -0
  95. package/lib/esm/hue-shift.js.map +1 -0
  96. package/lib/esm/index.d.ts +20 -0
  97. package/lib/esm/index.js +20 -0
  98. package/lib/esm/index.js.map +1 -1
  99. package/lib/esm/matrix.d.ts +135 -0
  100. package/lib/esm/matrix.js +589 -0
  101. package/lib/esm/matrix.js.map +1 -0
  102. package/lib/esm/package.json +1 -1
  103. package/lib/esm/pattern-refraction.d.ts +31 -0
  104. package/lib/esm/pattern-refraction.js +186 -0
  105. package/lib/esm/pattern-refraction.js.map +1 -0
  106. package/lib/esm/pixel-stretch.d.ts +21 -0
  107. package/lib/esm/pixel-stretch.js +74 -0
  108. package/lib/esm/pixel-stretch.js.map +1 -0
  109. package/lib/esm/rainbow.d.ts +13 -0
  110. package/lib/esm/rainbow.js +75 -0
  111. package/lib/esm/rainbow.js.map +1 -0
  112. package/lib/esm/rgb-glitch.d.ts +13 -0
  113. package/lib/esm/rgb-glitch.js +75 -0
  114. package/lib/esm/rgb-glitch.js.map +1 -0
  115. package/lib/esm/rgb-shift.d.ts +13 -0
  116. package/lib/esm/rgb-shift.js +72 -0
  117. package/lib/esm/rgb-shift.js.map +1 -0
  118. package/lib/esm/sinewave.d.ts +17 -0
  119. package/lib/esm/sinewave.js +72 -0
  120. package/lib/esm/sinewave.js.map +1 -0
  121. package/lib/esm/slice-shift.d.ts +21 -0
  122. package/lib/esm/slice-shift.js +71 -0
  123. package/lib/esm/slice-shift.js.map +1 -0
  124. package/lib/esm/tile-pixelate.d.ts +28 -0
  125. package/lib/esm/tile-pixelate.js +164 -0
  126. package/lib/esm/tile-pixelate.js.map +1 -0
  127. package/lib/esm/tritone.d.ts +18 -0
  128. package/lib/esm/tritone.js +63 -0
  129. package/lib/esm/tritone.js.map +1 -0
  130. package/lib/esm/vignette.d.ts +15 -0
  131. package/lib/esm/vignette.js +55 -0
  132. package/lib/esm/vignette.js.map +1 -0
  133. package/lib/esm/warp.d.ts +22 -0
  134. package/lib/esm/warp.js +88 -0
  135. package/lib/esm/warp.js.map +1 -0
  136. package/package.json +1 -1
@@ -0,0 +1,482 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _AsciiEffect_instances, _AsciiEffect_atlas, _AsciiEffect_cols, _AsciiEffect_rows, _AsciiEffect_charCount, _AsciiEffect_glyphAspect, _AsciiEffect_atlasCellPx, _AsciiEffect_tileColor, _AsciiEffect_ctx, _AsciiEffect_build;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.AsciiEffect = exports.ASCII_PRESETS = void 0;
16
+ /**
17
+ * Built-in character ramps, ordered dark → light. Suited for
18
+ * light-on-dark output (more "ink" = brighter); flip with
19
+ * `invert: true` for dark-on-light.
20
+ */
21
+ exports.ASCII_PRESETS = {
22
+ // Paul Bourke's short 10-step grey-scale ramp, reversed to dark → light.
23
+ // ref. https://paulbourke.net/dataformats/asciiart/
24
+ standard: " .:-=+*#%@",
25
+ minimal: " .#",
26
+ // Unicode shade blocks — smooth, font-independent gradient.
27
+ blocks: " ░▒▓█",
28
+ // Round symbols growing from a dot to a filled bullet.
29
+ dots: " .·•●",
30
+ // Outline → filled circles (the ◯ … ● family).
31
+ circles: " ◌○◉●",
32
+ // Paul Bourke's 70-step grey-scale ramp, reversed to dark → light.
33
+ // ref. https://paulbourke.net/dataformats/asciiart/
34
+ detailed: " .'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$",
35
+ };
36
+ const FRAG_ASCII = `#version 300 es
37
+ precision highp float;
38
+
39
+ in vec2 uvContent;
40
+ out vec4 outColor;
41
+
42
+ uniform sampler2D src;
43
+ uniform sampler2D atlas;
44
+ uniform vec4 srcRectUv;
45
+ uniform vec2 elementPx; // element size, physical px
46
+ uniform vec2 cellPx; // cell size, physical px
47
+ uniform float cols; // atlas columns
48
+ uniform float rows; // atlas rows
49
+ uniform float charCount; // number of glyphs in the ramp
50
+ uniform vec4 color; // fixed glyph colour (when colorFromSource == 0)
51
+ uniform vec4 background; // cell backdrop, non-premultiplied
52
+ uniform int colorFromSource; // 1 = tint glyph with the cell's avg colour
53
+ uniform int invert; // 1 = flip the luminance → glyph mapping
54
+ uniform float glyphAspect; // font's character box aspect (advance / em)
55
+ uniform int tileColor; // 1 = use the atlas tile's own RGBA (image tiles)
56
+ uniform float dither; // ordered-dither amount in index units (0 = off)
57
+ uniform vec2 atlasCellPx; // atlas cell size in texels (for edge inset)
58
+
59
+ // Box-average TAPS x TAPS samples per cell. A single centre tap throws
60
+ // away most of the cell; this keeps the glyph choice representative. 3x3
61
+ // keeps the luminance stable enough for the discrete glyph mapping while
62
+ // costing far fewer fetches than a 4x4 grid.
63
+ const int TAPS = 3;
64
+
65
+ vec4 readSrc(vec2 contentUv) {
66
+ vec2 p = clamp(contentUv, 0.0, 1.0);
67
+ return texture(src, srcRectUv.xy + p * srcRectUv.zw);
68
+ }
69
+
70
+ // 4x4 ordered (Bayer) dither threshold in (0, 1), keyed by cell index so
71
+ // the offset is constant across a cell (one character per cell).
72
+ float bayer4x4(vec2 cell) {
73
+ int x = int(mod(cell.x, 4.0));
74
+ int y = int(mod(cell.y, 4.0));
75
+ float m[16] = float[16](
76
+ 0.0, 8.0, 2.0, 10.0,
77
+ 12.0, 4.0, 14.0, 6.0,
78
+ 3.0, 11.0, 1.0, 9.0,
79
+ 15.0, 7.0, 13.0, 5.0
80
+ );
81
+ return (m[y * 4 + x] + 0.5) / 16.0;
82
+ }
83
+
84
+ void main() {
85
+ if (uvContent.x < 0.0 || uvContent.x > 1.0 ||
86
+ uvContent.y < 0.0 || uvContent.y > 1.0) {
87
+ outColor = vec4(0.0);
88
+ return;
89
+ }
90
+
91
+ // Anchor the grid at the element (srcRect) centre so cells are
92
+ // symmetric about the middle instead of growing from the bottom-left
93
+ // corner — any partial cells split evenly between opposite edges.
94
+ vec2 fragPx = uvContent * elementPx;
95
+ vec2 gridOrigin = elementPx * 0.5;
96
+ vec2 rel = fragPx - gridOrigin;
97
+ vec2 cellIdx = floor(rel / cellPx);
98
+ vec2 cellOriginPx = gridOrigin + cellIdx * cellPx;
99
+
100
+ vec4 acc = vec4(0.0);
101
+ for (int y = 0; y < TAPS; ++y) {
102
+ for (int x = 0; x < TAPS; ++x) {
103
+ vec2 o = (vec2(float(x), float(y)) + 0.5) / float(TAPS);
104
+ vec2 samplePx = cellOriginPx + o * cellPx;
105
+ acc += readSrc(samplePx / elementPx);
106
+ }
107
+ }
108
+ acc /= float(TAPS * TAPS);
109
+
110
+ float lum = dot(acc.rgb, vec3(0.299, 0.587, 0.114));
111
+ if (invert == 1) {
112
+ lum = 1.0 - lum;
113
+ }
114
+
115
+ // Pick the glyph and its cell in the atlas (top-row origin in canvas
116
+ // space; the texture is uploaded Y-flipped, hence the 1.0 - ... on v).
117
+ // Ordered dither perturbs the continuous index by up to ±0.5 cell
118
+ // (at dither == 1), so neighbouring cells round to different glyphs
119
+ // and short ramps gain apparent tonal steps without banding.
120
+ float li = lum * charCount;
121
+ if (dither > 0.0) {
122
+ li += (bayer4x4(cellIdx) - 0.5) * dither;
123
+ }
124
+ float idx = clamp(floor(li), 0.0, charCount - 1.0);
125
+ float col = mod(idx, cols);
126
+ float rowTop = floor(idx / cols);
127
+
128
+ // Fit the glyph's character box into the cell with its native
129
+ // aspect preserved (contain): scale by the limiting axis and centre,
130
+ // so a wide cell letterboxes left/right and a tall cell top/bottom
131
+ // instead of stretching the glyph to the cell's aspect.
132
+ vec2 local = fract(rel / cellPx);
133
+ float cellAspect = cellPx.x / cellPx.y;
134
+ vec2 frac = min(vec2(1.0), vec2(glyphAspect / cellAspect, cellAspect / glyphAspect));
135
+ vec2 gloc = (local - 0.5) / frac + 0.5;
136
+
137
+ vec4 tile = vec4(0.0);
138
+ if (gloc.x >= 0.0 && gloc.x <= 1.0 && gloc.y >= 0.0 && gloc.y <= 1.0) {
139
+ // Image tiles can fill the cell edge-to-edge, so linear filtering
140
+ // would bleed a neighbouring tile's colour across the shared
141
+ // border — inset the sample by half a texel to stop it. Glyphs
142
+ // have transparent side bearings, so they need no inset.
143
+ vec2 inset = tileColor == 1 ? 0.5 / atlasCellPx : vec2(0.0);
144
+ vec2 g2 = mix(inset, 1.0 - inset, gloc);
145
+ float u = (col + g2.x) / cols;
146
+ float v = 1.0 - (rowTop + 1.0 - g2.y) / rows;
147
+ tile = texture(atlas, vec2(u, v));
148
+ }
149
+
150
+ // Image tiles keep their own RGBA; glyph tiles are a coverage mask in
151
+ // .a, tinted by color (or the cell's average colour). The alpha is the
152
+ // same either way: tile coverage x color.a (global opacity) x the
153
+ // cell's source alpha, so transparent regions (e.g. text captures)
154
+ // fall back to the background.
155
+ vec3 fg;
156
+ if (tileColor == 1) {
157
+ fg = tile.rgb;
158
+ } else {
159
+ fg = colorFromSource == 1 ? acc.rgb : color.rgb;
160
+ }
161
+ float fgA = tile.a * color.a * acc.a;
162
+
163
+ float outA = fgA + background.a * (1.0 - fgA);
164
+ vec3 premul = fg * fgA + background.rgb * background.a * (1.0 - fgA);
165
+ outColor = vec4(premul, outA);
166
+ }
167
+ `;
168
+ const DEFAULT_PARAMS = {
169
+ grid: 12,
170
+ preset: "standard",
171
+ font: "monospace",
172
+ fontWeight: "normal",
173
+ color: [1, 1, 1, 1],
174
+ background: [0, 0, 0, 0],
175
+ colorFromSource: false,
176
+ invert: false,
177
+ dither: 0,
178
+ };
179
+ // Atlas glyph cell height (physical px). Fixed and oversized vs. typical
180
+ // on-screen cells so linear minification keeps glyphs crisp. Cell width
181
+ // is measured from the font's advance so glyphs sit flush horizontally.
182
+ const GLYPH_PX = 64;
183
+ /**
184
+ * Split a ramp into single-character cells (per Unicode code point).
185
+ */
186
+ function resolveChars(ramp) {
187
+ return Array.isArray(ramp) ? [...ramp] : Array.from(ramp);
188
+ }
189
+ /** Normalize `grid` to a `[width, height]` pair in CSS px. */
190
+ function resolveGrid(grid) {
191
+ return typeof grid === "number" ? [grid, grid] : grid;
192
+ }
193
+ /**
194
+ * Best-effort wait for `font` to be ready before rasterising the atlas,
195
+ * so the first build doesn't fall back to a system font. No-op outside
196
+ * the browser / when the Font Loading API is unavailable.
197
+ */
198
+ async function ensureFont(font, weight) {
199
+ const fonts = (typeof document !== "undefined"
200
+ ? document.fonts
201
+ : undefined);
202
+ if (!fonts?.load) {
203
+ return;
204
+ }
205
+ try {
206
+ await fonts.load(`${weight} ${GLYPH_PX}px ${font}`);
207
+ await fonts.ready;
208
+ }
209
+ catch {
210
+ // Font unavailable — fall through and let canvas pick a fallback.
211
+ }
212
+ }
213
+ /**
214
+ * Render the ramp into a single glyph atlas canvas, laid out as a near-
215
+ * square grid of cells. Cell height is `GLYPH_PX`; cell width tracks the
216
+ * widest glyph's advance so glyphs sit flush (no baked-in side bearings
217
+ * that would show as wide gaps on screen) yet wide glyphs (e.g. full-width
218
+ * CJK) stay within their cell. White glyphs on transparent; the shader
219
+ * reads coverage from the alpha channel.
220
+ */
221
+ function buildAtlas(chars, font, weight, aspectOverride) {
222
+ const n = Math.max(1, chars.length);
223
+ const cols = Math.ceil(Math.sqrt(n));
224
+ const rows = Math.ceil(n / cols);
225
+ const fontStr = `${weight} ${GLYPH_PX}px ${font}`;
226
+ const canvas = document.createElement("canvas");
227
+ // Cell width = explicit aspect, or the widest glyph's advance (measure
228
+ // first; resizing the canvas later resets context state).
229
+ let cellW;
230
+ if (aspectOverride && aspectOverride > 0) {
231
+ cellW = Math.max(1, Math.round(aspectOverride * GLYPH_PX));
232
+ }
233
+ else {
234
+ const probe = canvas.getContext("2d");
235
+ cellW = GLYPH_PX;
236
+ if (probe) {
237
+ probe.font = fontStr;
238
+ // Size the cell to the widest glyph in the ramp — not a fixed
239
+ // probe char — so wide glyphs (e.g. full-width CJK) don't spill
240
+ // into the neighbouring cell in the atlas.
241
+ let maxAdvance = 0;
242
+ for (const ch of chars) {
243
+ maxAdvance = Math.max(maxAdvance, probe.measureText(ch).width);
244
+ }
245
+ if (maxAdvance > 0) {
246
+ cellW = Math.max(1, Math.ceil(maxAdvance));
247
+ }
248
+ }
249
+ }
250
+ canvas.width = cols * cellW;
251
+ canvas.height = rows * GLYPH_PX;
252
+ const g = canvas.getContext("2d");
253
+ if (g) {
254
+ g.clearRect(0, 0, canvas.width, canvas.height);
255
+ g.fillStyle = "#fff";
256
+ g.textAlign = "center";
257
+ g.textBaseline = "middle";
258
+ g.font = fontStr;
259
+ for (let i = 0; i < chars.length; i++) {
260
+ const col = i % cols;
261
+ const row = Math.floor(i / cols);
262
+ const cx = col * cellW + cellW / 2;
263
+ const cy = row * GLYPH_PX + GLYPH_PX / 2;
264
+ // Clip to the cell so ink that overshoots the advance can't bleed
265
+ // into the next cell (extra guard on top of the max-advance width).
266
+ g.save();
267
+ g.beginPath();
268
+ g.rect(col * cellW, row * GLYPH_PX, cellW, GLYPH_PX);
269
+ g.clip();
270
+ g.fillText(chars[i], cx, cy);
271
+ g.restore();
272
+ }
273
+ }
274
+ return { canvas, cols, rows, cellW, cellH: GLYPH_PX };
275
+ }
276
+ // Upper bound on a single atlas axis (px). The image atlas is scaled to
277
+ // fit so it stays within common GPU max-texture-size limits.
278
+ const MAX_ATLAS_PX = 2048;
279
+ // Upper bound on a single tile cell (px) before the atlas-fit scale.
280
+ const MAX_TILE_PX = 256;
281
+ /** Intrinsic pixel size of an image source. */
282
+ function imageSize(src) {
283
+ if (typeof HTMLImageElement !== "undefined" &&
284
+ src instanceof HTMLImageElement) {
285
+ return [src.naturalWidth || src.width, src.naturalHeight || src.height];
286
+ }
287
+ const s = src;
288
+ return [s.width || 1, s.height || 1];
289
+ }
290
+ /** Load a tile source: decode URL strings, pass through ready images. */
291
+ async function resolveImage(src) {
292
+ if (typeof src !== "string") {
293
+ return src;
294
+ }
295
+ const img = new Image();
296
+ img.crossOrigin = "anonymous";
297
+ img.src = src;
298
+ try {
299
+ await img.decode();
300
+ }
301
+ catch {
302
+ // Broken / tainted source — drawImage will just paint nothing.
303
+ }
304
+ return img;
305
+ }
306
+ /**
307
+ * Render image tiles (dark → light order) into one atlas. All tiles share
308
+ * a single cell aspect (from the first tile) and are contain-fitted so
309
+ * odd sizes letterbox instead of distorting. The cell size is derived
310
+ * from the tallest tile, capped, then the whole atlas is scaled down to
311
+ * fit {@link MAX_ATLAS_PX}.
312
+ */
313
+ function buildImageAtlas(images) {
314
+ const n = Math.max(1, images.length);
315
+ const cols = Math.ceil(Math.sqrt(n));
316
+ const rows = Math.ceil(n / cols);
317
+ const sizes = images.map(imageSize);
318
+ const [w0, h0] = sizes[0] ?? [1, 1];
319
+ const aspect = w0 / Math.max(1, h0);
320
+ const tallest = Math.max(1, ...sizes.map((s) => s[1]));
321
+ let cellH = Math.min(MAX_TILE_PX, Math.max(8, Math.round(tallest)));
322
+ let cellW = Math.max(1, Math.round(cellH * aspect));
323
+ // Shrink uniformly so neither atlas axis exceeds the cap.
324
+ const fit = Math.min(1, MAX_ATLAS_PX / (cols * cellW), MAX_ATLAS_PX / (rows * cellH));
325
+ cellW = Math.max(1, Math.floor(cellW * fit));
326
+ cellH = Math.max(1, Math.floor(cellH * fit));
327
+ const canvas = document.createElement("canvas");
328
+ canvas.width = cols * cellW;
329
+ canvas.height = rows * cellH;
330
+ const g = canvas.getContext("2d");
331
+ if (g) {
332
+ g.clearRect(0, 0, canvas.width, canvas.height);
333
+ for (let i = 0; i < images.length; i++) {
334
+ const [iw, ih] = sizes[i];
335
+ const s = Math.min(cellW / iw, cellH / ih);
336
+ const dw = iw * s;
337
+ const dh = ih * s;
338
+ const dx = (i % cols) * cellW + (cellW - dw) / 2;
339
+ const dy = Math.floor(i / cols) * cellH + (cellH - dh) / 2;
340
+ g.drawImage(images[i], dx, dy, dw, dh);
341
+ }
342
+ }
343
+ return { canvas, cols, rows, cellW, cellH };
344
+ }
345
+ /**
346
+ * ASCII / glyph-mosaic effect.
347
+ *
348
+ * @example
349
+ * ```ts
350
+ * vfx.add(el, { effect: new AsciiEffect({ font: "Helvetica", grid: 16 }) });
351
+ * vfx.add(el, { effect: new AsciiEffect({ chars: [" ", ".", "+", "#"] }) });
352
+ * // Image tiles (dark → light), each keeping its own colour:
353
+ * vfx.add(el, { effect: new AsciiEffect({ tiles: ["/0.png", "/1.png"] }) });
354
+ * ```
355
+ *
356
+ * Glyphs keep their native aspect ratio regardless of the `grid` ratio:
357
+ * each is fitted (contain) into its cell, so a non-square cell letterboxes
358
+ * rather than stretching the character. Match `grid` to the font's
359
+ * character box (≈ advance : em) for gap-free tiling.
360
+ *
361
+ * `grid`, `color`, `background`, `colorFromSource`, `invert`, and `dither`
362
+ * are live (read every frame). `chars` / `tiles` / `font` / `fontWeight` /
363
+ * `charAspect` are baked into the atlas at `init()` — after changing them
364
+ * via `setParams`, call {@link AsciiEffect.updateAtlas} (or re-add the
365
+ * effect) to rebuild.
366
+ */
367
+ class AsciiEffect {
368
+ constructor(initial = {}) {
369
+ _AsciiEffect_instances.add(this);
370
+ _AsciiEffect_atlas.set(this, null);
371
+ _AsciiEffect_cols.set(this, 1);
372
+ _AsciiEffect_rows.set(this, 1);
373
+ _AsciiEffect_charCount.set(this, 1);
374
+ _AsciiEffect_glyphAspect.set(this, 1);
375
+ _AsciiEffect_atlasCellPx.set(this, [1, 1]);
376
+ _AsciiEffect_tileColor.set(this, false);
377
+ _AsciiEffect_ctx.set(this, null);
378
+ this.params = { ...DEFAULT_PARAMS, ...initial };
379
+ }
380
+ setParams(updates) {
381
+ Object.assign(this.params, updates);
382
+ }
383
+ async init(ctx) {
384
+ if (typeof document === "undefined") {
385
+ return;
386
+ }
387
+ __classPrivateFieldSet(this, _AsciiEffect_ctx, ctx, "f");
388
+ await __classPrivateFieldGet(this, _AsciiEffect_instances, "m", _AsciiEffect_build).call(this, ctx);
389
+ }
390
+ /**
391
+ * Rebuild the atlas from the current params, applying changes to the
392
+ * baked fields (`chars` / `tiles` / `font` / `fontWeight` /
393
+ * `charAspect`) without removing and re-adding the effect. Async — it
394
+ * may load fonts or decode image tiles. No-op before `init()`.
395
+ *
396
+ * Allocates a fresh atlas texture and disposes the previous one once the
397
+ * swap is live, so repeated calls don't leak. Still does real work
398
+ * (rasterising / decoding), so prefer occasional calls (e.g. on a
399
+ * settings change) over per-frame use.
400
+ */
401
+ async updateAtlas() {
402
+ if (!__classPrivateFieldGet(this, _AsciiEffect_ctx, "f")) {
403
+ return;
404
+ }
405
+ await __classPrivateFieldGet(this, _AsciiEffect_instances, "m", _AsciiEffect_build).call(this, __classPrivateFieldGet(this, _AsciiEffect_ctx, "f"));
406
+ }
407
+ render(ctx) {
408
+ if (!__classPrivateFieldGet(this, _AsciiEffect_atlas, "f")) {
409
+ return;
410
+ }
411
+ const [ew, eh] = ctx.dims.elementPixel;
412
+ const [gx, gy] = resolveGrid(this.params.grid);
413
+ const cellPx = [
414
+ Math.max(1, gx) * ctx.pixelRatio,
415
+ Math.max(1, gy) * ctx.pixelRatio,
416
+ ];
417
+ ctx.draw({
418
+ frag: FRAG_ASCII,
419
+ uniforms: {
420
+ src: ctx.src,
421
+ atlas: __classPrivateFieldGet(this, _AsciiEffect_atlas, "f"),
422
+ elementPx: [Math.max(1, ew), Math.max(1, eh)],
423
+ cellPx,
424
+ cols: __classPrivateFieldGet(this, _AsciiEffect_cols, "f"),
425
+ rows: __classPrivateFieldGet(this, _AsciiEffect_rows, "f"),
426
+ charCount: __classPrivateFieldGet(this, _AsciiEffect_charCount, "f"),
427
+ glyphAspect: __classPrivateFieldGet(this, _AsciiEffect_glyphAspect, "f"),
428
+ atlasCellPx: __classPrivateFieldGet(this, _AsciiEffect_atlasCellPx, "f"),
429
+ tileColor: __classPrivateFieldGet(this, _AsciiEffect_tileColor, "f") ? 1 : 0,
430
+ color: this.params.color,
431
+ background: this.params.background,
432
+ colorFromSource: this.params.colorFromSource ? 1 : 0,
433
+ invert: this.params.invert ? 1 : 0,
434
+ dither: Math.min(1, Math.max(0, this.params.dither)),
435
+ },
436
+ target: ctx.target,
437
+ });
438
+ }
439
+ dispose() {
440
+ __classPrivateFieldGet(this, _AsciiEffect_atlas, "f")?.dispose();
441
+ __classPrivateFieldSet(this, _AsciiEffect_atlas, null, "f");
442
+ __classPrivateFieldSet(this, _AsciiEffect_ctx, null, "f");
443
+ }
444
+ }
445
+ exports.AsciiEffect = AsciiEffect;
446
+ _AsciiEffect_atlas = new WeakMap(), _AsciiEffect_cols = new WeakMap(), _AsciiEffect_rows = new WeakMap(), _AsciiEffect_charCount = new WeakMap(), _AsciiEffect_glyphAspect = new WeakMap(), _AsciiEffect_atlasCellPx = new WeakMap(), _AsciiEffect_tileColor = new WeakMap(), _AsciiEffect_ctx = new WeakMap(), _AsciiEffect_instances = new WeakSet(), _AsciiEffect_build = async function _AsciiEffect_build(ctx) {
447
+ const tiles = this.params.tiles;
448
+ let built;
449
+ if (tiles && tiles.length > 0) {
450
+ const images = await Promise.all(tiles.map(resolveImage));
451
+ __classPrivateFieldSet(this, _AsciiEffect_charCount, images.length, "f");
452
+ __classPrivateFieldSet(this, _AsciiEffect_tileColor, true, "f");
453
+ built = buildImageAtlas(images);
454
+ }
455
+ else {
456
+ if (tiles && tiles.length === 0) {
457
+ console.warn("[VFX-JS] AsciiEffect: `tiles` is empty; falling back to characters.");
458
+ }
459
+ const chars = resolveChars(this.params.chars ?? exports.ASCII_PRESETS[this.params.preset]);
460
+ if (chars.length === 0) {
461
+ console.warn("[VFX-JS] AsciiEffect: empty character ramp; nothing will be rendered.");
462
+ }
463
+ __classPrivateFieldSet(this, _AsciiEffect_charCount, Math.max(1, chars.length), "f");
464
+ __classPrivateFieldSet(this, _AsciiEffect_tileColor, false, "f");
465
+ await ensureFont(this.params.font, this.params.fontWeight);
466
+ built = buildAtlas(chars, this.params.font, this.params.fontWeight, this.params.charAspect);
467
+ }
468
+ // Swap in the new atlas, then free the old one. Disposing after the
469
+ // swap means any render() that ran between the awaits above still
470
+ // sampled the previous, valid texture. No-op on the first build.
471
+ const previous = __classPrivateFieldGet(this, _AsciiEffect_atlas, "f");
472
+ __classPrivateFieldSet(this, _AsciiEffect_cols, built.cols, "f");
473
+ __classPrivateFieldSet(this, _AsciiEffect_rows, built.rows, "f");
474
+ __classPrivateFieldSet(this, _AsciiEffect_glyphAspect, built.cellW / built.cellH, "f");
475
+ __classPrivateFieldSet(this, _AsciiEffect_atlasCellPx, [built.cellW, built.cellH], "f");
476
+ __classPrivateFieldSet(this, _AsciiEffect_atlas, ctx.wrapTexture(built.canvas, {
477
+ autoUpdate: false,
478
+ filter: "linear",
479
+ }), "f");
480
+ previous?.dispose();
481
+ };
482
+ //# sourceMappingURL=ascii.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ascii.js","sourceRoot":"","sources":["../../src/ascii.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAmCA;;;;GAIG;AACU,QAAA,aAAa,GAAoC;IAC1D,yEAAyE;IACzE,oDAAoD;IACpD,QAAQ,EAAE,YAAY;IACtB,OAAO,EAAE,KAAK;IACd,4DAA4D;IAC5D,MAAM,EAAE,OAAO;IACf,uDAAuD;IACvD,IAAI,EAAE,OAAO;IACb,+CAA+C;IAC/C,OAAO,EAAE,OAAO;IAChB,mEAAmE;IACnE,oDAAoD;IACpD,QAAQ,EACJ,0EAA0E;CACjF,CAAC;AAEF,MAAM,UAAU,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmIlB,CAAC;AAsFF,MAAM,cAAc,GAAgB;IAChC,IAAI,EAAE,EAAE;IACR,MAAM,EAAE,UAAU;IAClB,IAAI,EAAE,WAAW;IACjB,UAAU,EAAE,QAAQ;IACpB,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACnB,UAAU,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACxB,eAAe,EAAE,KAAK;IACtB,MAAM,EAAE,KAAK;IACb,MAAM,EAAE,CAAC;CACZ,CAAC;AAEF,yEAAyE;AACzE,wEAAwE;AACxE,wEAAwE;AACxE,MAAM,QAAQ,GAAG,EAAE,CAAC;AAEpB;;GAEG;AACH,SAAS,YAAY,CAAC,IAAmB;IACrC,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,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,0BAAO,CAAC;IAC7B,uBAAA,IAAI,qBAAS,KAAK,CAAC,IAAI,MAAA,CAAC;IACxB,uBAAA,IAAI,qBAAS,KAAK,CAAC,IAAI,MAAA,CAAC;IACxB,uBAAA,IAAI,4BAAgB,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,MAAA,CAAC;IAC9C,uBAAA,IAAI,4BAAgB,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,MAAA,CAAC;IAC/C,uBAAA,IAAI,sBAAU,GAAG,CAAC,WAAW,CAAC,KAAK,CAAC,MAAM,EAAE;QACxC,UAAU,EAAE,KAAK;QACjB,MAAM,EAAE,QAAQ;KACnB,CAAC,MAAA,CAAC;IACH,QAAQ,EAAE,OAAO,EAAE,CAAC;AACxB,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { Effect, EffectContext } from "@vfx-js/core";
2
+ export type ChromaticParams = {
3
+ /** Strength of the colour separation. */
4
+ intensity: number;
5
+ /** Radius (in normalised units) where the aberration begins. */
6
+ radius: number;
7
+ /** Falloff exponent of the aberration curve. */
8
+ power: number;
9
+ };
10
+ export declare class ChromaticEffect implements Effect {
11
+ params: ChromaticParams;
12
+ constructor(initial?: Partial<ChromaticParams>);
13
+ setParams(updates: Partial<ChromaticParams>): void;
14
+ render(ctx: EffectContext): void;
15
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChromaticEffect = void 0;
4
+ const FRAG_CHROMATIC = `#version 300 es
5
+ precision highp float;
6
+ in vec2 uvContent;
7
+ out vec4 outColor;
8
+ uniform sampler2D src;
9
+ uniform vec4 srcRectUv;
10
+ uniform float aspect;
11
+ uniform float intensity;
12
+ uniform float radius;
13
+ uniform float power;
14
+
15
+ // Sample at a content-space UV mirror-wrapped into [0, 1], remapped into
16
+ // the padded src buffer via srcRectUv.
17
+ vec4 mirrorTex(vec2 uv) {
18
+ vec2 uv2 = 1. - abs(1. - mod(uv, 2.0));
19
+ return texture(src, srcRectUv.xy + uv2 * srcRectUv.zw);
20
+ }
21
+
22
+ void main() {
23
+ vec2 uv = uvContent;
24
+
25
+ vec2 p = uv * 2.0 - 1.0;
26
+ p.x *= aspect;
27
+
28
+ float l = max(length(p) - radius, 0.);
29
+ float d = pow(l, power) * (intensity * 0.1);
30
+
31
+ vec2 uvR = (uv - .5) / (1.0 + d * 1.) + 0.5;
32
+ vec2 uvG = (uv - .5) / (1.0 + d * 2.) + 0.5;
33
+ vec2 uvB = (uv - .5) / (1.0 + d * 3.) + 0.5;
34
+
35
+ vec4 cr = mirrorTex(uvR);
36
+ vec4 cg = mirrorTex(uvG);
37
+ vec4 cb = mirrorTex(uvB);
38
+
39
+ outColor = vec4(cr.r, cg.g, cb.b, (cr.a + cg.a + cb.a) / 3.0);
40
+ }
41
+ `;
42
+ const DEFAULT_PARAMS = {
43
+ intensity: 0.3,
44
+ radius: 0.0,
45
+ power: 2.0,
46
+ };
47
+ class ChromaticEffect {
48
+ constructor(initial = {}) {
49
+ this.params = { ...DEFAULT_PARAMS, ...initial };
50
+ }
51
+ setParams(updates) {
52
+ Object.assign(this.params, updates);
53
+ }
54
+ render(ctx) {
55
+ const [w, h] = ctx.dims.element;
56
+ ctx.draw({
57
+ frag: FRAG_CHROMATIC,
58
+ uniforms: {
59
+ src: ctx.src,
60
+ aspect: (w || 1) / (h || 1),
61
+ intensity: this.params.intensity,
62
+ radius: this.params.radius,
63
+ power: this.params.power,
64
+ },
65
+ target: ctx.target,
66
+ });
67
+ }
68
+ }
69
+ exports.ChromaticEffect = ChromaticEffect;
70
+ //# sourceMappingURL=chromatic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chromatic.js","sourceRoot":"","sources":["../../src/chromatic.ts"],"names":[],"mappings":";;;AAKA,MAAM,cAAc,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqCtB,CAAC;AAWF,MAAM,cAAc,GAAoB;IACpC,SAAS,EAAE,GAAG;IACd,MAAM,EAAE,GAAG;IACX,KAAK,EAAE,GAAG;CACb,CAAC;AAEF,MAAa,eAAe;IAGxB,YAAY,UAAoC,EAAE;QAC9C,IAAI,CAAC,MAAM,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,OAAO,EAAE,CAAC;IACpD,CAAC;IAED,SAAS,CAAC,OAAiC;QACvC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACxC,CAAC;IAED,MAAM,CAAC,GAAkB;QACrB,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC;QAChC,GAAG,CAAC,IAAI,CAAC;YACL,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE;gBACN,GAAG,EAAE,GAAG,CAAC,GAAG;gBACZ,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;gBAC3B,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS;gBAChC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM;gBAC1B,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK;aAC3B;YACD,MAAM,EAAE,GAAG,CAAC,MAAM;SACrB,CAAC,CAAC;IACP,CAAC;CACJ;AAzBD,0CAyBC"}
@@ -0,0 +1,23 @@
1
+ import type { Effect, EffectContext } from "@vfx-js/core";
2
+ export type ColoredEdgesParams = {
3
+ /** Edge detection threshold, in [0, 1]. */
4
+ threshold: number;
5
+ /** Sampling radius for the Sobel kernel, in px. */
6
+ thickness: number;
7
+ /** Edge contrast multiplier. */
8
+ intensity: number;
9
+ /** Blend the source back in over the edges, in [0, 1]. */
10
+ opacity: number;
11
+ /** Gradient start color. Accepts a hex string. */
12
+ color1: string;
13
+ /** Gradient end color. Accepts a hex string. */
14
+ color2: string;
15
+ /** Background color for non-edge areas. Accepts a hex string. */
16
+ background: string;
17
+ };
18
+ export declare class ColoredEdgesEffect implements Effect {
19
+ params: ColoredEdgesParams;
20
+ constructor(initial?: Partial<ColoredEdgesParams>);
21
+ setParams(updates: Partial<ColoredEdgesParams>): void;
22
+ render(ctx: EffectContext): void;
23
+ }