forwardfig 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,481 @@
1
+ (()=>{var{defineProperty:Ae,getOwnPropertyNames:er,getOwnPropertyDescriptor:or}=Object,rr=Object.prototype.hasOwnProperty;function sr(o){return this[o]}var nr=(o)=>{var n=(_e??=new WeakMap).get(o),i;if(n)return n;if(n=Ae({},"__esModule",{value:!0}),o&&typeof o==="object"||typeof o==="function"){for(var a of er(o))if(!rr.call(n,a))Ae(n,a,{get:sr.bind(o,a),enumerable:!(i=or(o,a))||i.enumerable})}return _e.set(o,n),n},_e;var ar=(o)=>o;function ir(o,n){this[o]=ar.bind(null,n)}var He=(o,n)=>{for(var i in n)Ae(o,i,{get:n[i],enumerable:!0,configurable:!0,set:ir.bind(n,i)})};var zr={};He(zr,{Figure:()=>ze,Graph:()=>Re,Module:()=>Ge,THEMES:()=>ue,Tensor:()=>Mt,act:()=>Io,add:()=>Jo,attention:()=>Mo,block:()=>ho,circle:()=>go,concat:()=>Ao,configure:()=>Yo,conv:()=>Co,dec:()=>xo,default:()=>Lr,draw:()=>Ko,dropout:()=>Po,embed:()=>Ro,enc:()=>bo,ffn:()=>So,figure:()=>Xo,gaussian:()=>Fo,image:()=>Go,layer:()=>Rt,layout:()=>Zt,linear:()=>Lo,loss:()=>jo,magnify:()=>Do,matmul:()=>No,matrix:()=>Ho,mha:()=>ko,mlp:()=>Oo,mul:()=>Zo,neurons:()=>$o,norm:()=>Eo,op:()=>Le,pointcloud:()=>Uo,pool:()=>To,preset:()=>tt,softmax:()=>zo,space:()=>Bo,sphere:()=>qo,stack:()=>vo,tensor:()=>yo,text:()=>Ve,thin:()=>mo,vector:()=>wo,voxels:()=>Wo,waveform:()=>Vo});var De={};He(De,{Figure:()=>ze,Graph:()=>Re,Module:()=>Ge,Tensor:()=>Mt,act:()=>Io,add:()=>Jo,attention:()=>Mo,block:()=>ho,circle:()=>go,concat:()=>Ao,configure:()=>Yo,conv:()=>Co,dec:()=>xo,dropout:()=>Po,embed:()=>Ro,enc:()=>bo,ffn:()=>So,figure:()=>Xo,gaussian:()=>Fo,image:()=>Go,layer:()=>Rt,linear:()=>Lo,loss:()=>jo,magnify:()=>Do,matmul:()=>No,matrix:()=>Ho,mha:()=>ko,mlp:()=>Oo,mul:()=>Zo,neurons:()=>$o,norm:()=>Eo,op:()=>Le,pointcloud:()=>Uo,pool:()=>To,preset:()=>tt,softmax:()=>zo,space:()=>Bo,sphere:()=>qo,stack:()=>vo,tensor:()=>yo,text:()=>Ve,thin:()=>mo,vector:()=>wo,voxels:()=>Wo,waveform:()=>Vo});var ye='Inter,"Helvetica Neue",Helvetica,Arial,sans-serif',Ct=400,to='"Latin Modern Math","STIX Two Math","Cambria Math","Times New Roman",serif',ie;function Te(o){ie=o,Ce.clear()}var eo=()=>{let o=globalThis.MathJax;return o?.tex2svg?(n)=>o.tex2svg(n,{display:!1}).querySelector("svg")?.outerHTML??"":null},cr=(o,n)=>{let i=ie===void 0?eo():ie;if(!i)return null;let a;try{a=i(o)}catch{return null}let u=/viewBox="([^"]+)"/.exec(a)?.[1];if(!u)return null;let[,h,x,g]=u.trim().split(/[\s,]+/).map(Number),v=n/1000;return{inner:a.replace(/^<svg[^>]*>/,"").replace(/<\/svg>\s*$/,""),viewBox:u,w:x*v,asc:-h*v,desc:(g+h)*v}},Jt=(o)=>{let n={},i=o.split(" ");for(let a=0;a<i.length;a+=2)n[i[a]]=i[a+1];return n},We=Jt("alpha α beta β gamma γ delta δ epsilon ϵ varepsilon ε zeta ζ eta η theta θ vartheta ϑ iota ι kappa κ lambda λ mu μ nu ν xi ξ pi π rho ρ sigma σ tau τ upsilon υ phi ϕ varphi φ chi χ psi ψ omega ω Gamma Γ Delta Δ Theta Θ Lambda Λ Xi Ξ Pi Π Sigma Σ Phi Φ Psi Ψ Omega Ω"),Je=Jt("cdot · times × oplus ⊕ otimes ⊗ odot ⊙ to → rightarrow → leftarrow ← gets ← Rightarrow ⇒ leftrightarrow ↔ mapsto ↦ sum ∑ prod ∏ int ∫ infty ∞ partial ∂ nabla ∇ in ∈ notin ∉ sim ∼ approx ≈ propto ∝ leq ≤ le ≤ geq ≥ ge ≥ neq ≠ ne ≠ pm ± mp ∓ ldots … dots … cdots ⋯ vdots ⋮ mid ∣ | ‖ ast ∗ star ⋆ circ ∘ bullet • forall ∀ exists ∃ emptyset ∅ cup ∪ cap ∩ subset ⊂ subseteq ⊆ top ⊤ perp ⊥ langle ⟨ rangle ⟩ ell ℓ hbar ℏ dagger † prime ′ { { } } % % _ _ # # & & $ $"),Ut={};for(let o of"+−×·±∓⊕⊗⊙∘∗⋆∪∩")Ut[o]="bin";for(let o of"=<>≤≥≠≈∼∝∈∉⊂⊆→←⇒↔↦∣")Ut[o]="rel";for(let o of",;")Ut[o]="punct";Ut["|"]="mid";for(let o of"([⟨")Ut[o]="open";for(let o of")]⟩")Ut[o]="close";var lr=new Set("log exp ln sin cos tanh max min arg argmax argmin softmax lim det tr sup inf Pr KL diag sign".split(" ")),Ze=Jt("hat ̂ widehat ̂ tilde ̃ widetilde ̃ bar ̄ overline ̅ dot ̇ ddot ̈ vec ⃗"),Ye=Jt(",   :   ;   quad   qquad    ! ​"),fr=Jt("B ℬ E ℰ F ℱ H ℋ I ℐ L ℒ M ℳ R ℛ"),ur=Jt("C ℂ H ℍ N ℕ P ℙ Q ℚ R ℝ Z ℤ"),Xe=(o)=>fr[o]||(/[A-Z]/.test(o)?String.fromCodePoint(119964+o.charCodeAt(0)-65):o),pr=(o)=>ur[o]||(/[A-Z]/.test(o)?String.fromCodePoint(120120+o.charCodeAt(0)-65):/\d/.test(o)?String.fromCodePoint(120792+ +o):o),Ke={mathbf:{up:!0,b:!0},bf:{up:!0,b:!0},boldsymbol:{b:!0},bm:{b:!0},mathrm:{up:!0},rm:{up:!0},operatorname:{up:!0},mathit:{},text:{up:!0,text:!0},textrm:{up:!0,text:!0},mathsf:{up:!0,sans:!0},textbf:{up:!0,b:!0,text:!0},mathcal:{up:!0,map:Xe},mathscr:{up:!0,map:Xe},mathbb:{up:!0,map:pr}},hr=new Set("left right big Big bigl bigr Bigl Bigr displaystyle".split(" "));function dr(o){let n=0,i=(h,x,g=!1)=>{if(x.text)return{t:h,it:!1,b:!!x.b,serif:!x.sans};if(h==="-")h="−";return{t:h,it:g&&!x.up,b:!!x.b,serif:!x.sans,cls:Ut[h]}};function a(h,x){let g=[];while(n<o.length){let v=o[n];if(v===x){n++;break}if(v==="^"||v==="_"){n++;let R=v==="^"?"sup":"sub",I=g[g.length-1];if(!I||"t"in I||I[R])g.push(I={});I[R]=u(h);continue}if(v===" "&&!h.text){n++;continue}g.push(...u(h))}return g}function u(h){while(o[n]===" "&&!h.text)n++;let x=o[n++];if(x===void 0)return[];if(x==="{")return a(h,"}");if(x!=="\\")return[i(h.map?h.map(x):x,h,/[A-Za-z]/.test(x)&&!h.map)];let g=/^[A-Za-z]+/.exec(o.slice(n))?.[0]||o[n]||"";if(n+=g.length,We[g])return[i(We[g],h,/^[a-z]/.test(g))];if(Je[g])return[i(Je[g],h)];if(Ye[g])return[i(Ye[g],h)];if(lr.has(g))return[i(g,{...h,up:!0})];if(Ke[g])return u({...h,...Ke[g]});if(Ze[g]){let v=u(h),R=v.find((I)=>("t"in I));if(R)R.t+=Ze[g];return v}if(g==="frac"){let v=u(h),R=u(h);return[...v,i("/",h),...R]}if(g==="sqrt")return[i("√",h),...u(h)];if(hr.has(g))return[];return[i(g,{...h,up:!0})]}return a({})}var Ce=new Map,ae,Qe=new Map;function mr(o,n,i,a,u='Inter,"Helvetica Neue",Helvetica,Arial,sans-serif'){return`${n?"italic ":""}${i===!0?700:i||400} ${o}px ${a?'"Latin Modern Math","STIX Two Math","Cambria Math","Times New Roman",serif':u}`}function gr(o){if(/[iljI.,:;'|! ]/.test(o))return 0.28;if(/[mwMW]/.test(o))return 0.86;if(/[A-Z]/.test(o))return 0.68;if(/[̀-ͯ⃗​]/.test(o))return 0;if(/[ -鿿＀-￯]/.test(o))return 1;if(o===" ")return 0.28;return 0.56}function br(o,n,i){let a=n+"\x00"+o,u=Qe.get(a);if(u!==void 0)return u;if(ae===void 0)ae=typeof document<"u"?document.createElement("canvas").getContext("2d"):null;if(ae)ae.font=n,u=ae.measureText(o).width;else{u=0;for(let h of o)u+=gr(h)*i}return Qe.set(a,u),u}function bt(o,n,i=400,a='Inter,"Helvetica Neue",Helvetica,Arial,sans-serif'){let u=o+"\x00"+n+"\x00"+i+"\x00"+a+"\x00"+(ie===void 0?!!eo():!!ie),h=Ce.get(u);if(h)return h;let x=[],g=[];for(let I of o.split(/\n|\\\\|<br\s*\/?>/)){let q=[],O=0,U=(X,V,_,ft=!1)=>{let W="",lt=!1,wt="",ot=!1;for(let H of X)if("t"in H){let it=H.cls??"ord",ct=!W||W==="bin"||W==="rel"||W==="open"||W==="punct";if(it==="bin"&&ct)it="ord";else if(it==="mid"&&(ct||ot))it=ot?"close":"open",ot=!ot;let ht=0;if(ft&&W&&it!=="punct"&&it!=="close"&&W!=="open"){if(it==="rel"!==(W==="rel"))ht=0.2777777777777778;else if(it==="bin"||W==="bin")ht=0.2222222222222222;else if(W==="punct"||it==="mid"||W==="mid")ht=0.16666666666666666}if(lt&&!H.it&&it!=="punct")ht+=0.08;O+=ht*_;let kt=br(H.t,mr(_,H.it,H.b||(H.serif?!1:i),H.serif,a),_),pt=q[q.length-1];if(!ht&&pt&&pt.dy===V&&pt.size===_&&pt.it===H.it&&pt.b===H.b&&pt.serif===H.serif)pt.t+=H.t;else q.push({t:H.t,x:O,dy:V,size:_,it:H.it,b:H.b,serif:H.serif});O+=kt,W=it,lt=H.it,wt=H.t.slice(-1)}else{let it=O,ct=Math.max(_*0.72,Math.min(_,8.5)),ht=it,kt=it;if(H.sup)O=it+(lt?(wt==="f"?0.2:0.08)*_:0),U(H.sup,V-_*0.38,ct),ht=O,O=it;if(H.sub)U(H.sub,V+_*0.24,ct),kt=O;O=Math.max(ht,kt)+0.06*_,lt=!1}};I.split("$").forEach((X,V)=>{let _=V%2?cr(X,n*1.1):null;if(_)q.push({t:"",x:O,dy:0,size:n,it:!1,b:!1,serif:!0,math:_}),O+=_.w;else if(V%2)U(dr(X),0,n*1.1,!0);else if(X)U([{t:X,it:!1,b:!1,serif:!1}],0,n)}),x.push(q),g.push(O)}let v=n*1.3,R={lines:x,widths:g,w:Math.max(0,...g),h:x.length*v,lh:v,size:n};return Ce.set(u,R),R}var Pe=13,$e=new Set(["tensor","stack","matrix","voxels","image","magnify","neurons","gaussian","space","sphere","pointcloud","waveform"]),je=(o)=>JSON.parse(o.attrs.values??"[]"),we=(o,n)=>+o.attrs.fontSize||(o.shape==="thin"||o.shape==="vector"||$e.has(o.shape)?n-1:n),ke=(o)=>{let n=(+o.tilt||0)*Math.PI/180,i=(+o.rotate||0)*Math.PI/180,[a,u]=[Math.cos(n),-Math.sin(n)*0.35],[h,x]=[Math.cos(i),Math.sin(i)];return[h*a-x*u,x*a+h*u,-x,h]},dt=(o,n)=>o?o.split(",").map(Number):n,Ie={"left-of":[-1,0],"right-of":[1,0],above:[0,-1],below:[0,1]},ce=14,ve=(o,n)=>o[0]<n[2]&&o[2]>n[0]&&o[1]<n[3]&&o[3]>n[1];function xr(o,n,i,a){let u=we(o,i),h=o.attrs.fontFamily||a,x=bt(o.label,u,500,h),g,v;switch(o.shape){case"thin":g=Math.max(x.w+26,64),v=x.h+9;break;case"op":g=v=22;break;case"circle":g=v=Math.max(42,x.w+18,x.h+16);break;case"tensor":{let[R,I,q]=dt(o.attrs.size,[36,64,20]);g=R+q*0.6,v=I+q*0.6;break}case"stack":{let[R,I]=dt(o.attrs.size,[44,60]),q=+o.attrs.n||4,O=+o.attrs.offset||7;g=R+(q-1)*O,v=I+(q-1)*O;break}case"enc":case"dec":if(n)g=x.w+40,v=Math.max(84,x.h+44);else g=Math.max(x.w+64,120),v=Math.max(52,x.h+26);break;case"neurons":{let R=(+o.attrs.n||4)*20+4;[g,v]=n?[22,R]:[R,22];break}case"vector":{let R=je(o),I=Math.max(36,...R.map((O)=>bt(O.trim(),u,Ct,h).w+14)),q=Math.max(20,u+8);[g,v]=n?[I,R.length*q+4]:[R.length*I+4,q+2];break}case"image":{let[R,I]=dt(o.attrs.size,[64,64]),q=+o.attrs.n||1,O=R+(q-1)*6,U=I+(q-1)*6,[X,V,_,ft]=ke(o.attrs);g=Math.abs(X)*O+Math.abs(_)*U,v=Math.abs(V)*O+Math.abs(ft)*U;break}case"magnify":[g,v]=dt(o.attrs.size,[64,64]);break;case"matrix":{let R=+o.attrs.cellSize||16;g=+o.attrs.cols*R,v=+o.attrs.rows*R;break}case"voxels":{let R=+o.attrs.cellSize||16,I=+o.attrs.depth*R*0.5;g=+o.attrs.cols*R+I,v=+o.attrs.rows*R+I;break}case"gaussian":[g,v]=dt(o.attrs.size,[120,84]);break;case"space":[g,v]=dt(o.attrs.size,[110,100]);break;case"sphere":[g,v]=dt(o.attrs.size,[84,84]);break;case"pointcloud":[g,v]=dt(o.attrs.size,[100,84]);break;case"waveform":[g,v]=dt(o.attrs.size,[120,44]);break;case"text":g=x.w+8,v=x.h+4;break;default:g=Math.max(x.w+30,76),v=x.h+18}o.w=+o.attrs.w||g,o.h=+o.attrs.h||v}function Zt(o){let n=o.dir==="LR"||o.dir==="RL",[i,a]=o.gap??(n?[56,36]:[30,44]),u=[...o.nodes.values()],h=o.fontSize??Pe,x=o.fontFamily??ye;for(let e of u)xr(e,n,h,x);let g=(e)=>n?e.w:e.h,v=(e)=>n?e.h:e.w,R=(e,r)=>o.dir==="TB"?[r,e]:o.dir==="BT"?[r,-e]:o.dir==="LR"?[e,r]:[-e,r],I=(e,r)=>o.dir==="TB"?[r,e]:o.dir==="BT"?[-r,e]:o.dir==="LR"?[e,r]:[-e,r],q=(e)=>("at"in e.attrs)||Object.keys(Ie).some((r)=>(r in e.attrs)),O=u.filter((e)=>!q(e)),U=new Set(O.map((e)=>e.id)),X=new Map(O.map((e)=>[e.id,e.id])),V=(e)=>{while(X.get(e)!==e)e=X.get(e);return e};for(let e of o.ranks)for(let r of e.slice(1))if(U.has(r)&&U.has(e[0]))X.set(V(r),V(e[0]));let _=new Map;for(let e of o.edges){if(!U.has(e.from)||!U.has(e.to))continue;let r=V(e.from),s=V(e.to);if(r===s)continue;if(!_.has(r))_.set(r,[]);_.get(r).push({to:s,e})}let ft=new Set,W=new Map,lt=[],wt=(e)=>{W.set(e,1);for(let{to:r,e:s}of _.get(e)??[]){let c=W.get(r);if(c===1)ft.add(s);else if(!c)wt(r)}W.set(e,2),lt.push(e)};for(let e of O)if(!W.has(V(e.id)))wt(V(e.id));let ot=new Map;for(let e of lt.reverse()){let r=ot.get(e)??0;ot.set(e,r);for(let{to:s,e:c}of _.get(e)??[])if(!ft.has(c))ot.set(s,Math.max(ot.get(s)??0,r+1))}let H=(e)=>ot.get(V(e.id)),it=new Map;for(let e of o.edges)if(U.has(e.from)&&U.has(e.to)&&!ft.has(e)&&!e.flags.has("skip")&&!e.flags.has("join")){if(!it.has(e.to))it.set(e.to,[]);it.get(e.to).push(o.nodes.get(e.from))}let ct=new Map,ht=new Set,kt=new Set,pt=0;for(let e of[...O].sort((r,s)=>H(r)-H(s)||r.order-s.order)){let r=H(e),s=(y)=>!kt.has(r+":"+y),c=e.attrs.lane!==void 0?+e.attrs.lane:e.attrs.align?ct.get(e.attrs.align):void 0;if(c===void 0){let y=(it.get(e.id)??[]).filter((f)=>H(f)<r),p=y.map((f)=>ct.get(f.id));if(y.length>1&&y.every((f)=>H(f)===H(y[0]))&&new Set(p).size===p.length){let f=p.reduce((S,E)=>S+E)/p.length;if(s(f))c=f}else for(let f of y){let S=ct.get(f.id);if(!ht.has(f.id)&&s(S)){c=S,ht.add(f.id);break}}if(c===void 0&&!y.length)for(let f of o.edges){if(f.from!==e.id||!ft.has(f)||!ct.has(f.to)||ht.has(f.to))continue;let S=ct.get(f.to);if(s(S)){c=S,ht.add(f.to);break}}}if(c===void 0||!s(c)){while(!s(pt))pt++;c=pt}pt=Math.max(pt,Math.floor(c)+1),kt.add(r+":"+c),ct.set(e.id,c)}let yt=(e)=>H(o.nodes.get(e)),Gt=o.edges.filter((e)=>U.has(e.from)&&U.has(e.to)&&!ft.has(e)&&!e.flags.has("skip")&&yt(e.from)<yt(e.to)),Pt=(e,r)=>ct.get(r<yt(e.to)?e.from:e.to),Xt=()=>{let e=0;for(let r=0;r<Gt.length;r++)for(let s=r+1;s<Gt.length;s++){let c=Gt[r],y=Gt[s],p=Math.max(yt(c.from),yt(y.from)),f=Math.min(yt(c.to),yt(y.to));if(p<f&&(Pt(c,p)-Pt(y,p))*(Pt(c,f)-Pt(y,f))<0)e++}return e},Dt=()=>{let e=0;for(let r of o.groups){let s=O.filter((p)=>p.groups.includes(r.id));if(!s.length)continue;let c=s.map((p)=>ct.get(p.id)),y=s.map(H);for(let p of O){let f=ct.get(p.id),S=H(p);if(!p.groups.includes(r.id)&&f>=Math.min(...c)&&f<=Math.max(...c)&&S>=Math.min(...y)&&S<=Math.max(...y))e++}}return e},Lt=new Set;for(let e of O){let r=ct.get(e.id);if(e.attrs.lane!==void 0)Lt.add(r);if(!Number.isInteger(r))Lt.add(Math.floor(r)).add(Math.ceil(r))}let he=(e)=>{for(let[r,s]of ct)if(s===e||s===e+1)ct.set(r,2*e+1-s)},zt=Xt(),Kt=zt?Dt():0;for(let e=0,r=!0;zt&&r&&e<pt;e++){r=!1;for(let s=0;s+1<pt&&zt;s++){if(Lt.has(s)||Lt.has(s+1))continue;he(s);let c=Xt(),y=c<zt?Dt():1/0;if(y<=Kt)zt=c,Kt=y,r=!0;else he(s)}}let jt=Math.max(0,...[...ct.values()].map(Math.ceil))+1,_t=Math.max(0,...O.map(H))+1,Ft=Array(jt).fill(0),Bt=Array(_t).fill(0),Ht=Array.from({length:jt},()=>new Set),Qt=Array.from({length:_t},()=>new Set);for(let e of O){let r=ct.get(e.id),s=H(e),c=e.groups.join();if(Number.isInteger(r))Ft[r]=Math.max(Ft[r],v(e)),Ht[r].add(c);Bt[s]=Math.max(Bt[s],g(e)),e.groups.forEach((y)=>Qt[s].add(y))}let te=(e,r)=>e.size!==r.size||[...e].some((s)=>!r.has(s)),ee=Array(_t).fill(i);for(let e of o.edges){let r=o.nodes.get(e.from),s=o.nodes.get(e.to);if(!e.label||!U.has(r.id)||!U.has(s.id)||H(s)<=H(r))continue;let c=bt(e.label,h-1,Ct,x);ee[H(s)]=Math.max(ee[H(s)],n?c.w+24:c.h+16)}let $t=[Ft[0]/2],oe=[Bt[0]/2];for(let e=1;e<jt;e++)$t[e]=$t[e-1]+Ft[e-1]/2+a+(te(Ht[e-1],Ht[e])?ce*2:0)+Ft[e]/2;for(let e=1;e<_t;e++)oe[e]=oe[e-1]+Bt[e-1]/2+ee[e]+(te(Qt[e-1],Qt[e])?ce*1.5:0)+Bt[e]/2;let de=(e)=>{let r=Math.floor(e),s=Math.ceil(e);return $t[r]+($t[s]-$t[r])*(e-r)},re=(e)=>{let[r,s]=dt(e.attrs.nudge,[0,0]);e.x+=r,e.y+=s};for(let e of O)[e.x,e.y]=R(oe[H(e)],de(ct.get(e.id))),re(e);let qt=u.filter(q),se=new Set(U);for(let e=0;qt.length&&e<=u.length;e++)qt=qt.filter((r)=>{if(r.attrs.at)return[r.x,r.y]=dt(r.attrs.at,[0,0]),re(r),se.add(r.id),!1;let s=Object.keys(Ie).find((S)=>(S in r.attrs)),c=o.nodes.get(r.attrs[s]);if(!c)throw Error(`${r.id}: ${s} refers to unknown node "${r.attrs[s]}"`);if(!se.has(c.id))return!0;let[y,p]=Ie[s],f=+r.attrs.dist||22;return r.x=c.x+y*(c.w/2+r.w/2+f),r.y=c.y+p*(c.h/2+r.h/2+f),re(r),se.add(r.id),!1});if(qt.length)throw Error(`circular placement: ${qt.map((e)=>e.id).join(", ")}`);let vt=new Map;for(let e of u){let[r,s]=I(e.x,e.y);vt.set(e.id,{u:r,v:s,hu:g(e)/2,hv:v(e)/2})}let ne=(e,r,s)=>{for(let c=0;c+1<e.length;c++){let[y,p]=[e[c],e[c+1]],f=Math.min(y[0],p[0]),S=Math.max(y[0],p[0]),E=Math.min(y[1],p[1]),m=Math.max(y[1],p[1]);for(let[w,z]of vt)if(w!==r&&w!==s&&f<z.u+z.hu+3&&S>z.u-z.hu-3&&E<z.v+z.hv+3&&m>z.v-z.hv-3)return!0}return!1},me=[],ge=(e,r,s,c,y)=>{let p=e>0?y:c;for(let f=!0;f;){f=!1;for(let S of vt.values())if(S.u-S.hu<s&&S.u+S.hu>r&&S.v+S.hv>c-1&&S.v-S.hv<y+1){let E=e>0?S.v+S.hv:S.v-S.hv;if(e>0?E>p:E<p)p=E,f=!0,e>0?y=p:c=p}}p+=e*16;for(let f=!0;f;){f=!1;for(let S of me)if(S.side===e&&S.a<s&&S.b>r&&Math.abs(S.v-p)<9)p+=e*9,f=!0}return me.push({side:e,a:r,b:s,v:p}),p},be=[],St=[];for(let e of o.edges){let r=vt.get(e.from),s=vt.get(e.to),c=o.nodes.get(e.from),y=o.nodes.get(e.to);if(e.op==="=>"&&c.shape==="neurons"&&y.shape==="neurons"){e.fc=!0;continue}if(e.from!==e.to&&!e.flags.has("skip")&&s.u-s.hu>=r.u+r.hu+2){let p=Math.max(r.v-r.hv,s.v-s.hv)+6,f=Math.min(r.v+r.hv,s.v+s.hv)-6,S=p<=f?Math.min(f,Math.max(p,r.hv<=s.hv?r.v:s.v)):NaN;be.push({e,S:r,T:s,ps:isNaN(S)?r.v:S,pt:isNaN(S)?s.v:S})}else St.push(e)}let xe=({e,S:r,T:s,ps:c,pt:y})=>{let p=r.u+r.hu,f=s.u-s.hu,S=(p+f)/2,E=(w)=>[[p,c],[w,c],[w,y],[f,y]],m=Math.abs(c-y)<0.5?[[[p,y],[f,y]]]:[E(Math.max(S,f-i/2)),E(Math.min(S,p+i/2))];if(e.flags.has("join")&&Math.abs(c-s.v)>s.hv){let w=c>s.v?1:-1;m.unshift([[p,c],[s.u,c],[s.u,s.v+w*s.hv]])}return m.find((w)=>!ne(w,e.from,e.to))},Ot=be.filter((e)=>xe(e)||(St.push(e.e),!1)),Nt=new Map;for(let e of Ot){if(!Nt.has(e.e.to))Nt.set(e.e.to,[]);Nt.get(e.e.to).push(e)}let Wt=(e,r,s)=>{let c=e.map((f)=>f.pt),y=r?e.indexOf(r):0;for(let f=y+1;f<e.length;f++)c[f]=Math.max(c[f],c[f-1]+12);for(let f=y-1;f>=0;f--)c[f]=Math.min(c[f],c[f+1]-12);let p=Math.max(0,c[c.length-1]-(s.v+s.hv-5))-Math.max(0,s.v-s.hv+5-c[0]);return{pt:c.map((f)=>f-p),moved:p!==0}};for(let e of Nt.values()){if(e.length<2)continue;let r=e[0].T;e.sort((p,f)=>p.pt-f.pt||p.S.v-f.S.v);let s=e.filter((p)=>Math.abs(p.ps-p.pt)<0.5).sort((p,f)=>Math.abs(p.pt-r.v)-Math.abs(f.pt-r.v))[0],c=e,y=Wt(c,s,r);if(s&&y.moved)c=e.filter((p)=>{if(p===s||Math.abs(p.S.v-r.v)<=r.hv)return!0;let f=p.S.v>r.v?1:-1,S=[[p.S.u+p.S.hu,p.S.v],[r.u,p.S.v],[r.u,r.v+f*r.hv]];if(ne(S,p.e.from,p.e.to))return!0;return p.alt=S,!1}),y=Wt(c,s,r);c.forEach((p,f)=>{p.pt=y.pt[f]});for(let p of c)if(Math.abs(p.pt-p.S.v)<p.S.hv-4)p.ps=p.pt}let At=(e,r,s=0)=>{if(e.pts=r.map(([c,y])=>R(c,y)),e.flags.has("curve")&&r.length===4)e.curve=s};for(let e of Ot){let r=e.alt??xe(e);if(r)At(e.e,r,1);else St.push(e.e)}let t=(e)=>o.edges.some((r)=>r!==e&&(r.from===e.from&&r.to===e.to||r.from===e.to&&r.to===e.from));for(let e=St.length-1;e>=0;e--){let r=St[e],s=vt.get(r.from),c=vt.get(r.to);if(r.from===r.to||r.flags.has("skip")||c.u+c.hu>s.u-s.hu-2||t(r))continue;let y=s.u-s.hu,p=c.u+c.hu,f=c.v>s.v?1:-1,S=(y+p)/2,E=Math.max(s.v-s.hv,c.v-c.hv)+6,m=Math.min(s.v+s.hv,c.v+c.hv)-6,w=[];if(E<=m){let P=Math.min(m,Math.max(E,s.hv<=c.hv?s.v:c.v));w.push([[y,P],[p,P]])}else w.push([[s.u,s.v+f*s.hv],[s.u,c.v],[p,c.v]],[[y,s.v],[S,s.v],[S,c.v],[p,c.v]]);let z=w.find((P)=>!ne(P,r.from,r.to));if(z)At(r,z,1),St.splice(e,1)}for(let e of St){let r=vt.get(e.from),s=vt.get(e.to),c=/^(left|top|-)/.test(e.attrs.side??"")?-1:1,y=Math.min(r.v-r.hv,s.v-s.hv),p=Math.max(r.v+r.hv,s.v+s.hv);if(e.from===e.to){let f=r.v+c*(r.hv+18);At(e,[[r.u+r.hu*0.4,r.v+c*r.hv],[r.u+r.hu*0.4,f],[r.u-r.hu*0.4,f],[r.u-r.hu*0.4,r.v+c*r.hv]],1.33)}else if(!e.flags.has("skip")&&r.u-r.hu<s.u+s.hu&&s.u-s.hu<r.u+r.hu){let f=(Math.max(r.u-r.hu,s.u-s.hu)+Math.min(r.u+r.hu,s.u+s.hu))/2,S=s.v>r.v?1:-1;At(e,[[f,r.v+S*r.hv],[f,s.v-S*s.hv]])}else if(s.u>r.u){let f=r.u+r.hu,S=Math.min(i/2,(s.u-s.hu-f)/2),E=o.nodes.get(e.from);if(o.nodes.get(e.to).groups.some((z)=>!E.groups.includes(z))){let z=Math.min(...[...vt.values()].filter((P)=>P.u-P.hu>f+1&&P.u<s.u&&P.v+P.hv>y&&P.v-P.hv<p).map((P)=>P.u-P.hu));if(z-ce+6-f>6)S=z-ce+6-f}let w=ge(c,f+S,s.u+s.hu,y,p);At(e,[[f,r.v],[f+S,r.v],[f+S,w],[s.u,w],[s.u,s.v+c*s.hv]])}else{let f=ge(c,Math.min(r.u-r.hu,s.u-s.hu),Math.max(r.u+r.hu,s.u+s.hu),y,p);At(e,[[r.u,r.v+c*r.hv],[r.u,f],[s.u,f],[s.u,s.v+c*s.hv]],1.33)}}for(let e of[...o.groups].reverse()){let r=1/0,s=1/0,c=-1/0,y=-1/0,p=(E,m,w=ce)=>{r=Math.min(r,E-w),s=Math.min(s,m-w),c=Math.max(c,E+w),y=Math.max(y,m+w)},f=new Set(u.filter((E)=>E.groups.includes(e.id)).map((E)=>E.id));for(let E of f){let m=o.nodes.get(E);p(m.x-m.w/2,m.y-m.h/2),p(m.x+m.w/2,m.y+m.h/2)}for(let E of o.edges)if(f.has(E.from)&&f.has(E.to))for(let[m,w]of E.pts)p(m,w);else if(f.has(E.to)&&E.pts.length===5)for(let[m,w]of E.pts.slice(2))p(m,w,6);for(let E of o.groups)if(E.parent===e.id&&E.w)p(E.x,E.y),p(E.x+E.w,E.y+E.h);if(r===1/0)continue;let S=e.label?18:0;Object.assign(e,{x:r,y:s-S,w:c-r,h:y-s+S})}let l=[];for(let e of u)if(l.push([e.x-e.w/2,e.y-e.h/2,e.x+e.w/2,e.y+e.h/2+(e.attrs.caption?20:0)]),$e.has(e.shape)&&e.label){let r=bt(e.label,we(e,h),500,e.attrs.fontFamily||x),s=e.y-e.h/2-11-+(e.attrs.ghost??e.attrs.heads??0)*5;l.push([e.x-r.w/2,s-r.h/2,e.x+r.w/2,s+r.h/2])}for(let e of o.groups)if(e.w&&e.label){let r=bt(e.label,h-2,600,x);l.push([e.x+12,e.y+13-r.h/2,e.x+12+r.w,e.y+13+r.h/2])}let d=o.edges.flatMap((e)=>e.pts.slice(1).map((r,s)=>{let c=e.pts[s];return[e,s,[Math.min(c[0],r[0])-2,Math.min(c[1],r[1])-2,Math.max(c[0],r[0])+2,Math.max(c[1],r[1])+2]]}));for(let e of o.edges){if(!e.label||e.pts.length<2)continue;let r=bt(e.label,h-1,Ct,x),s=e.pts[0],c=e.pts[e.pts.length-1],y=(C)=>Math.hypot(e.pts[C+1][0]-e.pts[C][0],e.pts[C+1][1]-e.pts[C][1]),p=0;for(let C=1;C+1<e.pts.length;C++)if(y(C)>y(p))p=C;if(e.flags.has("join")&&e.pts.length===3)p=1;let f=(C,Y,st)=>{let[b,k]=[e.pts[C],e.pts[C+1]],G=b[0]+(k[0]-b[0])*Y,N=b[1]+(k[1]-b[1])*Y;if(Math.abs(b[1]-k[1])<Math.abs(b[0]-k[0])){let L=(N>Math.max(s[1],c[1])+1?1:-1)*st;return[G,N+L*(r.h/2+5),C]}let T=(G<Math.min(s[0],c[0])-1?-1:1)*st;return[G+T*(r.w/2+7),N,C]},S=[p,...e.pts.slice(1).map((C,Y)=>Y).filter((C)=>C!==p&&y(C)>1).sort((C,Y)=>y(Y)-y(C))],E=[0.5,0.25,0.75].flatMap((C)=>S.flatMap((Y)=>[f(Y,C,1),f(Y,C,-1)])),m=([C,Y])=>[C-r.w/2,Y-r.h/2,C+r.w/2,Y+r.h/2],w=([C,Y,st])=>!l.some((b)=>ve(m([C,Y]),b))&&!d.some(([b,k,G])=>!(b===e&&k===st)&&ve(m([C,Y]),G)),[z,P]=E.find(w)??E[0];e.lx=z,e.ly=P,l.push(m([z,P]))}return o}var Tt,fe,Yt={},oo=new Map;function ro(o,n,i){let a=o.createShader(n);if(o.shaderSource(a,i),o.compileShader(a),!o.getShaderParameter(a,o.COMPILE_STATUS))throw Error(o.getShaderInfoLog(a)??"shader");return a}function le(o,n){let i=o.createProgram();if(o.attachShader(i,ro(o,o.VERTEX_SHADER,`
2
+ attribute vec2 aPos;
3
+ varying vec2 vUv;
4
+ void main() {
5
+ vUv = aPos * 0.5 + 0.5;
6
+ gl_Position = vec4(aPos, 0.0, 1.0);
7
+ }`)),o.attachShader(i,ro(o,o.FRAGMENT_SHADER,n)),o.bindAttribLocation(i,0,"aPos"),o.linkProgram(i),!o.getProgramParameter(i,o.LINK_STATUS))throw Error(o.getProgramInfoLog(i)??"link");return i}function yr(){if(Tt!==void 0)return Tt;if(Tt=null,typeof document>"u")return Tt;try{fe=document.createElement("canvas"),fe.width=fe.height=1024;let o=fe.getContext("webgl",{preserveDrawingBuffer:!0,premultipliedAlpha:!1});if(!o)return Tt;o.bindBuffer(o.ARRAY_BUFFER,o.createBuffer()),o.bufferData(o.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),o.STATIC_DRAW),o.enableVertexAttribArray(0),o.vertexAttribPointer(0,2,o.FLOAT,!1,0,0),Yt.ice=le(o,`
8
+ precision highp float;
9
+ varying vec2 vUv;
10
+ uniform vec2 uRes;
11
+ uniform float uTime;
12
+ uniform float uIntensity;
13
+ uniform float uSeed;
14
+ uniform float uCover;
15
+
16
+ vec2 hash2(vec2 p) {
17
+ p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));
18
+ return fract(sin(p + uSeed) * 43758.5453);
19
+ }
20
+
21
+ float noise(vec2 p) {
22
+ vec2 i = floor(p);
23
+ vec2 f = fract(p);
24
+ vec2 u = f * f * (3.0 - 2.0 * f);
25
+ float a = hash2(i).x;
26
+ float b = hash2(i + vec2(1.0, 0.0)).x;
27
+ float c = hash2(i + vec2(0.0, 1.0)).x;
28
+ float d = hash2(i + vec2(1.0, 1.0)).x;
29
+ return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
30
+ }
31
+
32
+ float fbm(vec2 p) {
33
+ float v = 0.0;
34
+ float a = 0.5;
35
+ for (int i = 0; i < 5; i++) {
36
+ v += a * noise(p);
37
+ p = p * 2.03 + 11.7;
38
+ a *= 0.5;
39
+ }
40
+ return v;
41
+ }
42
+
43
+ // Both materials share one block outline: a rounded rectangle with a bevel.
44
+ const float BEVEL = 9.0;
45
+
46
+ // Inside distance to the edge of a rounded rectangle filling the canvas.
47
+ float inside(vec2 px) {
48
+ vec2 hs = uRes * 0.5;
49
+ float r = 7.0;
50
+ vec2 q = abs(px - hs) - (hs - r);
51
+ return -(length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r);
52
+ }
53
+
54
+ // Distance to the nearest Voronoi cell border: thin values are cracks.
55
+ float cells(vec2 p) {
56
+ vec2 n = floor(p);
57
+ vec2 f = fract(p);
58
+ float d1 = 8.0;
59
+ float d2 = 8.0;
60
+ for (int j = -1; j <= 1; j++) {
61
+ for (int i = -1; i <= 1; i++) {
62
+ vec2 g = vec2(float(i), float(j));
63
+ vec2 r = g + hash2(n + g) - f;
64
+ float d = dot(r, r);
65
+ if (d < d1) { d2 = d1; d1 = d; }
66
+ else if (d < d2) { d2 = d; }
67
+ }
68
+ }
69
+ return sqrt(d2) - sqrt(d1);
70
+ }
71
+
72
+
73
+ float ridge(vec2 p) {
74
+ float v = 0.0;
75
+ float a = 0.5;
76
+ for (int i = 0; i < 5; i++) {
77
+ float n = 1.0 - abs(noise(p) * 2.0 - 1.0);
78
+ v += a * n * n;
79
+ p = p * 2.1 + vec2(1.7, 9.2);
80
+ a *= 0.5;
81
+ }
82
+ return v;
83
+ }
84
+
85
+ float fern(vec2 px) {
86
+ vec2 w = vec2(fbm(px / 30.0 + uSeed), fbm(px / 30.0 + 4.0 + uSeed));
87
+ return ridge(px / 13.0 + w * 2.6);
88
+ }
89
+
90
+ // Surface height: a bevel up from the edge plus a gently melted top.
91
+ float height(vec2 px) {
92
+ float d = inside(px);
93
+ float bevel = 1.0 - pow(1.0 - clamp(d / BEVEL, 0.0, 1.0), 2.0);
94
+ return bevel * 3.0 + fbm(px / 38.0 + uSeed) * 1.6;
95
+ }
96
+
97
+ void main() {
98
+ vec2 px = vUv * uRes;
99
+ float d = inside(px);
100
+ if (d < 0.0) { gl_FragColor = vec4(0.0); return; }
101
+
102
+ vec2 e = vec2(0.75, 0.0);
103
+ vec2 grad = vec2(height(px + e.xy) - height(px - e.xy), height(px + e.yx) - height(px - e.yx)) / (2.0 * e.x);
104
+ vec3 n = normalize(vec3(-grad * 1.4, 1.0));
105
+ vec3 light = normalize(vec3(-0.55, 0.65, 0.7));
106
+ vec3 h = normalize(light + vec3(0.0, 0.0, 1.0));
107
+ float spec = pow(max(dot(n, h), 0.0), 90.0);
108
+ float sheen = pow(max(dot(n, h), 0.0), 9.0);
109
+ float fresnel = pow(1.0 - n.z, 1.6);
110
+
111
+ float rim = 1.0 - smoothstep(0.0, BEVEL * 1.6, d);
112
+ float edgeLine = exp(-pow((d - 1.6) / 0.9, 2.0));
113
+ float innerEdge = exp(-pow((d - BEVEL) / 1.8, 2.0));
114
+ float core = smoothstep(BEVEL, BEVEL * 3.5, d) * smoothstep(0.35, 0.8, fbm(px / 26.0 + uSeed * 3.0));
115
+ float veins = (1.0 - smoothstep(0.0, 0.035, cells(px / 70.0 + uSeed))) * smoothstep(BEVEL, BEVEL * 2.0, d);
116
+
117
+ vec3 tint = vec3(0.56, 0.74, 0.88);
118
+ vec3 col = tint * (0.45 + 0.4 * vUv.y);
119
+ col = mix(col, vec3(0.86, 0.93, 0.99), rim * 0.6 + core * 0.35 + veins * 0.1);
120
+ col -= innerEdge * 0.12;
121
+ col += (spec * 1.4 + sheen * 0.22) * vec3(1.0, 1.0, 0.98);
122
+ col += edgeLine * 0.55;
123
+
124
+ float alpha = 0.16 + rim * 0.55 + fresnel * 0.5 + core * 0.3 + veins * 0.2
125
+ + spec + sheen * 0.15 + edgeLine * 0.6;
126
+
127
+ // frost: where it has reached, how thick it is, and its growing front
128
+ float toCenter = clamp(d / (min(uRes.x, uRes.y) * 0.5), 0.0, 1.0);
129
+ float seed = 1.0 - toCenter * 0.85 + (fbm(px / 18.0 + uSeed * 5.0) - 0.5) * 0.5;
130
+ float level = seed + uCover * 1.3 - 1.05;
131
+ float frozen = smoothstep(-0.03, 0.03, level);
132
+ float front = exp(-pow(level / 0.02, 2.0)) * step(uCover, 0.995);
133
+ float f = fern(px);
134
+ float frost = smoothstep(0.42, 0.8, f) * frozen * (0.3 + 0.7 * clamp(seed, 0.0, 1.0));
135
+ vec2 g = vec2(fern(px + e.xy) - fern(px - e.xy), fern(px + e.yx) - fern(px - e.yx)) / (2.0 * e.x);
136
+ vec3 fn = normalize(vec3(-g * 5.0, 1.0));
137
+ float fspec = pow(max(dot(fn, h), 0.0), 36.0) * frost;
138
+
139
+ col = mix(col, vec3(0.9, 0.95, 1.0), frost * 0.7);
140
+ col += fspec * 0.9 + front * vec3(0.75, 0.92, 1.0) * 0.9;
141
+ alpha = max(alpha, frost * 0.8) + fspec * 0.5;
142
+
143
+ // while freezing over something warm, the new ice is darker and thicker
144
+ float backing = frozen * clamp(1.0 - uCover, 0.0, 1.0);
145
+ col = mix(col, vec3(0.1, 0.14, 0.18), backing * 0.5 * (1.0 - frost));
146
+ alpha = max(alpha, backing * 0.85);
147
+
148
+ alpha = alpha * frozen + front * 0.8;
149
+ gl_FragColor = vec4(min(col, 1.0), clamp(alpha, 0.0, 0.97));
150
+ }`),Yt.heat=le(o,`
151
+ precision highp float;
152
+ varying vec2 vUv;
153
+ uniform vec2 uRes;
154
+ uniform float uTime;
155
+ uniform float uIntensity;
156
+ uniform float uSeed;
157
+ uniform float uCover;
158
+
159
+ vec2 hash2(vec2 p) {
160
+ p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));
161
+ return fract(sin(p + uSeed) * 43758.5453);
162
+ }
163
+
164
+ float noise(vec2 p) {
165
+ vec2 i = floor(p);
166
+ vec2 f = fract(p);
167
+ vec2 u = f * f * (3.0 - 2.0 * f);
168
+ float a = hash2(i).x;
169
+ float b = hash2(i + vec2(1.0, 0.0)).x;
170
+ float c = hash2(i + vec2(0.0, 1.0)).x;
171
+ float d = hash2(i + vec2(1.0, 1.0)).x;
172
+ return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
173
+ }
174
+
175
+ float fbm(vec2 p) {
176
+ float v = 0.0;
177
+ float a = 0.5;
178
+ for (int i = 0; i < 5; i++) {
179
+ v += a * noise(p);
180
+ p = p * 2.03 + 11.7;
181
+ a *= 0.5;
182
+ }
183
+ return v;
184
+ }
185
+
186
+ // Both materials share one block outline: a rounded rectangle with a bevel.
187
+ const float BEVEL = 9.0;
188
+
189
+ // Inside distance to the edge of a rounded rectangle filling the canvas.
190
+ float inside(vec2 px) {
191
+ vec2 hs = uRes * 0.5;
192
+ float r = 7.0;
193
+ vec2 q = abs(px - hs) - (hs - r);
194
+ return -(length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r);
195
+ }
196
+
197
+ // Distance to the nearest Voronoi cell border: thin values are cracks.
198
+ float cells(vec2 p) {
199
+ vec2 n = floor(p);
200
+ vec2 f = fract(p);
201
+ float d1 = 8.0;
202
+ float d2 = 8.0;
203
+ for (int j = -1; j <= 1; j++) {
204
+ for (int i = -1; i <= 1; i++) {
205
+ vec2 g = vec2(float(i), float(j));
206
+ vec2 r = g + hash2(n + g) - f;
207
+ float d = dot(r, r);
208
+ if (d < d1) { d2 = d1; d1 = d; }
209
+ else if (d < d2) { d2 = d; }
210
+ }
211
+ }
212
+ return sqrt(d2) - sqrt(d1);
213
+ }
214
+
215
+ vec3 lava(float h) {
216
+ vec3 col = mix(vec3(0.35, 0.04, 0.02), vec3(0.93, 0.33, 0.06), smoothstep(0.0, 0.55, h));
217
+ return mix(col, vec3(1.0, 0.86, 0.5), smoothstep(0.6, 1.0, h));
218
+ }
219
+
220
+ void main() {
221
+ vec2 px = vUv * uRes;
222
+ float d = inside(px);
223
+ if (d < 0.0) { gl_FragColor = vec4(0.0); return; }
224
+ float t = uTime;
225
+ float k = 0.55 + 0.45 * uIntensity;
226
+ vec2 p = px / 24.0;
227
+ vec2 flow = vec2(fbm(p * 0.55 + vec2(t * 0.04, 0.0)), fbm(p * 0.55 + vec2(3.1, -t * 0.035)));
228
+ vec2 q = p + (flow - 0.5) * 1.5 + vec2(t * 0.03, -t * 0.05);
229
+
230
+ float e = cells(q + uSeed);
231
+ float fine = cells(q * 2.4 + 5.0 + uSeed);
232
+ float crack = 1.0 - smoothstep(0.0, 0.035 + 0.075 * k, e);
233
+ float hairline = (1.0 - smoothstep(0.0, 0.025, fine)) * smoothstep(0.12, 0.3, e);
234
+ float glow = exp(-e * (11.0 - 5.0 * k));
235
+ float pulse = 0.75 + 0.25 * sin(t * 1.8 + fbm(q * 1.3) * 6.2832);
236
+
237
+ // crust: rough basalt, a little lighter in the middle of each plate
238
+ float rough = fbm(px / 5.0 + uSeed);
239
+ vec3 crust = mix(vec3(0.045, 0.032, 0.028), vec3(0.15, 0.11, 0.09), rough);
240
+ crust *= 0.75 + 0.5 * smoothstep(0.05, 0.5, e);
241
+
242
+ float heat = clamp(crack * pulse + hairline * 0.35 * k, 0.0, 1.0);
243
+ vec3 col = crust + glow * vec3(0.85, 0.24, 0.04) * 0.5 * k;
244
+ col = mix(col, lava(heat * (0.75 + 0.25 * k)), heat);
245
+ // a black frame of cooled rock, the same bevel the ice has: dark at the
246
+ // rim, a faint grey edge where the bevel catches the light
247
+ float frame = 1.0 - smoothstep(1.6, 3.2, d);
248
+ col = mix(col, vec3(0.018, 0.014, 0.012) + rough * 0.035, frame);
249
+ col += exp(-pow((d - 1.4) / 0.9, 2.0)) * 0.16;
250
+ gl_FragColor = vec4(min(col, 1.0), 1.0);
251
+ }`),Yt.glass=le(o,`
252
+ precision highp float;
253
+ varying vec2 vUv;
254
+ uniform vec2 uRes;
255
+ uniform float uTime;
256
+ uniform float uIntensity;
257
+ uniform float uSeed;
258
+ uniform float uCover;
259
+
260
+ vec2 hash2(vec2 p) {
261
+ p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));
262
+ return fract(sin(p + uSeed) * 43758.5453);
263
+ }
264
+
265
+ float noise(vec2 p) {
266
+ vec2 i = floor(p);
267
+ vec2 f = fract(p);
268
+ vec2 u = f * f * (3.0 - 2.0 * f);
269
+ float a = hash2(i).x;
270
+ float b = hash2(i + vec2(1.0, 0.0)).x;
271
+ float c = hash2(i + vec2(0.0, 1.0)).x;
272
+ float d = hash2(i + vec2(1.0, 1.0)).x;
273
+ return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
274
+ }
275
+
276
+ float fbm(vec2 p) {
277
+ float v = 0.0;
278
+ float a = 0.5;
279
+ for (int i = 0; i < 5; i++) {
280
+ v += a * noise(p);
281
+ p = p * 2.03 + 11.7;
282
+ a *= 0.5;
283
+ }
284
+ return v;
285
+ }
286
+
287
+ // Both materials share one block outline: a rounded rectangle with a bevel.
288
+ const float BEVEL = 9.0;
289
+
290
+ // Inside distance to the edge of a rounded rectangle filling the canvas.
291
+ float inside(vec2 px) {
292
+ vec2 hs = uRes * 0.5;
293
+ float r = 7.0;
294
+ vec2 q = abs(px - hs) - (hs - r);
295
+ return -(length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r);
296
+ }
297
+
298
+ // Distance to the nearest Voronoi cell border: thin values are cracks.
299
+ float cells(vec2 p) {
300
+ vec2 n = floor(p);
301
+ vec2 f = fract(p);
302
+ float d1 = 8.0;
303
+ float d2 = 8.0;
304
+ for (int j = -1; j <= 1; j++) {
305
+ for (int i = -1; i <= 1; i++) {
306
+ vec2 g = vec2(float(i), float(j));
307
+ vec2 r = g + hash2(n + g) - f;
308
+ float d = dot(r, r);
309
+ if (d < d1) { d2 = d1; d1 = d; }
310
+ else if (d < d2) { d2 = d; }
311
+ }
312
+ }
313
+ return sqrt(d2) - sqrt(d1);
314
+ }
315
+
316
+ void main() {
317
+ vec2 px = vUv * uRes;
318
+ float d = inside(px);
319
+ if (d < 0.0) { gl_FragColor = vec4(0.0); return; }
320
+ float frost = fbm(px / 9.0 + uSeed) * 0.6 + fbm(px / 40.0 + uSeed * 2.0) * 0.4;
321
+ float rim = 1.0 - smoothstep(0.0, BEVEL * 1.2, d);
322
+ float edge = exp(-pow((d - 1.3) / 0.9, 2.0));
323
+ float s1 = exp(-pow((vUv.x * 0.7 + vUv.y - 1.15) / 0.05, 2.0));
324
+ float s2 = exp(-pow((vUv.x * 0.7 + vUv.y - 1.32) / 0.02, 2.0));
325
+ float a = 0.2 + frost * 0.18 + rim * 0.35 * vUv.y + edge * 0.6 + s1 * 0.3 + s2 * 0.45;
326
+ vec3 col = vec3(1.0) - (1.0 - vUv.y) * rim * 0.3;
327
+ gl_FragColor = vec4(col, clamp(a, 0.0, 0.9));
328
+ }`),Yt.metal=le(o,`
329
+ precision highp float;
330
+ varying vec2 vUv;
331
+ uniform vec2 uRes;
332
+ uniform float uTime;
333
+ uniform float uIntensity;
334
+ uniform float uSeed;
335
+ uniform float uCover;
336
+
337
+ vec2 hash2(vec2 p) {
338
+ p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));
339
+ return fract(sin(p + uSeed) * 43758.5453);
340
+ }
341
+
342
+ float noise(vec2 p) {
343
+ vec2 i = floor(p);
344
+ vec2 f = fract(p);
345
+ vec2 u = f * f * (3.0 - 2.0 * f);
346
+ float a = hash2(i).x;
347
+ float b = hash2(i + vec2(1.0, 0.0)).x;
348
+ float c = hash2(i + vec2(0.0, 1.0)).x;
349
+ float d = hash2(i + vec2(1.0, 1.0)).x;
350
+ return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
351
+ }
352
+
353
+ float fbm(vec2 p) {
354
+ float v = 0.0;
355
+ float a = 0.5;
356
+ for (int i = 0; i < 5; i++) {
357
+ v += a * noise(p);
358
+ p = p * 2.03 + 11.7;
359
+ a *= 0.5;
360
+ }
361
+ return v;
362
+ }
363
+
364
+ // Both materials share one block outline: a rounded rectangle with a bevel.
365
+ const float BEVEL = 9.0;
366
+
367
+ // Inside distance to the edge of a rounded rectangle filling the canvas.
368
+ float inside(vec2 px) {
369
+ vec2 hs = uRes * 0.5;
370
+ float r = 7.0;
371
+ vec2 q = abs(px - hs) - (hs - r);
372
+ return -(length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r);
373
+ }
374
+
375
+ // Distance to the nearest Voronoi cell border: thin values are cracks.
376
+ float cells(vec2 p) {
377
+ vec2 n = floor(p);
378
+ vec2 f = fract(p);
379
+ float d1 = 8.0;
380
+ float d2 = 8.0;
381
+ for (int j = -1; j <= 1; j++) {
382
+ for (int i = -1; i <= 1; i++) {
383
+ vec2 g = vec2(float(i), float(j));
384
+ vec2 r = g + hash2(n + g) - f;
385
+ float d = dot(r, r);
386
+ if (d < d1) { d2 = d1; d1 = d; }
387
+ else if (d < d2) { d2 = d; }
388
+ }
389
+ }
390
+ return sqrt(d2) - sqrt(d1);
391
+ }
392
+
393
+ void main() {
394
+ vec2 px = vUv * uRes;
395
+ float d = inside(px);
396
+ if (d < 0.0) { gl_FragColor = vec4(0.0); return; }
397
+ float streak = fbm(vec2(px.x / 140.0, px.y * 0.7) + uSeed);
398
+ float fine = noise(vec2(px.x / 5.0, px.y * 3.0) + uSeed);
399
+ vec3 col = mix(vec3(0.50, 0.53, 0.57), vec3(0.80, 0.82, 0.85), vUv.y);
400
+ col += (streak - 0.5) * 0.22 + (fine - 0.5) * 0.07;
401
+ col += exp(-pow((vUv.x * 0.8 + (1.0 - vUv.y) * 0.5 - 0.55) / 0.16, 2.0)) * 0.22;
402
+ float bevel = 1.0 - smoothstep(0.0, BEVEL * 0.7, d);
403
+ col = mix(col, mix(vec3(0.32, 0.34, 0.37), vec3(0.96), vUv.y), bevel * 0.55);
404
+ col += exp(-pow((d - 1.2) / 0.8, 2.0)) * 0.25;
405
+ gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
406
+ }`),Yt.noise=le(o,`
407
+ precision highp float;
408
+ varying vec2 vUv;
409
+ uniform vec2 uRes;
410
+ uniform float uTime;
411
+ uniform float uIntensity;
412
+ uniform float uSeed;
413
+ uniform float uCover;
414
+
415
+ vec2 hash2(vec2 p) {
416
+ p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));
417
+ return fract(sin(p + uSeed) * 43758.5453);
418
+ }
419
+
420
+ float noise(vec2 p) {
421
+ vec2 i = floor(p);
422
+ vec2 f = fract(p);
423
+ vec2 u = f * f * (3.0 - 2.0 * f);
424
+ float a = hash2(i).x;
425
+ float b = hash2(i + vec2(1.0, 0.0)).x;
426
+ float c = hash2(i + vec2(0.0, 1.0)).x;
427
+ float d = hash2(i + vec2(1.0, 1.0)).x;
428
+ return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
429
+ }
430
+
431
+ float fbm(vec2 p) {
432
+ float v = 0.0;
433
+ float a = 0.5;
434
+ for (int i = 0; i < 5; i++) {
435
+ v += a * noise(p);
436
+ p = p * 2.03 + 11.7;
437
+ a *= 0.5;
438
+ }
439
+ return v;
440
+ }
441
+
442
+ // Both materials share one block outline: a rounded rectangle with a bevel.
443
+ const float BEVEL = 9.0;
444
+
445
+ // Inside distance to the edge of a rounded rectangle filling the canvas.
446
+ float inside(vec2 px) {
447
+ vec2 hs = uRes * 0.5;
448
+ float r = 7.0;
449
+ vec2 q = abs(px - hs) - (hs - r);
450
+ return -(length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r);
451
+ }
452
+
453
+ // Distance to the nearest Voronoi cell border: thin values are cracks.
454
+ float cells(vec2 p) {
455
+ vec2 n = floor(p);
456
+ vec2 f = fract(p);
457
+ float d1 = 8.0;
458
+ float d2 = 8.0;
459
+ for (int j = -1; j <= 1; j++) {
460
+ for (int i = -1; i <= 1; i++) {
461
+ vec2 g = vec2(float(i), float(j));
462
+ vec2 r = g + hash2(n + g) - f;
463
+ float d = dot(r, r);
464
+ if (d < d1) { d2 = d1; d1 = d; }
465
+ else if (d < d2) { d2 = d; }
466
+ }
467
+ }
468
+ return sqrt(d2) - sqrt(d1);
469
+ }
470
+
471
+ void main() {
472
+ vec2 px = vUv * uRes;
473
+ float d = inside(px);
474
+ if (d < 0.0) { gl_FragColor = vec4(0.0); return; }
475
+ vec2 c = floor(gl_FragCoord.xy);
476
+ vec2 h = hash2(c);
477
+ float g = sqrt(-2.0 * log(h.x + 1e-4)) * cos(6.2832 * h.y);
478
+ vec3 tint = vec3(hash2(c + 17.0).x, hash2(c + 41.0).x, hash2(c + 73.0).x) - 0.5;
479
+ vec3 col = clamp(0.5 + g * 0.18 + tint * 0.25, 0.0, 1.0);
480
+ gl_FragColor = vec4(col, 0.55 + exp(-pow((d - 1.2) / 0.9, 2.0)) * 0.3);
481
+ }`),Tt=o}catch{Tt=null}return Tt}function so(o,n,i,a=0){n=Math.round(n),i=Math.round(i);let u=`${o}:${n}x${i}:${a}`,h=oo.get(u);if(h)return h;let x=yr();if(!x||n<1||i<1)return null;let g=Math.min(Math.max(1,globalThis.devicePixelRatio||1),2,1024/Math.max(n,i)),v=Math.round(n*g),R=Math.round(i*g),I=Yt[o];x.viewport(0,0,v,R),x.useProgram(I);let q=(X)=>x.getUniformLocation(I,X);x.uniform2f(q("uRes"),n,i),x.uniform1f(q("uTime"),0),x.uniform1f(q("uIntensity"),0.5),x.uniform1f(q("uSeed"),a),x.uniform1f(q("uCover"),1),x.drawArrays(x.TRIANGLE_STRIP,0,4);let O=document.createElement("canvas");O.width=v,O.height=R,O.getContext("2d").drawImage(fe,0,1024-R,v,R,0,0,v,R);let U=O.toDataURL("image/webp",0.9);return oo.set(u,U),U}var ue={light:{none:["#e2e5e9","#7f8794"],bg:"none",text:"#1d2430",sub:"#5b6474",edge:"#39414f",halo:"#fff",group:["#f5f6f9","#d5dae2"],shadow:"rgba(30,40,60,.10)",dark:!1,ice:["#f7fdff","#d2effb","#aedcf2","#6fb9de"],fire:["#fff6e8","#ffcf9f","#ee7d36"],c:{white:["#ffffff","#8b95a5"],gray:["#eceff3","#9aa3b2"],pink:["#fde1e4","#e58c98"],orange:["#fde4c8","#e69a50"],yellow:["#f4f5cc","#c0c064"],green:["#d8f1de","#68b880"],blue:["#d6eafc","#5f9fd8"],purple:["#e4defa","#9584db"],red:["#ffd7d2","#dd6559"],teal:["#d1f0ec","#48a99f"]}},dark:{none:["#2a2f37","#8a93a2"],bg:"#0e1117",text:"#e8edf5",sub:"#9aa4b5",edge:"#c3cad6",halo:"#0e1117",group:["#161a22","#2b323f"],shadow:"rgba(0,0,0,.45)",dark:!0,ice:["#29506a","#163a50","#0f2c3e","#7fd4ff"],fire:["#4a2a12","#33180a","#ff9447"],c:{white:["#1b2029","#7d8799"],gray:["#1f242d","#8792a4"],pink:["#3a1e29","#ff8ea2"],orange:["#3a2916","#ffae5c"],yellow:["#2f3018","#e3da66"],green:["#142e22","#5ad08a"],blue:["#13273c","#5ab1ff"],purple:["#241e3f","#a892ff"],red:["#3c1b19","#ff786a"],teal:["#11312f","#4cd3c3"]}}},xt=(o)=>+o.toFixed(1),Z=(o,...n)=>o.reduce((i,a,u)=>i+(typeof n[u-1]==="number"?xt(n[u-1]):n[u-1])+a),Et=(o)=>o.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;"),Oe=(o)=>/^#([\da-f]{3}|[\da-f]{6})$/i.test(o),no=(o)=>{let n=o.replace("#",""),i=n.length===3?[...n].map((a)=>a+a).join(""):n;return[0,2,4].map((a)=>parseInt(i.slice(a,a+2),16))},ut=(o,n,i)=>{if(!Oe(o)||!Oe(n))return o;return i=Math.min(1,Math.max(0,i)),"#"+no(o).map((a,u)=>Math.round(a+(no(n)[u]-a)*i).toString(16).padStart(2,"0")).join("")},It=(o,n,i,a,u)=>(u=Math.min(u,i/2,a/2),Z`M${o+u},${n}h${i-2*u}a${u},${u} 0 0 1 ${u},${u}v${a-2*u}a${u},${u} 0 0 1 ${-u},${u}h${2*u-i}a${u},${u} 0 0 1 ${-u},${-u}v${2*u-a}a${u},${u} 0 0 1 ${u},${-u}z`),Me=(o,n,i)=>Z`M${o-i},${n}a${i},${i} 0 1 0 ${2*i},0a${i},${i} 0 1 0 ${-2*i},0z`,Vt=(...o)=>"M"+o.map(xt).join(",").replace(/([^,]*,[^,]*),/g,"$1L")+"z",fo=-0.65,uo=0.45,ao=Math.cos(fo),io=Math.sin(fo),co=Math.cos(uo),lo=Math.sin(uo),mt=(o,n,i)=>{let a=o*ao-n*io,u=o*io+n*ao;return[a,-i*co-u*lo,u*co-i*lo]},Fe=(o,n,i,a,u,h)=>{let x=o.map((V)=>V[0]),g=o.map((V)=>V[1]),v=Math.min(...x),R=Math.max(...x),I=Math.min(...g),q=Math.max(...g),O=Math.min((a-2*h)/(R-v||1),(u-2*h)/(q-I||1)),U=n+a/2-(v+R)/2*O,X=i+u/2-(I+q)/2*O;return(V)=>[U+V[0]*O,X+V[1]*O]},vr=(o)=>[(0.5+o*0.7548776662)%1,(0.5+o*0.569840291)%1],$r=(o,n)=>Array.from({length:n},(i,a)=>{let[u,h]=vr(a),x=u*2*Math.PI;if(o==="sphere"){let X=2*h-1,V=Math.sqrt(1-X*X);return[V*Math.cos(x),V*Math.sin(x),X]}if(o==="cube"){let X=a%6,V=2*u-1,_=2*h-1,ft=X<3?1:-1;return X%3===0?[ft,V,_]:X%3===1?[V,ft,_]:[V,_,ft]}let g=h*2*Math.PI,v=0.68,R=0.3,I=0.8,q=(v+R*Math.cos(g))*Math.cos(x),O=(v+R*Math.cos(g))*Math.sin(x),U=R*Math.sin(g);return[q,O*Math.cos(I)-U*Math.sin(I),O*Math.sin(I)+U*Math.cos(I)]}),wr=(o)=>Array.from({length:o},(n,i)=>{let a=i/o,h=0.06+0.9*[[0.14,0.9],[0.3,0.6],[0.47,1],[0.66,0.75],[0.84,0.5]].reduce((x,[g,v])=>x+v*Math.exp(-(((a-g)/0.07)**2)),0)*(0.55+0.45*Math.abs(Math.sin(i*1.7)*Math.cos(i*0.37)));return[-h*(0.85+0.15*Math.sin(i*2.3)),h]}),Se=["blue","orange","green","purple","red","teal","pink","yellow"],Be=(o)=>Se[((o|0)%Se.length+Se.length)%Se.length],kr=0;function Ee(o,n,i=!0){let a=ue[n??o.theme]??ue.light,u=`mm${kr++}-`,h=o.dir==="LR"||o.dir==="RL",x=o.fontSize??Pe,g=o.fontFamily??ye,v=new Map,R=new Map,I=(t)=>{if(!t)return"none";if(/^#?\w+$/.test(t))return t.replace("#","");if(!R.has(t))R.set(t,`css${R.size}`);return R.get(t)},q=1/0,O=1/0,U=-1/0,X=-1/0,V=(t,l,d=0,e=0)=>{q=Math.min(q,t),O=Math.min(O,l),U=Math.max(U,t+d),X=Math.max(X,l+e)},_=(t)=>{if(!t)return a.none;if(a.c[t])return a.c[t];if(Oe(t))return[a.dark?ut(t,"#0e1117",0.75):ut(t,"#ffffff",0.78),t];return[Et(t),a.edge]},ft=(t)=>{let l="c-"+I(t);if(!v.has(l)){let[d,e]=_(t),r=a.dark?ut(d,e,0.16):ut(d,"#ffffff",0.6);v.set(l,Z`<linearGradient id="${u}${l}" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="${r}"/><stop offset="1" stop-color="${d}"/></linearGradient>`)}return`url(#${u}${l})`},W=()=>{if(v.has("ice"))return;let[t,l,d]=a.ice;v.set("ice",`<linearGradient id="${u}ice" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="${t}"/><stop offset=".55" stop-color="${l}"/><stop offset="1" stop-color="${d}"/></linearGradient><pattern id="${u}frost" width="56" height="56" patternUnits="userSpaceOnUse"><path d="M0 14 16 20 25 6M16 20 22 37 41 33 56 45M41 33 47 12 56 9M22 37 9 56M47 12 33 0" fill="none" stroke="#fff" stroke-opacity="${a.dark?0.18:0.75}" stroke-width="1"/><path d="M5 30 12 33M30 50 36 46M50 25 54 30" stroke="${a.ice[3]}" stroke-opacity=".35" stroke-width=".8"/></pattern>`)},lt=()=>{if(v.has("fire"))return;let[t,l,d]=a.fire;v.set("fire",`<linearGradient id="${u}fire" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="${t}"/><stop offset="1" stop-color="${l}"/></linearGradient><linearGradient id="${u}flame" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#ffd24a"/><stop offset=".6" stop-color="${d}"/><stop offset="1" stop-color="#e0452b"/></linearGradient>`)},wt=(t)=>{let l=t.attrs.texture??(t.flags.has("frozen")?"ice":t.flags.has("train")?"magma":"");if(l==="ice")return W(),{fill:`url(#${u}ice)`,stroke:a.ice[3],fx:l};if(l==="magma")return lt(),{fill:`url(#${u}fire)`,stroke:a.fire[2],fx:l};if(l==="metal")return{fill:ft(t.color),stroke:a.dark?"#9aa3ae":"#6d747d",fx:l};return{fill:ft(t.color),stroke:_(t.color)[1],fx:l}},ot=(t,l,d,e,r,s,c="middle",y=g)=>{let p=bt(t,e,r,y),f="",S="";return p.lines.forEach((E,m)=>{let w=c==="middle"?l-p.widths[m]/2:c==="start"?l:l-p.widths[m],z=d-p.h/2+(m+0.5)*p.lh+e*0.36;for(let P of E){if(P.math){let C=P.math;S+=Z`<svg x="${w+P.x}" y="${z-C.asc}" width="${C.w}" height="${C.asc+C.desc}" viewBox="${C.viewBox}" overflow="visible" color="${s}">${C.inner}</svg>`;continue}if(f+=Z`<tspan x="${w+P.x}" y="${z+P.dy}"`,P.size!==e)f+=Z` font-size="${P.size}"`;if(P.it)f+=' font-style="italic"';if(P.serif)f+=` font-family='${to}' font-weight="${P.b?700:400}"`;else if(P.b)f+=' font-weight="700"';f+=`>${Et(P.t)}</tspan>`}}),V(l-(c==="middle"?p.w/2:c==="end"?p.w:0),d-p.h/2,p.w,p.h),(f?Z`<text${y!==g?` font-family="${Et(y)}"`:""} font-size="${e}" font-weight="${r}" fill="${s}" style="white-space:pre">${f}</text>`:"")+S},H=(t,l)=>t.flags.has("frozen")?"ice":t.flags.has("train")?"magma":l==="ice"||l==="magma"?l:"",it=(t,l,d)=>{if(t==="ice")return Z`<g transform="translate(${l},${d})"><circle r="7.5" fill="${a.dark?"#0f2c3e":"#fff"}" stroke="${a.ice[3]}"/><path d="M0-4.6V4.6M-4 -2.3 4 2.3M-4 2.3 4-2.3M-1.4-3.6 0-2.4 1.4-3.6M-1.4 3.6 0 2.4 1.4 3.6" fill="none" stroke="${a.ice[3]}" stroke-width="1.1" stroke-linecap="round"/></g>`;if(t!=="magma")return"";return lt(),Z`<g transform="translate(${l},${d})"><circle r="7.5" fill="${a.dark?"#33180a":"#fff"}" stroke="${a.fire[2]}"/><path d="M0.4-5C3 -2.4 3.9-.2 3.1 2.2 2.5 4.1-2.3 4.3-3 2.1-3.6.2-2.2-1.2-1.6-2.6-.9-1.4-.3-.9.2-.6.9-2 .9-3.5.4-5z" fill="url(#${u}flame)"/></g>`},ct=0,ht={ice:"ice",magma:"heat",glass:"glass",metal:"metal",noise:"noise"},kt=(t,l,d,e,r,s,c)=>{if(pt.has(t))return`<path d="${l}" fill="${yt(t,c)}"/>`;let y=i&&ht[t]?so(ht[t],r,s):null;if(!y){if(t==="noise")return`<path d="${l}" fill="${yt("grain",c)}"/>`;if(t==="glass")return`<path d="${l}" fill="#fff" fill-opacity="${a.dark?0.12:0.4}"/>`;if(t==="metal")return`<path d="${l}" fill="${Pt()}"/>`;return""}let p=`m-${t}-${Math.round(r)}x${Math.round(s)}`;if(!v.has(p))v.set(p,Z`<image id="${u}${p}" width="${Math.round(r)}" height="${Math.round(s)}" preserveAspectRatio="none" href="${y}"/>`);let f=`${u}k${ct++}`;return`<clipPath id="${f}"><path d="${l}"/></clipPath><g clip-path="url(#${f})">`+Z`<use href="#${u}${p}" x="${d}" y="${e}"/></g>`},pt=new Set(["quantized","sparse","hatch"]),yt=(t,l)=>{let d=`p-${t}-${I(l)}`;if(!v.has(d)){let[e,r]=_(l),s=24,c="",y="";if(t==="quantized")s=32,[0,2,1,3,2,0,3,1,1,3,0,2,3,1,2,0].forEach((p,f)=>{c+=`<rect x="${f%4*8}" y="${(f>>2)*8}" width="8" height="8" fill="${ut(e,r,p/3*0.7)}"/>`});else if(t==="sparse")[1,0,1,1,0,1,0,1,1,1,0,0,1,0,1,1].forEach((p,f)=>{if(p)c+=`<circle cx="${f%4*6+3}" cy="${(f>>2)*6+3}" r="1.5" fill="${r}" fill-opacity=".75"/>`});else if(t==="hatch")s=7,y=' patternTransform="rotate(45)"',c=`<path d="M0 0V7" stroke="${r}" stroke-opacity=".55" stroke-width="1.4"/>`;else{s=24;for(let p=0;p<64;p++)c+=`<rect x="${p%8*3}" y="${(p>>3)*3}" width="3" height="3" fill="${["#000","#fff"][p*37%7>3?1:0]}" fill-opacity="${0.08+p*53%11/40}"/>`}v.set(d,`<pattern id="${u}${d}" width="${s}" height="${s}" patternUnits="userSpaceOnUse"${y}>${c}</pattern>`)}return`url(#${u}${d})`},Gt=(t,l,d)=>{if(pt.has(t))return`<path d="${l}" fill="${yt(t,d)}"/>`;if(t==="noise")return`<path d="${l}" fill="${yt("grain",d)}"/>`;if(t==="ice")return W(),`<path d="${l}" fill="url(#${u}ice)"/><path d="${l}" fill="url(#${u}frost)"/>`;if(t==="magma")return lt(),`<path d="${l}" fill="url(#${u}fire)"/>`;if(t==="metal")return`<path d="${l}" fill="${Pt()}"/>`;if(t==="glass")return`<path d="${l}" fill="#fff" fill-opacity="${a.dark?0.15:0.45}"/>`;return""},Pt=()=>{if(!v.has("metal"))v.set("metal",`<linearGradient id="${u}metal" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#e4e7eb"/><stop offset=".45" stop-color="#a7adb5"/><stop offset=".55" stop-color="#cfd3d8"/><stop offset="1" stop-color="#8d949d"/></linearGradient>`);return`url(#${u}metal)`},Xt=(t,l,d,e)=>{let r=t+e,s=t+d-e;if(s-r<8)return"";let c=Z`M${r},${l+1}H${s}V${l+3.5}`,y=Math.max(2,Math.round((s-r)/9)),p=(s-r)/y;for(let f=0;f<y;f++)c+=Z`q${-p/2},${[4,7,3.5,6,5][f%5]} ${-p},0`;return`<path d="${c}z" fill="#fff" fill-opacity="${a.dark?0.55:0.92}"/>`},Dt=(t)=>{let[l,d]=dt(t.attrs.size,[64,64]),e=+t.attrs.n||1;return{W:l,H:d,k:e,x0:t.x-(l+(e-1)*6)/2,y0:t.y-(d+(e-1)*6)/2,fit:t.attrs.fit==="contain"?"xMidYMid meet":"xMidYMid slice",srcs:t.attrs.srcs?JSON.parse(t.attrs.srcs):[t.attrs.src??""]}},Lt=(t)=>{let l=o.edges.find((d)=>d.to===t.id&&o.nodes.get(d.from)?.shape==="image");return l&&o.nodes.get(l.from)},he=(t)=>{let l=o.nodes.get(t.to);return l.shape==="magnify"&&Lt(l)?.id===t.from},zt=(t,l)=>{if(t.attrs.region)return dt(t.attrs.region,[]);let d=+l.attrs.grid||4,[e,r]=dt(t.attrs.patch,[-1,-1]);return e>=0?[e/d,r/d,1/d,1/d]:[0.375,0.375,0.25,0.25]},Kt=(t)=>{let l=t.x-t.w/2,d=t.y-t.h/2,{fill:e,stroke:r,fx:s}=wt(t),c=t.flags.has("dashed")?' stroke-dasharray="4 3"':"",y=t.flags.has("bold")?2:1.2,p=`fill="${e}" stroke="${r}" stroke-width="${y}"${c}`,f=+(t.attrs.ghost??t.attrs.heads??0),S=!1,E=(b,k=[l,d,t.w,t.h],G=0)=>{let N="";for(let L=Math.min(f,6)-1;L>0;L--)N+=Z`<path d="${b}" transform="translate(${L*5},${-L*5})" ${p} opacity="${0.35+0.5/(L+1)}"/>`;if(!a.dark||s)N+=Z`<path d="${b}" transform="translate(0,1.6)" fill="${a.shadow}"/>`;N+=`<path d="${b}" ${p}/>`;let T=kt(s,b,k[0],k[1],k[2],k[3],t.color);if(T)S=!0,N+=T+`<path d="${b}" fill="none" stroke="${r}" stroke-width="${y}" stroke-opacity=".7"${c}/>`;else if(s==="ice")N+=`<path d="${b}" fill="url(#${u}frost)"/>`+(G?Xt(k[0],k[1],k[2],G):"");return V(l,d-f*5,t.w+f*5,t.h+f*5),N},m="",w=we(t,x),z=t.attrs.fontFamily||g,[P,C]=s==="metal"?["#1d2430","#e8ebee"]:[a.text,a.halo],Y=()=>S&&s==="magma"?`<g paint-order="stroke" stroke="#1a0d07" stroke-width="3" stroke-linejoin="round">${ot(t.label,t.x,t.y,w,600,"#fff4e8","middle",z)}</g>`:S&&s!=="ice"&&s!=="glass"?`<g paint-order="stroke" stroke="${C}" stroke-width="3" stroke-linejoin="round" stroke-opacity=".85">${ot(t.label,t.x,t.y,w,600,P,"middle",z)}</g>`:ot(t.label,t.x,t.y,w,500,a.text,"middle",z);switch(t.shape){case"text":m=Y();break;case"op":{m=E(Me(t.x,t.y,11),[t.x-11,t.y-11,22,22]);let b=t.label==="+"?Z`M${t.x-6.5},${t.y}h13M${t.x},${t.y-6.5}v13`:/^[*x×]$/.test(t.label)?Z`M${t.x-4.6},${t.y-4.6}l9.2,9.2M${t.x+4.6},${t.y-4.6}l-9.2,9.2`:t.label==="~"?Z`M${t.x-7},${t.y}q3.5,-8 7,0t7,0`:"";m+=b?`<path d="${b}" fill="none" stroke="${r}" stroke-width="1.5" stroke-linecap="round"/>`:ot(t.label,t.x,t.y,w,500,a.text,"middle",z);break}case"circle":m=E(Me(t.x,t.y,t.w/2),[l,d,t.w,t.h])+Y();break;case"tensor":{let[b,k,G]=dt(t.attrs.size,[36,64,20]),N=G*0.6,T=l,L=d+N,[Q,j]=_(t.color),M=s==="ice"?a.ice[0]:a.dark?ut(Q,j,0.3):ut(Q,"#fff",0.5),A=s==="ice"?a.ice[2]:a.dark?ut(Q,"#000",0.25):ut(Q,j,0.35);m=E(It(T,L,b,k,1.5),[T,L,b,k])+`<path d="${Vt(T,L,T+N,L-N,T+b+N,L-N,T+b,L)}" fill="${M}" stroke="${r}" stroke-width="1" stroke-linejoin="round"/><path d="${Vt(T+b,L,T+b+N,L-N,T+b+N,L+k-N,T+b,L+k)}" fill="${A}" stroke="${r}" stroke-width="1" stroke-linejoin="round"/>`;break}case"stack":{let[b,k]=dt(t.attrs.size,[44,60]),G=+t.attrs.n||4,N=+t.attrs.offset||7;for(let T=0;T<G;T++){let L=It(l+T*N,d+T*N,b,k,2);m+=T===G-1?E(L,[l+T*N,d+T*N,b,k]):`<path d="${L}" ${p}/>`}V(l,d,t.w,t.h);break}case"enc":case"dec":{let b=t.shape==="enc",G=h?b!==_t()?Vt(l,d,l+t.w,d+t.h*0.22,l+t.w,d+t.h*0.78,l,d+t.h):Vt(l,d+t.h*0.22,l+t.w,d,l+t.w,d+t.h,l,d+t.h*0.78):b!==Ft()?Vt(l,d,l+t.w,d,l+t.w*0.78,d+t.h,l+t.w*0.22,d+t.h):Vt(l+t.w*0.22,d,l+t.w*0.78,d,l+t.w,d+t.h,l,d+t.h);m=E(G)+Y();break}case"neurons":{let b=+t.attrs.n||4;for(let k=0;k<b;k++){let G=-((b-1)/2)*20+k*20;{let N=h?t.x:t.x+G,T=h?t.y+G:t.y;m+=E(Me(N,T,7),[N-7,T-7,14,14])}}break}case"matrix":case"voxels":{let b=+t.attrs.cellSize||16,k=[];if(t.shape==="matrix")JSON.parse(t.attrs.grid).forEach((Q,j)=>Q.forEach((M,A)=>k.push([[[l+A*b,d+j*b],[l+(A+1)*b,d+j*b],[l+(A+1)*b,d+(j+1)*b],[l+A*b,d+(j+1)*b]],M,0])));else{let Q=JSON.parse(t.attrs.faces),j=+t.attrs.cols,M=b*0.5,A=l,K=d+ +t.attrs.depth*M,et=(F,D)=>[A+F*b+D*M,K-D*M],B=(F,D)=>[A+j*b+D*M,K+F*b-D*M];Q.top.forEach((F,D)=>F.forEach((at,nt)=>k.push([[et(nt,D),et(nt+1,D),et(nt+1,D+1),et(nt,D+1)],at,0.3]))),Q.side.forEach((F,D)=>F.forEach((at,nt)=>k.push([[B(D,nt),B(D,nt+1),B(D+1,nt+1),B(D+1,nt)],at,-0.25]))),Q.front.forEach((F,D)=>F.forEach((at,nt)=>k.push([[[A+nt*b,K+D*b],[A+(nt+1)*b,K+D*b],[A+(nt+1)*b,K+(D+1)*b],[A+nt*b,K+(D+1)*b]],at,0])))}let[G,N]=_(t.color),T=a.dark?"#0e1117":"#ffffff";for(let[Q,j,M]of k){let A=j.c?a.c[j.c]?ut(a.c[j.c][0],a.c[j.c][1],0.55):Et(j.c):ut(a.dark?N:ut(G,"#ffffff",0.4),a.dark?G:N,a.dark?1-(j.v??0.5):j.v??0.5);A=M>0?ut(A,"#ffffff",M):M<0?ut(A,"#0b1a2e",-M):A;let K="M"+Q.map((F)=>F.map(xt).join(",")).join("L")+"z";if(m+=`<path d="${K}" fill="${A}" stroke="${T}" stroke-width="1.5" stroke-linejoin="round"/>`,j.t)m+=Gt(j.t,K,j.c??t.color);let[et,B]=Q.reduce((F,D)=>[F[0]+D[0]/4,F[1]+D[1]/4],[0,0]);if(j.l)m+=ot(j.l,et,B,Math.min(x-2,b*0.6),500,a.text);if(j.x)m+=jt(et-b*0.28,B-b*0.28,et+b*0.28,B+b*0.28,1.4)}let L=JSON.parse(t.attrs.axes??"{}");if(t.attrs.axes){let Q=+t.attrs.rows,j=+t.attrs.cols,M=t.shape==="voxels"?+t.attrs.depth*b*0.5:0,A=l,K=d+M,et=j*b,B=Q*b,F=x-1,D=o.edges.some((gt)=>gt.from===t.id||gt.to===t.id),at=K+B/2,nt=A+et/2;if(D&&h&&Math.abs(at-t.y)<F*0.85)at=t.y+F*0.85;let J=L.cols?bt(L.cols,F,500,g).w:0;if(D&&!h&&Math.abs(nt-t.x)<J/2+6)nt=t.x+J/2+6;let rt="";if(L.cols)rt+=ot(L.cols,nt,K+B+F*0.9,F,500,a.sub);if(L.rows)rt+=ot(L.rows,A-5,at,F,500,a.sub,"end");if(L.depth&&M)rt+=ot(L.depth,A+et+M/2+5,K+B-M/2+F*0.5,F,500,a.sub,"start");m+=`<g paint-order="stroke" stroke="${a.halo}" stroke-width="3" stroke-linejoin="round">${rt}</g>`}V(l,d,t.w,t.h);break}case"vector":{let b=je(t);m=E(It(l,d,t.w,t.h,3));let k=(h?t.h-4:t.w-4)/b.length;b.forEach((G,N)=>{let T=-((b.length-1)/2)*k+N*k;if(N)m+=h?Z`<path d="M${l+3},${t.y+T-k/2}h${t.w-6}" stroke="${r}" stroke-opacity=".5"/>`:Z`<path d="M${t.x+T-k/2},${d+3}v${t.h-6}" stroke="${r}" stroke-opacity=".5"/>`;m+=ot(G.trim(),h?t.x:t.x+T,h?t.y+T:t.y,w,500,a.text,"middle",z)});break}case"image":{let{W:b,H:k,k:G,x0:N,y0:T,fit:L,srcs:Q}=Dt(t),j=+t.attrs.grid||0;for(let B=0;B<G;B++){let F=N+B*6,D=T+B*6,at=It(F,D,b,k,3),nt=Q.length>1?Q[G-1-B]:B===G-1?Q[0]:void 0;if(!nt){m+=`<path d="${at}" fill="${a.dark?"#1b2029":"#fff"}" stroke="${r}"/>`;continue}let J=`${u}k${ct++}`;if(m+=`<clipPath id="${J}"><path d="${at}"/></clipPath>`+Z`<image href="${Et(nt)}" x="${F}" y="${D}" width="${b}" height="${k}" preserveAspectRatio="${L}" clip-path="url(#${J})"/>`,j>1&&B===G-1){let rt="";for(let gt=1;gt<j;gt++)rt+=Z`M${F+b*gt/j},${D}v${k}M${F},${D+k*gt/j}h${b}`;m+=`<path d="${rt}" stroke="#fff" stroke-opacity=".9"/>`}m+=`<path d="${at}" fill="none" stroke="${r}"/>`}let[M,A,K,et]=ke(t.attrs);if(M!==1||A!==0||K!==0||et!==1)m=Z`<g transform="matrix(${[M,A,K,et].map((B)=>B.toFixed(4)).join(" ")} ${t.x-M*t.x-K*t.y} ${t.y-A*t.x-et*t.y})">`+m+"</g>";V(l,d,t.w,t.h);break}case"magnify":{let b=Lt(t),k=Math.min(t.w,t.h)/2,G=t.attrs.lens!=="square",N=G?Me(t.x,t.y,k):It(l,d,t.w,t.h,4),T=`${u}k${ct++}`;if(m+=`<clipPath id="${T}"><path d="${N}"/></clipPath>`,t.attrs.src)m+=Z`<image href="${Et(t.attrs.src)}" x="${l}" y="${d}" width="${t.w}" height="${t.h}" preserveAspectRatio="${t.attrs.fit==="contain"?"xMidYMid meet":"xMidYMid slice"}" clip-path="url(#${T})"/>`;else if(b){let L=Dt(b),[Q,j,M,A]=zt(t,b);m+=Z`<g clip-path="url(#${T})"><svg x="${l}" y="${d}" width="${t.w}" height="${t.h}" viewBox="${Q*L.W} ${j*L.H} ${M*L.W} ${A*L.H}" preserveAspectRatio="xMidYMid slice">`+Z`<image href="${Et(L.srcs[0])}" width="${L.W}" height="${L.H}" preserveAspectRatio="${L.fit}"/></svg></g>`}else m+=`<path d="${N}" fill="${e}"/>`;if(m+=`<path d="${N}" fill="none" stroke="${r}" stroke-width="2.5"/>`,G){let L=k*Math.SQRT1_2;m+=Z`<path d="M${t.x+L+1},${t.y+L+1}l${k*0.5},${k*0.5}" stroke="${r}" stroke-width="5" stroke-linecap="round"/>`,V(t.x+L,t.y+L,k*0.6,k*0.6)}V(l,d,t.w,t.h);break}case"gaussian":{let b=JSON.parse(t.attrs.peaks??"[[0,0,1,0.45]]"),k=Math.min(40,+t.attrs.mesh||16),[G,N]=_(t.color),T=(B,F)=>b.reduce((D,[at,nt,J=1,rt=0.45])=>D+J*Math.exp(-((B-at)**2+(F-nt)**2)/(2*rt*rt)),0),L=Array.from({length:k+1},(B,F)=>Array.from({length:k+1},(D,at)=>T(-1+2*F/k,-1+2*at/k))),Q=Math.max(...L.flat())||1,j=L.map((B,F)=>B.map((D,at)=>mt(-1+2*F/k,-1+2*at/k,D/Q))),M=[[-1,-1],[1,-1],[1,1],[-1,1]].map(([B,F])=>mt(B,F,0)),A=Fe([...j.flat(),...M],l,d,t.w,t.h,2),K=(B)=>A(B).map(xt).join(",");m+=`<path d="M${M.map(K).join("L")}z" fill="${G}" fill-opacity=".5" stroke="${N}" stroke-opacity=".4"/>`;let et=[];for(let B=0;B<k;B++)for(let F=0;F<k;F++){let D=[j[B][F],j[B+1][F],j[B+1][F+1],j[B][F+1]];et.push([D.reduce((at,nt)=>at+nt[2],0),`M${D.map(K).join("L")}z`,(L[B][F]+L[B+1][F+1])/2/Q])}et.sort((B,F)=>F[0]-B[0]);for(let[,B,F]of et){let D=ut(G,N,0.12+0.88*F);m+=`<path d="${B}" fill="${D}" stroke="${ut(D,a.dark?"#ffffff":N,0.35)}" stroke-width=".5"/>`}V(l,d,t.w,t.h);break}case"space":{let[b,k]=_(t.color),G=JSON.parse(t.attrs.points??"[]"),N=Object.values({x:"x",y:"y",z:"z",...JSON.parse(t.attrs.axes??"{}")}),T=mt(1,0,0)[2]>mt(-1,0,0)[2]?1:-1,L=mt(0,1,0)[2]>mt(0,-1,0)[2]?1:-1,Q=[-1,1].flatMap((J)=>[-1,1].flatMap((rt)=>[-1,1].map((gt)=>mt(J,rt,gt)))),j=Fe(Q,l,d,t.w,t.h,10),M=(J,rt,gt)=>j(mt(J,rt,gt)).map(xt).join(","),A=(J)=>`<path d="M${J.map((rt)=>M(rt[0],rt[1],rt[2])).join("L")}z" fill="${b}" fill-opacity=".55" stroke="${k}" stroke-opacity=".45" stroke-linejoin="round"/>`;m+=A([[-1,-1,-1],[1,-1,-1],[1,1,-1],[-1,1,-1]])+A([[T,-1,-1],[T,1,-1],[T,1,1],[T,-1,1]])+A([[-1,L,-1],[1,L,-1],[1,L,1],[-1,L,1]]);let K="";for(let J of[-0.5,0,0.5])K+=`M${M(J,-1,-1)}L${M(J,1,-1)}M${M(-1,J,-1)}L${M(1,J,-1)}M${M(T,-1,J)}L${M(T,1,J)}M${M(-1,L,J)}L${M(1,L,J)}`;m+=`<path d="${K}" stroke="${k}" stroke-opacity=".18"/>`;let et=G.map((J)=>[...J,mt(J[0],J[1],J[2])[2]]).sort((J,rt)=>rt[rt.length-1]-J[J.length-1]);for(let J of et){let[rt,gt]=j(mt(J[0],J[1],J[2])),Ne=a.c[Be(J.length>4?J[3]:0)][1];m+=Z`<circle cx="${rt}" cy="${gt}" r="3.2" fill="${Ne}" stroke="${a.dark?"#0e1117":"#fff"}" stroke-width=".9"/>`}let B=(J,rt,gt,Ne)=>{let[Qo,tr]=j(mt(rt,gt,Ne));return ot(J,Qo,tr,x-2,400,a.sub)},[F,D]=[[T,-L],[-T,L]].sort((J,rt)=>j(mt(J[0],J[1],0))[0]-j(mt(rt[0],rt[1],0))[0])[0],[at,nt]=j(mt(F,D,0));m+=B(N[0],0,-L*1.22,-1)+B(N[1],-T*1.22,0,-1)+ot(N[2],at-8,nt,x-2,400,a.sub,"end"),V(l,d,t.w,t.h);break}case"pointcloud":{let[b,k]=_(t.color),N=(t.attrs.points?JSON.parse(t.attrs.points):$r(t.attrs.shape??"torus",+t.attrs.n||240)).map((M)=>[...mt(M[0],M[1],M[2]),M[3]]),T=Fe(N,l,d,t.w,t.h,4),L=N.map((M)=>M[2]),Q=Math.min(...L),j=Math.max(...L);N.sort((M,A)=>A[2]-M[2]);for(let M of N){let A=(j-M[2])/(j-Q||1),[K,et]=T(M),B=M[3]!==void 0?a.c[Be(M[3])][1]:ut(b,k,0.3+0.7*A);m+=Z`<circle cx="${K}" cy="${et}" r="${1+1.4*A}" fill="${B}" fill-opacity="${0.5+0.5*A}"/>`}V(l,d,t.w,t.h);break}case"waveform":{m=E(It(l,d,t.w,t.h,6));let b=t.attrs.wave?JSON.parse(t.attrs.wave):wr(72),k=6,G=t.y,N=t.h/2-k,T=(t.w-2*k)/b.length,L="";b.forEach(([Q,j],M)=>{let A=l+k+T*(M+0.5);L+=Z`M${A},${G-Math.max(j,0.02)*N}V${G-Math.min(Q,-0.02)*N}`}),m+=`<path d="${L}" stroke="${r}" stroke-width="${xt(Math.max(0.8,T*0.6))}" stroke-linecap="round"/>`;break}case"sphere":{let[b,k]=_(t.color),G=Math.min(t.w,t.h)/2-2,N="sph-"+I(t.color);if(!v.has(N))v.set(N,`<radialGradient id="${u}${N}" cx=".35" cy=".3" r=".8"><stop offset="0" stop-color="${a.dark?ut(b,k,0.35):"#ffffff"}"/><stop offset=".55" stop-color="${b}"/><stop offset="1" stop-color="${ut(b,k,0.35)}"/></radialGradient>`);m+=Z`<circle cx="${t.x}" cy="${t.y}" r="${G}" fill="url(#${u}${N})" stroke="${k}"/>`;let T="",L="",Q=(M)=>{let A;for(let K=0;K<=64;K++){let[et,B,F]=mt(...M(K/64*Math.PI*2)),D=F<=0,at=(D===A?"L":"M")+xt(t.x+et*G)+","+xt(t.y+B*G);if(D)T+=at;else L+=at;A=D}};for(let M of[-0.5,0,0.5]){let A=Math.sqrt(1-M*M);Q((K)=>[A*Math.cos(K),A*Math.sin(K),M])}for(let M of[0,Math.PI/3,2*Math.PI/3])Q((A)=>[Math.cos(A)*Math.cos(M),Math.cos(A)*Math.sin(M),Math.sin(A)]);m+=`<path d="${L}" fill="none" stroke="${k}" stroke-opacity=".22" stroke-dasharray="2 2"/><path d="${T}" fill="none" stroke="${k}" stroke-opacity=".55"/>`;let j=JSON.parse(t.attrs.points??"[]");for(let M of j){let A=Math.hypot(M[0],M[1],M[2])||1,[K,et,B]=mt(M[0]/A,M[1]/A,M[2]/A),F=a.c[Be(M[3]??0)][1];m+=Z`<circle cx="${t.x+K*G}" cy="${t.y+et*G}" r="3" fill="${F}" stroke="${a.dark?"#0e1117":"#fff"}" stroke-width=".8" opacity="${B<=0?1:0.3}"/>`}V(l,d,t.w,t.h);break}default:{let b=t.shape==="thin"?5:7;m=E(It(l,d,t.w,t.h,b),void 0,b)+Y()}}if($e.has(t.shape)&&t.label)m+=ot(t.label,t.x,d-f*5-11,w,500,a.sub,"middle",z);if(t.attrs.caption)m+=ot(t.attrs.caption,t.x,t.y+t.h/2+12,x-1,400,a.sub);let st=H(t,s);if(st){let b=t.shape==="op"||t.shape==="circle";m+=b?it(st,t.x+t.w*0.36,t.y-t.h*0.36):it(st,l+t.w-2,d+2)}if(t.flags.has("crossed")){let b=t.shape==="op"||t.shape==="circle"?t.w*0.15:3;m=`<g opacity=".5">${m}</g>`+jt(l+b,d+b,l+t.w-b,d+t.h-b,2.2)}return`<g>${m}</g>`},jt=(t,l,d,e,r)=>{let s=Z`M${t},${l}L${d},${e}M${d},${l}L${t},${e}`,c=a.c.red[1];return`<path d="${s}" stroke="${a.halo}" stroke-width="${r+2.5}" stroke-linecap="round"/><path d="${s}" stroke="${c}" stroke-width="${r}" stroke-linecap="round"/>`},_t=()=>o.dir==="RL",Ft=()=>o.dir==="BT",Bt=(t)=>{if(he(t))return"";let l=t.attrs.color,d=!l?a.edge:a.c[l]||Oe(l)?_(l)[1]:Et(l);if(t.fc)return Qt(t,d);let e=t.pts.map((m)=>[...m]);if(e.length<2)return"";let r=t.op==="=>"||t.flags.has("thick"),s=t.op.startsWith("..")||t.flags.has("dashed"),c=t.op==="--"||t.op===".."?0:t.op==="<->"?2:1,y=r?9:7.5,p=r?4.2:3.4,f="",S=(m,w)=>{let z=m[0]-w[0],P=m[1]-w[1],C=Math.hypot(z,P)||1,Y=z/C,st=P/C,b=m[0]-Y*y,k=m[1]-st*y;return f+=`<path d="${Vt(m[0],m[1],b-st*p,k+Y*p,m[0]-Y*y*0.72,m[1]-st*y*0.72,b+st*p,k-Y*p)}" fill="${d}"/>`,[m[0]-Y*y*0.7,m[1]-st*y*0.7]};for(let m of e)V(m[0]-2,m[1]-2,4,4);let E;if(t.curve){let[m,w,z,P]=e,C=t.curve,Y=[m[0]+(w[0]-m[0])*C,m[1]+(w[1]-m[1])*C],st=[P[0]+(z[0]-P[0])*C,P[1]+(z[1]-P[1])*C],b=c?S(P,st):P,k=c===2?S(m,Y):m;E=Z`M${k[0]},${k[1]}C${Y[0]},${Y[1]} ${st[0]},${st[1]} ${b[0]},${b[1]}`}else{if(c)e[e.length-1]=S(e[e.length-1],e[e.length-2]);if(c===2)e[0]=S(e[0],e[1]);E=Z`M${e[0][0]},${e[0][1]}`;for(let m=1;m<e.length-1;m++){let[w,z,P]=[e[m-1],e[m],e[m+1]],C=Math.hypot(z[0]-w[0],z[1]-w[1]),Y=Math.hypot(P[0]-z[0],P[1]-z[1]),st=Math.min(8,C/2,Y/2);E+=Z`L${z[0]+(w[0]-z[0])*st/(C||1)},${z[1]+(w[1]-z[1])*st/(C||1)}Q${z[0]},${z[1]} ${z[0]+(P[0]-z[0])*st/(Y||1)},${z[1]+(P[1]-z[1])*st/(Y||1)}`}E+=Z`L${e[e.length-1][0]},${e[e.length-1][1]}`}if(t.flags.has("crossed")){let m=0,w=-1;for(let C=0;C+1<t.pts.length;C++){let Y=Math.hypot(t.pts[C+1][0]-t.pts[C][0],t.pts[C+1][1]-t.pts[C][1]);if(Y>w)w=Y,m=C}let z=(t.pts[m][0]+t.pts[m+1][0])/2,P=(t.pts[m][1]+t.pts[m+1][1])/2;f+=jt(z-5,P-5,z+5,P+5,2)}return Z`<path d="${E}" fill="none" stroke="${d}" stroke-width="${r?2.2:1.4}" stroke-linecap="round" stroke-linejoin="round"${s?' stroke-dasharray="5 4"':""}/>`+f},Ht=(t)=>{let l=+t.attrs.n||4;return Array.from({length:l},(d,e)=>{let r=-((l-1)/2)*20+e*20;return h?[t.x,t.y+r]:[t.x+r,t.y]})},Qt=(t,l)=>{let d="";for(let[e,r]of Ht(te(t.from)))for(let[s,c]of Ht(te(t.to))){let y=Math.hypot(s-e,c-r),p=(s-e)/y,f=(c-r)/y;d+=Z`M${e+p*7},${r+f*7}L${s-p*7},${c-f*7}`}return`<path d="${d}" stroke="${l}" stroke-opacity=".45" stroke-width=".8"/>`},te=(t)=>o.nodes.get(t),ee=(t)=>{if(!t.w)return"";let{fill:l,stroke:d,fx:e}=t.attrs.texture||t.flags.has("frozen")||t.flags.has("train")?wt(t):{fill:t.attrs.color?_(t.attrs.color)[0]:a.group[0],stroke:t.attrs.color?_(t.attrs.color)[1]:a.group[1],fx:""},r=t.flags.has("dashed")?' stroke-dasharray="5 4"':"",s=It(t.x,t.y,t.w,t.h,12),c=`<path d="${s}" fill="${l}" stroke="${d}" stroke-width="1.2"${r}/>`,y=kt(e,s,t.x,t.y,t.w,t.h,t.attrs.color);if(y)c+=y+`<path d="${s}" fill="none" stroke="${d}" stroke-width="1.2" stroke-opacity=".7"/>`;else if(e==="ice")c+=`<path d="${s}" fill="url(#${u}frost)"/>`+Xt(t.x,t.y,t.w,12);if(V(t.x,t.y,t.w,t.h),t.label)c+=ot(t.label,t.x+12,t.y+13,x-2,600,a.sub,"start");if(t.attrs.repeat){let[p,f,S]=qt(t);c+=ot(t.attrs.repeat,p,f,x+2,500,a.text,S)}return c+=it(H(t,e),t.x+t.w-2,t.y+2),c},$t=[...o.nodes.values()],oe=$t.reduce((t,l)=>t+l.x,0)/($t.length||1),de=[],re=(t)=>{let l=new Set;for(let e=t;e;e=o.groups.find((r)=>r.id===e.parent))l.add(e.id);let d=$t.map((e)=>[e.x-e.w/2,e.y-e.h/2,e.x+e.w/2,e.y+e.h/2+(e.attrs.caption?20:0)]);for(let e of o.edges){for(let r=0;r+1<e.pts.length;r++){let[s,c]=[e.pts[r],e.pts[r+1]];d.push([Math.min(s[0],c[0])-2,Math.min(s[1],c[1])-2,Math.max(s[0],c[0])+2,Math.max(s[1],c[1])+2])}if(e.label&&e.lx!==void 0){let r=bt(e.label,x-1,Ct,g);d.push([e.lx-r.w/2,e.ly-r.h/2,e.lx+r.w/2,e.ly+r.h/2])}}for(let e of o.groups)if(e.w&&!l.has(e.id)&&e.parent!==t.id)d.push([e.x,e.y,e.x+e.w,e.y+e.h]);return[...d,...de]},qt=(t)=>{let l=bt(t.attrs.repeat,x+2,500,g),e={left:(f=0.5)=>[t.x-10,t.y+t.h*f,"end"],right:(f=0.5)=>[t.x+t.w+10,t.y+t.h*f,"start"],top:()=>[t.x+t.w/2,t.y-10-l.h/2,"middle"],bottom:()=>[t.x+t.w/2,t.y+t.h+10+l.h/2,"middle"]},r=([f,S,E])=>[E==="end"?f-l.w:E==="middle"?f-l.w/2:f,S-l.h/2,E==="start"?f+l.w:E==="middle"?f+l.w/2:f,S+l.h/2],s=t.x+t.w/2>oe?"right":"left",c=s==="right"?"left":"right",y=t.attrs["repeat-side"],p=y&&y!=="auto"?e[y]():void 0;if(!p){let f=[e[s](),e.bottom(),e[c](),e.top(),...[0.25,0.75].flatMap((E)=>[e[s](E),e[c](E)])],S=re(t);p=f.find((E)=>!S.some((m)=>ve(r(E),m)))??f[0]}return de.push(r(p)),p},se=$t.filter((t)=>t.shape==="magnify").map((t)=>{let l=Lt(t);if(!l)return"";let d=Dt(l),[e,r,s,c]=zt(t,l),[y,p,f,S]=ke(l.attrs),E=d.x0+(d.k-1)*6,m=d.y0+(d.k-1)*6,w=(M,A)=>{let K=E+M*d.W-l.x,et=m+A*d.H-l.y;return[l.x+y*K+f*et,l.y+p*K+S*et]},z=[w(e,r),w(e+s,r),w(e+s,r+c),w(e,r+c)],[P,C]=z.reduce((M,A)=>[M[0]+A[0]/4,M[1]+A[1]/4],[0,0]),Y=Math.hypot(t.x-P,t.y-C)||1,st=-(t.y-C)/Y,b=(t.x-P)/Y,k=(M)=>M[0]*st+M[1]*b,G=[...z].sort((M,A)=>k(M)-k(A)),N=Math.min(t.w,t.h)/2,T=t.attrs.lens==="square"?[[t.x-t.w/2,t.y-t.h/2],[t.x+t.w/2,t.y-t.h/2],[t.x+t.w/2,t.y+t.h/2],[t.x-t.w/2,t.y+t.h/2]].sort((M,A)=>k(M)-k(A)):[[t.x-st*N,t.y-b*N],[t.x+st*N,t.y+b*N]],[L,Q]=[T[0],T[T.length-1]],j=_(t.color)[1];return`<path d="M${z.map((M)=>M.map(xt).join(",")).join("L")}z" fill="none" stroke="${j}" stroke-width="2" stroke-linejoin="round"/><path d="M${G[0].map(xt)}L${L.map(xt)}M${G[3].map(xt)}L${Q.map(xt)}" stroke="${j}" stroke-opacity=".75" stroke-dasharray="3 3"/>`}).join(""),vt=["frozen","trainable","glass","metal","noise","quantized","sparse","hatch","crossed"],ne=({position:t,labels:l={}})=>{let d=new Map,e=(w,z)=>{if(!d.has(w))d.set(w,z)};for(let w of[...$t,...o.groups]){let z=w.attrs.texture??(w.flags.has("frozen")?"ice":w.flags.has("train")?"magma":""),P=H(w,z);if(P)e(P==="ice"?"frozen":"trainable");if(z&&z!=="ice"&&z!=="magma")e(z,w.color??w.attrs.color);if(w.flags.has("crossed"))e("crossed")}if(o.edges.some((w)=>w.flags.has("crossed")))e("crossed");let r=vt.filter((w)=>d.has(w)).map((w)=>[w,l[w]??(w==="crossed"?"Removed":w[0].toUpperCase()+w.slice(1))]);if(!r.length)return"";let s=30,c=18,y=x-1,p=t==="right",f=r.map(([,w])=>bt(w,y,Ct,g).w),S=p?U+28:(q+U)/2-f.reduce((w,z)=>w+s+12+z+20,-20)/2,E=p?O+c/2+6:X+22+c/2,m="";return r.forEach(([w,z],P)=>{let C=w==="frozen"?"frozen":w==="trainable"?"train":w==="crossed"?"crossed":"";if(m+=Kt({id:"",shape:"block",label:"",color:d.get(w),flags:new Set(C?[C]:[]),attrs:C?{}:{texture:w},groups:[],order:0,x:S+s/2,y:E,w:s,h:c})+ot(z,S+s+12,E,y,Ct,a.text,"start"),p)E+=c+14;else S+=s+12+f[P]+20}),m},me=o.groups.map(ee).join(""),ge=o.edges.map(Bt).join(""),be=[...o.nodes.values()].map(Kt).join(""),St=o.edges.filter((t)=>t.label&&t.lx!==void 0).map((t)=>ot(t.label,t.lx,t.ly,x-1,400,a.sub)).join(""),xe=o.legend?ne(o.legend):"";if(q===1/0)q=O=U=X=0;let Ot=14,Nt=U-q+Ot*2,Wt=X-O+Ot*2,At=a.bg!=="none"?Z`<rect x="${q-Ot}" y="${O-Ot}" width="${Nt}" height="${Wt}" rx="10" fill="${a.bg}"/>`:"";return Z`<svg xmlns="http://www.w3.org/2000/svg" viewBox="${q-Ot} ${O-Ot} ${Nt} ${Wt}" width="${Nt}" height="${Wt}" font-family="${Et(g)}" style="max-width:100%;height:auto" role="img">`+`<defs>${[...v.values()].join("")}</defs>${At}<g>${me}</g><g>${ge}</g><g>${be}</g><g>${se}</g><g paint-order="stroke" stroke="${a.halo}" stroke-width="4" stroke-linejoin="round">${St}</g>${xe}</svg>`}class Re{nodes=new Map;edges=[];groups=new Map;scope=[];ranks=[];ids=new Set;claim(o,n,i){if(o===void 0)while(this.ids.has(o=`${n}${i}`))i++;else if(this.ids.has(o))throw Error(`duplicate id "${o}"`);return this.ids.add(o),o}node(o){let n=this.nodes.get(o);if(!n){let i=this.claim(o.options.id,"n",this.nodes.size),a=this.scope.map((u)=>this.groups.get(u)?.id).filter((u)=>!!u);n={id:i,mods:[...this.scope],groups:a},this.nodes.set(o,n)}return n.id}apply(o,n){let i=this.node(o);for(let a of n)this.edges.push({from:a.node,to:o,style:a.style});return new Mt(this,i)}enter(o){let n=o.options;if((n.box??!!(n.label||n.repeat))&&!this.groups.has(o)){let i=[...this.scope].reverse().map((a)=>this.groups.get(a)).find(Boolean);this.groups.set(o,{id:this.claim(n.id,"g",this.groups.size),label:n.label??"",parent:i?.id,flags:new Set(n.dashed?["dashed"]:[]),attrs:{...n.repeat&&{repeat:n.repeat},...n.repeatSide&&{"repeat-side":n.repeatSide},...n.color&&{color:n.color},...n.texture&&{texture:n.texture}},x:0,y:0,w:0,h:0})}this.scope.push(o)}id(o){if(o instanceof Mt)return o.node;let n=this.nodes.get(o);if(!n)throw Error(`"${o.label}" is not part of this figure`);return n.id}}class Mt{graph;node;style;constructor(o,n,i={}){this.graph=o;this.node=n;this.style=i}edge(o){return new Mt(this.graph,this.node,{...this.style,...typeof o==="string"?{label:o}:o})}}var po=(o,n)=>{let i=o[0]?.graph;if(!i)throw Error(`${n} was called without inputs; start from fig.input()`);if(o.some((a)=>!(a instanceof Mt)||a.graph!==i))throw Error(`${n}: inputs must be Tensors of the same figure`);return i};function Rt(o,n,i,a=o,u={}){let h=(...x)=>po(x,`"${h.label}"`).apply(h,x);return Object.assign(h,{shape:o,kind:a,label:n,options:i,defaults:u,freeze(){return h.options.frozen=!0,h.options.trainable=!1,h},trainable(){return h.options.trainable=!0,h.options.frozen=!1,h}})}function tt(o,n="",i={}){let{kind:a=o,...u}=typeof i==="string"?{color:i}:i;return(h,x)=>{let[g,v]=typeof h==="string"?[h,x??{}]:[n,h??{}];return Rt(o,g,{...v},a,u)}}var ho=tt("block"),mo=tt("thin"),Ve=tt("text"),go=tt("circle"),Le=tt("op","+"),bo=tt("enc","Encoder","blue"),xo=tt("dec","Decoder","green"),yo=tt("tensor","","blue"),vo=tt("stack","","blue"),$o=tt("neurons");function wo(o,n,i){let[a,u]=typeof n==="string"?[n,i??{}]:["",n??{}];return Rt("vector",a,{...u,values:o})}var ko=tt("block","Multi-Head Attention",{color:"orange",kind:"mha"}),Mo=tt("block","Attention",{color:"orange",kind:"attention"}),So=tt("block","Feed Forward",{color:"blue",kind:"ffn"}),Oo=tt("block","MLP",{color:"blue",kind:"mlp"}),Eo=tt("thin","Add & Norm",{color:"yellow",kind:"norm"}),Ro=tt("block","Embedding",{color:"pink",kind:"embed"}),Lo=tt("thin","Linear",{color:"purple",kind:"linear"}),zo=tt("thin","Softmax",{color:"green",kind:"softmax"}),No=tt("thin","MatMul",{color:"purple",kind:"matmul"}),Ao=tt("thin","Concat",{color:"yellow",kind:"concat"}),Co=tt("block","Conv",{color:"teal",kind:"conv"}),To=tt("thin","Pool",{color:"gray",kind:"pool"}),Io=tt("thin","ReLU",{color:"green",kind:"act"}),Po=tt("thin","Dropout",{color:"gray",kind:"dropout"}),jo=tt("block","Loss",{color:"red",kind:"loss"}),Fo=tt("gaussian","","purple"),Bo=tt("space","","gray"),qo=tt("sphere","","blue"),Uo=tt("pointcloud","","blue");function Vo(o,n){let[i,{samples:a,...u}]=typeof o==="string"?[o,n??{}]:["",o??{}],h;if(a?.length){let x=Math.min(256,a.length),g=(v)=>Math.round(v*1000)/1000;h=Array.from({length:x},(v,R)=>{let I=1/0,q=-1/0;for(let O=Math.floor(R*a.length/x);O<Math.floor((R+1)*a.length/x);O++)I=Math.min(I,a[O]),q=Math.max(q,a[O]);return[g(I),g(q)]})}return Rt("waveform",i,{...u,...h&&{wave:h}},"waveform",{color:"teal"})}var qe=(o)=>typeof o==="string"?o:("toDataURL"in o)?o.toDataURL("image/png"):o.currentSrc||o.src||"";function Go(o,n,i){let[a,u]=typeof n==="string"?[n,i??{}]:["",n??{}],h=Array.isArray(o)?o.map(qe):[qe(o)];return Rt("image",a,{n:h.length>1?h.length:void 0,...u,src:h[0],...h.length>1&&{srcs:h}})}function Do(o,n){let[i,{src:a,...u}]=typeof o==="string"?[o,n??{}]:["",o??{}];return Rt("magnify",i,{...u,...a&&{src:qe(a)}},"magnify",{color:"orange"})}var _o=(o,n)=>o===void 0?{v:Math.abs(Math.sin(n*12.9898+4.1)*43758.5453)%1}:typeof o==="number"?{v:o}:typeof o==="string"?{c:o}:{v:o.value,c:o.color,t:o.texture,l:o.label,x:o.crossed};function Ho(o,n){let[i,{cells:a,...u}]=typeof o==="string"?[o,n??{}]:["",o??{}],h=u.rows??(Array.isArray(a)?a.length:5),x=u.cols??(Array.isArray(a)?Math.max(0,...a.map((R)=>R.length)):5),g=(R,I)=>typeof a==="function"?a(R,I):a?.[R]?.[I],v=Array.from({length:h},(R,I)=>Array.from({length:x},(q,O)=>_o(g(I,O),I*31+O)));return Rt("matrix",i,{...u,rows:h,cols:x,grid:v},"matrix",{color:"blue"})}function Wo(o,n){let[i,{cells:a,...u}]=typeof o==="string"?[o,n??{}]:["",o??{}],h=Array.isArray(a)?a:void 0,x=u.rows??h?.length??5,g=u.cols??(h?Math.max(0,...h.map((O)=>O.length)):5),v=u.depth??(h?Math.max(0,...h.flatMap((O)=>O.map((U)=>U.length))):5),R=(O,U,X)=>_o(typeof a==="function"?a(O,U,X):a?.[O]?.[U]?.[X],O*97+U*31+X*7),I=(O,U,X)=>Array.from({length:O},(V,_)=>Array.from({length:U},(ft,W)=>X(_,W))),q={front:I(x,g,(O,U)=>R(O,U,0)),top:I(v,g,(O,U)=>R(0,U,O)),side:I(x,v,(O,U)=>R(O,g-1,U))};return Rt("voxels",i,{...u,rows:x,cols:g,depth:v,faces:q},"voxels",{color:"blue"})}var Jo=(...o)=>Le("+")(...o),Zo=(...o)=>Le("*")(...o);class Ge{constructor(o={}){let n=(...i)=>{let a=po(i,n.constructor.name||"module");a.enter(n);try{return n.forward(...i)}finally{a.scope.pop()}};return delete n.name,delete n.length,Object.setPrototypeOf(n,new.target.prototype),n.options={...o},n}freeze(){return this.options.frozen=!0,this.options.trainable=!1,this}trainable(){return this.options.trainable=!0,this.options.frozen=!1,this}}var pe={};function Yo(o){if(o===null)Te(void 0);else if("tex"in o)Te(o.tex);let{tex:n,...i}=o??{};return pe=o===null?{}:Ue(pe,i),structuredClone(pe)}var Ue=(o,n)=>{let i={...o.kinds};for(let[a,u]of Object.entries(n.kinds??{}))i[a]={...i[a],...u};return{...o,...n,style:{...o.style,...n.style},kinds:i}},Mr=(...o)=>{let n={};for(let i of o)for(let[a,u]of Object.entries(i??{}))if(u!==void 0)n[a]=u;return n},Sr=new Set(["id","color","frozen","trainable","dashed","bold","crossed"]),Or={leftOf:"left-of",rightOf:"right-of"};class ze{options;g=new Re;constructor(o={}){this.options={...o}}input(o="",n={}){if(typeof o!=="string")Object.assign(o.options,n);let i=typeof o==="string"?Ve(o,n):o;return new Mt(this.g,this.g.node(i))}edge(o,n,i={}){if(o.graph!==this.g)throw Error("edge: tensor belongs to another figure");if(!(n instanceof Mt))this.g.node(n);return this.g.edges.push({from:o.node,to:n,style:{...o.style,...typeof i==="string"?{label:i}:i}}),this}rank(...o){return this.g.ranks.push(o),this}diagram(){let o=this.g,n=new Map,i=Ue(pe,this.options),a=0;for(let[h,x]of o.nodes){let g=[i,...x.mods.map((W)=>W.options)],v=h.options,R=Mr(h.defaults,...g.flatMap((W)=>[W?.style,W?.kinds?.[h.kind]]),v),I=h.label,q=(W)=>{if(!I.includes(W))return!1;return I=I.replace(W,"").trim(),!0},O=q("\uD83E\uDDCA"),U=q("\uD83D\uDD25"),X=[...x.mods].reverse().map((W)=>W.options).find((W)=>W.frozen||W.trainable),V=v.frozen||O?"frozen":v.trainable||U?"train":X?.frozen?"frozen":X?.trainable?"train":"";if(V&&!v.texture)delete R.texture;let _=new Set([V,R.dashed&&"dashed",R.bold&&"bold",R.crossed&&"crossed"].filter((W)=>!!W)),ft={};for(let[W,lt]of Object.entries(R)){if(Sr.has(W)||lt===void 0)continue;ft[Or[W]??W]=lt instanceof Mt||typeof lt==="function"?o.id(lt):Array.isArray(lt)&&lt.every((wt)=>typeof wt==="number")?lt.join(","):typeof lt==="object"?JSON.stringify(lt):String(lt)}n.set(x.id,{id:x.id,shape:h.shape,label:I,color:R.color,flags:_,attrs:ft,groups:x.groups,order:a++,x:0,y:0,w:0,h:0})}let u=o.edges.map(({from:h,to:x,style:g})=>({from:h,to:o.id(x),label:g.label,pts:[],op:g.dense?"=>":g.arrow==="none"?g.dashed?"..":"--":g.arrow==="both"?"<->":g.dashed?"..>":"->",flags:new Set([...["skip","curve","thick","dashed","crossed"].filter((v)=>g[v]),...g.join?["join"]:[]]),attrs:{...g.side&&{side:g.side},...g.color&&{color:g.color}}}));return{dir:i.flow??"TB",theme:i.theme??"light",gap:i.gap,fontSize:i.style?.fontSize,fontFamily:i.style?.fontFamily,legend:i.legend===!0?{}:i.legend||void 0,nodes:n,edges:u,groups:[...o.groups.values()].map((h)=>({...h,flags:new Set(h.flags)})),ranks:o.ranks.map((h)=>h.map((x)=>o.id(x)))}}svg(o={}){let n=this.diagram();return Ee(Zt(n),o.theme??n.theme,Ue(pe,this.options).materials??!0)}async toBlob(o={}){let n=o.format??"svg",i=this.svg({theme:o.theme});if(n==="svg")return new Blob([i],{type:"image/svg+xml"});if(!["png","jpeg","webp"].includes(n))throw Error(`export: unknown format "${n}" (svg, png, jpeg, webp)`);return Rr(i,{...o,format:n},`image/${n}`)}async download(o="figure.svg",n={}){let i=/\.(\w+)$/.exec(o)?.[1].toLowerCase(),a=n.format??(i==="jpg"?"jpeg":i)??"svg",u=URL.createObjectURL(await this.toBlob({...n,format:a}));Object.assign(document.createElement("a"),{href:u,download:o}).click(),setTimeout(()=>URL.revokeObjectURL(u),1000)}mount(o,n={}){let i=typeof o==="string"?document.querySelector(o):o;if(!i)throw Error(`mount: no element matches "${o}"`);return i.innerHTML=this.svg(n),this}}async function Er(o){let n=[...new Set([...o.matchAll(/<image href="(?!data:)([^"]+)"/g)].map((i)=>i[1].replace(/&amp;/g,"&")))];for(let i of n)try{let a=await(await fetch(i)).blob(),u=await new Promise((h,x)=>{let g=new FileReader;g.onload=()=>h(g.result),g.onerror=x,g.readAsDataURL(a)});o=o.split(`href="${i.replace(/&/g,"&amp;")}"`).join(`href="${u}"`)}catch{}return o}async function Rr(o,n,i){if(typeof document>"u")throw Error(`export: ${n.format} needs a browser (canvas); in Node use svg()`);let[,a,u]=/width="([\d.]+)" height="([\d.]+)"/.exec(o)??[],h=n.scale??2,x=new Image;x.src="data:image/svg+xml;charset=utf-8,"+encodeURIComponent(await Er(o)),await x.decode();let g=document.createElement("canvas");g.width=Math.ceil(+a*h),g.height=Math.ceil(+u*h);let v=g.getContext("2d"),R=n.background??(i==="image/jpeg"?"#ffffff":"");if(R)v.fillStyle=R,v.fillRect(0,0,g.width,g.height);return v.drawImage(x,0,0,g.width,g.height),new Promise((I,q)=>g.toBlob((O)=>O?I(O):q(Error("export: canvas.toBlob failed")),i,n.quality??0.92))}function Xo(o,n){let[i,a]=typeof o==="function"?[{},o]:[o,n],u=new ze(i);return a(u),u}function Ko(o,n={}){return Ee(Zt(o),n.theme,n.materials??!0)}var Lr={...De,draw:Ko,layout:Zt,THEMES:ue};})();