@mathzetta/qblur 0.0.0-stage → 0.1.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 +29 -0
- package/LICENSE +21 -0
- package/README.md +143 -2
- package/dist/cpu-worker.js +2 -0
- package/dist/qblur-apply.js +2 -0
- package/dist/qblur-element.js +2 -0
- package/dist/qblur.core.js +2 -0
- package/dist/qblur.external.global.min.js +2 -0
- package/dist/qblur.external.js +2 -0
- package/dist/qblur.global.min.js +2 -0
- package/dist/qblur.js +2 -0
- package/docs/filters.md +841 -0
- package/docs/library.md +604 -0
- package/package.json +112 -4
- package/src/apply-filter.js +245 -0
- package/src/apply.d.ts +64 -0
- package/src/apply.js +14 -0
- package/src/auto.js +41 -0
- package/src/cpu-helpers.js +416 -0
- package/src/cpu-runner.js +268 -0
- package/src/cpu-worker.js +58 -0
- package/src/cpu.js +581 -0
- package/src/element.d.ts +16 -0
- package/src/element.js +142 -0
- package/src/format.js +605 -0
- package/src/graphics.js +305 -0
- package/src/index.d.ts +283 -0
- package/src/index.js +13 -0
- package/src/library.js +173 -0
- package/src/png.js +154 -0
- package/src/runtime.js +229 -0
- package/src/save.js +108 -0
- package/src/strokes.js +342 -0
package/src/element.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <qblur-image>: a picture with a filter on it, as an HTML element.
|
|
3
|
+
*
|
|
4
|
+
* <script type="module" src="https://cdn.jsdelivr.net/npm/@mathzetta/qblur@0.1/dist/qblur-element.js"></script>
|
|
5
|
+
* <qblur-image src="photo.jpg" filter="Sepia" alt="The harbour at dusk"></qblur-image>
|
|
6
|
+
* <qblur-image src="photo.jpg" filter="if gray > cutoff then 1 else 0" params='{"cutoff": 0.4}'></qblur-image>
|
|
7
|
+
*
|
|
8
|
+
* Attributes (each change redraws):
|
|
9
|
+
* src the picture's address (from another site it must allow CORS; add crossorigin)
|
|
10
|
+
* filter maths, a built-in filter's name, filter JSON or a share link
|
|
11
|
+
* params JSON of knob values, { "name": value } — or set the `params` property to an object
|
|
12
|
+
* (whichever was set last counts)
|
|
13
|
+
* t the frame, for filters that move
|
|
14
|
+
* animate present: t counts up while the element is in the page and in view (30 a second)
|
|
15
|
+
* engine auto (default) | gpu | cpu
|
|
16
|
+
* alt what the picture shows, for screen readers
|
|
17
|
+
* crossorigin as on <img>
|
|
18
|
+
*
|
|
19
|
+
* The element draws on a canvas in its shadow root, the picture's size, scaled
|
|
20
|
+
* down to fit (max-width: 100%). It fires `load` after each drawing and
|
|
21
|
+
* `error` (event.detail: the Error) when the picture or the filter fails.
|
|
22
|
+
* `element.canvas` is the drawing (for toBlob), `element.filter` / `params`
|
|
23
|
+
* read and write the settings. Importing this module defines the element
|
|
24
|
+
* (once); `QBlurImage` is the class.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { applyFilter } from './apply-filter.js';
|
|
28
|
+
|
|
29
|
+
const Base = typeof HTMLElement !== 'undefined' ? HTMLElement : class {};
|
|
30
|
+
const FPS = 30;
|
|
31
|
+
|
|
32
|
+
export class QBlurImage extends Base {
|
|
33
|
+
static get observedAttributes() { return ['src', 'filter', 'params', 't', 'engine', 'alt', 'crossorigin', 'animate']; }
|
|
34
|
+
|
|
35
|
+
constructor() {
|
|
36
|
+
super();
|
|
37
|
+
const root = this.attachShadow({ mode: 'open' });
|
|
38
|
+
root.innerHTML = '<style>:host{display:inline-block;max-width:100%}canvas{display:block;max-width:100%;height:auto}</style>';
|
|
39
|
+
this.canvas = document.createElement('canvas');
|
|
40
|
+
this.canvas.setAttribute('role', 'img');
|
|
41
|
+
this.canvas.width = 0;
|
|
42
|
+
this.canvas.height = 0;
|
|
43
|
+
root.append(this.canvas);
|
|
44
|
+
this._params = null; // set by the property; otherwise the attribute
|
|
45
|
+
this._image = null; // the loaded picture, and its src
|
|
46
|
+
this._loading = null;
|
|
47
|
+
this._queued = false;
|
|
48
|
+
this._drawn = null;
|
|
49
|
+
this._frame = 0;
|
|
50
|
+
this._timer = null;
|
|
51
|
+
this._loop = 0; // which animation loop is the current one
|
|
52
|
+
this._visible = true;
|
|
53
|
+
this._watch = typeof IntersectionObserver !== 'undefined'
|
|
54
|
+
? new IntersectionObserver((entries) => { this._visible = entries[entries.length - 1].isIntersecting; }) : null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get filter() { return this.getAttribute('filter') ?? ''; }
|
|
58
|
+
set filter(value) { this.setAttribute('filter', value); }
|
|
59
|
+
|
|
60
|
+
get params() {
|
|
61
|
+
if (this._params) return { ...this._params };
|
|
62
|
+
const text = this.getAttribute('params');
|
|
63
|
+
if (!text) return {};
|
|
64
|
+
try { return JSON.parse(text); } catch { throw new Error(`params is not JSON: ${text}`); }
|
|
65
|
+
}
|
|
66
|
+
set params(value) { this._params = value ? { ...value } : null; this.redraw(); }
|
|
67
|
+
|
|
68
|
+
connectedCallback() {
|
|
69
|
+
// a property set before the element was defined (el.params = …) would hide the accessor: set it again through it
|
|
70
|
+
for (const name of ['filter', 'params']) {
|
|
71
|
+
if (Object.prototype.hasOwnProperty.call(this, name)) { const value = this[name]; delete this[name]; this[name] = value; }
|
|
72
|
+
}
|
|
73
|
+
this._watch?.observe(this);
|
|
74
|
+
this.redraw();
|
|
75
|
+
this._animate();
|
|
76
|
+
}
|
|
77
|
+
disconnectedCallback() { this._stop(); this._watch?.disconnect(); this._visible = true; }
|
|
78
|
+
|
|
79
|
+
attributeChangedCallback(name, before, after) {
|
|
80
|
+
if (before === after) return;
|
|
81
|
+
if (name === 'alt') { this.canvas.setAttribute('aria-label', after ?? ''); return; }
|
|
82
|
+
if (name === 'src' || name === 'crossorigin') { this._image = null; this._loading = null; }
|
|
83
|
+
if (name === 'params') this._params = null; // the attribute was written last: it counts
|
|
84
|
+
if (name === 'animate') { this._stop(); this._animate(); }
|
|
85
|
+
this.redraw();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Draws again (soon, once, however many settings changed). Resolves when drawn. */
|
|
89
|
+
redraw() {
|
|
90
|
+
if (!this.isConnected) return Promise.resolve();
|
|
91
|
+
if (this._queued) return this._drawn; // a drawing is already waiting: it will see this change
|
|
92
|
+
this._queued = true;
|
|
93
|
+
this._drawn = (this._drawn ?? Promise.resolve()).then(() => { this._queued = false; return this._draw(); }); // one at a time, in order
|
|
94
|
+
return this._drawn;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async _draw() {
|
|
98
|
+
const src = this.getAttribute('src');
|
|
99
|
+
const filter = this.filter.trim();
|
|
100
|
+
if (!src || !filter) return;
|
|
101
|
+
try {
|
|
102
|
+
const image = await this._load(src);
|
|
103
|
+
if (this.getAttribute('src') !== src) return; // changed while loading
|
|
104
|
+
const t = this.hasAttribute('animate') ? this._frame : Number(this.getAttribute('t') ?? 0) || 0;
|
|
105
|
+
await applyFilter(image, filter, { params: this.params, t, engine: this.getAttribute('engine') || 'auto', canvas: this.canvas });
|
|
106
|
+
this.dispatchEvent(new Event('load'));
|
|
107
|
+
this._animate(); // back on, if an error had stopped it
|
|
108
|
+
} catch (error) {
|
|
109
|
+
this.dispatchEvent(new CustomEvent('error', { detail: error }));
|
|
110
|
+
if (!this.hasAttribute('animate')) console.warn('<qblur-image>:', error.message);
|
|
111
|
+
this._stop();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
_load(src) {
|
|
116
|
+
if (this._image?.src === src) return Promise.resolve(this._image.image);
|
|
117
|
+
if (this._loading?.src === src) return this._loading.promise;
|
|
118
|
+
const img = new Image();
|
|
119
|
+
const cors = this.getAttribute('crossorigin');
|
|
120
|
+
if (cors !== null) img.crossOrigin = cors || 'anonymous';
|
|
121
|
+
img.src = src;
|
|
122
|
+
const loading = { src };
|
|
123
|
+
loading.promise = img.decode().then(() => { if (this._loading === loading) this._image = { src, image: img }; return img; },
|
|
124
|
+
() => { if (this._loading === loading) this._loading = null; throw new Error(`Could not load the picture ${src}`); }); // the next drawing asks again
|
|
125
|
+
this._loading = loading;
|
|
126
|
+
return loading.promise;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
_animate() {
|
|
130
|
+
if (!this.hasAttribute('animate') || !this.isConnected || this._timer) return;
|
|
131
|
+
const loop = ++this._loop; // a loop that was stopped while drawing must not go on
|
|
132
|
+
const tick = async () => {
|
|
133
|
+
if (this._visible) { this._frame++; await this.redraw(); } // out of view: wait, don't draw
|
|
134
|
+
if (loop === this._loop && this._timer) this._timer = setTimeout(tick, 1000 / FPS);
|
|
135
|
+
};
|
|
136
|
+
this._timer = setTimeout(tick, 1000 / FPS);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
_stop() { clearTimeout(this._timer); this._timer = null; this._loop++; }
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (typeof customElements !== 'undefined' && !customElements.get('qblur-image')) customElements.define('qblur-image', QBlurImage);
|