@yft-design/engine 0.0.35 → 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.
- package/dist/EngineCanvas.d.ts +2 -0
- package/dist/decoders/animatedImage.d.ts +4 -0
- package/dist/decoders/animatedImage.js +1 -0
- package/dist/decoders/apng2sprite.d.ts +6 -0
- package/dist/decoders/gif2sprite.d.ts +4 -2
- package/dist/decoders/spriteCanvas.d.ts +13 -0
- package/dist/decoders/webp2sprite.d.ts +2 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +47 -47
- package/dist/render/SourceManager.d.ts +3 -0
- package/package.json +5 -3
package/dist/EngineCanvas.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ import { MeshBatch } from './render/MeshBatch';
|
|
|
24
24
|
import { QuadBatch } from './render/QuadBatch';
|
|
25
25
|
import { SceneRenderer, type SceneRendererHost } from './render/SceneRenderer';
|
|
26
26
|
import { type SourceHost } from './render/SourceManager';
|
|
27
|
+
import type { AnimatedSourceDecoder } from './decoders/animatedImage';
|
|
27
28
|
import { type SceneExportHost } from './export/SceneExporter';
|
|
28
29
|
import type { ExportPixels, ExportRawPixels, ExportRegion } from './export/glExport';
|
|
29
30
|
import { type HandleId } from './render/selectionOverlay';
|
|
@@ -49,6 +50,7 @@ export declare class EngineCanvas implements PointerHost, TextEditingHost, Sourc
|
|
|
49
50
|
private readonly glyphAtlas;
|
|
50
51
|
readonly renderEpoch: RenderEpoch;
|
|
51
52
|
bakeKReuseFloor: number;
|
|
53
|
+
decodeAnimated: AnimatedSourceDecoder | null;
|
|
52
54
|
private readonly sources;
|
|
53
55
|
private readonly textRenderer;
|
|
54
56
|
readonly renderer: SceneRenderer;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { GifSprite } from './gif2sprite';
|
|
2
|
+
import type { SpriteDecodeOptions } from './spriteCanvas';
|
|
3
|
+
export type AnimatedSourceDecoder = (buffer: ArrayBuffer) => Promise<GifSprite>;
|
|
4
|
+
export declare const decodeAnimatedImage: (buffer: ArrayBuffer, options?: SpriteDecodeOptions) => Promise<GifSprite>;
|
|
@@ -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};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { GifSprite } from './gif2sprite';
|
|
2
|
+
import type { SpriteDecodeOptions } from './spriteCanvas';
|
|
3
|
+
export declare const isAnimatedPng: (buffer: ArrayBuffer) => boolean;
|
|
4
|
+
export declare const isAnimatedPngFile: (file: Blob) => Promise<boolean>;
|
|
5
|
+
export declare const isAnimatedPngUrl: (url: string) => Promise<boolean>;
|
|
6
|
+
export declare const bufferToApngSprite: (buffer: ArrayBuffer, options?: SpriteDecodeOptions) => Promise<GifSprite>;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import type { SpriteDecodeOptions, SpriteImageSource } from './spriteCanvas';
|
|
1
2
|
export interface GifSprite {
|
|
2
|
-
canvas:
|
|
3
|
+
canvas: SpriteImageSource;
|
|
3
4
|
frameWidth: number;
|
|
4
5
|
framesLength: number;
|
|
5
6
|
delay: number;
|
|
6
7
|
frameDelays: number[];
|
|
7
8
|
duration: number;
|
|
8
9
|
}
|
|
9
|
-
export declare const bufferToGifSprite: (buffer: ArrayBuffer) => Promise<GifSprite>;
|
|
10
|
+
export declare const bufferToGifSprite: (buffer: ArrayBuffer, options?: SpriteDecodeOptions) => Promise<GifSprite>;
|
|
11
|
+
export declare const rgbaFramesToSprite: (frames: ArrayBuffer[], sourceWidth: number, sourceHeight: number, frameDelays: number[], options?: SpriteDecodeOptions) => GifSprite;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type SpriteCanvas = HTMLCanvasElement | OffscreenCanvas;
|
|
2
|
+
export type SpriteImageSource = SpriteCanvas | ImageBitmap;
|
|
3
|
+
export type SpriteContext = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
|
|
4
|
+
export interface SpriteCanvasFactory {
|
|
5
|
+
create(width: number, height: number): SpriteCanvas;
|
|
6
|
+
}
|
|
7
|
+
export interface SpriteDecodeOptions {
|
|
8
|
+
canvasFactory?: SpriteCanvasFactory;
|
|
9
|
+
}
|
|
10
|
+
export declare const browserCanvasFactory: SpriteCanvasFactory;
|
|
11
|
+
export declare const spriteContext: (canvas: SpriteCanvas) => SpriteContext;
|
|
12
|
+
export declare const releaseSpriteCanvas: (canvas: SpriteCanvas) => void;
|
|
13
|
+
export declare const closeSpriteSource: (source: SpriteImageSource) => void;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { GifSprite } from './gif2sprite';
|
|
2
|
+
import type { SpriteDecodeOptions } from './spriteCanvas';
|
|
2
3
|
export declare const isWebPBuffer: (buffer: ArrayBuffer) => boolean;
|
|
3
4
|
export declare const isAnimatedWebP: (buffer: ArrayBuffer) => boolean;
|
|
4
5
|
export declare const isAnimatedWebPFile: (file: Blob) => Promise<boolean>;
|
|
5
6
|
export declare const isAnimatedWebPUrl: (url: string) => Promise<boolean>;
|
|
6
|
-
export declare const bufferToWebpSprite: (buffer: ArrayBuffer) => Promise<GifSprite>;
|
|
7
|
+
export declare const bufferToWebpSprite: (buffer: ArrayBuffer, options?: SpriteDecodeOptions) => Promise<GifSprite>;
|
package/dist/index.d.ts
CHANGED
|
@@ -28,6 +28,9 @@ export { anchorStyleAt } from './model/pathEdit';
|
|
|
28
28
|
export { clearAlphaHitMask } from './model/alphaHit';
|
|
29
29
|
export { pathEditKindOf, type PathSessionInfo } from './interact/pathSession';
|
|
30
30
|
export { isAnimatedWebPFile, isAnimatedWebPUrl } from './decoders/webp2sprite';
|
|
31
|
+
export { isAnimatedPng, isAnimatedPngFile, isAnimatedPngUrl } from './decoders/apng2sprite';
|
|
32
|
+
export { decodeAnimatedImage, type AnimatedSourceDecoder } from './decoders/animatedImage';
|
|
33
|
+
export type { GifSprite } from './decoders/gif2sprite';
|
|
31
34
|
export { fboPixelsToImageData, type ExportRegion, type ExportRawPixels } from './export/glExport';
|
|
32
35
|
export { type SerializedGradientFill } from './core/gradient';
|
|
33
36
|
export { arcTextKind, computeArcTextLayout } from './model/arcLayout';
|