@mathzetta/qblur 0.2.0 → 0.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.
package/docs/filters.md CHANGED
@@ -378,6 +378,25 @@ It is a mask, so a color space leaves it alone. The runner takes it as
378
378
  a picture named `selection` (white = in): `runner.setImage('selection',
379
379
  maskCanvas)`.
380
380
 
381
+ `away` is how far the pixel is from the selection, in pixels: 0 inside
382
+ it, 10 ten pixels out, and so on (0 everywhere when nothing is selected).
383
+ It makes halos and fades that follow the object's shape:
384
+
385
+ ```
386
+ mix((1, 0.8, 0.3), rgb, smoothstep(0, 60, away)) a warm glow round the selection, 60 pixels wide
387
+ mix(rgb, gaussian(6), min(1, away / 150)) sharp near the object, blurrier farther away
388
+ if away > 0 and away < 4 then (1, 1, 1) a 4-pixel outline just outside it
389
+ ```
390
+
391
+ <!-- dev -->
392
+ The runners work it out from the `selection` they are given, only for a
393
+ filter that uses `away` (an exact distance transform of the selection
394
+ measured at most 512 pixels across, about 12 ms; `awayPicture()` in
395
+ src/distance.js), so a page has nothing more to do. A page with its own
396
+ distances can pass them as `runner.setImage('away', picture)`: white is
397
+ 0, red = 1 − min(d, 255)/255, green = 1 − min(d, 1020)/1020.
398
+ <!-- /dev -->
399
+
381
400
  The editor's *Select & draw* tab makes one. *Trace the edge* is intelligent scissors
382
401
  (livewire): click a few points around the object and the line between
383
402
  them follows the edge — every pixel has a cost, low where the brightness
package/docs/library.md CHANGED
@@ -31,7 +31,7 @@ Some things people build with it: filters on upload in a photo or social app; ef
31
31
  webcam stream; a product page that recolors an item as the customer picks; generative art and
32
32
  animated backgrounds; previews in a CMS; high-contrast or color-blindness views for accessibility; a
