@yft-design/engine 0.0.36 → 0.0.37
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.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var F={create(e,t){if(typeof document<"u"){let a=document.createElement("canvas");return a.width=e,a.height=t,a}if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(e,t);throw new Error("Canvas is not available")}},w=e=>{let t=e.getContext("2d");if(!t)throw new Error("Failed to get canvas context");return t},M=e=>{e.width=0,e.height=0};var G=640,P=80,R=async(e,t={})=>{let{parseGIF:a,decompressFrame:r}=await import("gifuct-js"),n=a(e),s=null,i=(n.frames??[]).map(o=>o.image?(!o.gce&&s&&(o.gce=s),s=null,r(o,n.gct,!0)):(o.gce&&(s=o.gce),null)).filter(o=>o!==null);if(!i||!i.length)throw new Error("No frames available");let g=n.lsd,m=g?.width??i[0].dims.width,p=g?.height??i[0].dims.height,d=Math.max(m,p),h=d>G?G/d:1,c=Math.max(1,Math.round(m*h)),f=Math.max(1,Math.round(p*h)),S=i.reduce((o,D)=>o+(D.delay||0),0)/1e3,l=i.length>P?i.slice(0,P):i,y=l.reduce((o,D)=>o+(D.delay||0),0),u=l.length>0?y/l.length:100,v=t.canvasFactory??F,x=v.create(1,1),B=w(x),C=v.create(m,p),A=w(C),I=v.create(c*l.length,f),O=w(I);if(!B||!A||!O)throw new Error("Failed to get canvas context");C.width=m,C.height=p,I.width=c*l.length,I.height=f;let b,T;return l.forEach((o,D)=>{b?.disposalType===2?A.clearRect(b.dims.left,b.dims.top,b.dims.width,b.dims.height):b?.disposalType===3&&T&&A.putImageData(T,0,0),T=o.disposalType===3?A.getImageData(0,0,m,p):void 0,x.width=o.dims.width,x.height=o.dims.height;let U=B.createImageData(o.dims.width,o.dims.height);U.data.set(o.patch),B.putImageData(U,0,0),A.drawImage(x,o.dims.left,o.dims.top),O.drawImage(C,0,0,m,p,c*D,0,c,f),b=o}),M(x),M(C),{canvas:I,frameWidth:c,framesLength:l.length,delay:u,frameDelays:l.map(o=>o.delay||100),duration:S}},W=(e,t,a,r,n={})=>{if(!e.length)throw new Error("No frames available");let s=n.canvasFactory??F,i=Math.max(t,a),g=i>G?G/i:1,m=Math.max(1,Math.round(t*g)),p=Math.max(1,Math.round(a*g)),d=s.create(t,a),h=w(d),c=s.create(m*Math.min(e.length,P),p),f=w(c);if(!h||!f)throw new Error("Failed to get canvas context");d.width=t,d.height=a,c.width=m*Math.min(e.length,P),c.height=p;let S=e.slice(0,P);S.forEach((y,u)=>{let v=h.createImageData(t,a);v.data.set(new Uint8ClampedArray(y)),h.putImageData(v,0,0),f.drawImage(d,0,0,t,a,m*u,0,m,p)}),M(d);let l=S.map((y,u)=>Math.max(1,r[u]??100));return{canvas:c,frameWidth:m,framesLength:S.length,delay:l.reduce((y,u)=>y+u,0)/l.length,frameDelays:l,duration:l.reduce((y,u)=>y+u,0)/1e3}};var q=[137,80,78,71,13,10,26,10],L=e=>{let t=new Uint8Array(e);if(t.length<8||!q.every((r,n)=>t[n]===r))return!1;let a=new DataView(e);for(let r=8;r+12<=t.length;){let n=a.getUint32(r),s=r+n+12;if(s>t.length)return!1;let i=String.fromCharCode(...t.subarray(r+4,r+8));if(i==="acTL")return n===8&&a.getUint32(r+8)>0;if(i==="IDAT"||i==="IEND")return!1;r=s}return!1};var N=async(e,t={})=>{if(!L(e))throw new Error("Not an animated PNG");let{default:a}=await import("upng-js"),r=a.decode(e);return r.frames=r.frames.slice(0,80),W(a.toRGBA8(r),r.width,r.height,r.frames.map(n=>n.delay),t)};var k=640,H=80,E=100,_=(e,t)=>String.fromCharCode(e[t],e[t+1],e[t+2],e[t+3]),V=e=>{if(e.byteLength<12)return!1;let t=new Uint8Array(e,0,12);return _(t,0)==="RIFF"&&_(t,8)==="WEBP"};var j=(e,t,a,r={})=>{let n=(r.canvasFactory??F).create(e*a,t);n.width=e*a,n.height=t;let s=w(n);if(!s)throw new Error("Failed to get canvas context");return{canvas:n,ctx:s}},z=(e,t)=>{let a=Math.max(e,t),r=a>k?k/a:1;return{width:Math.max(1,Math.round(e*r)),height:Math.max(1,Math.round(t*r))}},X=async(e,t={})=>{let a=URL.createObjectURL(new Blob([e],{type:"image/webp"}));try{let r=await new Promise((m,p)=>{let d=new Image;d.onload=()=>m(d),d.onerror=()=>p(new Error("Failed to load WebP")),d.src=a}),{width:n,height:s}=z(r.naturalWidth,r.naturalHeight),{canvas:i,ctx:g}=j(n,s,1,t);return g.drawImage(r,0,0,n,s),{canvas:i,frameWidth:n,framesLength:1,delay:E,frameDelays:[E],duration:0}}finally{URL.revokeObjectURL(a)}},Y=async(e,t={})=>{let a=globalThis.ImageDecoder;if(!a||!await a.isTypeSupported("image/webp"))return X(e,t);let r=new a({data:e,type:"image/webp",preferAnimation:!0});try{await r.tracks.ready;let n=r.tracks.selectedTrack;if(!n?.frameCount||n.frameCount<2)return X(e,t);let s=Math.min(n.frameCount,H),i=await r.decode({frameIndex:0}),{width:g,height:m}=z(i.image.displayWidth,i.image.displayHeight),{canvas:p,ctx:d}=j(g,m,s,t),h=[];for(let c=0;c<s;c++){let{image:f}=c===0?i:await r.decode({frameIndex:c}),S=typeof f.duration=="number"&&f.duration>0?f.duration/1e3:E;h.push(S),d.drawImage(f,g*c,0,g,m),f.close()}return{canvas:p,frameWidth:g,framesLength:s,delay:h[0]||E,frameDelays:h,duration:h.reduce((c,f)=>c+f,0)/1e3}}finally{r.close()}};var oe=async(e,t={})=>{if(V(e))return Y(e,t);if(L(e))return N(e,t);let a=new Uint8Array(e,0,Math.min(6,e.byteLength)),r=String.fromCharCode(...a);if(r==="GIF87a"||r==="GIF89a")return R(e,t);throw new Error("Unsupported animated image (expected GIF, animated WebP or APNG)")};export{oe as decodeAnimatedImage};
|