@taole/giftstage 0.1.12 → 0.1.13
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/gift-stage.cjs.js +1 -1
- package/dist/gift-stage.es.js +1 -1
- package/package.json +1 -1
package/dist/gift-stage.cjs.js
CHANGED
|
@@ -8605,5 +8605,5 @@ async function xu(r) {
|
|
|
8605
8605
|
v_texcoord = a_texCoord;
|
|
8606
8606
|
v_alpha_texCoord = a_alpha_texCoord;
|
|
8607
8607
|
}
|
|
8608
|
-
`;function sf(i,A,t,e){const g=new Float32Array(t.size*dn),I=A.get(e);if(!I||I.length===0)return g;const[s,n]=i.info.rgbFrame;let B=0;for(const C of I){const Q=t.get(String(C.srcId));if(!Q)continue;if(B+dn>g.length)break;const[a,r,E,o]=C.frame,[h,c,f,l]=C.mFrame,u=fn(a+s,r+n,E,o,i.info.videoW,i.info.videoH),w=fn(h,c,f,l,i.info.videoW,i.info.videoH);g[B++]=Q,g[B++]=u[0],g[B++]=u[1],g[B++]=u[2],g[B++]=u[3],g[B++]=w[0],g[B++]=w[1],g[B++]=w[2],g[B++]=w[3]}return g}class nf{constructor(A,t,e,g,I){this.config=A,this.frameMap=t,dA(this,"canvas"),dA(this,"gl"),dA(this,"program"),dA(this,"vertexShader"),dA(this,"fragmentShader"),dA(this,"vertexBuffer"),dA(this,"videoTexture"),dA(this,"imagePosLoc"),dA(this,"resourceEntries"),dA(this,"textureUnitBySrcId",new Map);var s;if(e.size===0)throw new Error("VAP mix composer requires at least one slot resource");this.canvas=Af(g,I);const n=this.canvas.getContext("webgl2",{alpha:!0,antialias:!1,depth:!1,premultipliedAlpha:!1,preserveDrawingBuffer:!1,stencil:!1})||this.canvas.getContext("webgl",{alpha:!0,antialias:!1,depth:!1,premultipliedAlpha:!1,preserveDrawingBuffer:!1,stencil:!1})||this.canvas.getContext("experimental-webgl");if(!n)throw new Error("VAP mixed rendering requires WebGL support");this.gl=n;const B=Math.max(1,n.getParameter(n.MAX_TEXTURE_IMAGE_UNITS)),C=Math.max(0,B-1),Q=[];let a=1;for(const u of(s=A.src)!=null?s:[]){const w=e.get(String(u.srcId));if(!w)continue;if(a>C){console.warn("[GiftStage] VAP mixed resources exceed texture unit limit, skipping",{srcId:u.srcId,srcTag:u.srcTag,maxUnits:B});continue}const y=nC(n,a,w.source,!0),p={srcId:String(u.srcId),unit:a,texture:y};Q.push(p),this.textureUnitBySrcId.set(p.srcId,p.unit),a++}if(Q.length===0)throw new Error("VAP mixed rendering could not bind any slot resources");this.resourceEntries=Q;const r=tf(n,If,gf(this.resourceEntries.length));this.program=r.program,this.vertexShader=r.vertexShader,this.fragmentShader=r.fragmentShader,this.imagePosLoc=n.getUniformLocation(this.program,"image_pos"),this.videoTexture=nC(n,0);const E=n.getUniformLocation(this.program,"u_image_video");E!==null&&n.uniform1i(E,0);for(const u of this.resourceEntries){const w=n.getUniformLocation(this.program,`u_image${u.unit}`);w!==null&&n.uniform1i(w,u.unit)}const o=n.createBuffer();if(!o)throw new Error("Failed to create VAP mix vertex buffer");this.vertexBuffer=o,n.bindBuffer(n.ARRAY_BUFFER,this.vertexBuffer),n.bufferData(n.ARRAY_BUFFER,ef(A.info),n.STATIC_DRAW);const h=6*Float32Array.BYTES_PER_ELEMENT,c=n.getAttribLocation(this.program,"a_position"),f=n.getAttribLocation(this.program,"a_texCoord"),l=n.getAttribLocation(this.program,"a_alpha_texCoord");n.enableVertexAttribArray(c),n.vertexAttribPointer(c,2,n.FLOAT,!1,h,0),n.enableVertexAttribArray(f),n.vertexAttribPointer(f,2,n.FLOAT,!1,h,2*Float32Array.BYTES_PER_ELEMENT),n.enableVertexAttribArray(l),n.vertexAttribPointer(l,2,n.FLOAT,!1,h,4*Float32Array.BYTES_PER_ELEMENT),n.viewport(0,0,this.canvas.width,this.canvas.height),n.disable(n.BLEND),n.disable(n.DEPTH_TEST),n.clearColor(0,0,0,0),n.pixelStorei(n.UNPACK_PREMULTIPLY_ALPHA_WEBGL,0),n.pixelStorei(n.UNPACK_COLORSPACE_CONVERSION_WEBGL,n.NONE)}render(A,t){const e=this.gl;e.useProgram(this.program),e.activeTexture(e.TEXTURE0),e.bindTexture(e.TEXTURE_2D,this.videoTexture),e.pixelStorei(e.UNPACK_FLIP_Y_WEBGL,1),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,A),e.pixelStorei(e.UNPACK_FLIP_Y_WEBGL,0);const g=sf(this.config,this.frameMap,this.textureUnitBySrcId,t);return this.imagePosLoc!==null&&e.uniform1fv(this.imagePosLoc,g),e.clear(e.COLOR_BUFFER_BIT),e.drawArrays(e.TRIANGLE_STRIP,0,4),this.canvas}destroy(){const A=this.gl;for(const e of this.resourceEntries)A.deleteTexture(e.texture);A.deleteTexture(this.videoTexture),A.deleteBuffer(this.vertexBuffer),A.deleteProgram(this.program),A.deleteShader(this.vertexShader),A.deleteShader(this.fragmentShader);const t=A.getExtension("WEBGL_lose_context");t==null||t.loseContext()}}var Bf=Object.defineProperty,Cf=(i,A,t)=>A in i?Bf(i,A,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[A]=t,mA=(i,A,t)=>Cf(i,typeof A!="symbol"?A+"":A,t);const MQ=1397178947,xQ=12,Qf=8;class af{constructor(A=1024){mA(this,"buffer"),mA(this,"view"),mA(this,"bytes"),mA(this,"offset",0),mA(this,"encoder",new TextEncoder),this.buffer=new ArrayBuffer(A),this.view=new DataView(this.buffer),this.bytes=new Uint8Array(this.buffer)}ensure(A){const t=this.offset+A;if(t<=this.buffer.byteLength)return;let e=this.buffer.byteLength;for(;e<t;)e*=2;const g=new ArrayBuffer(e);new Uint8Array(g).set(this.bytes.subarray(0,this.offset)),this.buffer=g,this.view=new DataView(this.buffer),this.bytes=new Uint8Array(this.buffer)}u8(A){this.ensure(1),this.view.setUint8(this.offset,A),this.offset+=1}u16(A){this.ensure(2),this.view.setUint16(this.offset,A,!0),this.offset+=2}u32(A){this.ensure(4),this.view.setUint32(this.offset,A,!0),this.offset+=4}f32(A){this.ensure(4),this.view.setFloat32(this.offset,A,!0),this.offset+=4}bytesValue(A){this.u32(A.byteLength),this.ensure(A.byteLength),this.bytes.set(A,this.offset),this.offset+=A.byteLength}string(A){this.bytesValue(this.encoder.encode(A))}finish(){return this.buffer.slice(0,this.offset)}}class Ef{constructor(A){mA(this,"view"),mA(this,"bytes"),mA(this,"offset",0),mA(this,"decoder",new TextDecoder),this.view=new DataView(A),this.bytes=new Uint8Array(A)}u8(){const A=this.view.getUint8(this.offset);return this.offset+=1,A}u16(){const A=this.view.getUint16(this.offset,!0);return this.offset+=2,A}u32(){const A=this.view.getUint32(this.offset,!0);return this.offset+=4,A}f32(){const A=this.view.getFloat32(this.offset,!0);return this.offset+=4,A}bytesValue(){const A=this.u32(),t=this.bytes.slice(this.offset,this.offset+A);return this.offset+=A,t}string(){return this.decoder.decode(this.bytesValue())}}function rf(i){var A,t;let e=0;for(const g of i.atlasPages)e+=g.imageBytes.byteLength;for(const[g,I]of i.audioBlobs)e+=g.length*2+I.byteLength;for(const[g]of i.atlasRects)e+=g.length*2+20;for(const g of i.sprites){e+=g.imageKey.length*2+g.frames.length*64;for(const I of g.frames)if(e+=I.clipPath.length*2,I.shapes)for(const s of I.shapes)e+=((t=(A=s.pathData)==null?void 0:A.length)!=null?t:0)*2+96}return e}function of(i,A){var t;let e=0;A.fill&&(e|=1),A.stroke&&(e|=2),i.u8(e),A.fill&&(i.f32(A.fill.r),i.f32(A.fill.g),i.f32(A.fill.b),i.f32(A.fill.a)),A.stroke&&(i.f32(A.stroke.r),i.f32(A.stroke.g),i.f32(A.stroke.b),i.f32(A.stroke.a)),i.f32((t=A.strokeWidth)!=null?t:0)}function hf(i){const A=i.u8(),t=A&1?{r:i.f32(),g:i.f32(),b:i.f32(),a:i.f32()}:null,e=A&2?{r:i.f32(),g:i.f32(),b:i.f32(),a:i.f32()}:null;return{fill:t,stroke:e,strokeWidth:i.f32()}}function cf(i,A){var t,e,g,I;i.u8(A.kind==="path"?0:A.kind==="rect"?1:2),i.string((t=A.pathData)!=null?t:"");const s=(e=A.rect)!=null?e:{x:0,y:0,width:0,height:0,cornerRadius:0};i.f32(s.x),i.f32(s.y),i.f32(s.width),i.f32(s.height),i.f32((g=s.cornerRadius)!=null?g:0);const n=(I=A.ellipse)!=null?I:{x:0,y:0,radiusX:0,radiusY:0};i.f32(n.x),i.f32(n.y),i.f32(n.radiusX),i.f32(n.radiusY),of(i,A.style),i.f32(A.transform.a),i.f32(A.transform.b),i.f32(A.transform.c),i.f32(A.transform.d),i.f32(A.transform.tx),i.f32(A.transform.ty)}function lf(i){const A=i.u8(),t=i.string(),e={x:i.f32(),y:i.f32(),width:i.f32(),height:i.f32(),cornerRadius:i.f32()},g={x:i.f32(),y:i.f32(),radiusX:i.f32(),radiusY:i.f32()},I=hf(i),s={a:i.f32(),b:i.f32(),c:i.f32(),d:i.f32(),tx:i.f32(),ty:i.f32()};return A===1?{kind:"rect",rect:e,style:I,transform:s}:A===2?{kind:"ellipse",ellipse:g,style:I,transform:s}:{kind:"path",pathData:t,style:I,transform:s}}function BC(i){const A=new Uint8Array(i.byteLength);return A.set(i),A.buffer}function df(i){var A;const t=new af(Qf+i.atlasPages.reduce((e,g)=>e+g.imageBytes.byteLength+12,0)+i.sprites.length*64+i.audioBlobs.size*32+i.atlasRects.size*32);t.u32(MQ),t.u16(xQ),t.u16(0),t.f32(i.params.viewBoxWidth),t.f32(i.params.viewBoxHeight),t.f32(i.params.fps),t.u32(i.params.frames),t.u32(i.atlasPages.length);for(const e of i.atlasPages)t.u32(e.width),t.u32(e.height),t.bytesValue(new Uint8Array(e.imageBytes));t.u32(i.atlasRects.size);for(const[e,g]of i.atlasRects)t.string(e),t.u32((A=g.textureIndex)!=null?A:0),t.f32(g.x),t.f32(g.y),t.f32(g.w),t.f32(g.h);t.u32(i.audios.length);for(const e of i.audios)t.string(e.audioKey),t.u32(e.startFrame),t.u32(e.endFrame),t.f32(e.startTime),t.f32(e.totalTime);t.u32(i.audioBlobs.size);for(const[e,g]of i.audioBlobs)t.string(e),t.bytesValue(new Uint8Array(g));t.u32(i.sprites.length);for(const e of i.sprites){t.string(e.imageKey),t.u32(e.frames.length);for(const g of e.frames){let I=0;if(g.layout&&(I|=1),g.transform&&(I|=2),g.clipPath&&(I|=4),g.shapes&&g.shapes.length>0&&(I|=8),t.u8(I),t.f32(g.alpha),g.layout&&(t.f32(g.layout.x),t.f32(g.layout.y),t.f32(g.layout.width),t.f32(g.layout.height)),g.transform&&(t.f32(g.transform.a),t.f32(g.transform.b),t.f32(g.transform.c),t.f32(g.transform.d),t.f32(g.transform.tx),t.f32(g.transform.ty)),g.clipPath&&t.string(g.clipPath),g.shapes&&g.shapes.length>0){t.u32(g.shapes.length);for(const s of g.shapes)cf(t,s)}}}return t.finish()}function ff(i){const A=new Ef(i);if(A.u32()!==MQ)throw new Error("Invalid SVGA cache binary magic");if(A.u16()!==xQ)throw new Error("Unsupported SVGA cache binary version");A.u16();const t={viewBoxWidth:A.f32(),viewBoxHeight:A.f32(),fps:A.f32(),frames:A.u32()},e=A.u32(),g=[];for(let E=0;E<e;E++)g.push({width:A.u32(),height:A.u32(),imageBytes:BC(A.bytesValue())});const I=A.u32(),s=new Map;for(let E=0;E<I;E++){const o=A.string(),h=A.u32();s.set(o,{textureIndex:h,x:A.f32(),y:A.f32(),w:A.f32(),h:A.f32()})}const n=A.u32(),B=[];for(let E=0;E<n;E++)B.push({audioKey:A.string(),startFrame:A.u32(),endFrame:A.u32(),startTime:A.f32(),totalTime:A.f32()});const C=A.u32(),Q=new Map;for(let E=0;E<C;E++){const o=A.string();Q.set(o,BC(A.bytesValue()))}const a=A.u32(),r=[];for(let E=0;E<a;E++){const o=A.string(),h=A.u32(),c=[];for(let f=0;f<h;f++){const l=A.u8(),u=A.f32(),w=l&1?{x:A.f32(),y:A.f32(),width:A.f32(),height:A.f32()}:null,y=l&2?{a:A.f32(),b:A.f32(),c:A.f32(),d:A.f32(),tx:A.f32(),ty:A.f32()}:null,p=l&4?A.string():"";let m=null;if(l&8){const N=A.u32();m=[];for(let R=0;R<N;R++)m.push(lf(A))}c.push({alpha:u,layout:w,transform:y,clipPath:p,shapes:m})}r.push({imageKey:o,frames:c})}return{params:t,sprites:r,audios:B,audioBlobs:Q,atlasRects:s,atlasPages:g}}function EB(i){return`svga-asset:${i}`}function rB(i){return`svga-asset-bin:${i}`}async function uf(i){const A=eE(EB(i),"svga-asset");if(A)return A;const t=await iE(rB(i));if(!t)return null;try{const e=ff(t);return yC(EB(i),"svga-asset",e,t.byteLength),e}catch(e){return IE(rB(i)),null}}async function Xn(i,A){const t=df(A);yC(EB(i),"svga-asset",A,Math.max(t.byteLength,rf(A))),await gE(rB(i),t)}function xA(i){return typeof i=="number"&&Number.isFinite(i)&&i>0}function wf(i,A){const t=xA(i)?i*.5:0,e=xA(A)?A:0;return{width:t,height:e}}function Df(i,A,t,e){const g=xA(A.width)?A.width:void 0,I=xA(A.height)?A.height:void 0,s=xA(i.width)?i.width:void 0,n=xA(i.height)?i.height:void 0,B=i.useOriginalSize===!0;if(s&&n)return{width:s,height:n};if(s)return g&&I?{width:s,height:s*I/g}:{width:s,height:s};if(n)return g&&I?{width:n*g/I,height:n}:{width:n,height:n};let C=g!=null?g:0,Q=I!=null?I:0;if(C>0&&Q>0&&xA(t)&&xA(e)&&(!B||C>t||Q>e)){const a=Math.min(t/C,e/Q);C*=a,Q*=a}return{width:C,height:Q}}var yf=Object.defineProperty,pf=(i,A,t)=>A in i?yf(i,A,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[A]=t,z=(i,A,t)=>pf(i,typeof A!="symbol"?A+"":A,t);const rs=class OA{constructor(A){z(this,"sharedSVGAEntities",new Map),z(this,"pendingSharedSVGAEntities",new Map),z(this,"sharedVideoSources",new Map),z(this,"sharedHtmlVideos",new Map),z(this,"sharedVideoTextures",new Map),z(this,"pendingSharedHtmlVideos",new Map),z(this,"canvas"),z(this,"backend",null),z(this,"manager",null),z(this,"options"),z(this,"ready"),z(this,"destroyed",!1),z(this,"resizeObserver",null),z(this,"nextRenderOrder",0),z(this,"onWindowLayout",()=>{this.destroyed||this.syncCanvasSize()}),this.options=A,this.canvas=document.createElement("canvas"),this.canvas.style.cssText="position:fixed;inset:0;width:100vw;height:100vh;pointer-events:none;z-index:9999;",A.container.appendChild(this.canvas),this.ready=this.init()}backingStoreDpr(){const A=this.options.resolution;return A!=null&&A>0&&Number.isFinite(A)?A:typeof window!="undefined"&&window.devicePixelRatio>0?window.devicePixelRatio:1}syncCanvasSize(){var A;ra(this.canvas,this.backingStoreDpr()),(A=this.manager)==null||A.syncSlotLayoutFromOptions()}resourcePipelineConcurrency(){if(this.options.maxConcurrentParse!=null)return Math.max(1,Math.max(4,this.options.maxConcurrentParse));const A=typeof navigator!="undefined"&&typeof navigator.hardwareConcurrency=="number"?navigator.hardwareConcurrency/2:4;return Math.max(1,Math.max(4,A))}async init(){var A;this.syncCanvasSize(),await new Promise(I=>requestAnimationFrame(()=>I())),this.syncCanvasSize(),AE({maxConcurrent:this.resourcePipelineConcurrency(),maxMemoryBytes:OA.DEFAULT_RESOURCE_CACHE_MEMORY_BYTES,maxPersistentBytes:OA.DEFAULT_RESOURCE_CACHE_PERSISTENT_BYTES});const t=(A=this.options.preferWebGPU)!=null?A:!0,e=this.options.antialias,[g]=await Promise.all([EC(this.canvas,t,e,this.options.forceWebGL1===!0),fC()]);this.backend=g,this.manager=new MC(this.backend),this.resizeObserver=new ResizeObserver(()=>{this.destroyed||this.syncCanvasSize()}),this.resizeObserver.observe(this.canvas),window.addEventListener("resize",this.onWindowLayout),window.addEventListener("orientationchange",this.onWindowLayout)}currentCanvasCssSize(){const A=this.canvas.clientWidth||this.options.container.clientWidth||window.innerWidth||0,t=this.canvas.clientHeight||this.options.container.clientHeight||window.innerHeight||0;return{width:A,height:t}}async resolveGiftIntrinsicSize(A){switch(A.type){case"svga":{const t=await pr(A.source,{useWorker:this.options.svgaParseInWorker!==!1});return{width:t.viewBoxWidth,height:t.viewBoxHeight}}case"vap":{if(!A.config)throw new Error("VAP requires config");const t=await iB(A.config),e=tt(t.info);return{width:e.w,height:e.h}}case"alphaVideo":return this.probeVideoNaturalSize(A.source,"alphaVideo")}}async normalizeAddGiftOptions(A){if(A.width!=null&&A.height!=null)return A;const t=await this.resolveGiftIntrinsicSize(A),e=this.currentCanvasCssSize(),g=Df({width:A.width,height:A.height,useOriginalSize:A.useOriginalSize},t,e.width,e.height);return{...A,width:g.width,height:g.height}}async addGift(A){var t,e,g,I;if(console.info("[GiftStage] addGift await ready…",A.type),await this.ready,console.info("[GiftStage] addGift ready, loading…",A.type),this.destroyed)throw new Error("GiftStage is destroyed");const s=await this.normalizeAddGiftOptions(A),n=wC(),{sx:B,sy:C}=ms(this.canvas),Q=kC(s.x,s.y,s.width,s.height,this.canvas.clientWidth,this.canvas.clientHeight),a={id:n,type:s.type,state:"loading",options:s,zIndex:(t=s.zIndex)!=null?t:0,renderOrder:this.nextRenderOrder++,x:Q.x*B,y:Q.y*C,width:s.width*B,height:s.height*C,scale:1,paused:!1,currentFrame:0,accumulatedTime:0,lastTickTime:0,loopCount:0,videoEntity:null,svgaSlotTextures:void 0,videoElement:null,releaseMediaSource:null,releaseSharedSVGAAsset:null,releaseSharedVideoPlayback:null,releaseSharedVideoTexture:null,webcodecsPlayback:null,wcPlaybackTime:0,videoTextureHandle:null,vapMixState:null,onComplete:s.onComplete,flyTo:s.flyTo,scaleTo:s.scaleTo,flyDuration:s.flyDuration,easing:(e=s.easing)!=null?e:Ls,onFlyComplete:s.onFlyComplete},r={id:n,type:s.type,pause:()=>{a.paused=!0},resume:()=>{a.paused=!1},destroy:()=>{this.removeGift(n)}};this.manager.addSlot(a,r);try{switch(s.type){case"svga":await this.loadSVGA(a);break;case"vap":await this.loadVAP(a,s);break;case"alphaVideo":await this.loadAlphaVideo(a,s);break}a.state="playing",a.webcodecsPlayback&&console.info("[GiftStage:WC] gift playing",{giftId:a.id,type:a.type,layoutBufPx:{x:a.x,y:a.y,w:a.width,h:a.height}})}catch(E){a.state="finished",(I=(g=this.options).onError)==null||I.call(g,E instanceof Error?E:new Error(String(E)))}return r}async loadSVGA(A){var t,e,g;const I=typeof A.options.source=="string"?A.options.source:null;if(I){const a=await this.acquireSharedSVGAEntity(I);if(A.state==="finished"){a.release();return}A.releaseSharedSVGAAsset=a.release,await this.finishLoadSVGA(A,a.entity,a.audioBlobs);return}const{movie:s,imageBitmaps:n,audioBlobs:B}=await IB(A.options.source,{useWorker:this.options.svgaParseInWorker!==!1});if(A.state==="finished")return;const C=await this.buildSVGAAtlasIfNeeded(n,!!I);if(A.state==="finished"){for(const a of n.values())a.close();return}const Q={params:s.params,sprites:s.sprites,audios:s.audios,atlasTextureHandle:(t=C==null?void 0:C.textureHandle)!=null?t:null,atlasTextureHandles:(e=C==null?void 0:C.textureHandles)!=null?e:[],atlasRects:(g=C==null?void 0:C.rects)!=null?g:new Map,audioBuffers:new Map};await this.finishLoadSVGA(A,Q,B),I&&(C!=null&&C.encodedPngBytesPromises)&&Promise.all(C.encodedPngBytesPromises).then(a=>{a.some(r=>!r)||Xn(I,{params:s.params,sprites:s.sprites,audios:s.audios,audioBlobs:B,atlasRects:C.rects,atlasPages:C.pages.map((r,E)=>({width:r.atlasWidth,height:r.atlasHeight,imageBytes:a[E]}))})});for(const a of n.values())a.close()}async acquireSharedSVGAEntity(A){const t=A,e=this.sharedSVGAEntities.get(t);if(e)return e.refs++,{entity:e.entity,audioBlobs:e.audioBlobs,release:()=>this.releaseSharedSVGAEntity(t)};let g=this.pendingSharedSVGAEntities.get(t);g||(g=this.buildSharedSVGAEntity(A),this.pendingSharedSVGAEntities.set(t,g));let I;try{I=await g}catch(n){throw this.pendingSharedSVGAEntities.delete(t),n}let s=this.sharedSVGAEntities.get(t);return s?s.refs++:(s={refs:1,entity:I.entity,audioBlobs:I.audioBlobs},this.sharedSVGAEntities.set(t,s)),this.pendingSharedSVGAEntities.delete(t),{entity:s.entity,audioBlobs:s.audioBlobs,release:()=>this.releaseSharedSVGAEntity(t)}}async buildSharedSVGAEntity(A){var t,e,g,I;const s=await uf(A);if(s){const Q=await this.createCachedSVGAAtlasTextures(s.atlasPages);return{entity:{params:s.params,sprites:s.sprites,audios:s.audios,atlasTextureHandle:(t=Q[0])!=null?t:null,atlasTextureHandles:Q,atlasRects:s.atlasRects,audioBuffers:new Map},audioBlobs:s.audioBlobs}}const{movie:n,imageBitmaps:B,audioBlobs:C}=await IB(A,{useWorker:this.options.svgaParseInWorker!==!1});try{const Q=await this.buildSVGAAtlasIfNeeded(B,!0);return Q!=null&&Q.encodedPngBytesPromises?Promise.all(Q.encodedPngBytesPromises).then(a=>{a.some(r=>!r)||Xn(A,{params:n.params,sprites:n.sprites,audios:n.audios,audioBlobs:C,atlasRects:Q.rects,atlasPages:Q.pages.map((r,E)=>({width:r.atlasWidth,height:r.atlasHeight,imageBytes:a[E]}))})}):Q||Xn(A,{params:n.params,sprites:n.sprites,audios:n.audios,audioBlobs:C,atlasRects:new Map,atlasPages:[]}),{entity:{params:n.params,sprites:n.sprites,audios:n.audios,atlasTextureHandle:(e=Q==null?void 0:Q.textureHandle)!=null?e:null,atlasTextureHandles:(g=Q==null?void 0:Q.textureHandles)!=null?g:[],atlasRects:(I=Q==null?void 0:Q.rects)!=null?I:new Map,audioBuffers:new Map},audioBlobs:C}}finally{for(const Q of B.values())Q.close()}}releaseSharedSVGAEntity(A){const t=this.sharedSVGAEntities.get(A);t&&(t.refs=Math.max(0,t.refs-1),!(t.refs>0)&&(this.deleteSVGAAtlasTextures(t.entity),this.sharedSVGAEntities.delete(A)))}async createCachedSVGAAtlasTextures(A){const t=[];for(const e of A){const g=await createImageBitmap(new Blob([e.imageBytes],{type:"image/png"}),{premultiplyAlpha:"premultiply"}),I=this.backend.createTexture(g,{width:e.width,height:e.height,filter:"linear",premultiplyAlpha:!0,sourcePremultiplied:!0});g.close(),t.push(I)}return t}deleteSVGAAtlasTextures(A){var t,e;const g=(t=A.atlasTextureHandles)!=null&&t.length?A.atlasTextureHandles:A.atlasTextureHandle?[A.atlasTextureHandle]:[],I=new Set;for(const s of g)I.has(s.id)||(I.add(s.id),(e=this.backend)==null||e.deleteTexture(s))}async finishLoadSVGA(A,t,e){let g=0;for(const[I,s]of e)await this.manager.audioManager.decodeAudio(I,s),g++,g<e.size&&await new Promise(n=>{if(typeof requestAnimationFrame=="function"){requestAnimationFrame(()=>n());return}setTimeout(n,0)});A.videoEntity=t,A.options.svgaSlots&&(A.svgaSlotTextures=await hE(this.backend,A.videoEntity,A.options.svgaSlots))}svgaAtlasMaxSize(){var A,t,e;const g=(A=this.options.svgaAtlasMaxSize)!=null?A:OA.SVGA_ATLAS_MAX_SIZE,I=(e=(t=this.backend)==null?void 0:t.maxTextureSize)!=null?e:g;return Math.max(64,Math.min(I,g))}async buildSVGAAtlasIfNeeded(A,t){return A.size===0?null:KC(A,this.backend,this.svgaAtlasMaxSize(),OA.SVGA_ATLAS_PADDING,{captureEncodedPng:t,maxPages:this.svgaAtlasMaxPages()})}svgaAtlasMaxPages(){var A;return Math.max(1,Math.floor((A=this.options.svgaAtlasMaxPages)!=null?A:4))}shouldTryWebCodecs(){return this.options.preferWebCodecs===!0&&typeof VideoDecoder!="undefined"}logPlaybackBackend(A,t,e){const g=t==="webcodecs"?"WebCodecs (mp4box decode)":"HTMLVideoElement",I=e?` — ${e}`:"";console.info(`[GiftStage] ${A.type} [${A.id}]: ${g}${I}`)}async tryWebCodecsPlayback(A){if(!this.shouldTryWebCodecs())return null;try{const t=typeof A=="string"?await Ys(A):A;return await KA.create(t,void 0,{useWorker:this.options.webCodecsDecodeInWorker!==!1,maxDecodedFrames:this.options.webCodecsMaxDecodedFrames,decodeAheadFrames:this.options.webCodecsDecodeAheadFrames})}catch(t){const e=t instanceof Error?t.message:String(t);return console.warn(`[GiftStage] WebCodecs path failed, falling back to <video>: ${e}`),null}}async loadVAP(A,t){var e;if(!t.config)throw new Error("VAP requires config");const g=await iB(t.config);A.vapConfig=g;const I=await fE(g,t.vapSlots);if(I.size>0){const Q=tt(g.info),a=uE(g);let r=null,E=null;try{r=new nf(g,a,I,Q.w,Q.h)}catch(o){console.warn("[GiftStage] VAP mixed rendering fell back to CPU compositor",o),E=wE(Q.w,Q.h)}A.vapMixState={texture:this.backend.createEmptyTexture(Q.w,Q.h,{filter:"linear"}),width:Q.w,height:Q.h,composer:r,frameMap:a,resources:I,lastFrameIndex:-1,lastSourceRef:null,...E!=null?E:{}},A.videoTextureHandle=A.vapMixState.texture}console.info("[GiftStage:WC] loadVAP: try WebCodecs / video…");const n=await this.tryWebCodecsPlayback(t.source);if(n){A.webcodecsPlayback=n,A.wcPlaybackTime=0;const Q=n.getFrameForTime(0);if(!Q)throw n.destroy(),A.webcodecsPlayback=null,new Error("WebCodecs: missing first frame");if(!A.vapMixState){const r=Q.displayWidth||Q.codedWidth,E=Q.displayHeight||Q.codedHeight,{w:o,h}=IC(A,r,E,"vap",g),c=this.manager.tryAllocateWebCodecsAtlas(A.id,o,h,this.options.webCodecsVideoAtlas);if(c)A.videoTextureHandle=c.handle,A.wcAtlasRect=c.rect,this.backend.updateTextureRegion(c.handle,Q,c.rect.x,c.rect.y,o,h);else{const f=this.backend.createTexture(Q,{width:r,height:E,filter:"linear"});A.videoTextureHandle=f}}A.videoElement=null;const a=A.videoTextureHandle;console.info("[GiftStage:WC] VAP texture",{giftId:A.id,atlas:!!A.wcAtlasRect&&!A.vapMixState,mixed:!!A.vapMixState,tex:`${a.width}x${a.height}`,frame0:`${Q.displayWidth}x${Q.displayHeight}`,atlasCell:A.wcAtlasRect?`${A.wcAtlasRect.w}x${A.wcAtlasRect.h}`:void 0}),this.logPlaybackBackend(A,"webcodecs");return}const B=await this.tryAcquireSharedHtmlVideo(A,t);if(B)A.videoElement=B.video,A.releaseSharedVideoPlayback=B.release;else{const Q=await this.acquireVideoPlaybackSource(t.source);A.releaseMediaSource=(e=Q.release)!=null?e:null;const a=this.createVideo(Q.src,t.loop===0);A.videoElement=a}const C=A.videoElement;if(B){await B.ready;const Q=this.acquireSharedVideoTexture(B.cacheKey,C);A.videoTextureHandle=Q.texture,A.releaseSharedVideoTexture=Q.release}else await this.waitVideoReady(C),A.vapMixState||(A.videoTextureHandle=this.backend.createTexture(C,{width:C.videoWidth,height:C.videoHeight,filter:"linear"}));A.releaseSharedVideoPlayback||this.bindVideoLoopOnEnded(A,C),this.logPlaybackBackend(A,"video",this.shouldTryWebCodecs()?"fallback after WebCodecs unavailable or failed":"preferWebCodecs off or no VideoDecoder")}async loadAlphaVideo(A,t){var e;console.info("[GiftStage:WC] loadAlphaVideo: try WebCodecs / video…");const g=await this.tryWebCodecsPlayback(t.source);if(g){A.webcodecsPlayback=g,A.wcPlaybackTime=0;const n=g.getFrameForTime(0);if(!n)throw g.destroy(),A.webcodecsPlayback=null,new Error("WebCodecs: missing first frame");const B=n.displayWidth||n.codedWidth,C=n.displayHeight||n.codedHeight,{w:Q,h:a}=IC(A,B,C,"alphaVideo",void 0),r=this.manager.tryAllocateWebCodecsAtlas(A.id,Q,a,this.options.webCodecsVideoAtlas);if(r)A.videoTextureHandle=r.handle,A.wcAtlasRect=r.rect,this.backend.updateTextureRegion(r.handle,n,r.rect.x,r.rect.y,Q,a);else{const o=this.backend.createTexture(n,{width:B,height:C,filter:"linear"});A.videoTextureHandle=o}A.videoElement=null;const E=A.videoTextureHandle;console.info("[GiftStage:WC] alphaVideo texture",{giftId:A.id,atlas:!!A.wcAtlasRect,tex:`${E.width}x${E.height}`,frame0:`${n.displayWidth}x${n.displayHeight}`,atlasCell:A.wcAtlasRect?`${A.wcAtlasRect.w}x${A.wcAtlasRect.h}`:void 0}),this.logPlaybackBackend(A,"webcodecs");return}const I=await this.tryAcquireSharedHtmlVideo(A,t);if(I)A.videoElement=I.video,A.releaseSharedVideoPlayback=I.release;else{const n=await this.acquireVideoPlaybackSource(t.source);A.releaseMediaSource=(e=n.release)!=null?e:null;const B=this.createVideo(n.src,t.loop===0);A.videoElement=B}const s=A.videoElement;if(I){await I.ready;const n=this.acquireSharedVideoTexture(I.cacheKey,s);A.videoTextureHandle=n.texture,A.releaseSharedVideoTexture=n.release}else await this.waitVideoReady(s),A.videoTextureHandle=this.backend.createTexture(s,{width:s.videoWidth,height:s.videoHeight,filter:"linear"});A.releaseSharedVideoPlayback||this.bindVideoLoopOnEnded(A,s),this.logPlaybackBackend(A,"video",this.shouldTryWebCodecs()?"fallback after WebCodecs unavailable or failed":"preferWebCodecs off or no VideoDecoder")}bindVideoLoopOnEnded(A,t){t.addEventListener("ended",()=>{var e,g,I,s,n;if(A.state!=="playing")return;const B=(e=A.options.loop)!=null?e:1;if(B===0){t.currentTime=0,t.play();return}if(A.loopCount++,A.loopCount>=B){const C={id:A.id,type:A.type,pause:()=>{},resume:()=>{},destroy:()=>this.removeGift(A.id)};if((g=A.onComplete)==null||g.call(A,C),A.flyTo){A.state="fly";const Q=ms(this.canvas);this.manager.postAnimator.startFly(A,A.flyTo.x*Q.sx,A.flyTo.y*Q.sy,(I=A.scaleTo)!=null?I:.3,(s=A.flyDuration)!=null?s:600,(n=A.easing)!=null?n:Ls,()=>{var a;A.state="finished",(a=A.onFlyComplete)==null||a.call(A,C)})}else A.state="finished";return}t.currentTime=0,t.play()})}createVideo(A,t){const e=document.createElement("video");return e.crossOrigin="anonymous",e.autoplay=!0,e.preload="auto",e.muted=!0,e.playsInline=!0,e.setAttribute("playsinline",""),e.setAttribute("webkit-playsinline","true"),e.loop=t,e.style.cssText="display:none;",e.src=A,document.body.appendChild(e),e.load(),e}waitVideoMetadata(A){return new Promise((t,e)=>{const g=()=>{A.removeEventListener("loadedmetadata",I),A.removeEventListener("error",s)},I=()=>{g(),t()},s=()=>{g(),e(new Error("Video metadata load failed"))};A.addEventListener("loadedmetadata",I),A.addEventListener("error",s),A.readyState>=HTMLMediaElement.HAVE_METADATA&&(g(),t())})}async probeVideoNaturalSize(A,t="video"){var e;const g=await this.acquireVideoPlaybackSource(A),I=document.createElement("video");I.crossOrigin="anonymous",I.preload="metadata",I.muted=!0,I.playsInline=!0,I.setAttribute("playsinline",""),I.setAttribute("webkit-playsinline","true"),I.style.cssText="display:none;",I.src=g.src,document.body.appendChild(I),I.load();try{return await this.waitVideoMetadata(I),t==="alphaVideo"?wf(I.videoWidth,I.videoHeight):{width:I.videoWidth,height:I.videoHeight}}finally{try{I.pause(),I.removeAttribute("src"),I.load(),I.remove()}catch(s){}(e=g.release)==null||e.call(g)}}async acquireVideoPlaybackSource(A){if(typeof A!="string"){const g=URL.createObjectURL(new Blob([A]));return{src:g,release:()=>URL.revokeObjectURL(g)}}const t=this.normalizeSharedVideoSourceKey(A);if(!t)return{src:A};let e=this.sharedVideoSources.get(t);e||(e={refs:0,objectUrl:null,promise:this.fetchSharedVideoSource(t)},this.sharedVideoSources.set(t,e)),e.refs++;try{return{src:await e.promise,release:()=>this.releaseSharedVideoSource(t)}}catch(g){return this.releaseSharedVideoSource(t),console.warn("[GiftStage] shared video prefetch failed, using original url",g),{src:A}}}normalizeSharedVideoSourceKey(A){try{const t=new URL(A,window.location.href);return t.protocol==="blob:"||t.protocol==="data:"?null:t.href}catch(t){return null}}async fetchSharedVideoSource(A){const t=new Blob([await Ys(A)]),e=this.sharedVideoSources.get(A),g=URL.createObjectURL(t);return e&&(e.objectUrl=g,e.refs===0&&(URL.revokeObjectURL(g),this.sharedVideoSources.delete(A))),g}releaseSharedVideoSource(A){const t=this.sharedVideoSources.get(A);t&&(t.refs=Math.max(0,t.refs-1),t.refs===0&&t.objectUrl&&(URL.revokeObjectURL(t.objectUrl),this.sharedVideoSources.delete(A)))}async tryAcquireSharedHtmlVideo(A,t){var e,g;if(this.options.shareIdenticalVideoPlayback===!1||t.loop!==0||typeof t.source!="string"||A.type==="vap"&&A.vapMixState)return null;const I=this.normalizeSharedVideoSourceKey(t.source);if(!I)return null;const s=(e=this.options.sharedVideoPlaybackWindowMs)!=null?e:OA.DEFAULT_SHARED_VIDEO_PLAYBACK_WINDOW_MS,n=Math.max(0,s)/1e3,B=this.sharedHtmlVideos.get(I);if(B)return B.video.currentTime>n?null:(B.refs++,{cacheKey:I,video:B.video,ready:B.ready,release:()=>this.releaseSharedHtmlVideo(I)});let C=this.pendingSharedHtmlVideos.get(I);C||(C=(async()=>{var r;const E=await this.acquireVideoPlaybackSource(t.source),o=this.createVideo(E.src,!0),h=this.waitVideoReady(o);return{video:o,ready:h,releaseSource:(r=E.release)!=null?r:null}})(),this.pendingSharedHtmlVideos.set(I,C));let Q;try{Q=await C}catch(r){throw this.pendingSharedHtmlVideos.delete(I),r}let a=this.sharedHtmlVideos.get(I);return a?a.refs++:(a={refs:1,video:Q.video,ready:Q.ready,releaseSource:(g=Q.releaseSource)!=null?g:null},this.sharedHtmlVideos.set(I,a)),this.pendingSharedHtmlVideos.delete(I),{cacheKey:I,video:a.video,ready:a.ready,release:()=>this.releaseSharedHtmlVideo(I)}}acquireSharedVideoTexture(A,t){const e=this.sharedVideoTextures.get(A);if(e)return e.refs++,{texture:e.texture,release:()=>this.releaseSharedVideoTexture(A)};const g=this.backend.createTexture(t,{width:t.videoWidth,height:t.videoHeight,filter:"linear"});return this.sharedVideoTextures.set(A,{refs:1,texture:g}),{texture:g,release:()=>this.releaseSharedVideoTexture(A)}}releaseSharedVideoTexture(A){var t;const e=this.sharedVideoTextures.get(A);e&&(e.refs=Math.max(0,e.refs-1),!(e.refs>0)&&((t=this.backend)==null||t.deleteTexture(e.texture),this.sharedVideoTextures.delete(A)))}releaseSharedHtmlVideo(A){var t;const e=this.sharedHtmlVideos.get(A);if(e&&(e.refs=Math.max(0,e.refs-1),!(e.refs>0))){try{e.video.pause(),e.video.removeAttribute("src"),e.video.load(),e.video.remove()}catch(g){}(t=e.releaseSource)==null||t.call(e),this.sharedHtmlVideos.delete(A)}}waitForFirstVideoFrame(A){return new Promise(t=>{typeof A.requestVideoFrameCallback=="function"?A.requestVideoFrameCallback(()=>t()):requestAnimationFrame(()=>t())})}waitVideoReady(A){return new Promise((t,e)=>{const g=()=>{A.removeEventListener("canplay",s),A.removeEventListener("error",n)},I=async()=>{try{await A.play(),await this.waitForFirstVideoFrame(A),t()}catch(B){e(B)}},s=()=>{g(),I()},n=()=>{g(),e(new Error("Video load failed"))};A.addEventListener("canplay",s),A.addEventListener("error",n),A.readyState>=HTMLMediaElement.HAVE_FUTURE_DATA&&(g(),I())})}removeGift(A){var t;(t=this.manager)==null||t.removeSlot(A)}removeAll(){if(this.manager)for(const A of this.manager.getSlotIds())this.manager.removeSlot(A)}pause(){var A;(A=this.manager)==null||A.pause()}resume(){var A;(A=this.manager)==null||A.resume()}get activeCount(){var A,t;return(t=(A=this.manager)==null?void 0:A.activeCount)!=null?t:0}get backendType(){var A,t;return(t=(A=this.backend)==null?void 0:A.type)!=null?t:"webgl2"}destroy(){var A,t;this.destroyed=!0,(A=this.resizeObserver)==null||A.disconnect(),this.resizeObserver=null,window.removeEventListener("resize",this.onWindowLayout),window.removeEventListener("orientationchange",this.onWindowLayout),(t=this.manager)==null||t.destroy();for(const[e]of this.sharedHtmlVideos)this.releaseSharedHtmlVideo(e);for(const[e]of this.sharedSVGAEntities)this.releaseSharedSVGAEntity(e);for(const[e]of this.sharedVideoTextures)this.releaseSharedVideoTexture(e);this.pendingSharedSVGAEntities.clear(),this.pendingSharedHtmlVideos.clear();for(const[e,g]of this.sharedVideoSources)g.objectUrl&&URL.revokeObjectURL(g.objectUrl),this.sharedVideoSources.delete(e);this.canvas.remove(),this.backend=null,this.manager=null}};z(rs,"SVGA_ATLAS_MAX_SIZE",2048);z(rs,"SVGA_ATLAS_PADDING",2);z(rs,"DEFAULT_SHARED_VIDEO_PLAYBACK_WINDOW_MS",100);z(rs,"DEFAULT_RESOURCE_CACHE_MEMORY_BYTES",128*1024*1024);z(rs,"DEFAULT_RESOURCE_CACHE_PERSISTENT_BYTES",2048*1024*1024);let mf=rs;exports.AlphaVideoRenderer=NC;exports.AudioManager=UC;exports.BufferPool=uC;exports.GiftStage=mf;exports.PostAnimator=RC;exports.RenderManager=MC;exports.SVGABatchRenderer=DC;exports.VAPRenderer=SC;exports.WebCodecsMp4Playback=KA;exports.WebGL1Backend=Pn;exports.WebGL2Backend=QC;exports.WebGPUBackend=CC;exports.buildAtlas=KC;exports.buildBatchVerticesJS=Xs;exports.canSharedArrayBuffer=aa;exports.canWebGL1=Qa;exports.canWebGL2=Ca;exports.canWebGPU=Ba;exports.clamp=AB;exports.createBackend=EC;exports.decodeSVGAProto=YC;exports.easeInOutCubic=bE;exports.easeInOutQuad=xE;exports.easeInQuad=kE;exports.easeOutBack=YE;exports.easeOutCubic=Ls;exports.easeOutQuad=ME;exports.generateId=wC;exports.inflateJS=xC;exports.initWasm=fC;exports.isWasmReady=At;exports.lerp=$s;exports.linear=UE;exports.ortho=vA;exports.parseSVGA=IB;exports.parseVAPConfig=iB;
|
|
8608
|
+
`;function sf(i,A,t,e){const g=new Float32Array(t.size*dn),I=A.get(e);if(!I||I.length===0)return g;const[s,n]=i.info.rgbFrame;let B=0;for(const C of I){const Q=t.get(String(C.srcId));if(!Q)continue;if(B+dn>g.length)break;const[a,r,E,o]=C.frame,[h,c,f,l]=C.mFrame,u=fn(a+s,r+n,E,o,i.info.videoW,i.info.videoH),w=fn(h,c,f,l,i.info.videoW,i.info.videoH);g[B++]=Q,g[B++]=u[0],g[B++]=u[1],g[B++]=u[2],g[B++]=u[3],g[B++]=w[0],g[B++]=w[1],g[B++]=w[2],g[B++]=w[3]}return g}class nf{constructor(A,t,e,g,I){this.config=A,this.frameMap=t,dA(this,"canvas"),dA(this,"gl"),dA(this,"program"),dA(this,"vertexShader"),dA(this,"fragmentShader"),dA(this,"vertexBuffer"),dA(this,"videoTexture"),dA(this,"imagePosLoc"),dA(this,"resourceEntries"),dA(this,"textureUnitBySrcId",new Map);var s;if(e.size===0)throw new Error("VAP mix composer requires at least one slot resource");this.canvas=Af(g,I);const n=this.canvas.getContext("webgl2",{alpha:!0,antialias:!1,depth:!1,premultipliedAlpha:!1,preserveDrawingBuffer:!1,stencil:!1})||this.canvas.getContext("webgl",{alpha:!0,antialias:!1,depth:!1,premultipliedAlpha:!1,preserveDrawingBuffer:!1,stencil:!1})||this.canvas.getContext("experimental-webgl");if(!n)throw new Error("VAP mixed rendering requires WebGL support");this.gl=n;const B=Math.max(1,n.getParameter(n.MAX_TEXTURE_IMAGE_UNITS)),C=Math.max(0,B-1),Q=[];let a=1;for(const u of(s=A.src)!=null?s:[]){const w=e.get(String(u.srcId));if(!w)continue;if(a>C){console.warn("[GiftStage] VAP mixed resources exceed texture unit limit, skipping",{srcId:u.srcId,srcTag:u.srcTag,maxUnits:B});continue}const y=nC(n,a,w.source,!0),p={srcId:String(u.srcId),unit:a,texture:y};Q.push(p),this.textureUnitBySrcId.set(p.srcId,p.unit),a++}if(Q.length===0)throw new Error("VAP mixed rendering could not bind any slot resources");this.resourceEntries=Q;const r=tf(n,If,gf(this.resourceEntries.length));this.program=r.program,this.vertexShader=r.vertexShader,this.fragmentShader=r.fragmentShader,this.imagePosLoc=n.getUniformLocation(this.program,"image_pos"),this.videoTexture=nC(n,0);const E=n.getUniformLocation(this.program,"u_image_video");E!==null&&n.uniform1i(E,0);for(const u of this.resourceEntries){const w=n.getUniformLocation(this.program,`u_image${u.unit}`);w!==null&&n.uniform1i(w,u.unit)}const o=n.createBuffer();if(!o)throw new Error("Failed to create VAP mix vertex buffer");this.vertexBuffer=o,n.bindBuffer(n.ARRAY_BUFFER,this.vertexBuffer),n.bufferData(n.ARRAY_BUFFER,ef(A.info),n.STATIC_DRAW);const h=6*Float32Array.BYTES_PER_ELEMENT,c=n.getAttribLocation(this.program,"a_position"),f=n.getAttribLocation(this.program,"a_texCoord"),l=n.getAttribLocation(this.program,"a_alpha_texCoord");n.enableVertexAttribArray(c),n.vertexAttribPointer(c,2,n.FLOAT,!1,h,0),n.enableVertexAttribArray(f),n.vertexAttribPointer(f,2,n.FLOAT,!1,h,2*Float32Array.BYTES_PER_ELEMENT),n.enableVertexAttribArray(l),n.vertexAttribPointer(l,2,n.FLOAT,!1,h,4*Float32Array.BYTES_PER_ELEMENT),n.viewport(0,0,this.canvas.width,this.canvas.height),n.disable(n.BLEND),n.disable(n.DEPTH_TEST),n.clearColor(0,0,0,0),n.pixelStorei(n.UNPACK_PREMULTIPLY_ALPHA_WEBGL,0),n.pixelStorei(n.UNPACK_COLORSPACE_CONVERSION_WEBGL,n.NONE)}render(A,t){const e=this.gl;e.useProgram(this.program),e.activeTexture(e.TEXTURE0),e.bindTexture(e.TEXTURE_2D,this.videoTexture),e.pixelStorei(e.UNPACK_FLIP_Y_WEBGL,1),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,A),e.pixelStorei(e.UNPACK_FLIP_Y_WEBGL,0);const g=sf(this.config,this.frameMap,this.textureUnitBySrcId,t);return this.imagePosLoc!==null&&e.uniform1fv(this.imagePosLoc,g),e.clear(e.COLOR_BUFFER_BIT),e.drawArrays(e.TRIANGLE_STRIP,0,4),this.canvas}destroy(){const A=this.gl;for(const e of this.resourceEntries)A.deleteTexture(e.texture);A.deleteTexture(this.videoTexture),A.deleteBuffer(this.vertexBuffer),A.deleteProgram(this.program),A.deleteShader(this.vertexShader),A.deleteShader(this.fragmentShader);const t=A.getExtension("WEBGL_lose_context");t==null||t.loseContext()}}var Bf=Object.defineProperty,Cf=(i,A,t)=>A in i?Bf(i,A,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[A]=t,mA=(i,A,t)=>Cf(i,typeof A!="symbol"?A+"":A,t);const MQ=1397178947,xQ=12,Qf=8;class af{constructor(A=1024){mA(this,"buffer"),mA(this,"view"),mA(this,"bytes"),mA(this,"offset",0),mA(this,"encoder",new TextEncoder),this.buffer=new ArrayBuffer(A),this.view=new DataView(this.buffer),this.bytes=new Uint8Array(this.buffer)}ensure(A){const t=this.offset+A;if(t<=this.buffer.byteLength)return;let e=this.buffer.byteLength;for(;e<t;)e*=2;const g=new ArrayBuffer(e);new Uint8Array(g).set(this.bytes.subarray(0,this.offset)),this.buffer=g,this.view=new DataView(this.buffer),this.bytes=new Uint8Array(this.buffer)}u8(A){this.ensure(1),this.view.setUint8(this.offset,A),this.offset+=1}u16(A){this.ensure(2),this.view.setUint16(this.offset,A,!0),this.offset+=2}u32(A){this.ensure(4),this.view.setUint32(this.offset,A,!0),this.offset+=4}f32(A){this.ensure(4),this.view.setFloat32(this.offset,A,!0),this.offset+=4}bytesValue(A){this.u32(A.byteLength),this.ensure(A.byteLength),this.bytes.set(A,this.offset),this.offset+=A.byteLength}string(A){this.bytesValue(this.encoder.encode(A))}finish(){return this.buffer.slice(0,this.offset)}}class Ef{constructor(A){mA(this,"view"),mA(this,"bytes"),mA(this,"offset",0),mA(this,"decoder",new TextDecoder),this.view=new DataView(A),this.bytes=new Uint8Array(A)}u8(){const A=this.view.getUint8(this.offset);return this.offset+=1,A}u16(){const A=this.view.getUint16(this.offset,!0);return this.offset+=2,A}u32(){const A=this.view.getUint32(this.offset,!0);return this.offset+=4,A}f32(){const A=this.view.getFloat32(this.offset,!0);return this.offset+=4,A}bytesValue(){const A=this.u32(),t=this.bytes.slice(this.offset,this.offset+A);return this.offset+=A,t}string(){return this.decoder.decode(this.bytesValue())}}function rf(i){var A,t;let e=0;for(const g of i.atlasPages)e+=g.imageBytes.byteLength;for(const[g,I]of i.audioBlobs)e+=g.length*2+I.byteLength;for(const[g]of i.atlasRects)e+=g.length*2+20;for(const g of i.sprites){e+=g.imageKey.length*2+g.frames.length*64;for(const I of g.frames)if(e+=I.clipPath.length*2,I.shapes)for(const s of I.shapes)e+=((t=(A=s.pathData)==null?void 0:A.length)!=null?t:0)*2+96}return e}function of(i,A){var t;let e=0;A.fill&&(e|=1),A.stroke&&(e|=2),i.u8(e),A.fill&&(i.f32(A.fill.r),i.f32(A.fill.g),i.f32(A.fill.b),i.f32(A.fill.a)),A.stroke&&(i.f32(A.stroke.r),i.f32(A.stroke.g),i.f32(A.stroke.b),i.f32(A.stroke.a)),i.f32((t=A.strokeWidth)!=null?t:0)}function hf(i){const A=i.u8(),t=A&1?{r:i.f32(),g:i.f32(),b:i.f32(),a:i.f32()}:null,e=A&2?{r:i.f32(),g:i.f32(),b:i.f32(),a:i.f32()}:null;return{fill:t,stroke:e,strokeWidth:i.f32()}}function cf(i,A){var t,e,g,I;i.u8(A.kind==="path"?0:A.kind==="rect"?1:2),i.string((t=A.pathData)!=null?t:"");const s=(e=A.rect)!=null?e:{x:0,y:0,width:0,height:0,cornerRadius:0};i.f32(s.x),i.f32(s.y),i.f32(s.width),i.f32(s.height),i.f32((g=s.cornerRadius)!=null?g:0);const n=(I=A.ellipse)!=null?I:{x:0,y:0,radiusX:0,radiusY:0};i.f32(n.x),i.f32(n.y),i.f32(n.radiusX),i.f32(n.radiusY),of(i,A.style),i.f32(A.transform.a),i.f32(A.transform.b),i.f32(A.transform.c),i.f32(A.transform.d),i.f32(A.transform.tx),i.f32(A.transform.ty)}function lf(i){const A=i.u8(),t=i.string(),e={x:i.f32(),y:i.f32(),width:i.f32(),height:i.f32(),cornerRadius:i.f32()},g={x:i.f32(),y:i.f32(),radiusX:i.f32(),radiusY:i.f32()},I=hf(i),s={a:i.f32(),b:i.f32(),c:i.f32(),d:i.f32(),tx:i.f32(),ty:i.f32()};return A===1?{kind:"rect",rect:e,style:I,transform:s}:A===2?{kind:"ellipse",ellipse:g,style:I,transform:s}:{kind:"path",pathData:t,style:I,transform:s}}function BC(i){const A=new Uint8Array(i.byteLength);return A.set(i),A.buffer}function df(i){var A;const t=new af(Qf+i.atlasPages.reduce((e,g)=>e+g.imageBytes.byteLength+12,0)+i.sprites.length*64+i.audioBlobs.size*32+i.atlasRects.size*32);t.u32(MQ),t.u16(xQ),t.u16(0),t.f32(i.params.viewBoxWidth),t.f32(i.params.viewBoxHeight),t.f32(i.params.fps),t.u32(i.params.frames),t.u32(i.atlasPages.length);for(const e of i.atlasPages)t.u32(e.width),t.u32(e.height),t.bytesValue(new Uint8Array(e.imageBytes));t.u32(i.atlasRects.size);for(const[e,g]of i.atlasRects)t.string(e),t.u32((A=g.textureIndex)!=null?A:0),t.f32(g.x),t.f32(g.y),t.f32(g.w),t.f32(g.h);t.u32(i.audios.length);for(const e of i.audios)t.string(e.audioKey),t.u32(e.startFrame),t.u32(e.endFrame),t.f32(e.startTime),t.f32(e.totalTime);t.u32(i.audioBlobs.size);for(const[e,g]of i.audioBlobs)t.string(e),t.bytesValue(new Uint8Array(g));t.u32(i.sprites.length);for(const e of i.sprites){t.string(e.imageKey),t.u32(e.frames.length);for(const g of e.frames){let I=0;if(g.layout&&(I|=1),g.transform&&(I|=2),g.clipPath&&(I|=4),g.shapes&&g.shapes.length>0&&(I|=8),t.u8(I),t.f32(g.alpha),g.layout&&(t.f32(g.layout.x),t.f32(g.layout.y),t.f32(g.layout.width),t.f32(g.layout.height)),g.transform&&(t.f32(g.transform.a),t.f32(g.transform.b),t.f32(g.transform.c),t.f32(g.transform.d),t.f32(g.transform.tx),t.f32(g.transform.ty)),g.clipPath&&t.string(g.clipPath),g.shapes&&g.shapes.length>0){t.u32(g.shapes.length);for(const s of g.shapes)cf(t,s)}}}return t.finish()}function ff(i){const A=new Ef(i);if(A.u32()!==MQ)throw new Error("Invalid SVGA cache binary magic");if(A.u16()!==xQ)throw new Error("Unsupported SVGA cache binary version");A.u16();const t={viewBoxWidth:A.f32(),viewBoxHeight:A.f32(),fps:A.f32(),frames:A.u32()},e=A.u32(),g=[];for(let E=0;E<e;E++)g.push({width:A.u32(),height:A.u32(),imageBytes:BC(A.bytesValue())});const I=A.u32(),s=new Map;for(let E=0;E<I;E++){const o=A.string(),h=A.u32();s.set(o,{textureIndex:h,x:A.f32(),y:A.f32(),w:A.f32(),h:A.f32()})}const n=A.u32(),B=[];for(let E=0;E<n;E++)B.push({audioKey:A.string(),startFrame:A.u32(),endFrame:A.u32(),startTime:A.f32(),totalTime:A.f32()});const C=A.u32(),Q=new Map;for(let E=0;E<C;E++){const o=A.string();Q.set(o,BC(A.bytesValue()))}const a=A.u32(),r=[];for(let E=0;E<a;E++){const o=A.string(),h=A.u32(),c=[];for(let f=0;f<h;f++){const l=A.u8(),u=A.f32(),w=l&1?{x:A.f32(),y:A.f32(),width:A.f32(),height:A.f32()}:null,y=l&2?{a:A.f32(),b:A.f32(),c:A.f32(),d:A.f32(),tx:A.f32(),ty:A.f32()}:null,p=l&4?A.string():"";let m=null;if(l&8){const N=A.u32();m=[];for(let R=0;R<N;R++)m.push(lf(A))}c.push({alpha:u,layout:w,transform:y,clipPath:p,shapes:m})}r.push({imageKey:o,frames:c})}return{params:t,sprites:r,audios:B,audioBlobs:Q,atlasRects:s,atlasPages:g}}function EB(i){return`svga-asset:${i}`}function rB(i){return`svga-asset-bin:${i}`}async function uf(i){const A=eE(EB(i),"svga-asset");if(A)return A;const t=await iE(rB(i));if(!t)return null;try{const e=ff(t);return yC(EB(i),"svga-asset",e,t.byteLength),e}catch(e){return IE(rB(i)),null}}async function Xn(i,A){const t=df(A);yC(EB(i),"svga-asset",A,Math.max(t.byteLength,rf(A))),await gE(rB(i),t)}function xA(i){return typeof i=="number"&&Number.isFinite(i)&&i>0}function wf(i,A){const t=xA(i)?i*.5:0,e=xA(A)?A:0;return{width:t,height:e}}function Df(i,A,t,e){const g=xA(A.width)?A.width:void 0,I=xA(A.height)?A.height:void 0,s=xA(i.width)?i.width:void 0,n=xA(i.height)?i.height:void 0,B=i.useOriginalSize===!0;if(s&&n)return{width:s,height:n};if(s)return g&&I?{width:s,height:s*I/g}:{width:s,height:s};if(n)return g&&I?{width:n*g/I,height:n}:{width:n,height:n};let C=g!=null?g:0,Q=I!=null?I:0;if(C>0&&Q>0&&xA(t)&&xA(e)&&(!B||C>t||Q>e)){const a=Math.min(t/C,e/Q);C*=a,Q*=a}return{width:C,height:Q}}var yf=Object.defineProperty,pf=(i,A,t)=>A in i?yf(i,A,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[A]=t,z=(i,A,t)=>pf(i,typeof A!="symbol"?A+"":A,t);const rs=class OA{constructor(A){z(this,"sharedSVGAEntities",new Map),z(this,"pendingSharedSVGAEntities",new Map),z(this,"sharedVideoSources",new Map),z(this,"sharedHtmlVideos",new Map),z(this,"sharedVideoTextures",new Map),z(this,"pendingSharedHtmlVideos",new Map),z(this,"canvas"),z(this,"backend",null),z(this,"manager",null),z(this,"options"),z(this,"ready"),z(this,"destroyed",!1),z(this,"resizeObserver",null),z(this,"nextRenderOrder",0),z(this,"onWindowLayout",()=>{this.destroyed||this.syncCanvasSize()}),this.options=A,this.canvas=document.createElement("canvas"),this.canvas.style.cssText="position:fixed;inset:0;width:100vw;height:100vh;pointer-events:none;z-index:9999;",A.container.appendChild(this.canvas),this.ready=this.init()}backingStoreDpr(){const A=this.options.resolution;return A!=null&&A>0&&Number.isFinite(A)?A:typeof window!="undefined"&&window.devicePixelRatio>0?window.devicePixelRatio:1}syncCanvasSize(){var A;ra(this.canvas,this.backingStoreDpr()),(A=this.manager)==null||A.syncSlotLayoutFromOptions()}resourcePipelineConcurrency(){if(this.options.maxConcurrentParse!=null)return Math.max(1,Math.max(4,this.options.maxConcurrentParse));const A=typeof navigator!="undefined"&&typeof navigator.hardwareConcurrency=="number"?navigator.hardwareConcurrency/2:4;return Math.max(1,Math.max(4,A))}async init(){var A;this.syncCanvasSize(),await new Promise(I=>requestAnimationFrame(()=>I())),this.syncCanvasSize(),AE({maxConcurrent:this.resourcePipelineConcurrency(),maxMemoryBytes:OA.DEFAULT_RESOURCE_CACHE_MEMORY_BYTES,maxPersistentBytes:OA.DEFAULT_RESOURCE_CACHE_PERSISTENT_BYTES});const t=(A=this.options.preferWebGPU)!=null?A:!0,e=this.options.antialias,[g]=await Promise.all([EC(this.canvas,t,e,this.options.forceWebGL1===!0),fC()]);this.backend=g,this.manager=new MC(this.backend),this.resizeObserver=new ResizeObserver(()=>{this.destroyed||this.syncCanvasSize()}),this.resizeObserver.observe(this.canvas),window.addEventListener("resize",this.onWindowLayout),window.addEventListener("orientationchange",this.onWindowLayout)}currentCanvasCssSize(){const A=this.canvas.clientWidth||this.options.container.clientWidth||window.innerWidth||0,t=this.canvas.clientHeight||this.options.container.clientHeight||window.innerHeight||0;return{width:A,height:t}}async resolveGiftIntrinsicSize(A){switch(A.type){case"svga":{const t=await pr(A.source,{useWorker:this.options.svgaParseInWorker!==!1});return{width:t.viewBoxWidth,height:t.viewBoxHeight}}case"vap":{if(!A.config)throw new Error("VAP requires config");const t=await iB(A.config),e=tt(t.info);return{width:e.w,height:e.h}}case"alphaVideo":return this.probeVideoNaturalSize(A.source,"alphaVideo")}}async normalizeAddGiftOptions(A){if(A.width!=null&&A.height!=null)return A;const t=await this.resolveGiftIntrinsicSize(A),e=this.currentCanvasCssSize(),g=Df({width:A.width,height:A.height,useOriginalSize:A.useOriginalSize},t,e.width,e.height);return{...A,width:g.width,height:g.height}}async addGift(A){var t,e,g,I;if(console.info("[GiftStage] addGift await ready…",A.type),await this.ready,console.info("[GiftStage] addGift ready, loading…",A.type),this.destroyed)throw new Error("GiftStage is destroyed");const s=await this.normalizeAddGiftOptions(A),n=wC(),{sx:B,sy:C}=ms(this.canvas),Q=kC(s.x,s.y,s.width,s.height,this.canvas.clientWidth,this.canvas.clientHeight),a={id:n,type:s.type,state:"loading",options:s,zIndex:(t=s.zIndex)!=null?t:0,renderOrder:this.nextRenderOrder++,x:Q.x*B,y:Q.y*C,width:s.width*B,height:s.height*C,scale:1,paused:!1,currentFrame:0,accumulatedTime:0,lastTickTime:0,loopCount:0,videoEntity:null,svgaSlotTextures:void 0,videoElement:null,releaseMediaSource:null,releaseSharedSVGAAsset:null,releaseSharedVideoPlayback:null,releaseSharedVideoTexture:null,webcodecsPlayback:null,wcPlaybackTime:0,videoTextureHandle:null,vapMixState:null,onComplete:s.onComplete,flyTo:s.flyTo,scaleTo:s.scaleTo,flyDuration:s.flyDuration,easing:(e=s.easing)!=null?e:Ls,onFlyComplete:s.onFlyComplete},r={id:n,type:s.type,pause:()=>{a.paused=!0},resume:()=>{a.paused=!1},destroy:()=>{this.removeGift(n)}};this.manager.addSlot(a,r);try{switch(s.type){case"svga":await this.loadSVGA(a);break;case"vap":await this.loadVAP(a,s);break;case"alphaVideo":await this.loadAlphaVideo(a,s);break}a.state="playing",s.type==="svga"&&a.videoEntity&&this.manager.audioManager.tickFrame(a,a.currentFrame,r),a.webcodecsPlayback&&console.info("[GiftStage:WC] gift playing",{giftId:a.id,type:a.type,layoutBufPx:{x:a.x,y:a.y,w:a.width,h:a.height}})}catch(E){a.state="finished",(I=(g=this.options).onError)==null||I.call(g,E instanceof Error?E:new Error(String(E)))}return r}async loadSVGA(A){var t,e,g;const I=typeof A.options.source=="string"?A.options.source:null;if(I){const a=await this.acquireSharedSVGAEntity(I);if(A.state==="finished"){a.release();return}A.releaseSharedSVGAAsset=a.release,await this.finishLoadSVGA(A,a.entity,a.audioBlobs);return}const{movie:s,imageBitmaps:n,audioBlobs:B}=await IB(A.options.source,{useWorker:this.options.svgaParseInWorker!==!1});if(A.state==="finished")return;const C=await this.buildSVGAAtlasIfNeeded(n,!!I);if(A.state==="finished"){for(const a of n.values())a.close();return}const Q={params:s.params,sprites:s.sprites,audios:s.audios,atlasTextureHandle:(t=C==null?void 0:C.textureHandle)!=null?t:null,atlasTextureHandles:(e=C==null?void 0:C.textureHandles)!=null?e:[],atlasRects:(g=C==null?void 0:C.rects)!=null?g:new Map,audioBuffers:new Map};await this.finishLoadSVGA(A,Q,B),I&&(C!=null&&C.encodedPngBytesPromises)&&Promise.all(C.encodedPngBytesPromises).then(a=>{a.some(r=>!r)||Xn(I,{params:s.params,sprites:s.sprites,audios:s.audios,audioBlobs:B,atlasRects:C.rects,atlasPages:C.pages.map((r,E)=>({width:r.atlasWidth,height:r.atlasHeight,imageBytes:a[E]}))})});for(const a of n.values())a.close()}async acquireSharedSVGAEntity(A){const t=A,e=this.sharedSVGAEntities.get(t);if(e)return e.refs++,{entity:e.entity,audioBlobs:e.audioBlobs,release:()=>this.releaseSharedSVGAEntity(t)};let g=this.pendingSharedSVGAEntities.get(t);g||(g=this.buildSharedSVGAEntity(A),this.pendingSharedSVGAEntities.set(t,g));let I;try{I=await g}catch(n){throw this.pendingSharedSVGAEntities.delete(t),n}let s=this.sharedSVGAEntities.get(t);return s?s.refs++:(s={refs:1,entity:I.entity,audioBlobs:I.audioBlobs},this.sharedSVGAEntities.set(t,s)),this.pendingSharedSVGAEntities.delete(t),{entity:s.entity,audioBlobs:s.audioBlobs,release:()=>this.releaseSharedSVGAEntity(t)}}async buildSharedSVGAEntity(A){var t,e,g,I;const s=await uf(A);if(s){const Q=await this.createCachedSVGAAtlasTextures(s.atlasPages);return{entity:{params:s.params,sprites:s.sprites,audios:s.audios,atlasTextureHandle:(t=Q[0])!=null?t:null,atlasTextureHandles:Q,atlasRects:s.atlasRects,audioBuffers:new Map},audioBlobs:s.audioBlobs}}const{movie:n,imageBitmaps:B,audioBlobs:C}=await IB(A,{useWorker:this.options.svgaParseInWorker!==!1});try{const Q=await this.buildSVGAAtlasIfNeeded(B,!0);return Q!=null&&Q.encodedPngBytesPromises?Promise.all(Q.encodedPngBytesPromises).then(a=>{a.some(r=>!r)||Xn(A,{params:n.params,sprites:n.sprites,audios:n.audios,audioBlobs:C,atlasRects:Q.rects,atlasPages:Q.pages.map((r,E)=>({width:r.atlasWidth,height:r.atlasHeight,imageBytes:a[E]}))})}):Q||Xn(A,{params:n.params,sprites:n.sprites,audios:n.audios,audioBlobs:C,atlasRects:new Map,atlasPages:[]}),{entity:{params:n.params,sprites:n.sprites,audios:n.audios,atlasTextureHandle:(e=Q==null?void 0:Q.textureHandle)!=null?e:null,atlasTextureHandles:(g=Q==null?void 0:Q.textureHandles)!=null?g:[],atlasRects:(I=Q==null?void 0:Q.rects)!=null?I:new Map,audioBuffers:new Map},audioBlobs:C}}finally{for(const Q of B.values())Q.close()}}releaseSharedSVGAEntity(A){const t=this.sharedSVGAEntities.get(A);t&&(t.refs=Math.max(0,t.refs-1),!(t.refs>0)&&(this.deleteSVGAAtlasTextures(t.entity),this.sharedSVGAEntities.delete(A)))}async createCachedSVGAAtlasTextures(A){const t=[];for(const e of A){const g=await createImageBitmap(new Blob([e.imageBytes],{type:"image/png"}),{premultiplyAlpha:"premultiply"}),I=this.backend.createTexture(g,{width:e.width,height:e.height,filter:"linear",premultiplyAlpha:!0,sourcePremultiplied:!0});g.close(),t.push(I)}return t}deleteSVGAAtlasTextures(A){var t,e;const g=(t=A.atlasTextureHandles)!=null&&t.length?A.atlasTextureHandles:A.atlasTextureHandle?[A.atlasTextureHandle]:[],I=new Set;for(const s of g)I.has(s.id)||(I.add(s.id),(e=this.backend)==null||e.deleteTexture(s))}async finishLoadSVGA(A,t,e){let g=0;for(const[I,s]of e)await this.manager.audioManager.decodeAudio(I,s),g++,g<e.size&&await new Promise(n=>{if(typeof requestAnimationFrame=="function"){requestAnimationFrame(()=>n());return}setTimeout(n,0)});A.videoEntity=t,A.options.svgaSlots&&(A.svgaSlotTextures=await hE(this.backend,A.videoEntity,A.options.svgaSlots))}svgaAtlasMaxSize(){var A,t,e;const g=(A=this.options.svgaAtlasMaxSize)!=null?A:OA.SVGA_ATLAS_MAX_SIZE,I=(e=(t=this.backend)==null?void 0:t.maxTextureSize)!=null?e:g;return Math.max(64,Math.min(I,g))}async buildSVGAAtlasIfNeeded(A,t){return A.size===0?null:KC(A,this.backend,this.svgaAtlasMaxSize(),OA.SVGA_ATLAS_PADDING,{captureEncodedPng:t,maxPages:this.svgaAtlasMaxPages()})}svgaAtlasMaxPages(){var A;return Math.max(1,Math.floor((A=this.options.svgaAtlasMaxPages)!=null?A:4))}shouldTryWebCodecs(){return this.options.preferWebCodecs===!0&&typeof VideoDecoder!="undefined"}logPlaybackBackend(A,t,e){const g=t==="webcodecs"?"WebCodecs (mp4box decode)":"HTMLVideoElement",I=e?` — ${e}`:"";console.info(`[GiftStage] ${A.type} [${A.id}]: ${g}${I}`)}async tryWebCodecsPlayback(A){if(!this.shouldTryWebCodecs())return null;try{const t=typeof A=="string"?await Ys(A):A;return await KA.create(t,void 0,{useWorker:this.options.webCodecsDecodeInWorker!==!1,maxDecodedFrames:this.options.webCodecsMaxDecodedFrames,decodeAheadFrames:this.options.webCodecsDecodeAheadFrames})}catch(t){const e=t instanceof Error?t.message:String(t);return console.warn(`[GiftStage] WebCodecs path failed, falling back to <video>: ${e}`),null}}async loadVAP(A,t){var e;if(!t.config)throw new Error("VAP requires config");const g=await iB(t.config);A.vapConfig=g;const I=await fE(g,t.vapSlots);if(I.size>0){const Q=tt(g.info),a=uE(g);let r=null,E=null;try{r=new nf(g,a,I,Q.w,Q.h)}catch(o){console.warn("[GiftStage] VAP mixed rendering fell back to CPU compositor",o),E=wE(Q.w,Q.h)}A.vapMixState={texture:this.backend.createEmptyTexture(Q.w,Q.h,{filter:"linear"}),width:Q.w,height:Q.h,composer:r,frameMap:a,resources:I,lastFrameIndex:-1,lastSourceRef:null,...E!=null?E:{}},A.videoTextureHandle=A.vapMixState.texture}console.info("[GiftStage:WC] loadVAP: try WebCodecs / video…");const n=await this.tryWebCodecsPlayback(t.source);if(n){A.webcodecsPlayback=n,A.wcPlaybackTime=0;const Q=n.getFrameForTime(0);if(!Q)throw n.destroy(),A.webcodecsPlayback=null,new Error("WebCodecs: missing first frame");if(!A.vapMixState){const r=Q.displayWidth||Q.codedWidth,E=Q.displayHeight||Q.codedHeight,{w:o,h}=IC(A,r,E,"vap",g),c=this.manager.tryAllocateWebCodecsAtlas(A.id,o,h,this.options.webCodecsVideoAtlas);if(c)A.videoTextureHandle=c.handle,A.wcAtlasRect=c.rect,this.backend.updateTextureRegion(c.handle,Q,c.rect.x,c.rect.y,o,h);else{const f=this.backend.createTexture(Q,{width:r,height:E,filter:"linear"});A.videoTextureHandle=f}}A.videoElement=null;const a=A.videoTextureHandle;console.info("[GiftStage:WC] VAP texture",{giftId:A.id,atlas:!!A.wcAtlasRect&&!A.vapMixState,mixed:!!A.vapMixState,tex:`${a.width}x${a.height}`,frame0:`${Q.displayWidth}x${Q.displayHeight}`,atlasCell:A.wcAtlasRect?`${A.wcAtlasRect.w}x${A.wcAtlasRect.h}`:void 0}),this.logPlaybackBackend(A,"webcodecs");return}const B=await this.tryAcquireSharedHtmlVideo(A,t);if(B)A.videoElement=B.video,A.releaseSharedVideoPlayback=B.release;else{const Q=await this.acquireVideoPlaybackSource(t.source);A.releaseMediaSource=(e=Q.release)!=null?e:null;const a=this.createVideo(Q.src,t.loop===0);A.videoElement=a}const C=A.videoElement;if(B){await B.ready;const Q=this.acquireSharedVideoTexture(B.cacheKey,C);A.videoTextureHandle=Q.texture,A.releaseSharedVideoTexture=Q.release}else await this.waitVideoReady(C),A.vapMixState||(A.videoTextureHandle=this.backend.createTexture(C,{width:C.videoWidth,height:C.videoHeight,filter:"linear"}));A.releaseSharedVideoPlayback||this.bindVideoLoopOnEnded(A,C),this.logPlaybackBackend(A,"video",this.shouldTryWebCodecs()?"fallback after WebCodecs unavailable or failed":"preferWebCodecs off or no VideoDecoder")}async loadAlphaVideo(A,t){var e;console.info("[GiftStage:WC] loadAlphaVideo: try WebCodecs / video…");const g=await this.tryWebCodecsPlayback(t.source);if(g){A.webcodecsPlayback=g,A.wcPlaybackTime=0;const n=g.getFrameForTime(0);if(!n)throw g.destroy(),A.webcodecsPlayback=null,new Error("WebCodecs: missing first frame");const B=n.displayWidth||n.codedWidth,C=n.displayHeight||n.codedHeight,{w:Q,h:a}=IC(A,B,C,"alphaVideo",void 0),r=this.manager.tryAllocateWebCodecsAtlas(A.id,Q,a,this.options.webCodecsVideoAtlas);if(r)A.videoTextureHandle=r.handle,A.wcAtlasRect=r.rect,this.backend.updateTextureRegion(r.handle,n,r.rect.x,r.rect.y,Q,a);else{const o=this.backend.createTexture(n,{width:B,height:C,filter:"linear"});A.videoTextureHandle=o}A.videoElement=null;const E=A.videoTextureHandle;console.info("[GiftStage:WC] alphaVideo texture",{giftId:A.id,atlas:!!A.wcAtlasRect,tex:`${E.width}x${E.height}`,frame0:`${n.displayWidth}x${n.displayHeight}`,atlasCell:A.wcAtlasRect?`${A.wcAtlasRect.w}x${A.wcAtlasRect.h}`:void 0}),this.logPlaybackBackend(A,"webcodecs");return}const I=await this.tryAcquireSharedHtmlVideo(A,t);if(I)A.videoElement=I.video,A.releaseSharedVideoPlayback=I.release;else{const n=await this.acquireVideoPlaybackSource(t.source);A.releaseMediaSource=(e=n.release)!=null?e:null;const B=this.createVideo(n.src,t.loop===0);A.videoElement=B}const s=A.videoElement;if(I){await I.ready;const n=this.acquireSharedVideoTexture(I.cacheKey,s);A.videoTextureHandle=n.texture,A.releaseSharedVideoTexture=n.release}else await this.waitVideoReady(s),A.videoTextureHandle=this.backend.createTexture(s,{width:s.videoWidth,height:s.videoHeight,filter:"linear"});A.releaseSharedVideoPlayback||this.bindVideoLoopOnEnded(A,s),this.logPlaybackBackend(A,"video",this.shouldTryWebCodecs()?"fallback after WebCodecs unavailable or failed":"preferWebCodecs off or no VideoDecoder")}bindVideoLoopOnEnded(A,t){t.addEventListener("ended",()=>{var e,g,I,s,n;if(A.state!=="playing")return;const B=(e=A.options.loop)!=null?e:1;if(B===0){t.currentTime=0,t.play();return}if(A.loopCount++,A.loopCount>=B){const C={id:A.id,type:A.type,pause:()=>{},resume:()=>{},destroy:()=>this.removeGift(A.id)};if((g=A.onComplete)==null||g.call(A,C),A.flyTo){A.state="fly";const Q=ms(this.canvas);this.manager.postAnimator.startFly(A,A.flyTo.x*Q.sx,A.flyTo.y*Q.sy,(I=A.scaleTo)!=null?I:.3,(s=A.flyDuration)!=null?s:600,(n=A.easing)!=null?n:Ls,()=>{var a;A.state="finished",(a=A.onFlyComplete)==null||a.call(A,C)})}else A.state="finished";return}t.currentTime=0,t.play()})}createVideo(A,t){const e=document.createElement("video");return e.crossOrigin="anonymous",e.autoplay=!0,e.preload="auto",e.muted=!0,e.playsInline=!0,e.setAttribute("playsinline",""),e.setAttribute("webkit-playsinline","true"),e.loop=t,e.style.cssText="display:none;",e.src=A,document.body.appendChild(e),e.load(),e}waitVideoMetadata(A){return new Promise((t,e)=>{const g=()=>{A.removeEventListener("loadedmetadata",I),A.removeEventListener("error",s)},I=()=>{g(),t()},s=()=>{g(),e(new Error("Video metadata load failed"))};A.addEventListener("loadedmetadata",I),A.addEventListener("error",s),A.readyState>=HTMLMediaElement.HAVE_METADATA&&(g(),t())})}async probeVideoNaturalSize(A,t="video"){var e;const g=await this.acquireVideoPlaybackSource(A),I=document.createElement("video");I.crossOrigin="anonymous",I.preload="metadata",I.muted=!0,I.playsInline=!0,I.setAttribute("playsinline",""),I.setAttribute("webkit-playsinline","true"),I.style.cssText="display:none;",I.src=g.src,document.body.appendChild(I),I.load();try{return await this.waitVideoMetadata(I),t==="alphaVideo"?wf(I.videoWidth,I.videoHeight):{width:I.videoWidth,height:I.videoHeight}}finally{try{I.pause(),I.removeAttribute("src"),I.load(),I.remove()}catch(s){}(e=g.release)==null||e.call(g)}}async acquireVideoPlaybackSource(A){if(typeof A!="string"){const g=URL.createObjectURL(new Blob([A]));return{src:g,release:()=>URL.revokeObjectURL(g)}}const t=this.normalizeSharedVideoSourceKey(A);if(!t)return{src:A};let e=this.sharedVideoSources.get(t);e||(e={refs:0,objectUrl:null,promise:this.fetchSharedVideoSource(t)},this.sharedVideoSources.set(t,e)),e.refs++;try{return{src:await e.promise,release:()=>this.releaseSharedVideoSource(t)}}catch(g){return this.releaseSharedVideoSource(t),console.warn("[GiftStage] shared video prefetch failed, using original url",g),{src:A}}}normalizeSharedVideoSourceKey(A){try{const t=new URL(A,window.location.href);return t.protocol==="blob:"||t.protocol==="data:"?null:t.href}catch(t){return null}}async fetchSharedVideoSource(A){const t=new Blob([await Ys(A)]),e=this.sharedVideoSources.get(A),g=URL.createObjectURL(t);return e&&(e.objectUrl=g,e.refs===0&&(URL.revokeObjectURL(g),this.sharedVideoSources.delete(A))),g}releaseSharedVideoSource(A){const t=this.sharedVideoSources.get(A);t&&(t.refs=Math.max(0,t.refs-1),t.refs===0&&t.objectUrl&&(URL.revokeObjectURL(t.objectUrl),this.sharedVideoSources.delete(A)))}async tryAcquireSharedHtmlVideo(A,t){var e,g;if(this.options.shareIdenticalVideoPlayback===!1||t.loop!==0||typeof t.source!="string"||A.type==="vap"&&A.vapMixState)return null;const I=this.normalizeSharedVideoSourceKey(t.source);if(!I)return null;const s=(e=this.options.sharedVideoPlaybackWindowMs)!=null?e:OA.DEFAULT_SHARED_VIDEO_PLAYBACK_WINDOW_MS,n=Math.max(0,s)/1e3,B=this.sharedHtmlVideos.get(I);if(B)return B.video.currentTime>n?null:(B.refs++,{cacheKey:I,video:B.video,ready:B.ready,release:()=>this.releaseSharedHtmlVideo(I)});let C=this.pendingSharedHtmlVideos.get(I);C||(C=(async()=>{var r;const E=await this.acquireVideoPlaybackSource(t.source),o=this.createVideo(E.src,!0),h=this.waitVideoReady(o);return{video:o,ready:h,releaseSource:(r=E.release)!=null?r:null}})(),this.pendingSharedHtmlVideos.set(I,C));let Q;try{Q=await C}catch(r){throw this.pendingSharedHtmlVideos.delete(I),r}let a=this.sharedHtmlVideos.get(I);return a?a.refs++:(a={refs:1,video:Q.video,ready:Q.ready,releaseSource:(g=Q.releaseSource)!=null?g:null},this.sharedHtmlVideos.set(I,a)),this.pendingSharedHtmlVideos.delete(I),{cacheKey:I,video:a.video,ready:a.ready,release:()=>this.releaseSharedHtmlVideo(I)}}acquireSharedVideoTexture(A,t){const e=this.sharedVideoTextures.get(A);if(e)return e.refs++,{texture:e.texture,release:()=>this.releaseSharedVideoTexture(A)};const g=this.backend.createTexture(t,{width:t.videoWidth,height:t.videoHeight,filter:"linear"});return this.sharedVideoTextures.set(A,{refs:1,texture:g}),{texture:g,release:()=>this.releaseSharedVideoTexture(A)}}releaseSharedVideoTexture(A){var t;const e=this.sharedVideoTextures.get(A);e&&(e.refs=Math.max(0,e.refs-1),!(e.refs>0)&&((t=this.backend)==null||t.deleteTexture(e.texture),this.sharedVideoTextures.delete(A)))}releaseSharedHtmlVideo(A){var t;const e=this.sharedHtmlVideos.get(A);if(e&&(e.refs=Math.max(0,e.refs-1),!(e.refs>0))){try{e.video.pause(),e.video.removeAttribute("src"),e.video.load(),e.video.remove()}catch(g){}(t=e.releaseSource)==null||t.call(e),this.sharedHtmlVideos.delete(A)}}waitForFirstVideoFrame(A){return new Promise(t=>{typeof A.requestVideoFrameCallback=="function"?A.requestVideoFrameCallback(()=>t()):requestAnimationFrame(()=>t())})}waitVideoReady(A){return new Promise((t,e)=>{const g=()=>{A.removeEventListener("canplay",s),A.removeEventListener("error",n)},I=async()=>{try{await A.play(),await this.waitForFirstVideoFrame(A),t()}catch(B){e(B)}},s=()=>{g(),I()},n=()=>{g(),e(new Error("Video load failed"))};A.addEventListener("canplay",s),A.addEventListener("error",n),A.readyState>=HTMLMediaElement.HAVE_FUTURE_DATA&&(g(),I())})}removeGift(A){var t;(t=this.manager)==null||t.removeSlot(A)}removeAll(){if(this.manager)for(const A of this.manager.getSlotIds())this.manager.removeSlot(A)}pause(){var A;(A=this.manager)==null||A.pause()}resume(){var A;(A=this.manager)==null||A.resume()}get activeCount(){var A,t;return(t=(A=this.manager)==null?void 0:A.activeCount)!=null?t:0}get backendType(){var A,t;return(t=(A=this.backend)==null?void 0:A.type)!=null?t:"webgl2"}destroy(){var A,t;this.destroyed=!0,(A=this.resizeObserver)==null||A.disconnect(),this.resizeObserver=null,window.removeEventListener("resize",this.onWindowLayout),window.removeEventListener("orientationchange",this.onWindowLayout),(t=this.manager)==null||t.destroy();for(const[e]of this.sharedHtmlVideos)this.releaseSharedHtmlVideo(e);for(const[e]of this.sharedSVGAEntities)this.releaseSharedSVGAEntity(e);for(const[e]of this.sharedVideoTextures)this.releaseSharedVideoTexture(e);this.pendingSharedSVGAEntities.clear(),this.pendingSharedHtmlVideos.clear();for(const[e,g]of this.sharedVideoSources)g.objectUrl&&URL.revokeObjectURL(g.objectUrl),this.sharedVideoSources.delete(e);this.canvas.remove(),this.backend=null,this.manager=null}};z(rs,"SVGA_ATLAS_MAX_SIZE",2048);z(rs,"SVGA_ATLAS_PADDING",2);z(rs,"DEFAULT_SHARED_VIDEO_PLAYBACK_WINDOW_MS",100);z(rs,"DEFAULT_RESOURCE_CACHE_MEMORY_BYTES",128*1024*1024);z(rs,"DEFAULT_RESOURCE_CACHE_PERSISTENT_BYTES",2048*1024*1024);let mf=rs;exports.AlphaVideoRenderer=NC;exports.AudioManager=UC;exports.BufferPool=uC;exports.GiftStage=mf;exports.PostAnimator=RC;exports.RenderManager=MC;exports.SVGABatchRenderer=DC;exports.VAPRenderer=SC;exports.WebCodecsMp4Playback=KA;exports.WebGL1Backend=Pn;exports.WebGL2Backend=QC;exports.WebGPUBackend=CC;exports.buildAtlas=KC;exports.buildBatchVerticesJS=Xs;exports.canSharedArrayBuffer=aa;exports.canWebGL1=Qa;exports.canWebGL2=Ca;exports.canWebGPU=Ba;exports.clamp=AB;exports.createBackend=EC;exports.decodeSVGAProto=YC;exports.easeInOutCubic=bE;exports.easeInOutQuad=xE;exports.easeInQuad=kE;exports.easeOutBack=YE;exports.easeOutCubic=Ls;exports.easeOutQuad=ME;exports.generateId=wC;exports.inflateJS=xC;exports.initWasm=fC;exports.isWasmReady=At;exports.lerp=$s;exports.linear=UE;exports.ortho=vA;exports.parseSVGA=IB;exports.parseVAPConfig=iB;
|
|
8609
8609
|
//# sourceMappingURL=gift-stage.cjs.js.map
|
package/dist/gift-stage.es.js
CHANGED
|
@@ -22093,7 +22093,7 @@ const Es = class WA {
|
|
|
22093
22093
|
await this.loadAlphaVideo(a, s);
|
|
22094
22094
|
break;
|
|
22095
22095
|
}
|
|
22096
|
-
a.state = "playing", a.webcodecsPlayback && console.info("[GiftStage:WC] gift playing", {
|
|
22096
|
+
a.state = "playing", s.type === "svga" && a.videoEntity && this.manager.audioManager.tickFrame(a, a.currentFrame, r), a.webcodecsPlayback && console.info("[GiftStage:WC] gift playing", {
|
|
22097
22097
|
giftId: a.id,
|
|
22098
22098
|
type: a.type,
|
|
22099
22099
|
layoutBufPx: { x: a.x, y: a.y, w: a.width, h: a.height }
|
package/package.json
CHANGED