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
- canvasEl.width = width;
79
- canvasEl.height = height;
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);
@@ -1,5 +1,77 @@
1
1
  import { createElement } from "react";
2
2
 
3
3
  export const imagePreviewBootstrap = createElement("script", {
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]),c=Math.floor(i/9)+1,l=i%9+1,g=(d(t[1])+1)/166,a=new Array(l*c);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<c;m++)for(let f=0;f<l;f++){const w=Math.cos(Math.PI*r*f/e)*Math.cos(Math.PI*o*m/s);let M=a[f+m*l];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,c=new Worker("/canvas-worker.js"),l=t.transferControlToOffscreen();c.postMessage({canvas:l,width:s,height:n,blurhash:i}),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)} })();` },
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-blurhash-previews",
3
- "version": "0.0.3-beta33",
3
+ "version": "0.0.3-beta36",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
package/vite.config.ts CHANGED
@@ -12,7 +12,7 @@ export default defineConfig({
12
12
  fileName: () => "imageWithPreview.js",
13
13
  name: "imageWithPreview",
14
14
  },
15
- //minify: false,
15
+ minify: false,
16
16
  rollupOptions: {
17
17
  external: ["react", "react-dom", "next", "next/script"],
18
18
  },