modern-canvas 0.1.7 → 0.1.8

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/index.cjs CHANGED
@@ -5304,6 +5304,7 @@ exports.Text2D = class Text2D extends exports.Node2D {
5304
5304
  this.text.effects = this.effects;
5305
5305
  this.text.fonts = this.fonts;
5306
5306
  this.text.measureDom = this.measureDom;
5307
+ this.text.requestUpdate();
5307
5308
  }
5308
5309
  _onUpdateStyleProperty(key, value, oldValue) {
5309
5310
  if (key === "height")
package/dist/index.js CHANGED
@@ -29,7 +29,7 @@ ${r}`);return e.documentElement}else return n}const Xh="px",Hh=90,mo=["mm","cm",
29
29
  src: url(${e});
30
30
  }`)),document.head.appendChild(t),this}_parseUrls(r){const e=Array.from(new Set([...r.split(","),r]));return Array.from(new Set(e.map(t=>this.familyToUrl.get(t.trim())??this.familyToUrl.get(t)??t)))}get(r){let e;return r&&(e=this._parseUrls(r).reduce((i,s)=>i||this.loaded.get(s),void 0)),e??this.fallbackFont}set(r,e){return this.familyToUrl.set(r,e.src),this.loaded.set(e.src,e),this}delete(r){return this._parseUrls(r).forEach(e=>{this.familyToUrl.delete(e),this.loaded.delete(e)}),this}clear(){return this.familyToUrl.clear(),this.loading.clear(),this.loaded.clear(),this}async load(r,e={}){const{cancelOther:t,injectFontFace:i=!0,injectStyleTag:s=!0,...o}=e,{src:a}=r;if(this.loaded.has(a))return t&&(this.loading.forEach(d=>d.cancel()),this.loading.clear()),c(this.loaded.get(a));let l=this.loading.get(a);return l||(l=this._createRequest(a,o),this.loading.set(a,l)),t&&this.loading.forEach((d,p)=>{d!==l&&(d.cancel(),this.loading.delete(p))}),l.when.then(d=>{if(this.loaded.has(a))return c(this.loaded.get(a));{const p=f(d);return e.noAdd||this.loaded.set(r.src,p),p.familySet.forEach(m=>{this.familyToUrl.set(m,a),typeof document<"u"&&(i&&this.injectFontFace(m,d),s&&this.injectStyleTag(m,a))}),p}}).catch(d=>{if(d instanceof DOMException&&d.message==="The user aborted a request.")return f();throw d}).finally(()=>{this.loading.delete(a)});function h(){return r.family?Array.isArray(r.family)?r.family:[r.family]:[]}function c(d){return h().forEach(p=>{d.familySet.add(p)}),d}function f(d=new ArrayBuffer(0)){let p;function m(){return p||(p=d.byteLength?aa(d,!1):void 0),p}function _(){const y=m();if(y instanceof He||y instanceof Mi)return y.sfnt}return{...r,buffer:d,familySet:new Set(h()),getFont:m,getSfnt:_}}}async waitUntilLoad(){await Promise.all(Array.from(this.loading.values()).map(r=>r.when))}};u(ki,"defaultRequestInit",{cache:"force-cache"});let Ai=ki;const la=new Ai,cf=Object.freeze(Object.defineProperty({__proto__:null,BaseFont:gi,get Cff(){return zt},get Cmap(){return cr},CmapSubtableFormat0:Ls,CmapSubtableFormat12:$s,CmapSubtableFormat14:Nr,CmapSubtableFormat2:Br,CmapSubtableFormat4:Fs,CmapSubtableFormat6:jt,FontDataObject:je,FontDataView:_e,Fonts:Ai,get Glyf(){return xi},Glyph:Is,GlyphSet:Rs,get Gpos(){return js},get Gsub(){return _t},get Head(){return pe},get Hhea(){return Pe},get Hmtx(){return Pi},get Kern(){return Gs},get Loca(){return Ci},get Maxp(){return be},get Name(){return ur},get Os2(){return $},get Post(){return Xe},Sfnt:$t,TableDirectory:lr,Ttf:He,get Vhea(){return Ce},get Vmtx(){return Ti},Woff:Mi,WoffTableDirectoryEntry:qt,createCmapSegments:Ns,dataTypeToByteLength:Dr,defineColumn:v,defineSfntTable:xe,fonts:la,parseFont:aa},Symbol.toStringTag,{value:"Module"}));function Ws(n,r,e){if(typeof r=="string"&&r.startsWith("linear-gradient")){const{x0:t,y0:i,x1:s,y1:o,stops:a}=uf(r,e.left,e.top,e.width,e.height),l=n.createLinearGradient(t,i,s,o);return a.forEach(h=>l.addColorStop(h.offset,h.color)),l}return r}function Oi(n,r,e){n!=null&&n.color&&(n.color=Ws(e,n.color,r)),n!=null&&n.backgroundColor&&(n.backgroundColor=Ws(e,n.backgroundColor,r)),n!=null&&n.textStrokeColor&&(n.textStrokeColor=Ws(e,n.textStrokeColor,r))}function uf(n,r,e,t,i){var p;const s=((p=n.match(/linear-gradient\((.+)\)$/))==null?void 0:p[1])??"",o=s.split(",")[0],a=o.includes("deg")?o:"0deg",l=s.replace(a,"").matchAll(/(#|rgba|rgb)(.+?) ([\d.]+%)/gi),c=(Number(a.replace("deg",""))||0)*Math.PI/180,f=t*Math.sin(c),d=i*Math.cos(c);return{x0:r+t/2-f,y0:e+i/2+d,x1:r+t/2+f,y1:e+i/2-d,stops:Array.from(l).map(m=>{let _=m[2];return _.startsWith("(")?_=_.split(",").length>3?`rgba${_}`:`rgb${_}`:_=`#${_}`,{offset:Number(m[3].replace("%",""))/100,color:_}})}}function zr(n){const{ctx:r,path:e,fontSize:t,clipRect:i}=n;r.save(),r.beginPath();const s=e.style,o={...s,fill:n.color??s.fill,stroke:n.textStrokeColor??s.stroke,strokeWidth:n.textStrokeWidth?n.textStrokeWidth*t:s.strokeWidth,shadowOffsetX:(n.shadowOffsetX??0)*t,shadowOffsetY:(n.shadowOffsetY??0)*t,shadowBlur:(n.shadowBlur??0)*t,shadowColor:n.shadowColor};i&&(r.rect(i.left,i.top,i.width,i.height),r.clip(),r.beginPath()),e.drawTo(r,o),r.restore()}function ff(n,r,e){const{left:t,top:i,width:s,height:o}=e,a=n.canvas;a.dataset.viewBox=`${t} ${i} ${s} ${o}`,a.dataset.pixelRatio=String(r);const l=s,h=o;a.width=Math.max(1,Math.ceil(l*r)),a.height=Math.max(1,Math.ceil(h*r)),a.style.width=`${l}px`,a.style.height=`${h}px`,n.clearRect(0,0,a.width,a.height),n.scale(r,r),n.translate(-t,-i)}function df(n,r){const{paragraphs:e,computedStyle:t,glyphBox:i}=r;Oi(t,i,n),e.forEach(s=>{Oi(s.computedStyle,s.lineBox,n),s.fragments.forEach(o=>{Oi(o.computedStyle,o.inlineBox,n)})})}const pf=new Set(["©","®","÷"]),gf=new Set(["—","…","“","”","﹏","﹋","﹌","‘","’","˜"]),mf={1:"italic",32:"bold"},yf={1:"italic",2:"bold"},ha={100:-.2,200:-.1,300:0,400:0,normal:0,500:.1,600:.2,700:.3,bold:.3,800:.4,900:.5};class _f{constructor(r,e,t){u(this,"path",new we);u(this,"lineBox",new Y);u(this,"inlineBox",new Y);u(this,"glyphBox");u(this,"advanceWidth",0);u(this,"advanceHeight",0);u(this,"underlinePosition",0);u(this,"underlineThickness",0);u(this,"strikeoutPosition",0);u(this,"strikeoutSize",0);u(this,"ascender",0);u(this,"descender",0);u(this,"typoAscender",0);u(this,"typoDescender",0);u(this,"typoLineGap",0);u(this,"winAscent",0);u(this,"winDescent",0);u(this,"xHeight",0);u(this,"capHeight",0);u(this,"baseline",0);u(this,"centerDiviation",0);u(this,"fontStyle");this.content=r,this.index=e,this.parent=t}get center(){var r;return(r=this.glyphBox)==null?void 0:r.center}get computedStyle(){return this.parent.computedStyle}get isVertical(){return this.computedStyle.writingMode.includes("vertical")}get fontSize(){return this.computedStyle.fontSize}get fontHeight(){return this.fontSize*this.computedStyle.lineHeight}_getFontSfnt(r){const e=this.computedStyle.fontFamily,t=r??la,i=e?t.get(e):t.fallbackFont;return i==null?void 0:i.getSfnt()}updateGlyph(r=this._getFontSfnt()){if(!r)return this;const{hhea:e,os2:t,post:i,head:s}=r,o=s.unitsPerEm,a=e.ascent,l=e.descent,{content:h,computedStyle:c,isVertical:f}=this,{fontSize:d}=c,p=o/d,m=r.getAdvanceWidth(h,d),_=(a+Math.abs(l))/p,y=a/p;return this.advanceWidth=m,this.advanceHeight=_,this.inlineBox.width=f?_:m,this.inlineBox.height=f?m:_,this.underlinePosition=(a-i.underlinePosition)/p,this.underlineThickness=i.underlineThickness/p,this.strikeoutPosition=(a-t.yStrikeoutPosition)/p,this.strikeoutSize=t.yStrikeoutSize/p,this.ascender=a/p,this.descender=l/p,this.typoAscender=t.sTypoAscender/p,this.typoDescender=t.sTypoDescender/p,this.typoLineGap=t.sTypoLineGap/p,this.winAscent=t.usWinAscent/p,this.winDescent=t.usWinDescent/p,this.xHeight=t.sxHeight/p,this.capHeight=t.sCapHeight/p,this.baseline=y,this.centerDiviation=_/2-y,this.fontStyle=mf[t.fsSelection]??yf[s.macStyle],this}update(r){const e=this._getFontSfnt(r);if(!e)return this;this.updateGlyph(e);const{isVertical:t,content:i,computedStyle:s,baseline:o,inlineBox:a,ascender:l,descender:h,typoAscender:c,fontStyle:f,advanceWidth:d,advanceHeight:p}=this,{left:m,top:_}=a,y=s.fontStyle==="italic"&&f!=="italic";let S=m,C=_+o,x;const T=new we;if(t&&(S+=(p-d)/2,Math.abs(d-p)>.1&&(C-=(l-c)/(l+Math.abs(h))*p),x=void 0),t&&!pf.has(i)&&(i.codePointAt(0)<=256||gf.has(i))){T.addCommands(e.getPathCommands(i,S,_+o-(p-d)/2,s.fontSize));const P={y:_-(p-d)/2+p/2,x:S+d/2};y&&this._italic(T,t?{x:P.x,y:_-(p-d)/2+o}:void 0),T.rotate(90,P)}else x!==void 0?(T.addCommands(e.glyphs.get(x).getPathCommands(S,C,s.fontSize)),y&&this._italic(T,t?{x:S+d/2,y:_+c/(l+Math.abs(h))*p}:void 0)):(T.addCommands(e.getPathCommands(i,S,C,s.fontSize)),y&&this._italic(T,t?{x:S+p/2,y:C}:void 0));const b=s.fontWeight??400;return b in ha&&(b===700||b==="bold")&&f!=="bold"&&T.bold(ha[b]*s.fontSize*.05),T.style={fill:s.color,stroke:s.textStrokeWidth?s.textStrokeColor:"none",strokeWidth:s.textStrokeWidth?s.textStrokeWidth*s.fontSize*.03:0},this.path=T,this.glyphBox=this.getGlyphBoundingBox(),this}_italic(r,e){r.skew(-.24,0,e||{y:this.inlineBox.top+this.baseline,x:this.inlineBox.left+this.inlineBox.width/2})}getGlyphMinMax(r,e,t){var i;if((i=this.path.curves[0])!=null&&i.curves.length)return this.path.getMinMax(r,e,t)}getGlyphBoundingBox(r){const e=this.getGlyphMinMax(void 0,void 0,r);if(!e)return;const{min:t,max:i}=e;return new Y(t.x,t.y,i.x-t.x,i.y-t.y)}drawTo(r,e={}){const t=this.computedStyle,i={ctx:r,path:this.path,fontSize:t.fontSize,color:t.color,...e};if(this.glyphBox)zr(i);else{r.save(),r.beginPath();const s=this.path.style,o={...s,fill:i.color??s.fill,stroke:i.textStrokeColor??s.stroke,strokeWidth:i.textStrokeWidth?i.textStrokeWidth*i.fontSize:s.strokeWidth,shadowOffsetX:(i.shadowOffsetX??0)*i.fontSize,shadowOffsetY:(i.shadowOffsetY??0)*i.fontSize,shadowBlur:(i.shadowBlur??0)*i.fontSize,shadowColor:i.shadowColor};us(r,o),r.font=`${i.fontSize}px ${i.fontFamily}`,this.isVertical?(r.textBaseline="middle",r.fillText(this.content,this.inlineBox.left,this.inlineBox.top+this.inlineBox.height/2)):(r.textBaseline="alphabetic",r.fillText(this.content,this.inlineBox.left,this.inlineBox.top+this.baseline)),r.restore()}}}function Xs(n,r){return typeof n=="number"?n:n.endsWith("%")?(n=n.substring(0,n.length-1),Math.ceil(Number(n)/100*r.total)):n.endsWith("rem")?(n=n.substring(0,n.length-3),Number(n)*r.fontSize):n.endsWith("em")?(n=n.substring(0,n.length-2),Number(n)*r.fontSize):Number(n)}function ca(n){const r=Ot(n)?{}:n;return Object.keys(r).reduce((e,t)=>{let i=r[t];const s=ua(t),o=ua(i);return s&&(t=s),o&&(i=o),e[t]=i,e},{})}function Ot(n){return!n||n==="none"}function vf(n,r){const e=Object.keys(n),t=Object.keys(r);return Array.from(new Set([...e,...t])).every(s=>fr(n[s],r[s]))}function fr(n,r){const e=typeof n;return e===typeof r?e==="object"?vf(n,r):n===r:!1}function ua(n){const r=n.startsWith("#")?n.slice(1):n;if(!/^(?:[0-9A-F]{3}|[0-9A-F]{6})$/i.test(r))return null;const t=r.length===3?r.split("").map(a=>a+a).join(""):r,i=Number.parseInt(t.slice(0,2),16),s=Number.parseInt(t.slice(2,4),16),o=Number.parseInt(t.slice(4,6),16);return`rgb(${i}, ${s}, ${o})`}function Hs(n){if(!n)return n;const r={};for(const e in n)n[e]!==""&&n[e]!==void 0&&(r[e]=n[e]);return r}function Ys(n){return n.startsWith("http://")||n.startsWith("https://")||n.startsWith("blob://")}class bf{constructor(r,e={},t){u(this,"inlineBox",new Y);this.content=r,this.style=e,this.parent=t,this.updateComputedStyle().initCharacters()}get computedContent(){const r=this.computedStyle;return r.textTransform==="uppercase"?this.content.toUpperCase():r.textTransform==="lowercase"?this.content.toLowerCase():this.content}updateComputedStyle(){return this.computedStyle={...this.parent.computedStyle,...Hs(this.style)},this}initCharacters(){const r=[];let e=0;for(const t of this.computedContent)r.push(new _f(t,e++,this));return this.characters=r,this}}class Vr{constructor(r,e){u(this,"lineBox",new Y);u(this,"fragments",[]);this.style=r,this.parentStyle=e,this.updateComputedStyle()}updateComputedStyle(){return this.computedStyle={...Hs(this.parentStyle),...Hs(this.style)},this}addFragment(r,e){const t=new bf(r,e,this);return this.fragments.push(t),t}}function ip(n){return n}const gr=class gr{_styleToDomStyle(r){const e={};for(const t in r){const i=r[t];gr.notZeroStyles.has(t)&&i===0||(typeof i=="number"&&gr.pxStyles.has(t)?e[t]=`${i}px`:e[t]=i)}return e}createParagraphDom(r,e){const t=document.createDocumentFragment(),i=document.createElement("section"),s={...e},o=e.writingMode.includes("horizontal");switch(e.textAlign){case"start":case"left":s.justifyContent="start";break;case"center":s.justifyContent="center";break;case"end":case"right":s.justifyContent="end";break}switch(e.verticalAlign){case"top":s.alignItems="top";break;case"middle":s.alignItems="center";break;case"bottom":s.alignItems="end";break}const a=!!(s.justifyContent||s.alignItems);Object.assign(i.style,{boxSizing:"border-box",display:a?"inline-flex":void 0,width:"max-content",height:"max-content",whiteSpace:"pre-wrap",wordBreak:"break-all",...this._styleToDomStyle(s),position:"fixed",visibility:"hidden"});const l=document.createElement("ul");return Object.assign(l.style,{verticalAlign:"inherit",listStyleType:"inherit",padding:"0",margin:"0",width:a&&o?"100%":void 0,height:a&&!o?"100%":void 0}),r.forEach(h=>{const c=document.createElement("li");Object.assign(c.style,{verticalAlign:"inherit",...this._styleToDomStyle(h.style)}),h.fragments.forEach(f=>{const d=document.createElement("span");Object.assign(d.style,{verticalAlign:"inherit",...this._styleToDomStyle(f.style)}),d.appendChild(document.createTextNode(f.content)),c.appendChild(d)}),l.appendChild(c)}),i.appendChild(l),t.appendChild(i),document.body.appendChild(t),{dom:i,destory:()=>{var h;return(h=i.parentNode)==null?void 0:h.removeChild(i)}}}measureDomText(r){const e=document.createRange();e.selectNodeContents(r);const t=r.data??"";let i=0;return Array.from(t).map(s=>{var f;const o=i+=t.substring(i).indexOf(s),a=o+s.length;i+=s.length,e.setStart(r,Math.max(o,0)),e.setEnd(r,a);const l=((f=e.getClientRects)==null?void 0:f.call(e))??[e.getBoundingClientRect()];let h=l[l.length-1];l.length>1&&h.width<2&&(h=l[l.length-2]);const c=e.toString();if(c!==""&&h&&h.width+h.height!==0)return{content:c,top:h.top,left:h.left,height:h.height,width:h.width}}).filter(Boolean)}measureDom(r){const e=[],t=[],i=[];return r.querySelectorAll("li").forEach((s,o)=>{const a=s.getBoundingClientRect();e.push({paragraphIndex:o,left:a.left,top:a.top,width:a.width,height:a.height}),s.querySelectorAll(":scope > *").forEach((l,h)=>{const c=l.getBoundingClientRect();t.push({paragraphIndex:o,fragmentIndex:h,left:c.left,top:c.top,width:c.width,height:c.height});let f=0;!l.children.length&&l.firstChild instanceof window.Text?this.measureDomText(l.firstChild).forEach(d=>{i.push({...d,newParagraphIndex:-1,paragraphIndex:o,fragmentIndex:h,characterIndex:f++,textWidth:-1,textHeight:-1})}):l.querySelectorAll(":scope > *").forEach(d=>{d.firstChild instanceof window.Text&&this.measureDomText(d.firstChild).forEach(p=>{i.push({...p,newParagraphIndex:-1,paragraphIndex:o,fragmentIndex:h,characterIndex:f++,textWidth:-1,textHeight:-1})})})})}),{paragraphs:e,fragments:t,characters:i}}measureParagraphDom(r,e){const t=e.getBoundingClientRect(),i=this.measureDom(e);i.paragraphs.forEach(a=>{const l=r[a.paragraphIndex];l.lineBox.left=a.left-t.left,l.lineBox.top=a.top-t.top,l.lineBox.width=a.width,l.lineBox.height=a.height}),i.fragments.forEach(a=>{const l=r[a.paragraphIndex].fragments[a.fragmentIndex];l.inlineBox.left=a.left-t.left,l.inlineBox.top=a.top-t.top,l.inlineBox.width=a.width,l.inlineBox.height=a.height});const s=[];let o=0;return i.characters.forEach(a=>{const{paragraphIndex:l,fragmentIndex:h,characterIndex:c}=a;s.push({...a,newParagraphIndex:l,left:a.left-t.left,top:a.top-t.top});const f=r[l].fragments[h].characters[c],{fontHeight:d,isVertical:p}=f,m=s[o];f.inlineBox.left=m.left,f.inlineBox.top=m.top,f.inlineBox.width=m.width,f.inlineBox.height=m.height,p?(f.lineBox.left=m.left+(m.width-d)/2,f.lineBox.top=m.top,f.lineBox.width=d,f.lineBox.height=m.height):(f.lineBox.left=m.left,f.lineBox.top=m.top+(m.height-d)/2,f.lineBox.width=m.width,f.lineBox.height=d),o++}),{paragraphs:r,boundingBox:new Y(0,0,t.width,t.height)}}measure(r,e,t){let i;t||({dom:t,destory:i}=this.createParagraphDom(r,e));const s=this.measureParagraphDom(r,t);return i==null||i(),s}};u(gr,"notZeroStyles",new Set(["width","height"])),u(gr,"pxStyles",new Set(["width","height","fontSize","letterSpacing","textStrokeWidth","textIndent","shadowOffsetX","shadowOffsetY","shadowBlur","marginLeft","marginTop","marginRight","marginBottom","paddingLeft","paddingTop","paddingRight","paddingBottom"]));let Zs=gr;class wf{constructor(){u(this,"eventListeners",new Map)}addEventListener(r,e,t){const i={value:e,options:t},s=this.eventListeners.get(r);return s?Array.isArray(s)?s.push(i):this.eventListeners.set(r,[s,i]):this.eventListeners.set(r,i),this}removeEventListener(r,e,t){var s,o;if(!e)return this.eventListeners.delete(r),this;const i=this.eventListeners.get(r);if(!i)return this;if(Array.isArray(i)){const a=[];for(let l=0,h=i.length;l<h;l++){const c=i[l];(c.value!==e||typeof t=="object"&&(t!=null&&t.once)&&(typeof c.options=="boolean"||!((s=c.options)!=null&&s.once)))&&a.push(c)}a.length?this.eventListeners.set(r,a.length===1?a[0]:a):this.eventListeners.delete(r)}else i.value===e&&(typeof t=="boolean"||!(t!=null&&t.once)||typeof i.options=="boolean"||(o=i.options)!=null&&o.once)&&this.eventListeners.delete(r);return this}removeAllListeners(){return this.eventListeners.clear(),this}hasEventListener(r){return this.eventListeners.has(r)}dispatchEvent(r,e){var i,s;const t=this.eventListeners.get(r);if(t){if(Array.isArray(t))for(let o=t.length,a=0;a<o;a++){const l=t[a];typeof l.options=="object"&&((i=l.options)!=null&&i.once)&&this.off(r,l.value,l.options),l.value.apply(this,[e])}else typeof t.options=="object"&&((s=t.options)!=null&&s.once)&&this.off(r,t.value,t.options),t.value.apply(this,[e]);return!0}else return!1}on(r,e,t){return this.addEventListener(r,e,t)}once(r,e){return this.addEventListener(r,e,{once:!0})}off(r,e,t){return this.removeEventListener(r,e,t)}emit(r,e){this.dispatchEvent(r,e)}}function Ks(n){const{highlight:r,highlightImage:e,highlightReferImage:t,highlightColormap:i,highlightLine:s,highlightSize:o,highlightThickness:a}=n;return{image:(r==null?void 0:r.image)??e??"none",referImage:(r==null?void 0:r.referImage)??t??"none",colormap:(r==null?void 0:r.colormap)??i??"none",line:(r==null?void 0:r.line)??s??"none",size:(r==null?void 0:r.size)??o??"cover",thickness:(r==null?void 0:r.thickness)??a??"100%"}}function xf(){const n=[],r=[],e=new Map,t=new Map;async function i(o){if(!e.has(o)){e.set(o,o);try{e.set(o,await fetch(o).then(a=>a.text()))}catch(a){console.warn(a),e.delete(o)}}}function s(o){let a=t.get(o);if(!a){const l=go(Ys(o)?e.get(o)??o:o),h=bo(l);a={dom:l,pathSet:h},t.set(o,a)}return a}return{name:"highlight",paths:n,load:async o=>{const a=new Set;o.forEachCharacter(l=>{const{computedStyle:h}=l,{image:c,referImage:f}=Ks(h);Ys(c)&&a.add(c),Ys(f)&&a.add(f)}),await Promise.all(Array.from(a).map(l=>i(l)))},update:o=>{r.length=0,n.length=0;let a=[],l,h;o.forEachCharacter(c=>{const{computedStyle:f}=c,d=Ks(f),{image:p,colormap:m,line:_,size:y,thickness:S}=d;if(Ot(p))l!=null&&l.length&&(l=[],a.push(l));else{const{inlineBox:C,isVertical:x}=c,{fontSize:T}=f;(!h||fr(h.image,p)&&fr(h.colormap,m)&&fr(h.line,_)&&fr(h.size,y)&&fr(h.thickness,S))&&(l!=null&&l.length)&&(x?l[0].inlineBox.left===C.left:l[0].inlineBox.top===C.top)&&l[0].fontSize===T?l.push(c):(l=[],l.push(c),a.push(l))}h=d}),a=a.filter(c=>c.length);for(let c=0;c<a.length;c++){const f=a[c],d=f[0],p=Y.from(...f.filter(I=>I.glyphBox).map(I=>I.glyphBox)),{computedStyle:m}=d,{fontSize:_,writingMode:y}=m,{image:S,referImage:C,colormap:x,line:T,size:b,thickness:P}=Ks(m),E=y.includes("vertical"),w=Xs(P,{fontSize:_,total:p.width})/p.width,D=ca(x),{pathSet:F,dom:B}=s(S),U=F.getBoundingBox(!0),V=_/U.width*2,M=new Y().copy(p);E&&(M.width=p.height,M.height=p.width,M.left=p.left+p.width);const N=Math.floor(M.width);let q=N;if(b!=="cover"&&(q=Xs(b,{fontSize:_,total:p.width})||N,M.width=q),!Ot(C)&&Ot(T)){const I=s(C).pathSet.getBoundingBox(!0);U.copy(I)}else{let I;if(Ot(T))if(U.width/U.height>4){I="underline";const j=B.getAttribute("viewBox");if(j){const[k,L,K,X]=j.split(" ").map(G=>Number(G)),W=L+X/2;U.y<W&&U.y+U.height>W?I="line-through":U.y+U.height<W?I="overline":I="underline"}}else I="outline";else I=T;switch(I){case"outline":{const j=M.width*.2,k=M.height*.2;M.width+=j,M.height+=k,E?(M.x-=k/2,M.y-=j/2,M.x+=M.height):(M.x-=j/2,M.y-=k/2);break}case"overline":M.height=U.height*V,E?M.x=d.inlineBox.left+d.inlineBox.width:M.y=d.inlineBox.top;break;case"line-through":M.height=U.height*V,E?M.x=d.inlineBox.left+d.inlineBox.width-d.strikeoutPosition+M.height/2:M.y=d.inlineBox.top+d.strikeoutPosition-M.height/2;break;case"underline":M.height=U.height*V,E?M.x=d.inlineBox.left+d.inlineBox.width-d.underlinePosition:M.y=d.inlineBox.top+d.underlinePosition;break}}const R=new ke().translate(-U.x,-U.y).scale(M.width/U.width,M.height/U.height);E&&R.rotate(-Math.PI/2),R.translate(M.x,M.y);for(let I=0;I<Math.ceil(N/q);I++){const j=R.clone();E?j.translate(0,I*M.width):j.translate(I*M.width,0),F.paths.forEach(k=>{const L=k.clone().applyTransform(j);L.style.strokeWidth&&(L.style.strokeWidth*=V*w),L.style.strokeMiterlimit&&(L.style.strokeMiterlimit*=V),L.style.strokeDashoffset&&(L.style.strokeDashoffset*=V),L.style.strokeDasharray&&(L.style.strokeDasharray=L.style.strokeDasharray.map(K=>K*V)),L.style.fill&&L.style.fill in D&&(L.style.fill=D[L.style.fill]),L.style.stroke&&L.style.stroke in D&&(L.style.stroke=D[L.style.stroke]),n.push(L),N!==q&&(E?r[n.length-1]=new Y(p.left-p.width*2,p.top,p.width*4,p.height):r[n.length-1]=new Y(p.left,p.top-p.height*2,p.width,p.height*4))})}}},renderOrder:-1,render:(o,a)=>{n.forEach((l,h)=>{if(zr({ctx:o,path:l,fontSize:a.computedStyle.fontSize,clipRect:r[h]}),a.debug){const c=new vs([l]).getBoundingBox();c&&o.strokeRect(c.x,c.y,c.width,c.height)}})}}}function Pf(n,r){return`<svg width="${n*2}" height="${n*2}" xmlns="http://www.w3.org/2000/svg">
31
31
  <circle cx="${n}" cy="${n}" r="${n}" fill="${r}" />
32
- </svg>`}function Cf(){const n=[];return{name:"listStyle",paths:n,update:r=>{n.length=0;const{paragraphs:e,isVertical:t,fontSize:i}=r,s=i*.45;e.forEach(o=>{const{computedStyle:a}=o,{color:l,listStyleImage:h,listStyleColormap:c,listStyleSize:f,listStyleType:d}=a,p=ca(c);let m=f,_;if(!Ot(h))_=h;else if(!Ot(d)){const x=i*.38/2;switch(m=m==="cover"?x*2:m,d){case"disc":_=Pf(x,String(l));break}}if(!_)return;const y=bo(_),S=y.getBoundingBox();let C;o.fragments.forEach(x=>{x.characters.forEach(T=>{const{inlineBox:b}=T;if(t?(C==null?void 0:C.inlineBox.left)!==b.left:(C==null?void 0:C.inlineBox.top)!==b.top){C=T;const P=m==="cover"?1:Xs(m,{total:i,fontSize:i})/i,E=new ke;if(t){const w=i/S.height*P;E.translate(-S.left,-S.top).rotate(Math.PI/2).scale(w,w).translate(b.left+(b.width-S.height*w)/2,b.top-s)}else{const w=i/S.height*P;E.translate(-S.left,-S.top).scale(w,w).translate(b.left-S.width*w-s,b.top+(b.height-S.height*w)/2)}n.push(...y.paths.map(w=>{const D=w.clone();return D.applyTransform(E),D.style.fill&&D.style.fill in p&&(D.style.fill=p[D.style.fill]),D.style.stroke&&D.style.stroke in p&&(D.style.stroke=p[D.style.stroke]),D}))}})})})}}}const Ut=new O,dr=new ke,Dt=new ke;function Ef(){return{name:"render",getBoundingBox:n=>{const{characters:r,fontSize:e,effects:t}=n,i=[];return r.forEach(s=>{t==null||t.forEach(o=>{if(!s.glyphBox)return;const a=s.glyphBox.clone(),l=Js(n,o);Ut.set(a.left,a.top),Ut.applyMatrix3(l),a.left=Ut.x,a.top=Ut.y,Ut.set(a.right,a.bottom),Ut.applyMatrix3(l),a.width=Ut.x-a.left,a.height=Ut.y-a.top;const h=(o.shadowOffsetX??0)*e,c=(o.shadowOffsetY??0)*e,f=Math.max(.1,o.textStrokeWidth??0)*e;a.left+=h-f,a.top+=c-f,a.width+=f*2,a.height+=f*2,i.push(a)})}),i.length?Y.from(...i):void 0},render:(n,r)=>{const{paragraphs:e,glyphBox:t,effects:i,style:s}=r;function o(a,l){n.fillStyle=a,n.fillRect(l.left,l.top,l.width,l.height)}s!=null&&s.backgroundColor&&o(s.backgroundColor,new Y(0,0,n.canvas.width,n.canvas.height)),e.forEach(a=>{var l;(l=a.style)!=null&&l.backgroundColor&&o(a.style.backgroundColor,a.lineBox)}),i?i.forEach(a=>{Oi(a,t,n),n.save();const[l,h,c,f,d,p]=Js(r,a).transpose().elements;n.transform(l,f,h,d,c,p),r.forEachCharacter(m=>{var _;(_=m.parent.style)!=null&&_.backgroundColor&&o(m.parent.style.backgroundColor,m.inlineBox),m.drawTo(n,a)}),n.restore()}):e.forEach(a=>{a.fragments.forEach(l=>{var h;(h=l.style)!=null&&h.backgroundColor&&o(l.computedStyle.backgroundColor,l.inlineBox),l.characters.forEach(c=>{c.drawTo(n)})})}),r.debug&&e.forEach(a=>{n.strokeRect(a.lineBox.x,a.lineBox.y,a.lineBox.width,a.lineBox.height)})}}}function Js(n,r){const{fontSize:e,glyphBox:t}=n,i=(r.translateX??0)*e,s=(r.translateY??0)*e,o=Math.PI*2,a=(r.skewX??0)/360*o,l=(r.skewY??0)/360*o,{left:h,top:c,width:f,height:d}=t,p=h+f/2,m=c+d/2;return dr.identity(),Dt.makeTranslation(i,s),dr.multiply(Dt),Dt.makeTranslation(p,m),dr.multiply(Dt),Dt.set(1,Math.tan(a),0,Math.tan(l),1,0,0,0,1),dr.multiply(Dt),Dt.makeTranslation(-p,-m),dr.multiply(Dt),dr.clone()}function Tf(){const n=[];return{name:"textDecoration",paths:n,update:r=>{n.length=0;const e=[];let t,i;r.forEachCharacter(s=>{const{computedStyle:o,isVertical:a,inlineBox:l,underlinePosition:h,underlineThickness:c,strikeoutPosition:f,strikeoutSize:d}=s,{color:p,textDecoration:m,writingMode:_}=o;if(Ot(m))i=void 0;else{let y=!1;if((i==null?void 0:i.textDecoration)===m&&(i==null?void 0:i.writingMode)===_&&(i==null?void 0:i.color)===p&&(a?t[0].inlineBox.left===l.left:t[0].inlineBox.top===l.top))switch(m){case"underline":t[0].underlinePosition===h&&t[0].underlineThickness===c&&(y=!0);break;case"line-through":t[0].strikeoutPosition===f&&t[0].strikeoutSize===d&&(y=!0);break}y?t.push(s):(t=[],t.push(s),e.push(t)),i=o}}),e.forEach(s=>{const{computedStyle:o,isVertical:a,underlinePosition:l,underlineThickness:h,strikeoutPosition:c,strikeoutSize:f}=s[0],{color:d,textDecoration:p}=o,{left:m,top:_,width:y,height:S}=Y.from(...s.map(P=>P.inlineBox));let C=a?m+y:_;const x=a?-1:1;let T=0;switch(p){case"overline":T=h*2;break;case"underline":C+=x*l,T=h*2;break;case"line-through":C+=x*c,T=f*2;break}C-=T;let b;a?b=new we([{type:"M",x:C,y:_},{type:"L",x:C,y:_+S},{type:"L",x:C+T,y:_+S},{type:"L",x:C+T,y:_},{type:"Z"}],{fill:d}):b=new we([{type:"M",x:m,y:C},{type:"L",x:m+y,y:C},{type:"L",x:m+y,y:C+T},{type:"L",x:m,y:C+T},{type:"Z"}],{fill:d}),n.push(b)})},render:(r,e)=>{const{effects:t,computedStyle:i}=e;t?t.forEach(s=>{r.save();const[o,a,l,h,c,f]=Js(e,s).transpose().elements;r.transform(o,h,a,c,l,f),n.forEach(d=>{zr({ctx:r,path:d,fontSize:i.fontSize,...s})}),r.restore()}):n.forEach(s=>{zr({ctx:r,path:s,fontSize:i.fontSize})})}}}const Qs=mc();class Sf extends wf{constructor(e={}){super();u(this,"debug");u(this,"content");u(this,"style");u(this,"effects");u(this,"measureDom");u(this,"needsUpdate",!0);u(this,"computedStyle",{...Qs});u(this,"paragraphs",[]);u(this,"lineBox",new Y);u(this,"rawGlyphBox",new Y);u(this,"glyphBox",new Y);u(this,"pathBox",new Y);u(this,"boundingBox",new Y);u(this,"measurer",new Zs);u(this,"plugins",new Map);u(this,"fonts");this.debug=e.debug??!1,this.content=e.content??"",this.style=e.style??{},this.measureDom=e.measureDom,this.effects=e.effects,this.fonts=e.fonts,this.use(Cf()).use(Tf()).use(xf()).use(Ef()),this.updateParagraphs()}get fontSize(){return this.computedStyle.fontSize}get isVertical(){return this.computedStyle.writingMode.includes("vertical")}get characters(){return this.paragraphs.flatMap(e=>e.fragments.flatMap(t=>t.characters))}use(e){return this.plugins.set(e.name,e),this}forEachCharacter(e){return this.paragraphs.forEach((t,i)=>{t.fragments.forEach((s,o)=>{s.characters.forEach((a,l)=>{e(a,{paragraphIndex:i,fragmentIndex:o,characterIndex:l})})})}),this}async load(){await Promise.all(Array.from(this.plugins.values()).map(e=>{var t;return(t=e.load)==null?void 0:t.call(e,this)}))}updateParagraphs(){this.computedStyle={...Qs,...this.style};let{content:e,computedStyle:t}=this;const i=[];if(typeof e=="string"){const s=new Vr({},t);s.addFragment(e),i.push(s)}else{e=Array.isArray(e)?e:[e];for(const s of e)if(typeof s=="string"){const o=new Vr({},t);o.addFragment(s),i.push(o)}else if(Array.isArray(s)){const o=new Vr({},t);s.forEach(a=>{if(typeof a=="string")o.addFragment(a);else{const{content:l,...h}=a;l!==void 0&&o.addFragment(l,h)}}),i.push(o)}else if("fragments"in s){const{fragments:o,...a}=s,l=new Vr(a,t);o.forEach(h=>{const{content:c,...f}=h;c!==void 0&&l.addFragment(c,f)}),i.push(l)}else if("content"in s){const{content:o,...a}=s;if(o!==void 0){const l=new Vr(a,t);l.addFragment(o),i.push(l)}}}return this.paragraphs=i,this}measure(e=this.measureDom){const t={paragraphs:this.paragraphs,lineBox:this.lineBox,rawGlyphBox:this.rawGlyphBox,glyphBox:this.glyphBox,pathBox:this.pathBox,boundingBox:this.boundingBox};this.updateParagraphs();const i=this.measurer.measure(this.paragraphs,this.computedStyle,e);this.paragraphs=i.paragraphs,this.lineBox=i.boundingBox,this.characters.forEach(s=>{s.update(this.fonts)}),this.rawGlyphBox=this.getGlyphBox(),Array.from(this.plugins.values()).sort((s,o)=>(s.updateOrder??0)-(o.updateOrder??0)).forEach(s=>{var o;(o=s.update)==null||o.call(s,this)}),this.glyphBox=this.getGlyphBox(),this.updatePathBox().updateBoundingBox();for(const s in t)i[s]=this[s],this[s]=t[s];return this.emit("measure",{text:this,result:i}),i}getGlyphBox(){const e=O.MAX,t=O.MIN;return this.characters.forEach(i=>{if(!i.getGlyphMinMax(e,t)){const{inlineBox:s}=i,o=new O(s.left,s.top),a=new O(s.left+s.width,s.top+s.height);e.min(o,a),t.max(o,a)}}),new Y(e.x,e.y,t.x-e.x,t.y-e.y)}updatePathBox(){return this.pathBox=Y.from(this.glyphBox,...Array.from(this.plugins.values()).map(e=>e.getBoundingBox?e.getBoundingBox(this):new vs(e.paths??[]).getBoundingBox()).filter(Boolean)),this}updateBoundingBox(){const{lineBox:e,rawGlyphBox:t,pathBox:i}=this,s=Math.min(i.left,i.left+e.left-t.left),o=Math.min(i.top,i.top+e.top-t.top),a=Math.max(i.right,i.right+e.right-t.right),l=Math.max(i.bottom,i.bottom+e.bottom-t.bottom);return this.boundingBox=new Y(s,o,a-s,l-o),this}requestUpdate(){return this.needsUpdate=!0,this}update(){const e=this.measure();for(const t in e)this[t]=e[t];return this.emit("update",{text:this}),this.needsUpdate=!1,this}render(e){const{view:t,pixelRatio:i=2}=e,s=t.getContext("2d");s&&(this.needsUpdate&&this.update(),ff(s,i,this.boundingBox),df(s,this),Array.from(this.plugins.values()).sort((o,a)=>(o.renderOrder??0)-(a.renderOrder??0)).forEach(o=>{var a;if(o.render)(a=o.render)==null||a.call(o,s,this);else if(o.paths){const l=this.computedStyle;o.paths.forEach(h=>{zr({ctx:s,path:h,fontSize:l.fontSize})})}}),this.emit("render",{text:this,view:t,pixelRatio:i}))}}var Mf=Object.defineProperty,Af=Object.getOwnPropertyDescriptor,Wt=(n,r,e,t)=>{for(var i=t>1?void 0:t?Af(r,e):r,s=n.length-1,o;s>=0;s--)(o=n[s])&&(i=(t?o(r,e,i):o(i))||i);return t&&i&&Mf(r,e,i),i};const Of=new Set(Object.keys(Qs));g.Text2D=class extends g.Node2D{constructor(e){super();u(this,"effects");u(this,"measureDom");u(this,"fonts");u(this,"text",new Sf);u(this,"texture",new de(document.createElement("canvas")));u(this,"_subTextsCount",0);this.setProperties(e)}_onUpdateProperty(e,t,i){switch(super._onUpdateProperty(e,t,i),e){case"content":case"effects":case"measureDom":case"fonts":case"split":this._updateSplit(),this.requestRedraw();break}this._subTextsCount&&e==="effects"&&this._getSubTexts().forEach(s=>{s.setProperties({[e]:t})})}_updateText(){this.text.style=this.style.toJSON(),this.text.content=this.content??"",this.text.effects=this.effects,this.text.fonts=this.fonts,this.text.measureDom=this.measureDom}_onUpdateStyleProperty(e,t,i){if(e!=="height"){switch(super._onUpdateStyleProperty(e,t,i),e){case"width":this.split&&this._updateSubTexts();break}typeof e=="string"&&Of.has(e)&&(this._subTextsCount&&e!=="width"&&e!=="height"&&this._getSubTexts().forEach(s=>{s.style.setProperties({[e]:t})}),this.requestRedraw())}}_getSubTexts(){return this.getChildren(vr.FRONT).filter(e=>e instanceof g.Text2D)}_updateSubTexts(){const e=this._getSubTexts(),t=this.measure();let i=0;this.split&&t.paragraphs.forEach(s=>{s.fragments.forEach(o=>{o.characters.forEach(a=>{const l=e[i];l&&(l.style.left=a.inlineBox.left,l.style.top=a.inlineBox.top),i++})})})}measure(){this._updateText();const e=this.text.measure();return this.style.width||(this.style.width=e.boundingBox.width),this.style.height||(this.style.height=e.boundingBox.height),e}_updateSplit(){this._subTextsCount&&(this.getChildren(vr.FRONT).forEach(t=>this.removeChild(t)),this._subTextsCount=0);const e=this.measure();this.split&&e.paragraphs.forEach(t=>{t.fragments.forEach(i=>{i.characters.forEach(s=>{this.addChild(new g.Text2D({pixelRatio:this.pixelRatio,content:s.content,style:{...s.computedStyle,left:s.inlineBox.x,top:s.inlineBox.y,width:0,height:0,effects:this.effects}}),vr.FRONT),this._subTextsCount++})})})}_drawContent(){var e,t;if(!this.split){const i=(t=(e=this.children)==null?void 0:e.find(s=>"onText2DRender"in s))==null?void 0:t.onText2DRender;i?i():(this._updateText(),this.text.render({pixelRatio:this.pixelRatio,view:this.texture.source})),this.texture.requestUpload(),this.context.fillStyle=this.texture,this.context.textureTransform=new qe().scale(1/this.pixelRatio,1/this.pixelRatio),super._drawContent()}}},Wt([A({default:2})],g.Text2D.prototype,"pixelRatio",2),Wt([A({default:!1})],g.Text2D.prototype,"split",2),Wt([A({default:""})],g.Text2D.prototype,"content",2),Wt([A()],g.Text2D.prototype,"effects",2),Wt([ee()],g.Text2D.prototype,"measureDom",2),Wt([ee()],g.Text2D.prototype,"fonts",2),g.Text2D=Wt([H("Text2D")],g.Text2D);var Uf=Object.defineProperty,Df=Object.getOwnPropertyDescriptor,fa=(n,r,e,t)=>{for(var i=t>1?void 0:t?Df(r,e):r,s=n.length-1,o;s>=0;s--)(o=n[s])&&(i=(t?o(r,e,i):o(i))||i);return t&&i&&Uf(r,e,i),i};g.Video2D=class extends g.Node2D{constructor(e){super();u(this,"texture");u(this,"_wait",Promise.resolve());this.setProperties(e)}get duration(){var e;return(((e=this.texture)==null?void 0:e.duration)??0)*1e3}_onUpdateProperty(e,t,i){switch(super._onUpdateProperty(e,t,i),e){case"src":this._wait=this._load(t);break}}waitLoad(){return this._wait}async _load(e){this.texture=await mt.video.load(e),(!this.style.width||!this.style.height)&&(this.style.width=this.texture.width,this.style.height=this.texture.height),this.requestRedraw()}_drawContent(){const e=this.texture;e&&(this.context.fillStyle=e,this.context.textureTransform=new qe().scale(this.style.width/e.width,this.style.height/e.height)),super._drawContent()}_updateVideoCurrentTime(){let e=this.visibleRelativeTime;if(e<0)return;const t=this.texture;if(!t)return;const i=t.duration;e=i?e%(i*1e3):0,!t.isPlaying&&!t.seeking&&(e=~~e/1e3,t.currentTime!==e&&(t.currentTime=e))}_process(e){this._updateVideoCurrentTime(),super._process(e)}},fa([A({default:""})],g.Video2D.prototype,"src",2),g.Video2D=fa([H("Video2D")],g.Video2D);const Ni=class Ni extends dt{constructor(){super(...arguments);u(this,"playbackRate",1);u(this,"muted",!1);u(this,"volume",1);u(this,"paused",!1)}static get instance(){return this._instance||(this._instance=new Ni),this._instance}refresh(){this.emit("refresh")}refreshPaused(){this.emit("refreshPaused")}get processors(){return console.warn("HTML Audio does not support processors"),[]}set processors(e){console.warn("HTML Audio does not support processors")}get audioContext(){return console.warn("HTML Audio does not support audioContext"),null}toggleMute(){return this.muted=!this.muted,this.refresh(),this.muted}togglePause(){return this.paused=!this.paused,this.refreshPaused(),this.paused}destroy(){this.removeAllListeners()}};u(Ni,"_instance");let Ui=Ni;const Hr=class Hr extends dt{constructor(){super(...arguments);u(this,"_source",null);u(this,"_audio",null);u(this,"_end",0);u(this,"_pausedReal",!1);u(this,"_duration",0);u(this,"_start",0);u(this,"_playing",!1);u(this,"_paused",!1);u(this,"_playbackRate",1);u(this,"_volume",1);u(this,"_loop",!1);u(this,"_muted",!1);u(this,"_onUpdate",()=>{this._source&&(this.emit("progress",this.progress,this._duration),this._source.currentTime>=this._end&&!this._source.loop&&this._onComplete())})}get progress(){var e;return(((e=this._source)==null?void 0:e.currentTime)??0)/this._duration}get paused(){return this._paused}set paused(e){this._paused!==e&&(this._paused=e,this.refreshPaused())}get playbackRate(){return this._playbackRate}set playbackRate(e){this._playbackRate!==e&&(this._playbackRate=e,this.refresh())}get volume(){return this._volume}set volume(e){this._volume!==e&&(this._volume=e,this.refresh())}get loop(){return this._loop}set loop(e){this._loop!==e&&(this._loop=e,this.refresh())}get muted(){return this._muted}set muted(e){this._muted!==e&&(this._muted=e,this.refresh())}set(e,t){if(this[e]===void 0)throw new Error(`Property with name ${e} does not exist.`);switch(e){case"playbackRate":this.playbackRate=t;break;case"volume":this.volume=t;break;case"paused":this.paused=t;break;case"loop":this.loop=t;break;case"muted":this.muted=t;break}return this}_onPlay(){this._playing=!0}_onPause(){this._playing=!1}init(e){this._playing=!1,this._duration=e.source.duration;const t=this._source=e.source.cloneNode(!1);return t.src=e.parent.src,t.onplay=this._onPlay.bind(this),t.onpause=this._onPause.bind(this),e.context.on("refresh",this.refresh),e.context.on("refreshPaused",this.refreshPaused),this._audio=e,this}_stop(){this._source&&this._playing&&(this._source.onended=null,this._source.pause())}stop(){this._stop(),this._source&&this.emit("stop")}get processors(){return console.warn("HTML Audio does not support processors"),[]}set processors(e){console.warn("HTML Audio does not support processors")}refresh(){if(!this._audio||!this._source)return;const e=this._audio.context,t=this._audio.parent;this._source.loop=this._loop||t.loop;const i=e.volume*(e.muted?0:1),s=t.volume*(t.muted?0:1),o=this._volume*(this._muted?0:1);this._source.volume=o*i*s,this._source.playbackRate=this._playbackRate*e.playbackRate*t.playbackRate}refreshPaused(){if(!this._source||!this._audio)return;const e=this._paused||this._audio.parent.paused||this._audio.context.paused;e!==this._pausedReal&&(this._pausedReal=e,e?(this._stop(),this.emit("paused")):(this.emit("resumed"),this.play({start:this._source.currentTime,end:this._end,volume:this._volume,playbackRate:this._playbackRate,loop:this._loop})),this.emit("pause",e))}play(e={}){if(!this._source)return;const{start:t=0,end:i=0}=e;i&&console.assert(i>t,"End time is before start time"),e.playbackRate!==void 0&&(this._playbackRate=e.playbackRate),e.volume!==void 0&&(this._volume=e.volume),e.loop!==void 0&&(this._loop=e.loop),e.muted!==void 0&&(this._muted=e.muted),this.refresh(),this.loop&&i!==null&&(console.warn('Looping not support when specifying an "end" time'),this.loop=!1),this._start=t,this._end=i||this._duration,this._start=Math.max(0,this._start-Hr.PADDING),this._end=Math.min(this._end+Hr.PADDING,this._duration),this._source.onloadedmetadata=()=>{this._source&&(this._source.currentTime=t,this._source.onloadedmetadata=null,this.emit("progress",t,this._duration),ue.on(this._onUpdate))},this._source.onended=this._onComplete.bind(this),this._source.play(),this.emit("start")}_onComplete(){ue.off(this._onUpdate),this._stop(),this.emit("progress",1,this._duration),this.emit("end",this)}destroy(){ue.off(this._onUpdate),this.removeAllListeners();const e=this._source;e&&(e.onended=null,e.onplay=null,e.onpause=null,this._stop()),this._source=null,this._playbackRate=1,this._volume=1,this._loop=!1,this._end=0,this._start=0,this._duration=0,this._playing=!1,this._pausedReal=!1,this._paused=!1,this._muted=!1,this._audio&&(this._audio.context.off("refresh",this.refresh),this._audio.context.off("refreshPaused",this.refreshPaused),this._audio=null)}};u(Hr,"PADDING",.1);let Di=Hr;class da{constructor(r){u(this,"source",new globalThis.Audio);u(this,"_src","");this.parent=r}get src(){return this._src}set src(r){this._src!==r&&(this._src=r,this.load())}get duration(){return this.source.duration}get isPlayable(){return!!this.source&&this.source.readyState===4}get context(){return Ui.instance}async load(){return new Promise(r=>{this.source.onload=()=>r(this),this.source.src=this._src,this.source.load()})}createSound(){return new Di}}class en extends dt{constructor(e,t){super();u(this,"_processers",[]);this._input=e,this._output=t}get processors(){return this._processers}set processors(e){if(this._processers.forEach(t=>t.disconnect()),this._processers.length=0,this._input.connect(this._output),e.length){this._processers=e.slice(0),this._input.disconnect();let t;e.forEach(i=>{t?t.connect(i.destination):this._input.connect(i.destination),t=i}),t.connect(this._output)}}get destination(){return this._input}}class If{constructor(r,e=null){this.destination=r,this.source=e}connect(r){var e;(e=this.source)==null||e.connect(r)}disconnect(){var r;(r=this.source)==null||r.disconnect()}}function Rf(){if(qi)return new AudioContext;if(Wi){const n=globalThis.webkitAudioContext;return new n}else throw new Error("Failed to createAudioContext")}function Bf(n,r,e){if(Xi)return new OfflineAudioContext(n,r,e);if(yn){const t=globalThis.webkitOfflineAudioContext;return new t(n,r,e)}else throw new Error("Failed to createOfflineAudioContext")}const Fi=class Fi extends en{constructor(){const e=Rf(),t=Bf(1,2,Xi?Math.max(8e3,Math.min(96e3,e.sampleRate)):44100),i=e.createDynamicsCompressor(),s=e.createAnalyser();s.connect(i),i.connect(e.destination);super(s,i);u(this,"_context");u(this,"_offlineContext");u(this,"_locked");u(this,"muted",!1);u(this,"volume",1);u(this,"playbackRate",1);u(this,"autoPause",!0);u(this,"_paused",!1);u(this,"_pausedOnBlur",!1);u(this,"_compressor");u(this,"_analyser");u(this,"_unlock",()=>{this._locked&&(this.playEmptySound(),this._context.state==="running"&&(document.removeEventListener("mousedown",this._unlock,!0),document.removeEventListener("touchend",this._unlock,!0),document.removeEventListener("touchstart",this._unlock,!0),this._locked=!1))});this._context=e,this._offlineContext=t,this._compressor=i,this._analyser=s,this._locked=e.state==="suspended"&&(ft||mn),_r&&(this._locked&&(this._unlock(),document.addEventListener("mousedown",this._unlock,!0),document.addEventListener("touchstart",this._unlock,!0),document.addEventListener("touchend",this._unlock,!0)),globalThis.addEventListener("focus",this._onFocus),globalThis.addEventListener("blur",this._onBlur))}static get instance(){return this._instance||(this._instance=new Fi),this._instance}static get audioContext(){return this.instance.audioContext}static get offlineContext(){return this.instance.offlineContext}static setParamValue(e,t){this.instance.setParamValue(e,t)}static decode(e){return this.instance.decode(e)}get audioContext(){return this._context}get offlineContext(){return this._offlineContext}get paused(){return this._paused}set paused(e){e&&this._context.state==="running"?this._context.suspend():!e&&this._context.state==="suspended"&&this._context.resume(),this._paused=e}_onFocus(){if(!this.autoPause)return;const e=this._context.state;(e==="suspended"||e==="interrupted"||!this._locked)&&(this.paused=this._pausedOnBlur,this.refreshPaused())}_onBlur(){this.autoPause&&(this._locked||(this._pausedOnBlur=this._paused,this.paused=!0,this.refreshPaused()))}playEmptySound(){const e=this._context.createBufferSource();e.buffer=this._context.createBuffer(1,1,22050),e.connect(this._context.destination),e.start(0,0,0),e.context.state==="suspended"&&e.context.resume()}refresh(){this.emit("refresh")}refreshPaused(){this.emit("refreshPaused")}toggleMute(){return this.muted=!this.muted,this.refresh(),this.muted}togglePause(){return this.paused=!this.paused,this.refreshPaused(),this._paused}decode(e){return new Promise((t,i)=>{const s=a=>{i(new Error((a==null?void 0:a.message)||"Unable to decode file"))},o=this._offlineContext.decodeAudioData(e,t,s);o&&o.catch(s)})}setParamValue(e,t){e.setValueAtTime?e.setValueAtTime(t,this._context.currentTime):e.value=t}};u(Fi,"_instance");let It=Fi;class pa extends dt{constructor(){super(...arguments);u(this,"_audio",null);u(this,"_sourceNode",null);u(this,"_gain",null);u(this,"_progress",0);u(this,"_pausedReal",!1);u(this,"_paused",!1);u(this,"_volume",1);u(this,"_playbackRate",1);u(this,"_loop",!1);u(this,"_muted",!1);u(this,"_duration",0);u(this,"_end",0);u(this,"_elapsed",0);u(this,"_lastUpdate",this._now());u(this,"_processors",[]);u(this,"_onComplete",()=>{if(this._sourceNode){this._enableTicker(!1),this._sourceNode.onended=null,this._sourceNode.disconnect();try{this._sourceNode.buffer=null}catch(e){console.warn("Failed to set AudioBufferSourceNode.buffer to null:",e)}}this._sourceNode=null,this._progress=1,this.emit("progress",1,this._duration),this.emit("end",this)});u(this,"_updateListener",()=>this._update())}get progress(){return this._progress}get paused(){return this._paused}set paused(e){this._paused!==e&&(this._paused=e,this.refreshPaused())}get volume(){return this._volume}set volume(e){this._volume!==e&&(this._volume=e,this.refresh())}get playbackRate(){return this._playbackRate}set playbackRate(e){this._playbackRate!==e&&(this._playbackRate=e,this.refresh(),this._update(!0))}get loop(){return this._loop}set loop(e){this._loop!==e&&(this._loop=e,this.refresh())}get muted(){return this._muted}set muted(e){this._muted!==e&&(this._muted=e,this.refresh())}init(e){return this._audio=e,e.context.on("refresh",this.refresh),e.context.on("refreshPaused",this.refreshPaused),this}_now(){var e;return((e=this._audio)==null?void 0:e.context.audioContext.currentTime)??0}play(e={}){var a;if(!this._audio)return;const{end:t=0,start:i=0}=e;t&&console.assert(t>i,"End time is before start time"),this._end=t,this._elapsed=i,e.volume!==void 0&&(this._volume=e.volume),e.playbackRate!==void 0&&(this._playbackRate=e.playbackRate),e.muted!==void 0&&(this._muted=e.muted),e.loop!==void 0&&(this._loop=e.loop),e.processors!==void 0&&(this._processors=e.processors),this._paused=!1;const{source:s,gain:o}=this._audio.cloneSource();this._sourceNode=s,this._gain=o,this.refresh(),s.onended=this._onComplete.bind(this),this._duration=((a=s.buffer)==null?void 0:a.duration)??0,this._lastUpdate=this._now(),this._loop?(s.loopStart=i,s.loopEnd=t,s.start(0,i)):t?s.start(0,i,t-i):s.start(0,i),this.emit("start"),this._update(!0),this._enableTicker(!0)}_stop(){if(this._sourceNode){this._enableTicker(!1),this._sourceNode.onended=null,this._sourceNode.stop(0),this._sourceNode.disconnect();try{this._sourceNode.buffer=null}catch(e){console.warn("Failed to set AudioBufferSourceNode.buffer to null:",e)}this._sourceNode=null}}stop(){this._sourceNode&&(this._stop(),this.emit("stop"))}_update(e=!1){if(this._sourceNode){const t=this._now(),i=t-this._lastUpdate;if(i>0||e){this._elapsed+=i*this._sourceNode.playbackRate.value,this._lastUpdate=t;const s=this._duration;let o;if(this._sourceNode.loopStart){const a=this._sourceNode.loopEnd-this._sourceNode.loopStart;o=(this._sourceNode.loopStart+this._elapsed%a)/s}else o=this._elapsed%s/s;this._progress=o,this.emit("progress",o,s)}}}refresh(){if(!this._audio||!this._sourceNode)return;const e=this._audio.context,t=this._audio.parent;this._sourceNode.loop=this._loop||t.loop,e.setParamValue(this._gain.gain,this._volume*(this._muted?0:1)*t.volume*(t.muted?0:1)*e.volume*(e.muted?0:1)),e.setParamValue(this._sourceNode.playbackRate,this._playbackRate*t.playbackRate*e.playbackRate),this.applyProcessors()}applyProcessors(){if(this._sourceNode&&this._processors.length){this._sourceNode.disconnect();let e=this._sourceNode;this._processors.forEach(t=>{e.connect(t.destination),e=t}),e.connect(this._gain)}}refreshPaused(){if(!this._audio)return;const e=this._paused||this._audio.parent.paused||this._audio.context.paused;e!==this._pausedReal&&(this._pausedReal=e,e?(this._stop(),this.emit("paused")):(this.emit("resumed"),this.play({start:this._elapsed%this._duration,end:this._end,playbackRate:this._playbackRate,loop:this._loop,volume:this._volume})),this.emit("pause",e))}_enableTicker(e){ue.off(this._updateListener),e&&ue.on(this._updateListener)}destroy(){var e,t,i;this.removeAllListeners(),this._stop(),(e=this._gain)==null||e.disconnect(),this._gain=null,(t=this._audio)==null||t.context.off("refresh",this.refresh),(i=this._audio)==null||i.context.off("refreshPaused",this.refreshPaused),this._audio=null,this._processors.forEach(s=>s.disconnect()),this._processors.length=0,this._end=0,this._playbackRate=1,this._volume=1,this._loop=!1,this._elapsed=0,this._duration=0,this._paused=!1,this._muted=!1,this._pausedReal=!1}}class ga extends en{constructor(e){const t=It.audioContext,i=t.createBufferSource(),s=t.createGain(),o=t.createAnalyser();i.connect(o),o.connect(s),s.connect(It.instance.destination);super(o,s);u(this,"_sourceBuffer");u(this,"_sourceNode");u(this,"_sourceLoad");u(this,"gain");u(this,"analyser");this.parent=e,this._sourceNode=i,this.gain=s,this.analyser=o}get context(){return It.instance}get isPlayable(){return!!this._sourceNode.buffer}get duration(){var e;return((e=this._sourceNode.buffer)==null?void 0:e.duration)??0}get buffer(){return this._sourceNode.buffer}set buffer(e){this._sourceNode.buffer=e}async load(){return this._sourceLoad||(this._sourceLoad=new Promise(e=>{this._sourceBuffer?this._decode(this._sourceBuffer).then(()=>e(this)):this.parent.src?this._loadUrl(this.parent.src).then(()=>e(this)):e(this)})),this._sourceLoad}_loadUrl(e){return new Promise(t=>{fetch(e).then(i=>i.arrayBuffer()).then(i=>this._decode(i)).finally(()=>t(this))})}_decode(e){return Promise.resolve(e instanceof AudioBuffer?e:It.decode(e)).then(t=>(this.parent.isLoaded=!0,this.buffer=t,t))}cloneSource(){const e=this.context,t=this._sourceNode,i=e.audioContext.createBufferSource(),s=e.audioContext.createGain();return i.buffer=t.buffer,i.loop=t.loop,e.setParamValue(i.playbackRate,t.playbackRate.value),i.connect(s),s.connect(this.destination),{source:i,gain:s}}createSound(){return new pa}}var ma=Object.defineProperty,Lf=Object.getOwnPropertyDescriptor,kf=(n,r,e)=>r in n?ma(n,r,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[r]=e,Nf=(n,r,e,t)=>{for(var i=t>1?void 0:t?Lf(r,e):r,s=n.length-1,o;s>=0;s--)(o=n[s])&&(i=(t?o(r,e,i):o(i))||i);return t&&i&&ma(r,e,i),i},Ff=(n,r,e)=>kf(n,r+"",e);g.Audio=class extends g.Node{constructor(e=""){super();u(this,"_sounds",[]);u(this,"_platformAudio",_n?new ga(this):new da(this));u(this,"_src");u(this,"isLoaded",!1);u(this,"_volume",1);u(this,"_muted",!1);u(this,"_loop",!1);u(this,"_playbackRate",1);u(this,"_isPlaying",!1);u(this,"_paused",!1);u(this,"multiple",!1);u(this,"start",0);u(this,"end",0);u(this,"_onComplete",e=>{if(this._sounds){const t=this._sounds.indexOf(e);t>-1&&this._sounds.splice(t,1),this._isPlaying=this._sounds.length>0}this._recycleSound(e)});u(this,"_prevTime",0);u(this,"_timer",0);this.src=e}get platformAudio(){return this._platformAudio}get src(){return this._src}set src(e){this._src!==e&&(this._src=e,this.load())}get isPlayable(){return this.isLoaded&&this._platformAudio.isPlayable}get duration(){return this._platformAudio.duration*1e3}get volume(){return this._volume}set volume(e){this._volume!==e&&(this._volume=e,this.refresh())}get muted(){return this._muted}set muted(e){this._muted!==e&&(this._muted=e,this.refresh())}get loop(){return this._loop}set loop(e){this._loop!==e&&(this._loop=e,this.refresh())}get playbackRate(){return this._playbackRate}set playbackRate(e){this._playbackRate!==e&&(this._playbackRate=e,this.refresh())}get isPlaying(){return this._isPlaying}get paused(){return this._paused}set paused(e){this._paused!==e&&(this._paused=e,this.refreshPaused())}async load(){return await this._platformAudio.load(),this}pause(){return this._isPlaying=!1,this.paused=!0,this}resume(){return this._isPlaying=this._sounds.length>0,this.paused=!1,this}stop(){if(!this.isPlayable)return this;this._isPlaying=!1;for(let e=this._sounds.length-1;e>=0;e--)this._sounds[e].stop();return this}play(e={}){if(!this.isLoaded)return;this.multiple||this._removeSounds(),this._isPlaying=!0;const t=this._createSound();this._sounds.push(t),t.once("end",()=>{var o;(o=e.complete)==null||o.call(e),this._onComplete(t)}),t.once("stop",()=>this._onComplete(t));const i=((e==null?void 0:e.start)??this.start)/1e3,s=((e==null?void 0:e.end)??this.end)/1e3;return t.play({...e,start:i,end:s}),t}_removeSounds(){for(let e=this._sounds.length-1;e>=0;e--)this._recycleSound(this._sounds[e]);this._sounds.length=0}_createSound(){return g.Audio._soundPool.length>0?g.Audio._soundPool.pop().init(this._platformAudio):this._platformAudio.createSound().init(this._platformAudio)}refresh(){for(let e=this._sounds.length,t=0;t<e;t++)this._sounds[t].refresh()}refreshPaused(){for(let e=this._sounds.length,t=0;t<e;t++)this._sounds[t].refreshPaused()}_recycleSound(e){e.destroy(),g.Audio._soundPool.includes(e)||g.Audio._soundPool.push(e)}_process(e){var t;if(super._process(e),this.isRenderable()){const i=((t=this._tree)==null?void 0:t.timeline.current)??0;i-this._prevTime>0&&(this._timer||(this._setTimeStop(),this.play({start:this.start+(i-this.visibleDelay)%this.duration})),this._isPlaying&&this._setTimeStop()),this._prevTime=i}}_setTimeStop(){this._timer&&clearTimeout(this._timer),this._timer=setTimeout(()=>{this.stop(),this._timer=0},100)}},Ff(g.Audio,"_soundPool",[]),g.Audio=Nf([H("Audio")],g.Audio);class $f extends g.Node2D{}var zf=Object.defineProperty,Vf=Object.getOwnPropertyDescriptor,Ii=(n,r,e,t)=>{for(var i=t>1?void 0:t?Vf(r,e):r,s=n.length-1,o;s>=0;s--)(o=n[s])&&(i=(t?o(r,e,i):o(i))||i);return t&&i&&zf(r,e,i),i};g.AudioWaveform=class extends g.Node2D{constructor(e={}){super();u(this,"src");u(this,"gap",0);u(this,"color","#000000");u(this,"_audioBuffer");u(this,"_src",_r?new de(document.createElement("canvas")):void 0);u(this,"_needsUpdateTexture",!1);this.setProperties(e)}_onUpdateProperty(e,t,i){switch(super._onUpdateProperty(e,t,i),e){case"src":this._loadSrc(t);break;case"gap":case"color":case"width":case"height":this._needsUpdateTexture=!0;break}}async _loadSrc(e){await globalThis.fetch(e).then(t=>t.arrayBuffer()).then(t=>It.decode(t)).then(t=>{this._audioBuffer=t,this.syncTexture(!0)})}syncTexture(e=!1){var f,d;const t=this._audioBuffer;if(!t||!e&&!this._needsUpdateTexture)return;this._needsUpdateTexture=!1;const i=(f=this._src)==null?void 0:f.source;if(!i)return;const{width:s=0,height:o=0}=this.style;i.width=s,i.height=o;const a=i.getContext("2d");if(!a){console.warn("Failed to getContext('2d') in syncTexture");return}a.fillStyle=this.color;const l=t.getChannelData(0),h=Math.ceil(l.length/s),c=o/2;for(let p=1,m=-1,_=0;_<s;_++){for(let y=0;y<h;y++){const S=l[_*h+y];S<p&&(p=S),S>m&&(m=S)}if(!this.gap||_%(this.gap*2)===0){const y=_,S=(1+p)*c,C=this.gap||1,x=Math.max(1,(m-p)*c);a.fillRect(y,S,C,x),p=1,m=-1}}(d=this._src)==null||d.requestUpload(),this.requestRedraw()}_process(e){this.syncTexture(),super._process(e)}_drawSrc(){const e=this._src;e!=null&&e.valid&&(this.context.fillStyle=e,this.context.textureTransform=new qe().scale(this.style.width/e.width,this.style.height/e.height))}},Ii([A()],g.AudioWaveform.prototype,"src",2),Ii([A()],g.AudioWaveform.prototype,"gap",2),Ii([A()],g.AudioWaveform.prototype,"color",2),g.AudioWaveform=Ii([H("AudioWaveform")],g.AudioWaveform);const jf=/^uniform \w+ (.+?);\s*\/\/\s*=\s*([^;]+?)[\s;]*$/gm,Gf=/vec(\d)\((.*)\)/;function qf(n){n=n.trim();const r=n.match(Gf);if(r){const e=Array.from({length:Number(r[1])},()=>0);return r[2].split(",").forEach((t,i)=>{e[i]=Number(t.trim())}),e}return Number(n)}const Yr=class Yr extends ne{constructor(e){super();u(this,"vert",`attribute vec2 position;
32
+ </svg>`}function Cf(){const n=[];return{name:"listStyle",paths:n,update:r=>{n.length=0;const{paragraphs:e,isVertical:t,fontSize:i}=r,s=i*.45;e.forEach(o=>{const{computedStyle:a}=o,{color:l,listStyleImage:h,listStyleColormap:c,listStyleSize:f,listStyleType:d}=a,p=ca(c);let m=f,_;if(!Ot(h))_=h;else if(!Ot(d)){const x=i*.38/2;switch(m=m==="cover"?x*2:m,d){case"disc":_=Pf(x,String(l));break}}if(!_)return;const y=bo(_),S=y.getBoundingBox();let C;o.fragments.forEach(x=>{x.characters.forEach(T=>{const{inlineBox:b}=T;if(t?(C==null?void 0:C.inlineBox.left)!==b.left:(C==null?void 0:C.inlineBox.top)!==b.top){C=T;const P=m==="cover"?1:Xs(m,{total:i,fontSize:i})/i,E=new ke;if(t){const w=i/S.height*P;E.translate(-S.left,-S.top).rotate(Math.PI/2).scale(w,w).translate(b.left+(b.width-S.height*w)/2,b.top-s)}else{const w=i/S.height*P;E.translate(-S.left,-S.top).scale(w,w).translate(b.left-S.width*w-s,b.top+(b.height-S.height*w)/2)}n.push(...y.paths.map(w=>{const D=w.clone();return D.applyTransform(E),D.style.fill&&D.style.fill in p&&(D.style.fill=p[D.style.fill]),D.style.stroke&&D.style.stroke in p&&(D.style.stroke=p[D.style.stroke]),D}))}})})})}}}const Ut=new O,dr=new ke,Dt=new ke;function Ef(){return{name:"render",getBoundingBox:n=>{const{characters:r,fontSize:e,effects:t}=n,i=[];return r.forEach(s=>{t==null||t.forEach(o=>{if(!s.glyphBox)return;const a=s.glyphBox.clone(),l=Js(n,o);Ut.set(a.left,a.top),Ut.applyMatrix3(l),a.left=Ut.x,a.top=Ut.y,Ut.set(a.right,a.bottom),Ut.applyMatrix3(l),a.width=Ut.x-a.left,a.height=Ut.y-a.top;const h=(o.shadowOffsetX??0)*e,c=(o.shadowOffsetY??0)*e,f=Math.max(.1,o.textStrokeWidth??0)*e;a.left+=h-f,a.top+=c-f,a.width+=f*2,a.height+=f*2,i.push(a)})}),i.length?Y.from(...i):void 0},render:(n,r)=>{const{paragraphs:e,glyphBox:t,effects:i,style:s}=r;function o(a,l){n.fillStyle=a,n.fillRect(l.left,l.top,l.width,l.height)}s!=null&&s.backgroundColor&&o(s.backgroundColor,new Y(0,0,n.canvas.width,n.canvas.height)),e.forEach(a=>{var l;(l=a.style)!=null&&l.backgroundColor&&o(a.style.backgroundColor,a.lineBox)}),i?i.forEach(a=>{Oi(a,t,n),n.save();const[l,h,c,f,d,p]=Js(r,a).transpose().elements;n.transform(l,f,h,d,c,p),r.forEachCharacter(m=>{var _;(_=m.parent.style)!=null&&_.backgroundColor&&o(m.parent.style.backgroundColor,m.inlineBox),m.drawTo(n,a)}),n.restore()}):e.forEach(a=>{a.fragments.forEach(l=>{var h;(h=l.style)!=null&&h.backgroundColor&&o(l.computedStyle.backgroundColor,l.inlineBox),l.characters.forEach(c=>{c.drawTo(n)})})}),r.debug&&e.forEach(a=>{n.strokeRect(a.lineBox.x,a.lineBox.y,a.lineBox.width,a.lineBox.height)})}}}function Js(n,r){const{fontSize:e,glyphBox:t}=n,i=(r.translateX??0)*e,s=(r.translateY??0)*e,o=Math.PI*2,a=(r.skewX??0)/360*o,l=(r.skewY??0)/360*o,{left:h,top:c,width:f,height:d}=t,p=h+f/2,m=c+d/2;return dr.identity(),Dt.makeTranslation(i,s),dr.multiply(Dt),Dt.makeTranslation(p,m),dr.multiply(Dt),Dt.set(1,Math.tan(a),0,Math.tan(l),1,0,0,0,1),dr.multiply(Dt),Dt.makeTranslation(-p,-m),dr.multiply(Dt),dr.clone()}function Tf(){const n=[];return{name:"textDecoration",paths:n,update:r=>{n.length=0;const e=[];let t,i;r.forEachCharacter(s=>{const{computedStyle:o,isVertical:a,inlineBox:l,underlinePosition:h,underlineThickness:c,strikeoutPosition:f,strikeoutSize:d}=s,{color:p,textDecoration:m,writingMode:_}=o;if(Ot(m))i=void 0;else{let y=!1;if((i==null?void 0:i.textDecoration)===m&&(i==null?void 0:i.writingMode)===_&&(i==null?void 0:i.color)===p&&(a?t[0].inlineBox.left===l.left:t[0].inlineBox.top===l.top))switch(m){case"underline":t[0].underlinePosition===h&&t[0].underlineThickness===c&&(y=!0);break;case"line-through":t[0].strikeoutPosition===f&&t[0].strikeoutSize===d&&(y=!0);break}y?t.push(s):(t=[],t.push(s),e.push(t)),i=o}}),e.forEach(s=>{const{computedStyle:o,isVertical:a,underlinePosition:l,underlineThickness:h,strikeoutPosition:c,strikeoutSize:f}=s[0],{color:d,textDecoration:p}=o,{left:m,top:_,width:y,height:S}=Y.from(...s.map(P=>P.inlineBox));let C=a?m+y:_;const x=a?-1:1;let T=0;switch(p){case"overline":T=h*2;break;case"underline":C+=x*l,T=h*2;break;case"line-through":C+=x*c,T=f*2;break}C-=T;let b;a?b=new we([{type:"M",x:C,y:_},{type:"L",x:C,y:_+S},{type:"L",x:C+T,y:_+S},{type:"L",x:C+T,y:_},{type:"Z"}],{fill:d}):b=new we([{type:"M",x:m,y:C},{type:"L",x:m+y,y:C},{type:"L",x:m+y,y:C+T},{type:"L",x:m,y:C+T},{type:"Z"}],{fill:d}),n.push(b)})},render:(r,e)=>{const{effects:t,computedStyle:i}=e;t?t.forEach(s=>{r.save();const[o,a,l,h,c,f]=Js(e,s).transpose().elements;r.transform(o,h,a,c,l,f),n.forEach(d=>{zr({ctx:r,path:d,fontSize:i.fontSize,...s})}),r.restore()}):n.forEach(s=>{zr({ctx:r,path:s,fontSize:i.fontSize})})}}}const Qs=mc();class Sf extends wf{constructor(e={}){super();u(this,"debug");u(this,"content");u(this,"style");u(this,"effects");u(this,"measureDom");u(this,"needsUpdate",!0);u(this,"computedStyle",{...Qs});u(this,"paragraphs",[]);u(this,"lineBox",new Y);u(this,"rawGlyphBox",new Y);u(this,"glyphBox",new Y);u(this,"pathBox",new Y);u(this,"boundingBox",new Y);u(this,"measurer",new Zs);u(this,"plugins",new Map);u(this,"fonts");this.debug=e.debug??!1,this.content=e.content??"",this.style=e.style??{},this.measureDom=e.measureDom,this.effects=e.effects,this.fonts=e.fonts,this.use(Cf()).use(Tf()).use(xf()).use(Ef()),this.updateParagraphs()}get fontSize(){return this.computedStyle.fontSize}get isVertical(){return this.computedStyle.writingMode.includes("vertical")}get characters(){return this.paragraphs.flatMap(e=>e.fragments.flatMap(t=>t.characters))}use(e){return this.plugins.set(e.name,e),this}forEachCharacter(e){return this.paragraphs.forEach((t,i)=>{t.fragments.forEach((s,o)=>{s.characters.forEach((a,l)=>{e(a,{paragraphIndex:i,fragmentIndex:o,characterIndex:l})})})}),this}async load(){await Promise.all(Array.from(this.plugins.values()).map(e=>{var t;return(t=e.load)==null?void 0:t.call(e,this)}))}updateParagraphs(){this.computedStyle={...Qs,...this.style};let{content:e,computedStyle:t}=this;const i=[];if(typeof e=="string"){const s=new Vr({},t);s.addFragment(e),i.push(s)}else{e=Array.isArray(e)?e:[e];for(const s of e)if(typeof s=="string"){const o=new Vr({},t);o.addFragment(s),i.push(o)}else if(Array.isArray(s)){const o=new Vr({},t);s.forEach(a=>{if(typeof a=="string")o.addFragment(a);else{const{content:l,...h}=a;l!==void 0&&o.addFragment(l,h)}}),i.push(o)}else if("fragments"in s){const{fragments:o,...a}=s,l=new Vr(a,t);o.forEach(h=>{const{content:c,...f}=h;c!==void 0&&l.addFragment(c,f)}),i.push(l)}else if("content"in s){const{content:o,...a}=s;if(o!==void 0){const l=new Vr(a,t);l.addFragment(o),i.push(l)}}}return this.paragraphs=i,this}measure(e=this.measureDom){const t={paragraphs:this.paragraphs,lineBox:this.lineBox,rawGlyphBox:this.rawGlyphBox,glyphBox:this.glyphBox,pathBox:this.pathBox,boundingBox:this.boundingBox};this.updateParagraphs();const i=this.measurer.measure(this.paragraphs,this.computedStyle,e);this.paragraphs=i.paragraphs,this.lineBox=i.boundingBox,this.characters.forEach(s=>{s.update(this.fonts)}),this.rawGlyphBox=this.getGlyphBox(),Array.from(this.plugins.values()).sort((s,o)=>(s.updateOrder??0)-(o.updateOrder??0)).forEach(s=>{var o;(o=s.update)==null||o.call(s,this)}),this.glyphBox=this.getGlyphBox(),this.updatePathBox().updateBoundingBox();for(const s in t)i[s]=this[s],this[s]=t[s];return this.emit("measure",{text:this,result:i}),i}getGlyphBox(){const e=O.MAX,t=O.MIN;return this.characters.forEach(i=>{if(!i.getGlyphMinMax(e,t)){const{inlineBox:s}=i,o=new O(s.left,s.top),a=new O(s.left+s.width,s.top+s.height);e.min(o,a),t.max(o,a)}}),new Y(e.x,e.y,t.x-e.x,t.y-e.y)}updatePathBox(){return this.pathBox=Y.from(this.glyphBox,...Array.from(this.plugins.values()).map(e=>e.getBoundingBox?e.getBoundingBox(this):new vs(e.paths??[]).getBoundingBox()).filter(Boolean)),this}updateBoundingBox(){const{lineBox:e,rawGlyphBox:t,pathBox:i}=this,s=Math.min(i.left,i.left+e.left-t.left),o=Math.min(i.top,i.top+e.top-t.top),a=Math.max(i.right,i.right+e.right-t.right),l=Math.max(i.bottom,i.bottom+e.bottom-t.bottom);return this.boundingBox=new Y(s,o,a-s,l-o),this}requestUpdate(){return this.needsUpdate=!0,this}update(){const e=this.measure();for(const t in e)this[t]=e[t];return this.emit("update",{text:this}),this.needsUpdate=!1,this}render(e){const{view:t,pixelRatio:i=2}=e,s=t.getContext("2d");s&&(this.needsUpdate&&this.update(),ff(s,i,this.boundingBox),df(s,this),Array.from(this.plugins.values()).sort((o,a)=>(o.renderOrder??0)-(a.renderOrder??0)).forEach(o=>{var a;if(o.render)(a=o.render)==null||a.call(o,s,this);else if(o.paths){const l=this.computedStyle;o.paths.forEach(h=>{zr({ctx:s,path:h,fontSize:l.fontSize})})}}),this.emit("render",{text:this,view:t,pixelRatio:i}))}}var Mf=Object.defineProperty,Af=Object.getOwnPropertyDescriptor,Wt=(n,r,e,t)=>{for(var i=t>1?void 0:t?Af(r,e):r,s=n.length-1,o;s>=0;s--)(o=n[s])&&(i=(t?o(r,e,i):o(i))||i);return t&&i&&Mf(r,e,i),i};const Of=new Set(Object.keys(Qs));g.Text2D=class extends g.Node2D{constructor(e){super();u(this,"effects");u(this,"measureDom");u(this,"fonts");u(this,"text",new Sf);u(this,"texture",new de(document.createElement("canvas")));u(this,"_subTextsCount",0);this.setProperties(e)}_onUpdateProperty(e,t,i){switch(super._onUpdateProperty(e,t,i),e){case"content":case"effects":case"measureDom":case"fonts":case"split":this._updateSplit(),this.requestRedraw();break}this._subTextsCount&&e==="effects"&&this._getSubTexts().forEach(s=>{s.setProperties({[e]:t})})}_updateText(){this.text.style=this.style.toJSON(),this.text.content=this.content??"",this.text.effects=this.effects,this.text.fonts=this.fonts,this.text.measureDom=this.measureDom,this.text.requestUpdate()}_onUpdateStyleProperty(e,t,i){if(e!=="height"){switch(super._onUpdateStyleProperty(e,t,i),e){case"width":this.split&&this._updateSubTexts();break}typeof e=="string"&&Of.has(e)&&(this._subTextsCount&&e!=="width"&&e!=="height"&&this._getSubTexts().forEach(s=>{s.style.setProperties({[e]:t})}),this.requestRedraw())}}_getSubTexts(){return this.getChildren(vr.FRONT).filter(e=>e instanceof g.Text2D)}_updateSubTexts(){const e=this._getSubTexts(),t=this.measure();let i=0;this.split&&t.paragraphs.forEach(s=>{s.fragments.forEach(o=>{o.characters.forEach(a=>{const l=e[i];l&&(l.style.left=a.inlineBox.left,l.style.top=a.inlineBox.top),i++})})})}measure(){this._updateText();const e=this.text.measure();return this.style.width||(this.style.width=e.boundingBox.width),this.style.height||(this.style.height=e.boundingBox.height),e}_updateSplit(){this._subTextsCount&&(this.getChildren(vr.FRONT).forEach(t=>this.removeChild(t)),this._subTextsCount=0);const e=this.measure();this.split&&e.paragraphs.forEach(t=>{t.fragments.forEach(i=>{i.characters.forEach(s=>{this.addChild(new g.Text2D({pixelRatio:this.pixelRatio,content:s.content,style:{...s.computedStyle,left:s.inlineBox.x,top:s.inlineBox.y,width:0,height:0,effects:this.effects}}),vr.FRONT),this._subTextsCount++})})})}_drawContent(){var e,t;if(!this.split){const i=(t=(e=this.children)==null?void 0:e.find(s=>"onText2DRender"in s))==null?void 0:t.onText2DRender;i?i():(this._updateText(),this.text.render({pixelRatio:this.pixelRatio,view:this.texture.source})),this.texture.requestUpload(),this.context.fillStyle=this.texture,this.context.textureTransform=new qe().scale(1/this.pixelRatio,1/this.pixelRatio),super._drawContent()}}},Wt([A({default:2})],g.Text2D.prototype,"pixelRatio",2),Wt([A({default:!1})],g.Text2D.prototype,"split",2),Wt([A({default:""})],g.Text2D.prototype,"content",2),Wt([A()],g.Text2D.prototype,"effects",2),Wt([ee()],g.Text2D.prototype,"measureDom",2),Wt([ee()],g.Text2D.prototype,"fonts",2),g.Text2D=Wt([H("Text2D")],g.Text2D);var Uf=Object.defineProperty,Df=Object.getOwnPropertyDescriptor,fa=(n,r,e,t)=>{for(var i=t>1?void 0:t?Df(r,e):r,s=n.length-1,o;s>=0;s--)(o=n[s])&&(i=(t?o(r,e,i):o(i))||i);return t&&i&&Uf(r,e,i),i};g.Video2D=class extends g.Node2D{constructor(e){super();u(this,"texture");u(this,"_wait",Promise.resolve());this.setProperties(e)}get duration(){var e;return(((e=this.texture)==null?void 0:e.duration)??0)*1e3}_onUpdateProperty(e,t,i){switch(super._onUpdateProperty(e,t,i),e){case"src":this._wait=this._load(t);break}}waitLoad(){return this._wait}async _load(e){this.texture=await mt.video.load(e),(!this.style.width||!this.style.height)&&(this.style.width=this.texture.width,this.style.height=this.texture.height),this.requestRedraw()}_drawContent(){const e=this.texture;e&&(this.context.fillStyle=e,this.context.textureTransform=new qe().scale(this.style.width/e.width,this.style.height/e.height)),super._drawContent()}_updateVideoCurrentTime(){let e=this.visibleRelativeTime;if(e<0)return;const t=this.texture;if(!t)return;const i=t.duration;e=i?e%(i*1e3):0,!t.isPlaying&&!t.seeking&&(e=~~e/1e3,t.currentTime!==e&&(t.currentTime=e))}_process(e){this._updateVideoCurrentTime(),super._process(e)}},fa([A({default:""})],g.Video2D.prototype,"src",2),g.Video2D=fa([H("Video2D")],g.Video2D);const Ni=class Ni extends dt{constructor(){super(...arguments);u(this,"playbackRate",1);u(this,"muted",!1);u(this,"volume",1);u(this,"paused",!1)}static get instance(){return this._instance||(this._instance=new Ni),this._instance}refresh(){this.emit("refresh")}refreshPaused(){this.emit("refreshPaused")}get processors(){return console.warn("HTML Audio does not support processors"),[]}set processors(e){console.warn("HTML Audio does not support processors")}get audioContext(){return console.warn("HTML Audio does not support audioContext"),null}toggleMute(){return this.muted=!this.muted,this.refresh(),this.muted}togglePause(){return this.paused=!this.paused,this.refreshPaused(),this.paused}destroy(){this.removeAllListeners()}};u(Ni,"_instance");let Ui=Ni;const Hr=class Hr extends dt{constructor(){super(...arguments);u(this,"_source",null);u(this,"_audio",null);u(this,"_end",0);u(this,"_pausedReal",!1);u(this,"_duration",0);u(this,"_start",0);u(this,"_playing",!1);u(this,"_paused",!1);u(this,"_playbackRate",1);u(this,"_volume",1);u(this,"_loop",!1);u(this,"_muted",!1);u(this,"_onUpdate",()=>{this._source&&(this.emit("progress",this.progress,this._duration),this._source.currentTime>=this._end&&!this._source.loop&&this._onComplete())})}get progress(){var e;return(((e=this._source)==null?void 0:e.currentTime)??0)/this._duration}get paused(){return this._paused}set paused(e){this._paused!==e&&(this._paused=e,this.refreshPaused())}get playbackRate(){return this._playbackRate}set playbackRate(e){this._playbackRate!==e&&(this._playbackRate=e,this.refresh())}get volume(){return this._volume}set volume(e){this._volume!==e&&(this._volume=e,this.refresh())}get loop(){return this._loop}set loop(e){this._loop!==e&&(this._loop=e,this.refresh())}get muted(){return this._muted}set muted(e){this._muted!==e&&(this._muted=e,this.refresh())}set(e,t){if(this[e]===void 0)throw new Error(`Property with name ${e} does not exist.`);switch(e){case"playbackRate":this.playbackRate=t;break;case"volume":this.volume=t;break;case"paused":this.paused=t;break;case"loop":this.loop=t;break;case"muted":this.muted=t;break}return this}_onPlay(){this._playing=!0}_onPause(){this._playing=!1}init(e){this._playing=!1,this._duration=e.source.duration;const t=this._source=e.source.cloneNode(!1);return t.src=e.parent.src,t.onplay=this._onPlay.bind(this),t.onpause=this._onPause.bind(this),e.context.on("refresh",this.refresh),e.context.on("refreshPaused",this.refreshPaused),this._audio=e,this}_stop(){this._source&&this._playing&&(this._source.onended=null,this._source.pause())}stop(){this._stop(),this._source&&this.emit("stop")}get processors(){return console.warn("HTML Audio does not support processors"),[]}set processors(e){console.warn("HTML Audio does not support processors")}refresh(){if(!this._audio||!this._source)return;const e=this._audio.context,t=this._audio.parent;this._source.loop=this._loop||t.loop;const i=e.volume*(e.muted?0:1),s=t.volume*(t.muted?0:1),o=this._volume*(this._muted?0:1);this._source.volume=o*i*s,this._source.playbackRate=this._playbackRate*e.playbackRate*t.playbackRate}refreshPaused(){if(!this._source||!this._audio)return;const e=this._paused||this._audio.parent.paused||this._audio.context.paused;e!==this._pausedReal&&(this._pausedReal=e,e?(this._stop(),this.emit("paused")):(this.emit("resumed"),this.play({start:this._source.currentTime,end:this._end,volume:this._volume,playbackRate:this._playbackRate,loop:this._loop})),this.emit("pause",e))}play(e={}){if(!this._source)return;const{start:t=0,end:i=0}=e;i&&console.assert(i>t,"End time is before start time"),e.playbackRate!==void 0&&(this._playbackRate=e.playbackRate),e.volume!==void 0&&(this._volume=e.volume),e.loop!==void 0&&(this._loop=e.loop),e.muted!==void 0&&(this._muted=e.muted),this.refresh(),this.loop&&i!==null&&(console.warn('Looping not support when specifying an "end" time'),this.loop=!1),this._start=t,this._end=i||this._duration,this._start=Math.max(0,this._start-Hr.PADDING),this._end=Math.min(this._end+Hr.PADDING,this._duration),this._source.onloadedmetadata=()=>{this._source&&(this._source.currentTime=t,this._source.onloadedmetadata=null,this.emit("progress",t,this._duration),ue.on(this._onUpdate))},this._source.onended=this._onComplete.bind(this),this._source.play(),this.emit("start")}_onComplete(){ue.off(this._onUpdate),this._stop(),this.emit("progress",1,this._duration),this.emit("end",this)}destroy(){ue.off(this._onUpdate),this.removeAllListeners();const e=this._source;e&&(e.onended=null,e.onplay=null,e.onpause=null,this._stop()),this._source=null,this._playbackRate=1,this._volume=1,this._loop=!1,this._end=0,this._start=0,this._duration=0,this._playing=!1,this._pausedReal=!1,this._paused=!1,this._muted=!1,this._audio&&(this._audio.context.off("refresh",this.refresh),this._audio.context.off("refreshPaused",this.refreshPaused),this._audio=null)}};u(Hr,"PADDING",.1);let Di=Hr;class da{constructor(r){u(this,"source",new globalThis.Audio);u(this,"_src","");this.parent=r}get src(){return this._src}set src(r){this._src!==r&&(this._src=r,this.load())}get duration(){return this.source.duration}get isPlayable(){return!!this.source&&this.source.readyState===4}get context(){return Ui.instance}async load(){return new Promise(r=>{this.source.onload=()=>r(this),this.source.src=this._src,this.source.load()})}createSound(){return new Di}}class en extends dt{constructor(e,t){super();u(this,"_processers",[]);this._input=e,this._output=t}get processors(){return this._processers}set processors(e){if(this._processers.forEach(t=>t.disconnect()),this._processers.length=0,this._input.connect(this._output),e.length){this._processers=e.slice(0),this._input.disconnect();let t;e.forEach(i=>{t?t.connect(i.destination):this._input.connect(i.destination),t=i}),t.connect(this._output)}}get destination(){return this._input}}class If{constructor(r,e=null){this.destination=r,this.source=e}connect(r){var e;(e=this.source)==null||e.connect(r)}disconnect(){var r;(r=this.source)==null||r.disconnect()}}function Rf(){if(qi)return new AudioContext;if(Wi){const n=globalThis.webkitAudioContext;return new n}else throw new Error("Failed to createAudioContext")}function Bf(n,r,e){if(Xi)return new OfflineAudioContext(n,r,e);if(yn){const t=globalThis.webkitOfflineAudioContext;return new t(n,r,e)}else throw new Error("Failed to createOfflineAudioContext")}const Fi=class Fi extends en{constructor(){const e=Rf(),t=Bf(1,2,Xi?Math.max(8e3,Math.min(96e3,e.sampleRate)):44100),i=e.createDynamicsCompressor(),s=e.createAnalyser();s.connect(i),i.connect(e.destination);super(s,i);u(this,"_context");u(this,"_offlineContext");u(this,"_locked");u(this,"muted",!1);u(this,"volume",1);u(this,"playbackRate",1);u(this,"autoPause",!0);u(this,"_paused",!1);u(this,"_pausedOnBlur",!1);u(this,"_compressor");u(this,"_analyser");u(this,"_unlock",()=>{this._locked&&(this.playEmptySound(),this._context.state==="running"&&(document.removeEventListener("mousedown",this._unlock,!0),document.removeEventListener("touchend",this._unlock,!0),document.removeEventListener("touchstart",this._unlock,!0),this._locked=!1))});this._context=e,this._offlineContext=t,this._compressor=i,this._analyser=s,this._locked=e.state==="suspended"&&(ft||mn),_r&&(this._locked&&(this._unlock(),document.addEventListener("mousedown",this._unlock,!0),document.addEventListener("touchstart",this._unlock,!0),document.addEventListener("touchend",this._unlock,!0)),globalThis.addEventListener("focus",this._onFocus),globalThis.addEventListener("blur",this._onBlur))}static get instance(){return this._instance||(this._instance=new Fi),this._instance}static get audioContext(){return this.instance.audioContext}static get offlineContext(){return this.instance.offlineContext}static setParamValue(e,t){this.instance.setParamValue(e,t)}static decode(e){return this.instance.decode(e)}get audioContext(){return this._context}get offlineContext(){return this._offlineContext}get paused(){return this._paused}set paused(e){e&&this._context.state==="running"?this._context.suspend():!e&&this._context.state==="suspended"&&this._context.resume(),this._paused=e}_onFocus(){if(!this.autoPause)return;const e=this._context.state;(e==="suspended"||e==="interrupted"||!this._locked)&&(this.paused=this._pausedOnBlur,this.refreshPaused())}_onBlur(){this.autoPause&&(this._locked||(this._pausedOnBlur=this._paused,this.paused=!0,this.refreshPaused()))}playEmptySound(){const e=this._context.createBufferSource();e.buffer=this._context.createBuffer(1,1,22050),e.connect(this._context.destination),e.start(0,0,0),e.context.state==="suspended"&&e.context.resume()}refresh(){this.emit("refresh")}refreshPaused(){this.emit("refreshPaused")}toggleMute(){return this.muted=!this.muted,this.refresh(),this.muted}togglePause(){return this.paused=!this.paused,this.refreshPaused(),this._paused}decode(e){return new Promise((t,i)=>{const s=a=>{i(new Error((a==null?void 0:a.message)||"Unable to decode file"))},o=this._offlineContext.decodeAudioData(e,t,s);o&&o.catch(s)})}setParamValue(e,t){e.setValueAtTime?e.setValueAtTime(t,this._context.currentTime):e.value=t}};u(Fi,"_instance");let It=Fi;class pa extends dt{constructor(){super(...arguments);u(this,"_audio",null);u(this,"_sourceNode",null);u(this,"_gain",null);u(this,"_progress",0);u(this,"_pausedReal",!1);u(this,"_paused",!1);u(this,"_volume",1);u(this,"_playbackRate",1);u(this,"_loop",!1);u(this,"_muted",!1);u(this,"_duration",0);u(this,"_end",0);u(this,"_elapsed",0);u(this,"_lastUpdate",this._now());u(this,"_processors",[]);u(this,"_onComplete",()=>{if(this._sourceNode){this._enableTicker(!1),this._sourceNode.onended=null,this._sourceNode.disconnect();try{this._sourceNode.buffer=null}catch(e){console.warn("Failed to set AudioBufferSourceNode.buffer to null:",e)}}this._sourceNode=null,this._progress=1,this.emit("progress",1,this._duration),this.emit("end",this)});u(this,"_updateListener",()=>this._update())}get progress(){return this._progress}get paused(){return this._paused}set paused(e){this._paused!==e&&(this._paused=e,this.refreshPaused())}get volume(){return this._volume}set volume(e){this._volume!==e&&(this._volume=e,this.refresh())}get playbackRate(){return this._playbackRate}set playbackRate(e){this._playbackRate!==e&&(this._playbackRate=e,this.refresh(),this._update(!0))}get loop(){return this._loop}set loop(e){this._loop!==e&&(this._loop=e,this.refresh())}get muted(){return this._muted}set muted(e){this._muted!==e&&(this._muted=e,this.refresh())}init(e){return this._audio=e,e.context.on("refresh",this.refresh),e.context.on("refreshPaused",this.refreshPaused),this}_now(){var e;return((e=this._audio)==null?void 0:e.context.audioContext.currentTime)??0}play(e={}){var a;if(!this._audio)return;const{end:t=0,start:i=0}=e;t&&console.assert(t>i,"End time is before start time"),this._end=t,this._elapsed=i,e.volume!==void 0&&(this._volume=e.volume),e.playbackRate!==void 0&&(this._playbackRate=e.playbackRate),e.muted!==void 0&&(this._muted=e.muted),e.loop!==void 0&&(this._loop=e.loop),e.processors!==void 0&&(this._processors=e.processors),this._paused=!1;const{source:s,gain:o}=this._audio.cloneSource();this._sourceNode=s,this._gain=o,this.refresh(),s.onended=this._onComplete.bind(this),this._duration=((a=s.buffer)==null?void 0:a.duration)??0,this._lastUpdate=this._now(),this._loop?(s.loopStart=i,s.loopEnd=t,s.start(0,i)):t?s.start(0,i,t-i):s.start(0,i),this.emit("start"),this._update(!0),this._enableTicker(!0)}_stop(){if(this._sourceNode){this._enableTicker(!1),this._sourceNode.onended=null,this._sourceNode.stop(0),this._sourceNode.disconnect();try{this._sourceNode.buffer=null}catch(e){console.warn("Failed to set AudioBufferSourceNode.buffer to null:",e)}this._sourceNode=null}}stop(){this._sourceNode&&(this._stop(),this.emit("stop"))}_update(e=!1){if(this._sourceNode){const t=this._now(),i=t-this._lastUpdate;if(i>0||e){this._elapsed+=i*this._sourceNode.playbackRate.value,this._lastUpdate=t;const s=this._duration;let o;if(this._sourceNode.loopStart){const a=this._sourceNode.loopEnd-this._sourceNode.loopStart;o=(this._sourceNode.loopStart+this._elapsed%a)/s}else o=this._elapsed%s/s;this._progress=o,this.emit("progress",o,s)}}}refresh(){if(!this._audio||!this._sourceNode)return;const e=this._audio.context,t=this._audio.parent;this._sourceNode.loop=this._loop||t.loop,e.setParamValue(this._gain.gain,this._volume*(this._muted?0:1)*t.volume*(t.muted?0:1)*e.volume*(e.muted?0:1)),e.setParamValue(this._sourceNode.playbackRate,this._playbackRate*t.playbackRate*e.playbackRate),this.applyProcessors()}applyProcessors(){if(this._sourceNode&&this._processors.length){this._sourceNode.disconnect();let e=this._sourceNode;this._processors.forEach(t=>{e.connect(t.destination),e=t}),e.connect(this._gain)}}refreshPaused(){if(!this._audio)return;const e=this._paused||this._audio.parent.paused||this._audio.context.paused;e!==this._pausedReal&&(this._pausedReal=e,e?(this._stop(),this.emit("paused")):(this.emit("resumed"),this.play({start:this._elapsed%this._duration,end:this._end,playbackRate:this._playbackRate,loop:this._loop,volume:this._volume})),this.emit("pause",e))}_enableTicker(e){ue.off(this._updateListener),e&&ue.on(this._updateListener)}destroy(){var e,t,i;this.removeAllListeners(),this._stop(),(e=this._gain)==null||e.disconnect(),this._gain=null,(t=this._audio)==null||t.context.off("refresh",this.refresh),(i=this._audio)==null||i.context.off("refreshPaused",this.refreshPaused),this._audio=null,this._processors.forEach(s=>s.disconnect()),this._processors.length=0,this._end=0,this._playbackRate=1,this._volume=1,this._loop=!1,this._elapsed=0,this._duration=0,this._paused=!1,this._muted=!1,this._pausedReal=!1}}class ga extends en{constructor(e){const t=It.audioContext,i=t.createBufferSource(),s=t.createGain(),o=t.createAnalyser();i.connect(o),o.connect(s),s.connect(It.instance.destination);super(o,s);u(this,"_sourceBuffer");u(this,"_sourceNode");u(this,"_sourceLoad");u(this,"gain");u(this,"analyser");this.parent=e,this._sourceNode=i,this.gain=s,this.analyser=o}get context(){return It.instance}get isPlayable(){return!!this._sourceNode.buffer}get duration(){var e;return((e=this._sourceNode.buffer)==null?void 0:e.duration)??0}get buffer(){return this._sourceNode.buffer}set buffer(e){this._sourceNode.buffer=e}async load(){return this._sourceLoad||(this._sourceLoad=new Promise(e=>{this._sourceBuffer?this._decode(this._sourceBuffer).then(()=>e(this)):this.parent.src?this._loadUrl(this.parent.src).then(()=>e(this)):e(this)})),this._sourceLoad}_loadUrl(e){return new Promise(t=>{fetch(e).then(i=>i.arrayBuffer()).then(i=>this._decode(i)).finally(()=>t(this))})}_decode(e){return Promise.resolve(e instanceof AudioBuffer?e:It.decode(e)).then(t=>(this.parent.isLoaded=!0,this.buffer=t,t))}cloneSource(){const e=this.context,t=this._sourceNode,i=e.audioContext.createBufferSource(),s=e.audioContext.createGain();return i.buffer=t.buffer,i.loop=t.loop,e.setParamValue(i.playbackRate,t.playbackRate.value),i.connect(s),s.connect(this.destination),{source:i,gain:s}}createSound(){return new pa}}var ma=Object.defineProperty,Lf=Object.getOwnPropertyDescriptor,kf=(n,r,e)=>r in n?ma(n,r,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[r]=e,Nf=(n,r,e,t)=>{for(var i=t>1?void 0:t?Lf(r,e):r,s=n.length-1,o;s>=0;s--)(o=n[s])&&(i=(t?o(r,e,i):o(i))||i);return t&&i&&ma(r,e,i),i},Ff=(n,r,e)=>kf(n,r+"",e);g.Audio=class extends g.Node{constructor(e=""){super();u(this,"_sounds",[]);u(this,"_platformAudio",_n?new ga(this):new da(this));u(this,"_src");u(this,"isLoaded",!1);u(this,"_volume",1);u(this,"_muted",!1);u(this,"_loop",!1);u(this,"_playbackRate",1);u(this,"_isPlaying",!1);u(this,"_paused",!1);u(this,"multiple",!1);u(this,"start",0);u(this,"end",0);u(this,"_onComplete",e=>{if(this._sounds){const t=this._sounds.indexOf(e);t>-1&&this._sounds.splice(t,1),this._isPlaying=this._sounds.length>0}this._recycleSound(e)});u(this,"_prevTime",0);u(this,"_timer",0);this.src=e}get platformAudio(){return this._platformAudio}get src(){return this._src}set src(e){this._src!==e&&(this._src=e,this.load())}get isPlayable(){return this.isLoaded&&this._platformAudio.isPlayable}get duration(){return this._platformAudio.duration*1e3}get volume(){return this._volume}set volume(e){this._volume!==e&&(this._volume=e,this.refresh())}get muted(){return this._muted}set muted(e){this._muted!==e&&(this._muted=e,this.refresh())}get loop(){return this._loop}set loop(e){this._loop!==e&&(this._loop=e,this.refresh())}get playbackRate(){return this._playbackRate}set playbackRate(e){this._playbackRate!==e&&(this._playbackRate=e,this.refresh())}get isPlaying(){return this._isPlaying}get paused(){return this._paused}set paused(e){this._paused!==e&&(this._paused=e,this.refreshPaused())}async load(){return await this._platformAudio.load(),this}pause(){return this._isPlaying=!1,this.paused=!0,this}resume(){return this._isPlaying=this._sounds.length>0,this.paused=!1,this}stop(){if(!this.isPlayable)return this;this._isPlaying=!1;for(let e=this._sounds.length-1;e>=0;e--)this._sounds[e].stop();return this}play(e={}){if(!this.isLoaded)return;this.multiple||this._removeSounds(),this._isPlaying=!0;const t=this._createSound();this._sounds.push(t),t.once("end",()=>{var o;(o=e.complete)==null||o.call(e),this._onComplete(t)}),t.once("stop",()=>this._onComplete(t));const i=((e==null?void 0:e.start)??this.start)/1e3,s=((e==null?void 0:e.end)??this.end)/1e3;return t.play({...e,start:i,end:s}),t}_removeSounds(){for(let e=this._sounds.length-1;e>=0;e--)this._recycleSound(this._sounds[e]);this._sounds.length=0}_createSound(){return g.Audio._soundPool.length>0?g.Audio._soundPool.pop().init(this._platformAudio):this._platformAudio.createSound().init(this._platformAudio)}refresh(){for(let e=this._sounds.length,t=0;t<e;t++)this._sounds[t].refresh()}refreshPaused(){for(let e=this._sounds.length,t=0;t<e;t++)this._sounds[t].refreshPaused()}_recycleSound(e){e.destroy(),g.Audio._soundPool.includes(e)||g.Audio._soundPool.push(e)}_process(e){var t;if(super._process(e),this.isRenderable()){const i=((t=this._tree)==null?void 0:t.timeline.current)??0;i-this._prevTime>0&&(this._timer||(this._setTimeStop(),this.play({start:this.start+(i-this.visibleDelay)%this.duration})),this._isPlaying&&this._setTimeStop()),this._prevTime=i}}_setTimeStop(){this._timer&&clearTimeout(this._timer),this._timer=setTimeout(()=>{this.stop(),this._timer=0},100)}},Ff(g.Audio,"_soundPool",[]),g.Audio=Nf([H("Audio")],g.Audio);class $f extends g.Node2D{}var zf=Object.defineProperty,Vf=Object.getOwnPropertyDescriptor,Ii=(n,r,e,t)=>{for(var i=t>1?void 0:t?Vf(r,e):r,s=n.length-1,o;s>=0;s--)(o=n[s])&&(i=(t?o(r,e,i):o(i))||i);return t&&i&&zf(r,e,i),i};g.AudioWaveform=class extends g.Node2D{constructor(e={}){super();u(this,"src");u(this,"gap",0);u(this,"color","#000000");u(this,"_audioBuffer");u(this,"_src",_r?new de(document.createElement("canvas")):void 0);u(this,"_needsUpdateTexture",!1);this.setProperties(e)}_onUpdateProperty(e,t,i){switch(super._onUpdateProperty(e,t,i),e){case"src":this._loadSrc(t);break;case"gap":case"color":case"width":case"height":this._needsUpdateTexture=!0;break}}async _loadSrc(e){await globalThis.fetch(e).then(t=>t.arrayBuffer()).then(t=>It.decode(t)).then(t=>{this._audioBuffer=t,this.syncTexture(!0)})}syncTexture(e=!1){var f,d;const t=this._audioBuffer;if(!t||!e&&!this._needsUpdateTexture)return;this._needsUpdateTexture=!1;const i=(f=this._src)==null?void 0:f.source;if(!i)return;const{width:s=0,height:o=0}=this.style;i.width=s,i.height=o;const a=i.getContext("2d");if(!a){console.warn("Failed to getContext('2d') in syncTexture");return}a.fillStyle=this.color;const l=t.getChannelData(0),h=Math.ceil(l.length/s),c=o/2;for(let p=1,m=-1,_=0;_<s;_++){for(let y=0;y<h;y++){const S=l[_*h+y];S<p&&(p=S),S>m&&(m=S)}if(!this.gap||_%(this.gap*2)===0){const y=_,S=(1+p)*c,C=this.gap||1,x=Math.max(1,(m-p)*c);a.fillRect(y,S,C,x),p=1,m=-1}}(d=this._src)==null||d.requestUpload(),this.requestRedraw()}_process(e){this.syncTexture(),super._process(e)}_drawSrc(){const e=this._src;e!=null&&e.valid&&(this.context.fillStyle=e,this.context.textureTransform=new qe().scale(this.style.width/e.width,this.style.height/e.height))}},Ii([A()],g.AudioWaveform.prototype,"src",2),Ii([A()],g.AudioWaveform.prototype,"gap",2),Ii([A()],g.AudioWaveform.prototype,"color",2),g.AudioWaveform=Ii([H("AudioWaveform")],g.AudioWaveform);const jf=/^uniform \w+ (.+?);\s*\/\/\s*=\s*([^;]+?)[\s;]*$/gm,Gf=/vec(\d)\((.*)\)/;function qf(n){n=n.trim();const r=n.match(Gf);if(r){const e=Array.from({length:Number(r[1])},()=>0);return r[2].split(",").forEach((t,i)=>{e[i]=Number(t.trim())}),e}return Number(n)}const Yr=class Yr extends ne{constructor(e){super();u(this,"vert",`attribute vec2 position;
33
33
  attribute vec2 uv;
34
34
  varying vec2 vUv;
35
35
  void main() {
package/dist/index.mjs CHANGED
@@ -5298,6 +5298,7 @@ let Text2D = class extends Node2D {
5298
5298
  this.text.effects = this.effects;
5299
5299
  this.text.fonts = this.fonts;
5300
5300
  this.text.measureDom = this.measureDom;
5301
+ this.text.requestUpdate();
5301
5302
  }
5302
5303
  _onUpdateStyleProperty(key, value, oldValue) {
5303
5304
  if (key === "height")
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-canvas",
3
3
  "type": "module",
4
- "version": "0.1.7",
4
+ "version": "0.1.8",
5
5
  "packageManager": "pnpm@9.15.1",
6
6
  "description": "A JavaScript WebGL rendering engine.",
7
7
  "author": "wxm",