next-blurhash-previews 0.0.3-beta31 → 0.0.3-beta34
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/bin/generateBlurhash.js
CHANGED
@@ -18,24 +18,10 @@ export async function getSharpImage(imgPath) {
|
|
18
18
|
}
|
19
19
|
|
20
20
|
export async function getBlurhash(path) {
|
21
|
-
|
21
|
+
const blurhashImage = await getSharpImage(path);
|
22
22
|
const dimensions = await blurhashImage.metadata();
|
23
23
|
|
24
24
|
const { width, height } = dimensions;
|
25
|
-
let blurhashW = width;
|
26
|
-
let blurhashH = height;
|
27
|
-
|
28
|
-
if (width > 200) {
|
29
|
-
const newBuffer = await blurhashImage.resize(200).toBuffer();
|
30
|
-
blurhashImage = sharp(newBuffer);
|
31
|
-
|
32
|
-
let newMetadata = await blurhashImage.metadata();
|
33
|
-
blurhashW = newMetadata.width;
|
34
|
-
blurhashH = newMetadata.height;
|
35
|
-
console.log("Adjustied", blurhashW, blurhashH);
|
36
|
-
} else {
|
37
|
-
console.log("Not adjusting", path, width);
|
38
|
-
}
|
39
25
|
|
40
26
|
return new Promise((res, rej) => {
|
41
27
|
blurhashImage
|
@@ -48,19 +34,13 @@ export async function getBlurhash(path) {
|
|
48
34
|
} else {
|
49
35
|
const blurhash = encode(
|
50
36
|
new Uint8ClampedArray(buffer),
|
51
|
-
|
52
|
-
|
37
|
+
width,
|
38
|
+
height,
|
53
39
|
4,
|
54
40
|
4
|
55
41
|
);
|
56
42
|
if (isBlurhashValid(blurhash)) {
|
57
|
-
return res({
|
58
|
-
blurhash,
|
59
|
-
dw: width,
|
60
|
-
dh: height,
|
61
|
-
w: blurhashW,
|
62
|
-
h: blurhashH,
|
63
|
-
});
|
43
|
+
return res({ blurhash, w: width, h: height });
|
64
44
|
} else {
|
65
45
|
console.log("FAIL");
|
66
46
|
return rej("FAIL");
|
@@ -1,12 +1,6 @@
|
|
1
1
|
import { decode } from "../node_modules/blurhash/dist/esm/index";
|
2
2
|
|
3
|
-
type blurhash = {
|
4
|
-
w: number;
|
5
|
-
h: number;
|
6
|
-
dw: number;
|
7
|
-
dh: number;
|
8
|
-
blurhash: string;
|
9
|
-
};
|
3
|
+
type blurhash = { w: number; h: number; blurhash: string };
|
10
4
|
|
11
5
|
class ImageWithPreview extends HTMLElement {
|
12
6
|
sd: ShadowRoot;
|
@@ -55,23 +49,18 @@ class ImageWithPreview extends HTMLElement {
|
|
55
49
|
}
|
56
50
|
|
57
51
|
#imgLoad = () => {
|
58
|
-
|
52
|
+
this.sd.innerHTML = `<slot name="image"></slot>`;
|
59
53
|
};
|
60
54
|
|
61
55
|
attributeChangedCallback(name) {
|
62
56
|
if (this.#canvasEl && name === "preview") {
|
63
|
-
|
64
|
-
console.log(this.getAttribute("preview"), this.getAttribute("url"), time);
|
57
|
+
this.#updatePreview();
|
65
58
|
}
|
66
59
|
}
|
67
60
|
|
68
61
|
#updatePreview() {
|
69
62
|
const previewObj = JSON.parse(this.getAttribute("preview")!);
|
70
|
-
|
71
|
-
const start = +new Date();
|
72
63
|
updateBlurHashPreview(this.#canvasEl, previewObj);
|
73
|
-
const end = +new Date();
|
74
|
-
return end - start;
|
75
64
|
}
|
76
65
|
}
|
77
66
|
|
@@ -80,14 +69,20 @@ if (!customElements.get("blurhash-image")) {
|
|
80
69
|
}
|
81
70
|
|
82
71
|
function updateBlurHashPreview(canvasEl: HTMLCanvasElement, preview: blurhash) {
|
83
|
-
const { w, h
|
72
|
+
const { w: width, h: height, blurhash } = preview;
|
73
|
+
|
74
|
+
const worker = new Worker("/canvas-worker.js");
|
84
75
|
|
85
|
-
|
86
|
-
|
76
|
+
const offscreen = (canvasEl as any).transferControlToOffscreen();
|
77
|
+
worker.postMessage({ canvas: offscreen, width, height, blurhash }, [
|
78
|
+
offscreen,
|
79
|
+
]);
|
80
|
+
canvasEl.width = width;
|
81
|
+
canvasEl.height = height;
|
87
82
|
|
88
|
-
const pixels = decode(
|
83
|
+
const pixels = decode(blurhash, width, height);
|
89
84
|
const ctx = canvasEl.getContext("2d")!;
|
90
|
-
const imageData = ctx.createImageData(
|
85
|
+
const imageData = ctx.createImageData(width, height);
|
91
86
|
imageData.data.set(pixels);
|
92
87
|
ctx.putImageData(imageData, 0, 0);
|
93
88
|
}
|
package/imagePreviewBootstrap.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { createElement } from "react";
|
2
2
|
|
3
3
|
export const imagePreviewBootstrap = createElement("script", {
|
4
|
-
dangerouslySetInnerHTML: { __html: `(() => { "use strict";const
|
4
|
+
dangerouslySetInnerHTML: { __html: `(() => { "use strict";const T=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","#","$","%","*","+",",","-",".",":",";","=","?","@","[","]","^","_","{","|","}","~"],d=t=>{let e=0;for(let s=0;s<t.length;s++){const n=t[s],i=T.indexOf(n);e=e*83+i}return e},b=t=>{let e=t/255;return e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},p=t=>{let e=Math.max(0,Math.min(1,t));return e<=.0031308?Math.round(e*12.92*255+.5):Math.round((1.055*Math.pow(e,.4166666666666667)-.055)*255+.5)},k=t=>t<0?-1:1,v=(t,e)=>k(t)*Math.pow(Math.abs(t),e);class B extends Error{constructor(e){super(e),this.name="ValidationError",this.message=e}}const E=t=>{if(!t||t.length<6)throw new B("The blurhash string must be at least 6 characters");const e=d(t[0]),s=Math.floor(e/9)+1,n=e%9+1;if(t.length!==4+2*n*s)throw new B(\`blurhash length mismatch: length is \${t.length} but it should be \${4+2*n*s}\`)},A=t=>{const e=t>>16,s=t>>8&255,n=t&255;return[b(e),b(s),b(n)]},G=(t,e)=>{const s=Math.floor(t/361),n=Math.floor(t/19)%19,i=t%19;return[v((s-9)/9,2)*e,v((n-9)/9,2)*e,v((i-9)/9,2)*e]},I=(t,e,s,n)=>{E(t),n=n|1;const i=d(t[0]),l=Math.floor(i/9)+1,c=i%9+1,g=(d(t[1])+1)/166,a=new Array(c*l);for(let o=0;o<a.length;o++)if(o===0){const r=d(t.substring(2,6));a[o]=A(r)}else{const r=d(t.substring(4+o*2,6+o*2));a[o]=G(r,g*n)}const h=e*4,u=new Uint8ClampedArray(h*s);for(let o=0;o<s;o++)for(let r=0;r<e;r++){let C=0,q=0,y=0;for(let m=0;m<l;m++)for(let f=0;f<c;f++){const w=Math.cos(Math.PI*r*f/e)*Math.cos(Math.PI*o*m/s);let M=a[f+m*c];C+=M[0]*w,q+=M[1]*w,y+=M[2]*w}let L=p(C),P=p(q),R=p(y);u[4*r+0+o*h]=L,u[4*r+1+o*h]=P,u[4*r+2+o*h]=R,u[4*r+3+o*h]=255}return u};class O extends HTMLElement{sd;mo;static observedAttributes=["preview"];get#t(){return this.querySelector("img")}get#e(){return this.querySelector("canvas")}constructor(){super(),this.sd=this.attachShadow({mode:"open"}),this.sd.innerHTML='<slot name="preview"></slot>'}#s=()=>{if(this.#t&&this.#e)return this.mo?.disconnect(),this.#t.complete?this.#n():(this.#o(),this.#t.addEventListener("load",this.#n)),!0};connectedCallback(){this.#s()||(this.mo=new MutationObserver(this.#s),this.mo.observe(this,{subtree:!0,childList:!0,attributes:!1}))}#n=()=>{this.sd.innerHTML='<slot name="image"></slot>'};attributeChangedCallback(e){this.#e&&e==="preview"&&this.#o()}#o(){const e=JSON.parse(this.getAttribute("preview"));D(this.#e,e)}}customElements.get("blurhash-image")||customElements.define("blurhash-image",O);function D(t,e){const{w:s,h:n,blurhash:i}=e,l=new Worker("/canvas-worker.js"),c=t.transferControlToOffscreen();l.postMessage({canvas:c,width:s,height:n,blurhash:i},[c]),t.width=s,t.height=n;const x=I(i,s,n),g=t.getContext("2d"),a=g.createImageData(s,n);a.data.set(x),g.putImageData(a,0,0)} })();` },
|
5
5
|
});
|