next-blurhash-previews 0.0.3-beta33 → 0.0.3-beta36
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.
@@ -49,7 +49,7 @@ class ImageWithPreview extends HTMLElement {
|
|
49
49
|
}
|
50
50
|
|
51
51
|
#imgLoad = () => {
|
52
|
-
this.sd.innerHTML = `<slot name="image"></slot>`;
|
52
|
+
//this.sd.innerHTML = `<slot name="image"></slot>`;
|
53
53
|
};
|
54
54
|
|
55
55
|
attributeChangedCallback(name) {
|
@@ -70,14 +70,17 @@ if (!customElements.get("blurhash-image")) {
|
|
70
70
|
|
71
71
|
function updateBlurHashPreview(canvasEl: HTMLCanvasElement, preview: blurhash) {
|
72
72
|
const { w: width, h: height, blurhash } = preview;
|
73
|
+
canvasEl.width = width;
|
74
|
+
canvasEl.height = height;
|
73
75
|
|
74
76
|
const worker = new Worker("/canvas-worker.js");
|
75
77
|
|
76
78
|
const offscreen = (canvasEl as any).transferControlToOffscreen();
|
77
|
-
worker.postMessage({ canvas: offscreen, width, height, blurhash }
|
78
|
-
|
79
|
-
|
79
|
+
worker.postMessage({ canvas: offscreen, width, height, blurhash }, [
|
80
|
+
offscreen,
|
81
|
+
]);
|
80
82
|
|
83
|
+
return;
|
81
84
|
const pixels = decode(blurhash, width, height);
|
82
85
|
const ctx = canvasEl.getContext("2d")!;
|
83
86
|
const imageData = ctx.createImageData(width, height);
|
package/imagePreviewBootstrap.js
CHANGED
@@ -1,5 +1,77 @@
|
|
1
1
|
import { createElement } from "react";
|
2
2
|
|
3
3
|
export const imagePreviewBootstrap = createElement("script", {
|
4
|
-
dangerouslySetInnerHTML: { __html: `(() => { "use strict";
|
4
|
+
dangerouslySetInnerHTML: { __html: `(() => { "use strict";
|
5
|
+
class ImageWithPreview extends HTMLElement {
|
6
|
+
sd;
|
7
|
+
mo;
|
8
|
+
static observedAttributes = ["preview"];
|
9
|
+
get #imgEl() {
|
10
|
+
return this.querySelector("img");
|
11
|
+
}
|
12
|
+
get #canvasEl() {
|
13
|
+
return this.querySelector("canvas");
|
14
|
+
}
|
15
|
+
constructor() {
|
16
|
+
super();
|
17
|
+
this.sd = this.attachShadow({
|
18
|
+
mode: "open"
|
19
|
+
});
|
20
|
+
this.sd.innerHTML = \`<slot name="preview"></slot>\`;
|
21
|
+
}
|
22
|
+
#checkReady = () => {
|
23
|
+
if (this.#imgEl && this.#canvasEl) {
|
24
|
+
this.mo?.disconnect();
|
25
|
+
if (this.#imgEl.complete) {
|
26
|
+
this.#imgLoad();
|
27
|
+
} else {
|
28
|
+
this.#updatePreview();
|
29
|
+
this.#imgEl.addEventListener("load", this.#imgLoad);
|
30
|
+
}
|
31
|
+
return true;
|
32
|
+
}
|
33
|
+
};
|
34
|
+
connectedCallback() {
|
35
|
+
if (!this.#checkReady()) {
|
36
|
+
this.mo = new MutationObserver(this.#checkReady);
|
37
|
+
this.mo.observe(this, {
|
38
|
+
subtree: true,
|
39
|
+
childList: true,
|
40
|
+
attributes: false
|
41
|
+
});
|
42
|
+
}
|
43
|
+
}
|
44
|
+
#imgLoad = () => {
|
45
|
+
};
|
46
|
+
attributeChangedCallback(name) {
|
47
|
+
if (this.#canvasEl && name === "preview") {
|
48
|
+
this.#updatePreview();
|
49
|
+
}
|
50
|
+
}
|
51
|
+
#updatePreview() {
|
52
|
+
const previewObj = JSON.parse(this.getAttribute("preview"));
|
53
|
+
updateBlurHashPreview(this.#canvasEl, previewObj);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
if (!customElements.get("blurhash-image")) {
|
57
|
+
customElements.define("blurhash-image", ImageWithPreview);
|
58
|
+
}
|
59
|
+
function updateBlurHashPreview(canvasEl, preview) {
|
60
|
+
const {
|
61
|
+
w: width,
|
62
|
+
h: height,
|
63
|
+
blurhash
|
64
|
+
} = preview;
|
65
|
+
canvasEl.width = width;
|
66
|
+
canvasEl.height = height;
|
67
|
+
const worker = new Worker("/canvas-worker.js");
|
68
|
+
const offscreen = canvasEl.transferControlToOffscreen();
|
69
|
+
worker.postMessage({
|
70
|
+
canvas: offscreen,
|
71
|
+
width,
|
72
|
+
height,
|
73
|
+
blurhash
|
74
|
+
}, [offscreen]);
|
75
|
+
return;
|
76
|
+
} })();` },
|
5
77
|
});
|
package/package.json
CHANGED
package/vite.config.ts
CHANGED