@mathzetta/qblur 0.1.2 → 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/CHANGELOG.md +45 -0
- package/README.md +24 -21
- package/dist/cpu-worker.js +2 -2
- package/dist/qblur-apply.js +2 -2
- package/dist/qblur-element.js +2 -2
- package/dist/qblur.core.js +2 -2
- package/dist/qblur.external.global.min.js +2 -2
- package/dist/qblur.external.js +2 -2
- package/dist/qblur.global.min.js +2 -2
- package/dist/qblur.js +2 -2
- package/docs/filters.md +110 -88
- package/docs/library.md +57 -52
- package/package.json +4 -4
- package/src/apply-filter.js +8 -7
- package/src/apply.d.ts +12 -11
- package/src/cpu-helpers.js +7 -7
- package/src/cpu-runner.js +3 -1
- package/src/cpu.js +55 -23
- package/src/distance.js +145 -0
- package/src/element.d.ts +1 -1
- package/src/element.js +3 -3
- package/src/format.js +15 -14
- package/src/graphics.js +11 -11
- package/src/index.d.ts +26 -11
- package/src/index.js +1 -0
- package/src/library.js +35 -29
- package/src/png.js +18 -17
- package/src/runtime.js +2 -1
- package/src/save.js +7 -7
- package/src/strokes.js +4 -4
package/src/index.d.ts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* qblur — image filters written as
|
|
2
|
+
* qblur — image filters written as math. Types for `import … from '@mathzetta/qblur'`
|
|
3
3
|
* (and dist/qblur.js, dist/qblur.external.js, window.QBlur in the classic builds).
|
|
4
4
|
* The format itself is described in docs/filters.md; these follow src/.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
// ---- the filter document (mathzetta-filter/1) ----------------------------------------------
|
|
8
8
|
|
|
9
|
-
export type
|
|
9
|
+
export type Color = [number, number, number];
|
|
10
10
|
|
|
11
|
-
/** A knob: `0.5` (a number 0–1), `[1, 0, 0]` (a
|
|
12
|
-
export type ParamSpec = number |
|
|
11
|
+
/** A knob: `0.5` (a number 0–1), `[1, 0, 0]` (a color), or in full. */
|
|
12
|
+
export type ParamSpec = number | Color | {
|
|
13
13
|
type?: 'number' | 'color';
|
|
14
|
-
default?: number |
|
|
14
|
+
default?: number | Color;
|
|
15
15
|
min?: number;
|
|
16
16
|
max?: number;
|
|
17
17
|
step?: number;
|
|
18
18
|
label?: string;
|
|
19
19
|
/** A menu of these values. */
|
|
20
20
|
options?: number[];
|
|
21
|
-
/** "box": typed in, as
|
|
21
|
+
/** "box": typed in, as math; the default is a slider. */
|
|
22
22
|
input?: 'slider' | 'box';
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
export interface NormalizedParam {
|
|
26
26
|
type: 'number' | 'color';
|
|
27
|
-
default: number |
|
|
27
|
+
default: number | Color;
|
|
28
28
|
min?: number;
|
|
29
29
|
max?: number;
|
|
30
30
|
step?: number;
|
|
@@ -55,7 +55,7 @@ export interface GraphicSpec {
|
|
|
55
55
|
/** One expression for every channel, or one per channel. */
|
|
56
56
|
export type ColorExpression = string | Partial<Record<'r' | 'g' | 'b' | 'a' | 'rgb' | 'rgba', string>>;
|
|
57
57
|
|
|
58
|
-
export type
|
|
58
|
+
export type ColorSpace = 'srgb' | 'linear' | 'hsv' | 'hsl' | 'lab' | 'oklab' | 'ycbcr';
|
|
59
59
|
|
|
60
60
|
/** A step of a chain: an expression, a filter, or a filter with settings. */
|
|
61
61
|
export type Step = string | FilterDocument | {
|
|
@@ -67,7 +67,7 @@ export type Step = string | FilterDocument | {
|
|
|
67
67
|
skip?: boolean;
|
|
68
68
|
};
|
|
69
69
|
|
|
70
|
-
/** A filter as published:
|
|
70
|
+
/** A filter as published: math plus a name, an author, its knobs and the pictures it needs. */
|
|
71
71
|
export interface FilterDocument {
|
|
72
72
|
format?: 'mathzetta-filter/1';
|
|
73
73
|
name?: string;
|
|
@@ -78,7 +78,7 @@ export interface FilterDocument {
|
|
|
78
78
|
params?: Record<string, ParamSpec>;
|
|
79
79
|
images?: string[] | Record<string, ImageSpec>;
|
|
80
80
|
graphics?: Record<string, GraphicSpec>;
|
|
81
|
-
space?:
|
|
81
|
+
space?: ColorSpace | { in?: ColorSpace; out?: ColorSpace };
|
|
82
82
|
/** A filter has either `color` or `steps`. */
|
|
83
83
|
color?: ColorExpression;
|
|
84
84
|
steps?: Step[];
|
|
@@ -104,7 +104,7 @@ export interface NormalizedFilter {
|
|
|
104
104
|
params?: Record<string, NormalizedParam>;
|
|
105
105
|
images?: Record<string, ImageSpec>;
|
|
106
106
|
graphics?: Record<string, GraphicSpec>;
|
|
107
|
-
space?: { in:
|
|
107
|
+
space?: { in: ColorSpace; out: ColorSpace };
|
|
108
108
|
color?: string | Partial<Record<'r' | 'g' | 'b' | 'a' | 'rgb' | 'rgba', string>>;
|
|
109
109
|
steps?: NormalizedStep[];
|
|
110
110
|
}
|
|
@@ -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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The built-in filters: what the editor offers, and a
|
|
2
|
+
* The built-in filters: what the editor offers, and a catalog anyone
|
|
3
3
|
* embedding the runner can show. Each is a filter document (docs/filters.md)
|
|
4
4
|
* plus a `group` for display (not part of the format; sharing drops it);
|
|
5
5
|
* `npm test` checks that every one validates, compiles and survives a link.
|
|
@@ -9,25 +9,25 @@ const FILTERS = [
|
|
|
9
9
|
{ name: 'Wave', group: 'Shape & motion', description: 'Rows slide back and forth as t advances',
|
|
10
10
|
params: { amplitude: { default: 8, min: 0, max: 40 }, wavelength: { default: 10, min: 1, max: 60 }, speed: { default: 5, min: 1, max: 30 } },
|
|
11
11
|
color: 'pixel(px + amplitude sin(py / wavelength + t / speed), py)' },
|
|
12
|
-
{ name: 'Invert', group: '
|
|
13
|
-
{ name: 'Grayscale', group: '
|
|
14
|
-
{ name: 'Threshold', group: '
|
|
15
|
-
{ name: 'Posterize', group: '
|
|
16
|
-
{ name: 'Brightness & contrast', group: '
|
|
12
|
+
{ name: 'Invert', group: 'Color & tone', color: '(1 - r, 1 - g, 1 - b)' },
|
|
13
|
+
{ name: 'Grayscale', group: 'Color & tone', color: 'gray' },
|
|
14
|
+
{ name: 'Threshold', group: 'Color & tone', params: { cutoff: 0.5 }, color: 'if gray > cutoff then 1 else 0' },
|
|
15
|
+
{ name: 'Posterize', group: 'Color & tone', params: { levels: { default: 4, min: 2, max: 16, step: 1 } }, color: 'min(floor(rgb * levels) / (levels - 1), 1)' },
|
|
16
|
+
{ name: 'Brightness & contrast', group: 'Color & tone',
|
|
17
17
|
params: { brightness: { default: 0, min: -0.5, max: 0.5 }, contrast: { default: 1, min: 0, max: 3 } },
|
|
18
18
|
color: '(rgb - 0.5) contrast + 0.5 + brightness' },
|
|
19
|
-
{ name: 'Tint', group: '
|
|
20
|
-
{ name: 'Sepia', group: '
|
|
21
|
-
{ name: '
|
|
19
|
+
{ name: 'Tint', group: 'Color & tone', params: { tint: [1, 0.85, 0.7] }, color: 'rgb * tint' },
|
|
20
|
+
{ name: 'Sepia', group: 'Color & tone', color: '(0.393r + 0.769g + 0.189b, 0.349r + 0.686g + 0.168b, 0.272r + 0.534g + 0.131b)' },
|
|
21
|
+
{ name: 'Color circle', group: 'Color & tone', description: 'Black and white, except a soft circle that keeps its color',
|
|
22
22
|
params: { across: { default: 0.5, min: 0, max: 1, label: 'where, across' }, down: { default: 0.5, min: 0, max: 1, label: 'where, down' },
|
|
23
23
|
radius: { default: 0.3, min: 0, max: 1 }, softness: { default: 0.08, min: 0, max: 0.5 } },
|
|
24
|
-
notes: ['The
|
|
24
|
+
notes: ['The color stays inside a circle and fades to gray over the softness. Sizes are parts of the picture\'s shorter side, so it stays round.',
|
|
25
25
|
{ param: 'across' }, { param: 'down' }, { param: 'radius' }, { param: 'softness' }],
|
|
26
26
|
color: 'mix((gray, gray, gray), rgb, 1 - smoothstep(radius, radius + softness, distance((px, py), (across w, down h)) / min(w, h)))' },
|
|
27
|
-
{ name: 'Vignette', group: '
|
|
27
|
+
{ name: 'Vignette', group: 'Color & tone', params: { strength: { default: 1.6, min: 0, max: 4 } }, color: 'rgb * (1 - strength ((x - 0.5)^2 + (y - 0.5)^2))' },
|
|
28
28
|
{ name: 'Sobel edges', group: 'Blur & edges', description: 'How fast the brightness changes', params: { gain: { default: 2, min: 0.2, max: 10 } }, color: 'gain sobel()' },
|
|
29
29
|
{ name: 'Edge map', group: 'Blur & edges', description: 'White where an edge is stronger than the cutoff', params: { cutoff: { default: 0.3, min: 0, max: 2 } }, color: 'if sobel() > cutoff then 1 else 0' },
|
|
30
|
-
{ name: 'Outline', group: 'Blur & edges', description: 'Ink lines over the
|
|
30
|
+
{ name: 'Outline', group: 'Blur & edges', description: 'Ink lines over the color', params: { gain: { default: 3, min: 0, max: 10 } }, color: 'rgb (1 - gain sobel())' },
|
|
31
31
|
{ name: 'Box blur', group: 'Blur & edges', params: { radius: { default: 2, min: 0, max: 8, step: 1 } }, color: 'blur(radius)' },
|
|
32
32
|
{ name: 'Gaussian blur', group: 'Blur & edges', description: 'Computed in two passes, across then down, so it can be wide', params: { sigma: { default: 3, min: 0, max: 8 } }, color: 'gaussian(sigma)' },
|
|
33
33
|
{ name: 'Canny edges', group: 'Blur & edges', description: 'Thin, clean edge lines: smooth, gradient, thinning, two thresholds, hysteresis — a chain',
|
|
@@ -35,7 +35,7 @@ const FILTERS = [
|
|
|
35
35
|
{ params: { sigma: { default: 1.4, min: 0.5, max: 4 } }, color: 'gaussian(sigma)' },
|
|
36
36
|
// gradient strength (r) and its direction as one of four buckets (g: 0, ¼, ½, ¾ for 0°, 45°, 90°, 135°)
|
|
37
37
|
{ color: '(min(sobel() / 4, 1), floor(fract((atan2(sobely(), sobelx()) + π/8) / π) 4) / 4, 0)' },
|
|
38
|
-
// non-maximum suppression: keep a pixel only if it is at least as strong as its two
|
|
38
|
+
// non-maximum suppression: keep a pixel only if it is at least as strong as its two neighbors along the gradient
|
|
39
39
|
{ color: 'if green(px, py) < 0.125 then (if r >= red(px - 1, py) & r >= red(px + 1, py) then r else 0)'
|
|
40
40
|
+ ' else if green(px, py) < 0.375 then (if r >= red(px + 1, py + 1) & r >= red(px - 1, py - 1) then r else 0)'
|
|
41
41
|
+ ' else if green(px, py) < 0.625 then (if r >= red(px, py - 1) & r >= red(px, py + 1) then r else 0)'
|
|
@@ -62,7 +62,7 @@ const FILTERS = [
|
|
|
62
62
|
{ name: 'Scanlines', group: 'Shape & motion', params: { period: { default: 4, min: 2, max: 16, step: 1 } }, color: 'if py mod period < period / 2 then rgb else rgb * 0.6' },
|
|
63
63
|
{ name: 'Pulse', group: 'Shape & motion', description: 'Brightness breathes with t', params: { speed: { default: 6, min: 1, max: 30 }, depth: { default: 0.25, min: 0, max: 1 } },
|
|
64
64
|
color: 'rgb (1 - depth + depth sin(t / speed))' },
|
|
65
|
-
{ name: 'Chroma key', group: '
|
|
65
|
+
{ name: 'Chroma key', group: 'Color & tone', description: 'Replaces one color with another',
|
|
66
66
|
params: { key: [0.22, 0.71, 0.29], tolerance: { default: 0.35, min: 0, max: 1 }, background: [0.1, 0.1, 0.4] },
|
|
67
67
|
color: 'if distance(rgb, key) < tolerance then background else rgb' },
|
|
68
68
|
{ name: 'Through a mask', group: 'Other pictures', description: 'Put this last: the steps above show only where the mask is light',
|
|
@@ -79,33 +79,39 @@ 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',
|
|
85
91
|
graphics: { title: { text: 'BIG TITLE', font: 'Impact, Haettenschweiler, sans-serif', weight: 'normal', size: 0.16, y: 0.14, outline: { color: '#000000', width: 0.08 }, shadow: null } },
|
|
86
92
|
color: 'mix(rgb, title, alpha(title))' },
|
|
87
|
-
{ name: 'Gradient text', group: 'Text & shapes', description: 'The letters
|
|
93
|
+
{ name: 'Gradient text', group: 'Text & shapes', description: 'The letters colored by math: a sunset from left to right',
|
|
88
94
|
graphics: { title: { text: 'Golden hour', font: 'Georgia, serif', size: 0.13, y: 0.5, shadow: { color: 'rgba(0,0,0,0.5)', blur: 0.2, dx: 0, dy: 0.05 } } },
|
|
89
95
|
color: 'mix(rgb, mix((1, 0.35, 0.3), (1, 0.85, 0.25), x), alpha(title))' },
|
|
90
|
-
{ name: 'Neon sign', group: 'Text & shapes', description: 'White letters with a
|
|
96
|
+
{ name: 'Neon sign', group: 'Text & shapes', description: 'White letters with a colored glow',
|
|
91
97
|
graphics: { title: { text: 'OPEN', font: 'Trebuchet MS, sans-serif', size: 0.18, y: 0.5, shadow: { color: '#00e5ff', blur: 0.45, dx: 0, dy: 0 } } },
|
|
92
98
|
color: 'mix(rgb * 0.6, title, alpha(title))' },
|
|
93
|
-
{ name: 'Layered shadow', group: 'Text & shapes', description: 'Two shadow layers, each with its own filter: a soft glow that darkens and breathes with t, and a hard offset
|
|
99
|
+
{ name: 'Layered shadow', group: 'Text & shapes', description: 'Two shadow layers, each with its own filter: a soft glow that darkens and breathes with t, and a hard offset colored by a gradient',
|
|
94
100
|
graphics: { title: { text: 'LAYERS', font: 'Impact, Haettenschweiler, sans-serif', weight: 'normal', size: 0.17, y: 0.5, shadow: null,
|
|
95
101
|
shadows: [{ color: '#000000', blur: 0.7, dx: 0, dy: 0.08 }, { color: '#000000', blur: 0, dx: 0.07, dy: 0.07 }] } },
|
|
96
102
|
color: 'mix(mix(mix(rgb, rgb * 0.25, alpha(title_shadow1) (0.7 + 0.3 sin(t / 10))), mix((1, 0.2, 0.6), (0.2, 0.6, 1), x), alpha(title_shadow2)), title, alpha(title))' },
|
|
97
|
-
{ name: 'Ink brush', group: 'Brushes', description: 'Paint in a
|
|
103
|
+
{ name: 'Ink brush', group: 'Brushes', description: 'Paint in a color; draw on the picture with "draw" under the step',
|
|
98
104
|
graphics: { brush: { type: 'strokes', brush: 'round', size: 0.025 } }, params: { ink: [0.95, 0.2, 0.25] }, color: 'mix(rgb, ink, alpha(brush))' },
|
|
99
105
|
{ name: 'Blur brush', group: 'Brushes', description: 'Paint a blur in — stronger every time a stroke goes over the same place',
|
|
100
106
|
graphics: { brush: { type: 'strokes', brush: 'soft', size: 0.08 } }, color: 'mix(rgb, gaussian(2 + 3 min(4 green(brush), 3)), alpha(brush))' },
|
|
101
107
|
{ name: 'Darken brush', group: 'Brushes', description: 'Each pass darkens a little more, like layers of a wash',
|
|
102
108
|
graphics: { brush: { type: 'strokes', brush: 'soft', size: 0.07, flow: 0.7 } }, color: 'rgb (1 - 0.22 min(4 green(brush), 4))' },
|
|
103
|
-
{ name: '
|
|
109
|
+
{ name: 'Color reveal brush', group: 'Brushes', description: 'The picture starts gray; the brush paints its color back',
|
|
104
110
|
graphics: { brush: { type: 'strokes', brush: 'soft', size: 0.1 } }, color: 'mix((gray, gray, gray), rgb, alpha(brush))' },
|
|
105
111
|
{ name: 'Fuse brush', group: 'Brushes', description: 'Paint glows as it goes down, then cools to charcoal (red(brush) is its age)',
|
|
106
112
|
graphics: { brush: { type: 'strokes', brush: 'round', size: 0.02, age: 40 } },
|
|
107
113
|
color: 'mix(rgb, mix((1, 0.9, 0.5), (0.1, 0.07, 0.06), red(brush)), alpha(brush)) + (1, 0.5, 0.1) alpha(brush) max(0, 1 - 3 red(brush)) 0.6' },
|
|
108
|
-
{ name: 'Rainbow brush', group: 'Brushes', description: 'The
|
|
114
|
+
{ name: 'Rainbow brush', group: 'Brushes', description: 'The color runs round the wheel along the drawing (blue(brush) is how far through it)',
|
|
109
115
|
graphics: { brush: { type: 'strokes', brush: 'marker', size: 0.035 } }, color: 'mix(rgb, fromhsv(blue(brush), 0.85, 1), alpha(brush))' },
|
|
110
116
|
{ name: 'Chalk outline', group: 'Brushes', description: 'Chalk round an object: select it, then "outline the selection" under the step',
|
|
111
117
|
graphics: { brush: { type: 'strokes', brush: 'chalk', size: 0.018, timing: 'even', speed: 2 } }, color: 'mix(rgb, (0.97, 0.97, 0.92), alpha(brush))' },
|
|
@@ -134,17 +140,17 @@ const FILTERS = [
|
|
|
134
140
|
images: { inset: { description: 'any size: it is fitted whole', place: 'fit', zoom: 0.35, x: 0.8, y: 0.78 } }, color: 'mix(rgb, inset, alpha(inset))' },
|
|
135
141
|
{ name: 'Moving overlay', group: 'Other pictures', description: 'Another picture scrolling across at half strength', images: ['overlay'], params: { speed: { default: 2, min: -10, max: 10 } },
|
|
136
142
|
color: 'mix(rgb, shift(overlay, speed t, 0), 0.5)' },
|
|
137
|
-
{ name: 'Hue shift', group: '
|
|
143
|
+
{ name: 'Hue shift', group: 'Color & tone', description: 'Turns every color around the hue wheel (in HSV)', space: 'hsv',
|
|
138
144
|
params: { amount: { default: 0.1, min: -0.5, max: 0.5 } }, color: '(fract(r + amount), g, b)' },
|
|
139
|
-
{ name: 'Saturation', group: '
|
|
145
|
+
{ name: 'Saturation', group: 'Color & tone', description: 'More or less color (in HSV)', space: 'hsv',
|
|
140
146
|
params: { amount: { default: 1.5, min: 0, max: 3 } }, color: '(r, clamp(g amount, 0, 1), b)' },
|
|
141
|
-
{ name: 'Lightness', group: '
|
|
147
|
+
{ name: 'Lightness', group: 'Color & tone', description: 'Lighter or darker without changing the colors (in Lab)', space: 'lab',
|
|
142
148
|
params: { amount: { default: 1.15, min: 0.5, max: 2 } }, color: '(clamp(r amount, 0, 1), g, b)' },
|
|
143
|
-
{ name: 'Vivid', group: '
|
|
149
|
+
{ name: 'Vivid', group: 'Color & tone', description: 'Colorfulness, perceptually even (in OKLab)', space: 'oklab',
|
|
144
150
|
params: { amount: { default: 1.6, min: 0, max: 3 } }, color: '(r, 0.5 + (g - 0.5) amount, 0.5 + (b - 0.5) amount)' },
|
|
145
151
|
{ name: 'Linear-light blur', group: 'Blur & edges', description: 'A Gaussian in linear light: bright spots stay bright, as a lens does', space: 'linear',
|
|
146
152
|
params: { sigma: { default: 3, min: 0, max: 8 } }, color: 'gaussian(sigma)' },
|
|
147
|
-
{ name: 'Film grain', group: '
|
|
153
|
+
{ name: 'Film grain', group: 'Color & tone', description: 'Noise that changes every frame', params: { amount: { default: 0.15, min: 0, max: 0.6 } }, color: 'rgb + amount (random() - 0.5)' },
|
|
148
154
|
{ name: 'Swirl', group: 'Shape & motion', params: { strength: { default: 0.02, min: 0, max: 0.1 } },
|
|
149
155
|
color: 'pixel(w/2 + (px - w/2) cos(strength t gray) - (py - h/2) sin(strength t gray), h/2 + (px - w/2) sin(strength t gray) + (py - h/2) cos(strength t gray))' },
|
|
150
156
|
];
|
|
@@ -152,10 +158,10 @@ const byName = (name) => FILTERS.find((f) => f.name === name);
|
|
|
152
158
|
FILTERS.push(
|
|
153
159
|
{ name: 'Old film', group: 'Chains', description: 'Sepia, a vignette and faint scanlines — a chain of three',
|
|
154
160
|
steps: [byName('Sepia'), { filter: byName('Vignette'), with: { strength: 1.2 } }, { filter: byName('Scanlines'), opacity: 0.5 }] },
|
|
155
|
-
{ name: 'Comic', group: 'Chains', description: 'Bold flat
|
|
161
|
+
{ name: 'Comic', group: 'Chains', description: 'Bold flat colors with ink lines: a small blur first, so a photo\'s fine texture isn\'t inked — a chain of four',
|
|
156
162
|
steps: [{ filter: byName('Gaussian blur'), with: { sigma: 1.6 } }, { filter: byName('Vivid'), with: { amount: 1.5 } },
|
|
157
163
|
{ filter: byName('Posterize'), with: { levels: 4 } }, { filter: byName('Outline'), with: { gain: 1.2 } }] },
|
|
158
|
-
{ name: 'Cartoon', group: 'Chains', description: 'Blur, then flatten the
|
|
164
|
+
{ name: 'Cartoon', group: 'Chains', description: 'Blur, then flatten the colors, then outline — a chain of three',
|
|
159
165
|
steps: [{ filter: byName('Gaussian blur'), with: { sigma: 1.5 } }, { filter: byName('Posterize'), with: { levels: 6 } }, { filter: byName('Outline'), with: { gain: 2 } }] },
|
|
160
166
|
{ name: 'Dreamy', group: 'Chains', description: 'A soft glow mixed over the picture',
|
|
161
167
|
steps: [{ filter: byName('Bloom'), with: { amount: 1.2 } }, { filter: byName('Gaussian blur'), with: { sigma: 2 }, opacity: 0.4 }] },
|
|
@@ -163,11 +169,11 @@ FILTERS.push(
|
|
|
163
169
|
steps: [{ filter: byName('Gaussian blur'), with: { sigma: 4 } }, byName('Through a mask')] },
|
|
164
170
|
{ name: 'Title card', group: 'Chains', description: 'A frosted panel with a caption on it — a chain of two',
|
|
165
171
|
steps: [byName('Frosted panel'), { filter: byName('Caption'), with: { title: { text: 'A title on a panel', y: 0.86 } } }] },
|
|
166
|
-
{ name: '
|
|
172
|
+
{ name: 'Color spotlight', group: 'Chains', description: 'Gray except where the mask is dark — a chain',
|
|
167
173
|
steps: [byName('Grayscale'), byName('Through a mask')] },
|
|
168
174
|
);
|
|
169
175
|
|
|
170
176
|
export const LIBRARY = FILTERS;
|
|
171
177
|
|
|
172
178
|
/** The groups the editor shows the library in, in this order; every filter names one. */
|
|
173
|
-
export const GROUPS = ['
|
|
179
|
+
export const GROUPS = ['Color & tone', 'Blur & edges', 'Shape & motion', 'Text & shapes', 'Brushes', 'Other pictures', 'Chains'];
|
package/src/png.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A PNG writer that can do what the browser's cannot: an indexed picture
|
|
3
|
-
* (PNG-8) at 1, 2, 4 or 8 bits a pixel when the picture has few
|
|
3
|
+
* (PNG-8) at 1, 2, 4 or 8 bits a pixel when the picture has few colors —
|
|
4
4
|
* a threshold or posterize result is 5–20× smaller that way — and RGB
|
|
5
|
-
* rather than RGBA when nothing is transparent.
|
|
5
|
+
* rather than RGBA when nothing is transparent. Truecolor rows use the
|
|
6
6
|
* Paeth filter, which deflate likes for photos.
|
|
7
7
|
*
|
|
8
|
-
* const { blob, kind,
|
|
8
|
+
* const { blob, kind, colors, bits } = await encodePng({ width, height, data, palette: 'auto', colors: 256, dither: true });
|
|
9
9
|
*
|
|
10
|
-
* `palette`: 'auto' (indexed if the picture has ≤ 256
|
|
11
|
-
*
|
|
12
|
-
* optional dithering — that needs a `quantize(data,
|
|
10
|
+
* `palette`: 'auto' (indexed if the picture has ≤ 256 colors, else
|
|
11
|
+
* truecolor), 'none', or 'force' (reduce to `colors` colors, with
|
|
12
|
+
* optional dithering — that needs a `quantize(data, colors, { dither, width })`
|
|
13
13
|
* returning { palette, indices }: the package has no quantizer of its own,
|
|
14
14
|
* so it stays dependency-free; the editor passes one built on gifenc).
|
|
15
15
|
* Deflate comes from CompressionStream (every current browser, and Node
|
|
@@ -48,8 +48,8 @@ async function deflate(bytes) {
|
|
|
48
48
|
return new Uint8Array(await new Response(stream.readable).arrayBuffer());
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
/** How many distinct RGBA
|
|
52
|
-
export function
|
|
51
|
+
/** How many distinct RGBA colors, counting up to `limit`. */
|
|
52
|
+
export function colorCount(data, limit = 257) {
|
|
53
53
|
const seen = new Set();
|
|
54
54
|
for (let i = 0; i < data.length; i += 4) {
|
|
55
55
|
seen.add(((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]) >>> 0);
|
|
@@ -58,7 +58,7 @@ export function colourCount(data, limit = 257) {
|
|
|
58
58
|
return seen.size;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
/** Every
|
|
61
|
+
/** Every color as it is, when there are at most 256: { palette: [[r, g, b, a]…], indices }. */
|
|
62
62
|
export function exactPalette(data) {
|
|
63
63
|
const index = new Map();
|
|
64
64
|
const palette = [];
|
|
@@ -83,10 +83,10 @@ function paeth(a, b, c) {
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
|
-
* @param {object} o { width, height, data: RGBA bytes, palette: 'auto' | 'none' | 'force',
|
|
87
|
-
* @returns {Promise<{ blob: Blob, kind: 'palette' | 'rgb' | 'rgba',
|
|
86
|
+
* @param {object} o { width, height, data: RGBA bytes, palette: 'auto' | 'none' | 'force', colors, dither, quantize }
|
|
87
|
+
* @returns {Promise<{ blob: Blob, kind: 'palette' | 'rgb' | 'rgba', colors: number, bits: number } | null>}
|
|
88
88
|
*/
|
|
89
|
-
export async function encodePng({ width, height, data, palette = 'auto',
|
|
89
|
+
export async function encodePng({ width, height, data, palette = 'auto', colors = 256, dither = true, quantize = null }) {
|
|
90
90
|
if (typeof CompressionStream === 'undefined') return null;
|
|
91
91
|
if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || !data || data.length < width * height * 4) {
|
|
92
92
|
throw new Error('encodePng: width and height are whole numbers of pixels, and data holds 4 bytes for each');
|
|
@@ -94,10 +94,10 @@ export async function encodePng({ width, height, data, palette = 'auto', colours
|
|
|
94
94
|
let indexed = null;
|
|
95
95
|
if (palette === 'auto' || palette === 'force') {
|
|
96
96
|
indexed = exactPalette(data);
|
|
97
|
-
if (indexed && indexed.palette.length >
|
|
97
|
+
if (indexed && indexed.palette.length > colors) indexed = null;
|
|
98
98
|
if (!indexed && palette === 'force') {
|
|
99
|
-
if (!quantize) throw new Error('encodePng: palette "force" needs a quantize function (the picture has more than `
|
|
100
|
-
indexed = quantize(data, Math.min(256, Math.max(2,
|
|
99
|
+
if (!quantize) throw new Error('encodePng: palette "force" needs a quantize function (the picture has more than `colors` colors)');
|
|
100
|
+
indexed = quantize(data, Math.min(256, Math.max(2, colors)), { dither, width });
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
const chunks = [new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10])];
|
|
@@ -110,7 +110,7 @@ export async function encodePng({ width, height, data, palette = 'auto', colours
|
|
|
110
110
|
const n = indexed.palette.length;
|
|
111
111
|
bits = n <= 2 ? 1 : n <= 4 ? 2 : n <= 16 ? 4 : 8;
|
|
112
112
|
kind = 'palette';
|
|
113
|
-
ihdr[8] = bits; ihdr[9] = 3; //
|
|
113
|
+
ihdr[8] = bits; ihdr[9] = 3; // color type 3: indexed
|
|
114
114
|
const stride = Math.ceil(width * bits / 8);
|
|
115
115
|
rows = new Uint8Array(height * (1 + stride));
|
|
116
116
|
for (let y = 0; y < height; y++) {
|
|
@@ -153,5 +153,6 @@ export async function encodePng({ width, height, data, palette = 'auto', colours
|
|
|
153
153
|
chunks.push(chunk('IHDR', ihdr));
|
|
154
154
|
}
|
|
155
155
|
chunks.push(chunk('IDAT', await deflate(rows)), chunk('IEND', new Uint8Array(0)));
|
|
156
|
-
|
|
156
|
+
const count = indexed ? indexed.palette.length : colorCount(data);
|
|
157
|
+
return { blob: new Blob(chunks, { type: 'image/png' }), kind, colors: count, bits };
|
|
157
158
|
}
|
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) {
|
package/src/save.js
CHANGED
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
* const gif = await encodeGif(frames, { gifenc, delay: 40 }); // bring your own gifenc
|
|
8
8
|
*
|
|
9
9
|
* PNG is written by the package itself (src/png.js: indexed when the picture
|
|
10
|
-
* has at most 256
|
|
10
|
+
* has at most 256 colors, RGB when nothing is transparent), so it works
|
|
11
11
|
* anywhere CompressionStream does — every current browser and Node ≥ 18.
|
|
12
|
-
* JPEG and WebP need the browser's encoder (a canvas). GIF needs a
|
|
12
|
+
* JPEG and WebP need the browser's encoder (a canvas). GIF needs a color
|
|
13
13
|
* quantizer and an LZW encoder; the package has no dependencies, so
|
|
14
14
|
* encodeGif() takes gifenc (npm i gifenc) as an option rather than bundling it.
|
|
15
15
|
*/
|
|
@@ -52,7 +52,7 @@ function canvasOf(picture) {
|
|
|
52
52
|
* @param {HTMLCanvasElement|OffscreenCanvas|{ data, width, height }} what
|
|
53
53
|
* @param {{ type?: string, quality?: number, palette?: 'auto' | 'none' }} [options]
|
|
54
54
|
* type: 'image/png' (the default), 'image/jpeg' or 'image/webp' (browser only); quality 0–1 for the last two.
|
|
55
|
-
* palette (PNG): 'auto' writes an indexed PNG when there are at most 256
|
|
55
|
+
* palette (PNG): 'auto' writes an indexed PNG when there are at most 256 colors, 'none' never does.
|
|
56
56
|
* @returns {Promise<Blob>}
|
|
57
57
|
*/
|
|
58
58
|
export async function toBlob(what, { type = 'image/png', quality, palette = 'auto' } = {}) {
|
|
@@ -69,12 +69,12 @@ export async function toBlob(what, { type = 'image/png', quality, palette = 'aut
|
|
|
69
69
|
/**
|
|
70
70
|
* An animated GIF from frames (canvases or pictures, all one size), each `delay` ms.
|
|
71
71
|
* Needs gifenc: `import * as gifenc from 'gifenc'` and pass it as `{ gifenc }`.
|
|
72
|
-
* Each frame gets its own palette of up to `
|
|
72
|
+
* Each frame gets its own palette of up to `colors` colors.
|
|
73
73
|
* @param {Iterable|AsyncIterable} frames
|
|
74
|
-
* @param {{ gifenc: object, delay?: number, repeat?: number,
|
|
74
|
+
* @param {{ gifenc: object, delay?: number, repeat?: number, colors?: number }} options
|
|
75
75
|
* @returns {Promise<Blob>}
|
|
76
76
|
*/
|
|
77
|
-
export async function encodeGif(frames, { gifenc, delay = 1000 / 30, repeat = 0,
|
|
77
|
+
export async function encodeGif(frames, { gifenc, delay = 1000 / 30, repeat = 0, colors = 256 } = {}) {
|
|
78
78
|
const G = gifenc && (gifenc.GIFEncoder ? gifenc : gifenc.default);
|
|
79
79
|
if (!G || typeof G.GIFEncoder !== 'function') throw new Error('encodeGif needs gifenc: import * as gifenc from \'gifenc\' and pass { gifenc }');
|
|
80
80
|
const encoder = G.GIFEncoder();
|
|
@@ -87,7 +87,7 @@ export async function encodeGif(frames, { gifenc, delay = 1000 / 30, repeat = 0,
|
|
|
87
87
|
size = [width, height];
|
|
88
88
|
const transparent = hasTransparency(data);
|
|
89
89
|
const format = transparent ? 'rgba4444' : 'rgb565';
|
|
90
|
-
const palette = G.quantize(data, Math.max(2, Math.min(256,
|
|
90
|
+
const palette = G.quantize(data, Math.max(2, Math.min(256, colors)), { format, oneBitAlpha: transparent });
|
|
91
91
|
const index = G.applyPalette(data, palette, format);
|
|
92
92
|
const options = { palette, delay: Math.round(delay), repeat };
|
|
93
93
|
if (transparent) {
|
package/src/strokes.js
CHANGED
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
*
|
|
4
4
|
* A strokes graphic records each stroke's points as the user drew them —
|
|
5
5
|
* [x, y, ms, pressure], x and y as fractions of the frame — and a brush.
|
|
6
|
-
* For frame t it becomes a picture whose channels are data, not
|
|
6
|
+
* For frame t it becomes a picture whose channels are data, not color:
|
|
7
7
|
*
|
|
8
8
|
* alpha coverage: how much paint is here so far, 0–1
|
|
9
9
|
* red age: how long ago this pixel was last painted, 0 (just now) → 1 (`age` frames or more)
|
|
10
10
|
* green passes ÷ 4: how many strokes have gone over it (soft brushes count partly)
|
|
11
11
|
* blue progress: how far through the whole drawing it was painted, 0 → 1
|
|
12
12
|
*
|
|
13
|
-
* so the
|
|
13
|
+
* so the math decides what the paint does:
|
|
14
14
|
*
|
|
15
15
|
* mix(rgb, ink, alpha(brush)) ink
|
|
16
16
|
* mix(rgb, gaussian(2 + 3 min(4 green(brush), 3)), alpha(brush)) a blur that grows with every pass
|
|
@@ -265,8 +265,8 @@ export function rasterizeStrokes(spec, w, h, t = Infinity, { mask = null } = {})
|
|
|
265
265
|
|
|
266
266
|
/**
|
|
267
267
|
* The outlines of a mask (values > 0.5 are in), each a closed polyline of pixel
|
|
268
|
-
*
|
|
269
|
-
* Moore-
|
|
268
|
+
* centers, simplified; parts smaller than `minArea` pixels are left out.
|
|
269
|
+
* Moore-neighbor tracing, one outline per connected part.
|
|
270
270
|
*/
|
|
271
271
|
export function traceContours(mask, w, h, { minArea = 40, tolerance = 0.8 } = {}) {
|
|
272
272
|
const inside = (x, y) => x >= 0 && y >= 0 && x < w && y < h && mask[y * w + x] > 0.5;
|