33
33
  lesson on how pictures are just numbers. The [small apps](https://qblur.com/avatar/) on qblur.com —
34
- profile picture, color splash, make it move, fancy text — are each a page of this library and a few
34
+ profile picture, make it move, fancy text — are each a page of this library and a few
35
35
  controls.
36
36
 
37
37
  ## Start here
@@ -41,7 +41,7 @@ controls.
41
41
  ```html runnable
42
42
  <img id="photo" src="photo.jpg" alt="" hidden>
43
43
  <canvas id="out"></canvas>
44
- <script src="https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.2/dist/qblur.global.min.js"></script>
44
+ <script src="https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.3/dist/qblur.global.min.js"></script>
45
45
  <script>
46
46
  const img = document.getElementById('photo');
47
47
  img.decode().then(() => QBlur.applyFilter(img, 'Sepia', { canvas: document.getElementById('out') }));
@@ -70,7 +70,7 @@ picture that redraws many times a second as a knob moves, a runner of your own (
70
70
 
71
71
  ```html runnable
72
72
  <qblur-image id="out" src="photo.jpg" filter="Vignette" params='{"strength": 2.5}' alt="A street of colored houses"></qblur-image>
73
- <script type="module" src="https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.2/dist/qblur-element.js"></script>
73
+ <script type="module" src="https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.3/dist/qblur-element.js"></script>
74
74
  ```
75
75
 
76
76
  `<qblur-image>` draws its picture with the filter on it, and again whenever an attribute changes:
@@ -85,7 +85,7 @@ picture from another site must allow CORS). It fires `load` after each drawing a
85
85
  ```html runnable
86
86
  <img id="photo" src="photo.jpg" alt="">
87
87
  <canvas id="out"></canvas>
88
- <script src="https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.2/dist/qblur.global.min.js"></script>
88
+ <script src="https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.3/dist/qblur.global.min.js"></script>
89
89
  <script>
90
90
  const img = document.getElementById('photo'), canvas = document.getElementById('out');
91
91
  img.decode().then(() => {
@@ -107,7 +107,7 @@ a WebGL context and the CPU when it doesn't; pass the same canvas to `draw` eith
107
107
  <canvas id="out"></canvas>
108
108
  <label>Vignette <input id="strength" type="range" min="0" max="4" step="0.01" value="1.6"></label>
109
109
  <script type="module">
110
- import { compileFilter, createRunner, LIBRARY } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.2/dist/qblur.js';
110
+ import { compileFilter, createRunner, LIBRARY } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.3/dist/qblur.js';
111
111
 
112
112
  const img = new Image();
113
113
  img.src = 'photo.jpg';
@@ -173,7 +173,7 @@ at one file:
173
173
  } }
174
174
  </script>
175
175
  <script type="module">
176
- import { compileFilter, createRunner } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.2/dist/qblur.external.js';
176
+ import { compileFilter, createRunner } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.3/dist/qblur.external.js';
177
177
  const img = new Image();
178
178
  img.src = 'photo.jpg';
179
179
  await img.decode();
@@ -186,11 +186,11 @@ at one file:
186
186
  ```
187
187
 
188
188
  With classic scripts: `<script src="…/mathzetta@1/build/mathzetta.glsl.min.js">` (the GLSL flavor, which
189
- is what filters use) and then `…/@mathzetta/qblur@0.2/dist/qblur.external.global.min.js`.
189
+ is what filters use) and then `…/@mathzetta/qblur@0.3/dist/qblur.external.global.min.js`.
190
190
 
191
- Pin a version (`@0.2` follows fixes, `@0.2.0` never changes) and, for a fixed version, add an `integrity`
191
+ Pin a version (`@0.3` follows fixes, `@0.3.0` never changes) and, for a fixed version, add an `integrity`
192
192
  hash so the browser checks the file. jsDelivr shows each file's hash on its package page, or make it
193
- yourself: `curl -s https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.2.0/dist/qblur.js | openssl dgst -sha384 -binary | openssl base64 -A`
193
+ yourself: `curl -s https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.3.0/dist/qblur.js | openssl dgst -sha384 -binary | openssl base64 -A`
194
194
  gives the part after `sha384-`.
195
195
 
196
196
  ## Filters
@@ -341,7 +341,7 @@ so a page can build its controls from the filter:
341
341
  <canvas id="out"></canvas>
342
342
  <form id="knobs"></form>
343
343
  <script type="module">
344
- import { compileFilter, createRunner, normalizeFilter, filterParams } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.2/dist/qblur.js';
344
+ import { compileFilter, createRunner, normalizeFilter, filterParams } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.3/dist/qblur.js';
345
345
 
346
346
  const def = {
347
347
  name: 'Duotone',
@@ -384,7 +384,7 @@ In a chain, `filterParams` gives each knob's own default; the values a chain set
384
384
  ```html runnable
385
385
  <canvas id="out"></canvas>
386
386
  <script type="module">
387
- import { compileFilter, createRunner } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.2/dist/qblur.js';
387
+ import { compileFilter, createRunner } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.3/dist/qblur.js';
388
388
  const img = new Image();
389
389
  img.src = 'photo.jpg';
390
390
  await img.decode();
@@ -404,7 +404,7 @@ A video is a picture that changes: give the runner each frame.
404
404
  <video id="clip" src="clip.mp4" muted loop playsinline autoplay hidden></video>
405
405
  <canvas id="out"></canvas>
406
406
  <script type="module">
407
- import { compileFilter, createRunner } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.2/dist/qblur.js';
407
+ import { compileFilter, createRunner } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.3/dist/qblur.js';
408
408
  const video = document.getElementById('clip'), canvas = document.getElementById('out');
409
409
  await new Promise((r) => { video.onloadeddata = r; if (video.readyState >= 2) r(); });
410
410
  canvas.width = video.videoWidth;
@@ -431,7 +431,7 @@ this pixel, `luma(mask)` its brightness, `alpha(mask)` its transparency — and
431
431
  ```html runnable
432
432
  <canvas id="out"></canvas>
433
433
  <script type="module">
434
- import { compileFilter, createRunner, LIBRARY } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.2/dist/qblur.js';
434
+ import { compileFilter, createRunner, LIBRARY } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.3/dist/qblur.js';
435
435
  const img = new Image();
436
436
  img.src = 'photo.jpg';
437
437
  await img.decode();
@@ -456,7 +456,10 @@ A picture of another shape is placed over the frame: `cover` (the default), `fit
456
456
  with `zoom`, `x`, `y` and `angle` — set per step (`{ filter, with: { overlay: { place: 'fit', zoom: 0.5 } } }`)
457
457
  or live (`runner.passes[i].placements.overlay.x = 0.2`; changing `place` needs a new compile).
458
458
  `selected` is the picture named `selection` (the editor's selection): give it a mask to limit a filter to
459
- part of the picture.
459
+ part of the picture. `away` is how many pixels a pixel is from that selection (0 inside it, and everywhere
460
+ without one): the runner works it out from the `selection` you give, only for a filter that uses it
461
+ (`mix((1, 0.8, 0.3), rgb, smoothstep(0, 60, away))` is a glow round the selection). To give distances of your own,
462
+ `setImage('away', awayPicture(mask))` — or any picture in that encoding.
460
463
 
461
464
  ## Text and shapes
462
465
 
@@ -467,7 +470,7 @@ frame, so they are crisp at any size.
467
470
  ```html runnable
468
471
  <canvas id="out"></canvas>
469
472
  <script type="module">
470
- import { compileFilter, createRunner, renderGraphics } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.2/dist/qblur.js';
473
+ import { compileFilter, createRunner, renderGraphics } from 'https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.3/dist/qblur.js';
471
474
  const img = new Image();
472
475
  img.src = 'photo.jpg';
473
476
  await img.decode();
@@ -587,6 +590,8 @@ Everything the package exports — from `@mathzetta/qblur/apply`, the first seve
587
590
  | `normalizeStrokes`, `strokeTimeline`, `rasterizeStrokes(spec, w, h, t, { mask })` | brush strokes, frame by frame |
588
591
  | `BRUSHES`, `TIMINGS`, `WIDTH_FROM`, `CLIPS`, `FPS`, `STROKES_DEFAULTS` | brush options |
589
592
  | `traceContours(mask, w, h)`, `simplify`, `simplifyLoop` | the outlines of a mask, as polylines |
593
+ | `awayPicture(selection, { size })` | the picture `away` is read from, for a selection mask (white = in): its distances measured at most `size` (`AWAY_SIZE`, 512) across; the runners make it themselves |
594
+ | `encodeAway(data, w, h, { channel, scale })`, `distanceTo(inside, w, h)`, `awayFromChannels(red, green)`, `usesAway(compiled)` | the same from a mask's pixels; the exact distance transform; decoding; whether a filter reads `away` |
590
595
  | `compileCpuPass(pass)` | one pass as checked JavaScript (what the CPU runner runs; `.uses` says if it reads `t`, other pictures, `random`) |
591
596
 
592
597
  ## Good to know
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mathzetta/qblur",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Shareable image filters written as math: a JSON format with params, pictures and chains, compiled to WebGL shaders by MathZetta.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -67,7 +67,7 @@
67
67
  "check:docs": "node tools/check-docs.mjs"
68
68
  },
69
69
  "peerDependencies": {
70
- "mathzetta": "^1.4.0"
70
+ "mathzetta": "^1.5.0"
71
71
  },
72
72
  "devDependencies": {
73
73
  "@mathzetta/inlinemath": "file:../mathzetta/packages/inlinemath",
package/src/cpu-runner.js CHANGED
@@ -27,6 +27,7 @@
27
27
  */
28
28
 
29
29
  import { compileCpuPass } from './cpu.js';
30
+ import { deriveAway } from './distance.js';
30
31
 
31
32
  export function createCpuRunner(compiled, options = {}) {
32
33
  const precision = options.precision === 'float' || options.precision === 'half' ? 'float' : 'byte';
@@ -47,6 +48,7 @@ export function createCpuRunner(compiled, options = {}) {
47
48
 
48
49
  setImage(name, image) {
49
50
  if (typeof name !== 'string') { image = name; name = null; }
51
+ if (name && name !== 'source' && image == null) { delete pictures[name]; return runner; } // back to "not given" (white)
50
52
  const picture = toPicture(image);
51
53
  if (!name || name === 'source') source = picture;
52
54
  else pictures[name] = picture;
@@ -158,7 +160,7 @@ export function createCpuRunner(compiled, options = {}) {
158
160
  sent = new Map();
159
161
  },
160
162
  };
161
- return runner;
163
+ return deriveAway(runner, compiled);
162
164
  }
163
165
 
164
166
  /** Clamp to 0–1 and round to 1/255: what an 8-bit texture keeps. */
package/src/cpu.js CHANGED
@@ -171,6 +171,10 @@ class Generator {
171
171
  if (v === 'rgb') return vec(['$r', '$g', '$b']);
172
172
  if (v === 'unchanged') return f('$u'); // the kept value; as a whole if/else branch it takes the other branch's type (ternary)
173
173
  if (v === 'selected') return f(this.pictureHere('selection').c[0]); // the selection mask: white = 1; white everywhere when none
174
+ if (v === 'away') { // pixels from the selection, from the picture `away` (src/distance.js; white = 0)
175
+ const [red, green] = this.pictureHere('away').c;
176
+ return f(`(${red} > 0 ? 255 * (1 - ${red}) : 1020 * (1 - ${green}))`);
177
+ }
174
178
  if (CONSTANTS[v]) return f(CONSTANTS[v]);
175
179
  if (v === 'true' || v === 'false') return bool(v);
176
180
  if (Object.prototype.hasOwnProperty.call(this.params, v)) {
@@ -201,7 +205,7 @@ class Generator {
201
205
  this.pictureNames.add(name);
202
206
  this.body.push(`$h_picture($Q_${name}, $qw_${name}, $qh_${name}, $w, $h, $qz_${name}, $qx_${name}, $qy_${name}, $qa_${name}, $qm_${name}, ${X}, ${Y}, ${out});`);
203
207
  }
204
- if (this.space && name !== 'selection') this.body.push(`$h_col_to${this.space}(${out}[0], ${out}[1], ${out}[2], ${out});`); // a mask is not a color
208
+ if (this.space && name !== 'selection' && name !== 'away') this.body.push(`$h_col_to${this.space}(${out}[0], ${out}[1], ${out}[2], ${out});`); // a mask is not a color
205
209
  return vec([0, 1, 2, 3].map((k) => `${out}[${k}]`));
206
210
  }
207
211
 
@@ -0,0 +1,145 @@
1
+ /**
2
+ * `away`: how far each pixel is from the selection, in pixels — 0 inside it, and 0 everywhere when nothing is selected.
3
+ *
4
+ * The runners make it themselves from the picture given as `selection`, and only for a filter that uses `away`
5
+ * (runner.setImage('away', …) gives one made elsewhere instead). The distance is the exact Euclidean distance transform
6
+ * of Felzenszwalb & Huttenlocher: a pass down each column, then one across each row, each linear in the pixels. A
7
+ * selection is measured at most AWAY_SIZE pixels on its long side (about 12 ms for 512 × 512), the distances scaled
8
+ * back to the selection's own pixels — the frame's, normally.
9
+ *
10
+ * The picture holds it in 8-bit channels, white where it is 0 (so a picture not given — white — means "0 everywhere"):
11
+ * red = 1 − min(d, 255) / 255 near: whole pixels up to 255 away
12
+ * green = 1 − min(d, 1020) / 1020 far: 4-pixel steps up to 1020 away
13
+ * blue = 1 − min(d_in, 255) / 255 inside the selection: how far from its edge
14
+ * and the shader reads `away` = red > 0 ? 255 (1 − red) : 1020 (1 − green) (mathzetta/glsl's filterShader; src/cpu.js).
15
+ */
16
+
17
+ const INF = 1e20;
18
+ /** The largest side a selection is measured at (the distances are scaled back up). */
19
+ export const AWAY_SIZE = 512;
20
+
21
+ /** 1-D squared distance transform of f (n values) into d, with scratch v (Int32Array n) and z (Float64Array n + 1). */
22
+ function edt1d(f, n, d, v, z) {
23
+ let k = 0;
24
+ v[0] = 0; z[0] = -INF; z[1] = INF;
25
+ for (let q = 1; q < n; q++) {
26
+ const fq = f[q] + q * q;
27
+ let p = v[k], s = (fq - (f[p] + p * p)) / (2 * (q - p));
28
+ while (s <= z[k]) { k--; p = v[k]; s = (fq - (f[p] + p * p)) / (2 * (q - p)); } // never below 0: INF is finite, so s > -INF
29
+ k++; v[k] = q; z[k] = s; z[k + 1] = INF;
30
+ }
31
+ k = 0;
32
+ for (let q = 0; q < n; q++) {
33
+ while (z[k + 1] < q) k++;
34
+ const p = v[k];
35
+ d[q] = (q - p) * (q - p) + f[p];
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Distance (in pixels) from every pixel to the nearest pixel where `inside(i)` is true; 0 on the shape.
41
+ * `inside` is a function of the pixel index, or a typed array (non-zero = on the shape). Infinity when there is no shape.
42
+ */
43
+ export function distanceTo(inside, width, height) {
44
+ const on = typeof inside === 'function' ? inside : (i) => inside[i] !== 0;
45
+ const n = Math.max(width, height);
46
+ const f = new Float64Array(n), d = new Float64Array(n), v = new Int32Array(n), z = new Float64Array(n + 1);
47
+ const grid = new Float64Array(width * height);
48
+ for (let i = 0; i < grid.length; i++) grid[i] = on(i) ? 0 : INF;
49
+ for (let x = 0; x < width; x++) { // down each column
50
+ for (let y = 0; y < height; y++) f[y] = grid[y * width + x];
51
+ edt1d(f, height, d, v, z);
52
+ for (let y = 0; y < height; y++) grid[y * width + x] = d[y];
53
+ }
54
+ const out = new Float32Array(width * height);
55
+ for (let y = 0; y < height; y++) { // then across each row
56
+ const row = y * width;
57
+ for (let x = 0; x < width; x++) f[x] = grid[row + x];
58
+ edt1d(f, width, d, v, z);
59
+ for (let x = 0; x < width; x++) out[row + x] = d[x] >= INF / 2 ? Infinity : Math.sqrt(d[x]);
60
+ }
61
+ return out;
62
+ }
63
+
64
+ /**
65
+ * The `away` picture from a mask's pixels (`data`: 4 per pixel, bytes 0–255 or floats 0–1; on the shape where `channel` —
66
+ * 0, red, as `selected` reads it — is at least half). `scale` multiplies the distances (a mask measured at half size: 2).
67
+ * @returns {{ data: Uint8ClampedArray, width: number, height: number }}
68
+ */
69
+ export function encodeAway(data, width, height, { channel = 0, scale = 1 } = {}) {
70
+ const half = data instanceof Float32Array || data instanceof Float64Array ? 0.5 : 128;
71
+ const at = (i) => data[i * 4 + channel] >= half;
72
+ const out = distanceTo(at, width, height);
73
+ const inn = distanceTo((i) => !at(i), width, height);
74
+ const px = new Uint8ClampedArray(width * height * 4);
75
+ for (let i = 0, j = 0; i < out.length; i++, j += 4) {
76
+ const d = out[i] * scale, e = inn[i] * scale;
77
+ px[j] = 255 - Math.min(d, 255);
78
+ px[j + 1] = 255 - Math.min(d, 1020) / 4;
79
+ px[j + 2] = 255 - Math.min(e, 255);
80
+ px[j + 3] = 255;
81
+ }
82
+ return { data: px, width, height };
83
+ }
84
+
85
+ /** `away` (in pixels) back from the picture's channels, 0–1 — what the shader and the CPU runner compute. */
86
+ export const awayFromChannels = (red, green) => (red > 0 ? 255 * (1 - red) : 1020 * (1 - green));
87
+
88
+ /**
89
+ * The `away` picture for a selection: ImageData, { data, width, height } (bytes or floats), or in a browser an
90
+ * <img>/<canvas>/<video>/ImageBitmap; null for none (then `away` is 0 everywhere). Measured at most `size` on the long side.
91
+ */
92
+ export function awayPicture(selection, { size = AWAY_SIZE } = {}) {
93
+ if (!selection) return null;
94
+ const W = selection.naturalWidth || selection.videoWidth || selection.width, H = selection.naturalHeight || selection.videoHeight || selection.height;
95
+ if (!W || !H) return null;
96
+ const k = Math.min(1, size / Math.max(W, H)), w = Math.max(1, Math.round(W * k)), h = Math.max(1, Math.round(H * k));
97
+ let data;
98
+ if (selection.data && !(typeof HTMLElement !== 'undefined' && selection instanceof HTMLElement)) {
99
+ data = selection.data;
100
+ if (w !== W || h !== H) { // nearest pixel: a mask, so no need to average
101
+ const small = new (data.constructor)(w * h * 4);
102
+ for (let y = 0; y < h; y++) {
103
+ const sy = Math.min(H - 1, Math.floor(((y + 0.5) * H) / h));
104
+ for (let x = 0; x < w; x++) {
105
+ const sx = Math.min(W - 1, Math.floor(((x + 0.5) * W) / w)), from = (sy * W + sx) * 4, to = (y * w + x) * 4;
106
+ for (let c = 0; c < 4; c++) small[to + c] = data[from + c];
107
+ }
108
+ }
109
+ data = small;
110
+ }
111
+ } else {
112
+ const canvas = typeof OffscreenCanvas !== 'undefined' ? new OffscreenCanvas(w, h) : Object.assign(document.createElement('canvas'), { width: w, height: h });
113
+ const g = canvas.getContext('2d', { willReadFrequently: true });
114
+ g.drawImage(selection, 0, 0, w, h);
115
+ data = g.getImageData(0, 0, w, h).data;
116
+ }
117
+ return encodeAway(data, w, h, { scale: W / w });
118
+ }
119
+
120
+ /** Whether a compiled filter (compileFilter()) reads `away`. */
121
+ export const usesAway = (compiled) => compiled.passes.some((pass) => pass.images?.includes('away'));
122
+
123
+ /**
124
+ * For a filter that reads `away`: the runner makes its picture from each `selection` it is given (and drops it with the
125
+ * selection), unless the page gives one itself — runner.setImage('away', picture); setImage('away', null) goes back to
126
+ * making it. Other runners are returned untouched.
127
+ */
128
+ export function deriveAway(runner, compiled) {
129
+ if (!usesAway(compiled)) return runner;
130
+ const set = runner.setImage.bind(runner);
131
+ let given = false, selection = null;
132
+ runner.setImage = (name, image) => {
133
+ if (name === 'away') {
134
+ given = image != null;
135
+ return set('away', given ? image : awayPicture(selection));
136
+ }
137
+ set(name, image);
138
+ if (name === 'selection') {
139
+ selection = image ?? null;
140
+ if (!given) set('away', awayPicture(selection));
141
+ }
142
+ return runner;
143
+ };
144
+ return runner;
145
+ }
package/src/element.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * <qblur-image>: a picture with a filter on it, as an HTML element.
3
3
  *
4
- * <script type="module" src="https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.2/dist/qblur-element.js"></script>
4
+ * <script type="module" src="https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.3/dist/qblur-element.js"></script>
5
5
  * <qblur-image src="photo.jpg" filter="Sepia" alt="The harbor at dusk"></qblur-image>
6
6
  * <qblur-image src="photo.jpg" filter="if gray > cutoff then 1 else 0" params='{"cutoff": 0.4}'></qblur-image>
7
7
  *
package/src/format.js CHANGED
@@ -498,6 +498,7 @@ function makePass(filter, settings, opacity, path, version) {
498
498
  const images = Object.keys(placements);
499
499
  if (fragment.includes('uniform sampler2D mz_img_source;')) images.push('source');
500
500
  if (fragment.includes('uniform sampler2D mz_img_selection;')) images.push('selection');
501
+ if (fragment.includes('uniform sampler2D mz_img_away;')) images.push('away'); // `away`: the runners make it from the selection
501
502
  // `color` is the filter's own expression(s): the CPU runner compiles that, the GPU runner the fragment shader.
502
503
  return { name: filter.name ?? '', author: filter.author ?? '', path, color: filter.color, space: filter._space ?? 'srgb', fragment, params, values, opacity, images, placements, graphics, keepInput: 0, blendKept: false };
503
504
  }
package/src/index.d.ts CHANGED
@@ -229,6 +229,21 @@ export function createFilterRunner(gl: WebGLRenderingContext | WebGL2RenderingCo
229
229
  export function createCpuRunner(compiled: CompiledFilter, options?: CpuRunnerOptions): CpuRunner;
230
230
  /** ImageData, { data, width, height }, or a drawable in a browser → 0–1 floats. */
231
231
  export function toPicture(image: PictureSource): { data: Float32Array; width: number; height: number };
232
+ /**
233
+ * `away`, how far each pixel is from the selection (in pixels), as the picture a filter reads it from (red = 1 − min(d, 255)/255,
234
+ * green = 1 − min(d, 1020)/1020, blue = the same inside, to 255). The runners make it from `selection` themselves; this is for
235
+ * making it elsewhere and passing it as runner.setImage('away', …). Measured at most `size` (AWAY_SIZE) on the long side.
236
+ */
237
+ export function awayPicture(selection: PictureSource | null, options?: { size?: number }): { data: Uint8ClampedArray; width: number; height: number } | null;
238
+ /** The same from a mask's pixels (4 per pixel, bytes or 0–1 floats; on the shape where `channel`, red by default, is at least half). */
239
+ export function encodeAway(data: ArrayLike<number>, width: number, height: number, options?: { channel?: number; scale?: number }): { data: Uint8ClampedArray; width: number; height: number };
240
+ /** The exact Euclidean distance (pixels) from each pixel to the nearest one on the shape; Infinity with no shape. */
241
+ export function distanceTo(inside: ArrayLike<number> | ((index: number) => boolean), width: number, height: number): Float32Array;
242
+ /** `away` in pixels from the picture's red and green channels (0–1). */
243
+ export function awayFromChannels(red: number, green: number): number;
244
+ /** Whether a compiled filter reads `away`. */
245
+ export function usesAway(compiled: CompiledFilter): boolean;
246
+ export const AWAY_SIZE: number;
232
247
  /** One pass compiled to JavaScript for the CPU (after the generated-code gate). */
233
248
  export function compileCpuPass(pass: Pass): { source: string; uses: Set<string>; pictures: string[]; run(ctx: object): void };
234
249
 
package/src/index.js CHANGED
@@ -7,6 +7,7 @@ export {
7
7
  export { createFilterRunner } from './runtime.js';
8
8
  export { createCpuRunner, toPicture } from './cpu-runner.js';
9
9
  export { compileCpuPass } from './cpu.js';
10
+ export { awayPicture, encodeAway, distanceTo, awayFromChannels, usesAway, AWAY_SIZE } from './distance.js';
10
11
  export { createRunner, chooseEngine } from './auto.js';
11
12
  export { LIBRARY, GROUPS } from './library.js';
12
13
  export { BRUSHES, TIMINGS, WIDTH_FROM, CLIPS, FPS, STROKES_DEFAULTS, normalizeStrokes, strokeTimeline, rasterizeStrokes, traceContours, simplify, simplifyLoop } from './strokes.js';
package/src/library.js CHANGED
@@ -79,6 +79,12 @@ const FILTERS = [
79
79
  { name: 'Only inside the selection', group: 'Other pictures', description: 'Put this last: the steps above show only inside what you selected on the picture (everywhere, without a selection)',
80
80
  color: 'mix(source, rgb, selected)' },
81
81
  { name: 'Only outside the selection', group: 'Other pictures', description: 'Put this last: the steps above show only outside what you selected', color: 'mix(rgb, source, selected)' },
82
+ { name: 'Glow round the selection', group: 'Other pictures', description: 'A colored halo round what you selected, fading over `reach` pixels (`away` is the distance from the selection)',
83
+ params: { glow: [1, 0.8, 0.3], reach: { default: 60, min: 2, max: 400 }, amount: { default: 0.9, min: 0, max: 1 } },
84
+ color: 'mix(rgb, glow, amount (1 - smoothstep(0, reach, away)) step(0.5, away))' },
85
+ { name: 'Blur away from the selection', group: 'Other pictures', description: 'Sharp on what you selected, blurrier the farther a pixel is from it, all blurred `reach` pixels away',
86
+ params: { sigma: { default: 6, min: 0.5, max: 8 }, reach: { default: 200, min: 1, max: 1000 } },
87
+ color: 'mix(rgb, gaussian(sigma), min(1, away / reach))' },
82
88
  { name: 'Caption', group: 'Text & shapes', description: 'Words along the bottom, with a soft shadow; change them under the step',
83
89
  graphics: { title: { text: 'Your caption here', y: 0.88 } }, color: 'mix(rgb, title, alpha(title))' },
84
90
  { name: 'Poster title', group: 'Text & shapes', description: 'Big outlined letters across the top',
package/src/runtime.js CHANGED
@@ -19,6 +19,7 @@
19
19
  * where the GPU can render to such textures; `runner.precision` says
20
20
  * what was granted.
21
21
  */
22
+ import { deriveAway } from './distance.js';
22
23
  import { filterVertexShader } from 'mathzetta/glsl';
23
24
 
24
25
  export function createFilterRunner(gl, compiled, options = {}) {
@@ -188,7 +189,7 @@ export function createFilterRunner(gl, compiled, options = {}) {
188
189
  gl.deleteBuffer(triangle);
189
190
  },
190
191
  };
191
- return runner;
192
+ return deriveAway(runner, compiled);
192
193
  }
193
194
 
194
195
  function clampAndFilter(gl, filter = gl.LINEAR) {