moodi 0.2.1 → 0.3.0

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/README.md CHANGED
@@ -74,6 +74,7 @@ With the Web Component, put an `<img>` inside `<moodi-buddy>`; it shows until th
74
74
  | `interactive` | `boolean` | `true` | Hover lift, click reaction |
75
75
  | `shadow` | `boolean` | `true` | Soft ground shadow |
76
76
  | `sound` | `boolean` | `false` | Short synthesized effect per mood (and when it appears) |
77
+ | `settle` | `number` | `0` | ms after a mood change when the body calms to idle breathing (expression stays); `0` = never |
77
78
 
78
79
  Boolean attributes on the Web Component take `"true"` / `"false"`.
79
80
 
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- 'use strict';var three=require('three');var Ue=["ghost","pebble","blob","drop","bean","cube"],Je=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],M={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:true,interactive:true,shadow:true,sound:false},Ot={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},Bt={idle:{label:"Idle",description:"Breathing softly. Blinks now and then."},thinking:{label:"Thinking",description:"Eyes drift up. Little thoughts pop."},loading:{label:"Loading",description:"Half-lidded, wobbling patiently."},listening:{label:"Listening",description:"All eyes. Nods along to every word."},speaking:{label:"Speaking",description:"Chatty mouth, bouncing to the rhythm."},happy:{label:"Happy",description:"Bounces with joy. Cheeks glow."},surprised:{label:"Surprised",description:"Wide eyes, tiny o. Did not see that coming."},sad:{label:"Sad",description:"Slumps a little. Needs a minute."},error:{label:"Error",description:"Something broke. Shakes it off."},sleeping:{label:"Sleeping",description:"Deep, slow breaths. Do not disturb."},love:{label:"Love",description:"Heart eyes, floating on air."}},Lt={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},ce=e=>Ue.includes(e),de=e=>Je.includes(e);var L=null;function X(e,o){if(!e)return o;let t=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(e.trim());if(t)return xe(t[1]);if(L||(L=document.createElement("canvas").getContext("2d")),!L)return o;L.fillStyle="#010203",L.fillStyle=e;let s=String(L.fillStyle);if(s==="#010203"&&e.trim()!=="#010203")return o;if(s.startsWith("#"))return xe(s.slice(1));L.canvas.width=L.canvas.height=1,L.clearRect(0,0,1,1),L.fillRect(0,0,1,1);let i=L.getImageData(0,0,1,1).data;return [i[0]/255,i[1]/255,i[2]/255]}function xe(e){e.length===3&&(e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]);let o=parseInt(e,16);return [(o>>16&255)/255,(o>>8&255)/255,(o&255)/255]}var G=e=>`rgb(${Math.round(e[0]*255)},${Math.round(e[1]*255)},${Math.round(e[2]*255)})`,x=(e,o,t)=>[e[0]+(o[0]-e[0])*t,e[1]+(o[1]-e[1])*t,e[2]+(o[2]-e[2])*t],ue=e=>e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4),pe=e=>.2126*ue(e[0])+.7152*ue(e[1])+.0722*ue(e[2]),Ze=[.07,.06,.08],Qe=[1,.97,.93],et=[1,.42,.48],Q=e=>pe(e)>.06?x(Ze,e,.06):Qe,ee=e=>x(e,et,.78);var U=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],q={eyeOpen:1,eyeSize:1,lookX:0,lookY:0,lidCover:0,lidTilt:0,happyEye:0,closedEye:0,xEye:0,heartEye:0,mouthW:.1,mouthCurve:.35,mouthOpen:0,mouthRound:0,mouthSkew:0,mouthWave:0,blush:.45},te={idle:{...q},thinking:{...q,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...q,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...q,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...q,mouthW:.09,mouthCurve:.25},happy:{...q,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...q,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...q,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...q,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...q,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...q,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Fe={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},be=e=>({...e}),tt=.31,ot=-0.04,st=.098,me=.128,it=.2,fe=.05;function ze(e,o,t,s,i,r,h,c,l,p){let n=e.canvas.width;e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,n,n),e.setTransform(n/2,0,0,n/2,n/2,n/2),e.lineCap="round",e.lineJoin="round";let a=o.lookX*.075,u=o.lookY*.06;if(o.blush>.01){let m=n*.018;e.save(),"filter"in e&&(e.filter=`blur(${m}px)`);for(let g of [-1,1]){let b=g*.52+a,d=.16+u,y=e.createRadialGradient(b,d,0,b,d,.16);y.addColorStop(0,i),y.addColorStop(1,"rgba(0,0,0,0)"),e.globalAlpha=Math.min(1,o.blush)*.62,e.fillStyle=y,e.beginPath(),e.ellipse(b,d,.17,.12,0,0,Math.PI*2),e.fill();}e.restore();let f=Math.min(1,Math.max(0,(o.blush-.6)/.4));if(f>.01){e.save(),e.globalAlpha=f*.55,e.strokeStyle=i,e.lineWidth=.016,e.lineCap="round";for(let g of [-1,1]){let b=g*.52+a,d=.16+u;for(let y=-1;y<=1;y++){let P=b+y*.045;e.beginPath(),e.moveTo(P+.018,d-.035),e.lineTo(P-.018,d+.035),e.stroke();}}e.restore();}}e.fillStyle=s,e.strokeStyle=s,e.lineWidth=fe;let w=Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye),A=1-w;for(let m of [-1,1]){let f=m*tt+a,g=ot+u,b=st*o.eyeSize;if(A>.01){e.globalAlpha=A;let d=Math.max(.06,o.eyeOpen*t),y=me*o.eyeSize*d;if(e.save(),o.lidCover>.01||o.lidTilt>.01){let W=g-me*o.eyeSize+2*me*o.eyeSize*o.lidCover;e.translate(f,W),e.rotate(m*o.lidTilt*.6),e.beginPath(),e.rect(-1,0,2,2),e.clip(),e.rotate(-m*o.lidTilt*.6),e.translate(-f,-W);}r>.5&&Re(e,f,g,b*2.1,y*1.9,s,.35*A);let P=e.createRadialGradient(f,g-y*.2,0,f,g,Math.max(b,y));if(P.addColorStop(0,h),P.addColorStop(1,s),e.fillStyle=P,e.beginPath(),e.ellipse(f,g,b,y,0,0,Math.PI*2),e.fill(),e.fillStyle=s,d>.35){let R=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),le=c==="#fff"?.3:.34;e.globalAlpha=A*Math.min(1,(d-.35)*3),e.fillStyle=c,e.beginPath(),e.arc(f-b*.1+R*b*.5,g-y*.16+W*y*.42,b*le,0,Math.PI*2),e.fill(),e.fillStyle=s;}e.restore();}if(r>.5&&w>.01&&Re(e,f,g,b*2,b*1.8,s,.3*w),e.fillStyle=s,o.happyEye>.01){let d=.6+.4*o.happyEye;e.globalAlpha=Math.min(1,o.happyEye),e.beginPath(),e.arc(f,g+.045,b*1.15*d,Math.PI*1.12,Math.PI*1.88),e.stroke();}if(o.closedEye>.01){let d=.6+.4*o.closedEye;e.globalAlpha=Math.min(1,o.closedEye),e.beginPath(),e.moveTo(f-b*1.15*d,g+.01),e.quadraticCurveTo(f,g+.07*d,f+b*1.15*d,g+.01),e.stroke();}if(o.xEye>.01){let d=b*.95*(.6+.4*o.xEye);e.globalAlpha=Math.min(1,o.xEye),e.beginPath(),e.moveTo(f-d,g-d),e.lineTo(f+d,g+d),e.moveTo(f+d,g-d),e.lineTo(f-d,g+d),e.stroke();}o.heartEye>.01&&(e.globalAlpha=Math.min(1,o.heartEye),ge(e,f,g,b*1.35*(.5+.5*o.heartEye)),e.fill());}let k=a+o.mouthSkew*.07,S=it+u,E=Math.min(1,Math.max(0,o.mouthRound)),C=o.mouthW,O=e.createLinearGradient(k-C-.02,S,k+C+.02,S);O.addColorStop(0,p),O.addColorStop(.5,h),O.addColorStop(1,p);let B=(m,f,g,b)=>{e.fillStyle=l,m(),e.fill(),f>.02&&(e.save(),m(),e.clip(),e.fillStyle=i,e.globalAlpha*=Math.min(1,(f-.02)*12),e.beginPath(),e.ellipse(k,g,b,f*.55,0,0,Math.PI*2),e.fill(),e.restore()),e.strokeStyle=O;let d=e.lineWidth;e.lineWidth=d*.6,m(),e.stroke(),e.lineWidth=d;};if(E<.99){e.globalAlpha=1-E;let m=18,f=y=>o.mouthCurve*.075*(1-y*y),g=(y,P)=>{let R=y/m*2-1,W=k+R*C,le=S+f(R)+o.mouthSkew*R*.03+o.mouthWave*.018*Math.sin((R+1)*Math.PI*2)+P*(1-R*R);return [W,le]},b=Math.max(0,o.mouthOpen),d=()=>{for(let y=0;y<=m;y++){let[P,R]=g(y,-b*.15);y?e.lineTo(P,R):e.moveTo(P,R);}};b>.003?B(()=>{e.beginPath(),d();for(let P=m;P>=0;P--){let[R,W]=g(P,b);e.lineTo(R,W);}e.closePath();},b,g(m/2,b)[1],C*.55):(e.strokeStyle=O,e.beginPath(),d(),e.stroke());}if(E>.01){e.globalAlpha=E;let m=Math.max(.02,o.mouthOpen*.55+.01),f=()=>{e.beginPath(),e.ellipse(k,S+.01,m*.85,m,0,0,Math.PI*2);};e.lineWidth=fe*.8,B(f,o.mouthOpen,S+.01+m*.8,m*.6),e.lineWidth=fe;}e.globalAlpha=1;}function Re(e,o,t,s,i,r,h){let c=e.createRadialGradient(o,t,0,o,t,1);c.addColorStop(0,r),c.addColorStop(1,"rgba(0,0,0,0)"),e.save(),e.globalAlpha=h,e.translate(o,t),e.scale(s,i),e.translate(-o,-t),e.fillStyle=c,e.beginPath(),e.arc(o,t,1,0,Math.PI*2),e.fill(),e.restore();}function ge(e,o,t,s){e.beginPath(),e.moveTo(o,t+s*.85),e.bezierCurveTo(o-s*1.25,t+s*.05,o-s*.75,t-s*.95,o,t-s*.35),e.bezierCurveTo(o+s*.75,t-s*.95,o+s*1.25,t+s*.05,o,t+s*.85),e.closePath();}var ie=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:v,cos:nt,abs:rt,max:oe,exp:at,PI:se}=Math;function ye(e,o,t,s){switch(s.y=s.rx=s.ry=s.rz=s.st=s.mouth=s.lookX=s.lookY=0,e){case "idle":{let i=v(o*1.6);s.y=.015*i,s.st=.018*i;break}case "thinking":{s.rz=.07*v(o*1.1),s.y=.02*v(o*2.2),s.ry=.15,s.rx=-0.06;break}case "loading":{let i=o*2.4;s.rx=.12*v(i),s.rz=.12*nt(i),s.ry=.22*v(i),s.y=.02*v(i*2),s.lookX=.55*v(i);break}case "listening":{let i=oe(0,v(o*3.2));s.rx=.09*i*i;let r=rt(v(o*7.1)*.5+v(o*11.3)*.3+v(o*3.7)*.2);s.st=.03*r;break}case "speaking":{let i=oe(0,v(o*9)*.6+v(o*14.3)*.4)*(.65+.35*v(o*1.7));s.mouth=.11*i,s.st=.04*i,s.rz=.03*v(o*2.1);break}case "happy":{let i=o*1.35%1;s.y=.2*v(se*i);let r=oe(0,1-i/.14)+oe(0,(i-.9)/.1);s.st=.08*v(se*i)-.14*r,s.rz=.05*v(o*1.35*se);break}case "surprised":{if(t<.45){let i=v(se*t/.45);s.y=.16*i,s.st=.12*i;}s.rz=.01*v(o*38)*at(-t*2),s.rx=-0.06;break}case "sad":{let i=v(o*1.1);s.y=-0.06,s.rx=.2,s.st=-0.04+.015*i,s.rz=-0.04;break}case "error":{let i=t%2.8;if(i<.55){let r=1-i/.55;s.ry=.26*v(i*42)*r,s.rz=.05*v(i*42+1)*r;}s.st=-0.02;break}case "sleeping":{let i=v(o*1);s.st=.04*i,s.y=-0.03,s.rz=.1,s.rx=.1;break}case "love":{s.y=.07+.06*v(o*1.6),s.rz=.05*v(o*.8),s.st=.015*v(o*1.6+1);break}}return s}function Oe(e,o,t,s){for(let i of Object.keys(s))s[i]=e[i]+(o[i]-e[i])*t;return s}var Be={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var j=new Set,T=null,$=0,V=0,ne=0,Me,Le=false,H="idle",D={x:0,y:0,lastMove:-1e9,known:false},z={reducedMotion:false,lowPower:false};function lt(){if(Le)return;Le=true,addEventListener("pointermove",o=>{D.x=o.clientX,D.y=o.clientY,D.lastMove=performance.now(),D.known=true;},{passive:true}),document.addEventListener("visibilitychange",_);let e=matchMedia("(prefers-reduced-motion: reduce)");z.reducedMotion=e.matches,e.addEventListener?.("change",o=>z.reducedMotion=o.matches);}function qe(e,o){return T||(T=new three.WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),T.setPixelRatio(1),T.setClearColor(0,0),z.lowPower=ct(T.getContext()),T.setScissorTest(true),T.domElement.addEventListener("webglcontextlost",t=>{t.preventDefault(),T?.dispose(),T=null,$=V=0,H="idle";})),(e>$||o>V)&&($=Math.max($,e),V=Math.max(V,o),T.setSize($,V,false)),T}var Ie=()=>V;function ct(e){try{let o=e.getExtension("WEBGL_debug_renderer_info"),t=String(e.getParameter(o?o.UNMASKED_RENDERER_WEBGL:e.RENDERER));return /swiftshader|llvmpipe|softpipe|software|basic render/i.test(t)}catch{return false}}function Ye(e,o){if(H==="ready")return true;if(H==="idle"&&T){H="compiling";let t=T;t.compileAsync(e,o).catch(()=>{}).then(()=>{t===T&&(H="ready"),_();});}return false}function We(e){lt(),clearTimeout(Me),j.add(e),_();}function De(e){j.delete(e),j.size===0&&(clearTimeout(Me),Me=setTimeout(()=>{j.size||!T||(T.dispose(),T.forceContextLoss(),T=null,$=V=0,H="idle");},1500));}function _(){if(!ne){for(let e of j)if(e.active){ne=requestAnimationFrame(_e);return}}}var dt=10,Ge=new WeakMap,ve=0;function _e(e){ne=0;let o=[...j].filter(r=>r.active);if(!o.length)return;let t=performance.now(),s=o.length,i=0;for(;i<s;i++){let r=o[(ve+i)%s],h=Ge.get(r)??e-16;if(Ge.set(r,e),r.tick(Math.min(.05,Math.max(0,(e-h)/1e3)),e),performance.now()-t>dt){i++;break}}ve=(ve+i)%s,ne=requestAnimationFrame(_e);}var J={pebble:{faceY:.02,faceScale:1.3,animated:false},blob:{faceY:.02,faceScale:1.28,animated:true},drop:{faceY:-0.24,faceScale:1.22,animated:false},bean:{faceY:.14,faceScale:.92,animated:false},cube:{faceY:0,faceScale:1.22,animated:false},ghost:{faceY:.22,faceScale:1.24,animated:true}};function Ne(e){let o=new three.SphereGeometry(1,e*2,Math.round(e*1.5)),t=o.getAttribute("position"),s=o.getIndex(),i=new Map,r=[],h=new Uint32Array(t.count);for(let n=0;n<t.count;n++){let a=t.getX(n),u=t.getY(n),w=t.getZ(n),A=`${Math.round(a*1e4)},${Math.round(u*1e4)},${Math.round(w*1e4)}`,k=i.get(A);if(k===void 0){k=r.length/3,i.set(A,k);let S=Math.hypot(a,u,w)||1;r.push(a/S,u/S,w/S);}h[n]=k;}let c=[];for(let n=0;n<s.count;n+=3){let a=h[s.getX(n)],u=h[s.getX(n+1)],w=h[s.getX(n+2)];a!==u&&u!==w&&a!==w&&c.push(a,u,w);}o.dispose();let l=new three.BufferGeometry,p=new Float32Array(r);return l.setAttribute("position",new three.BufferAttribute(new Float32Array(p),3)),l.setIndex(c),{geometry:l,dirs:p}}var ft=(e,o,t)=>{let s=Math.min(1,Math.max(0,(t-e)/(o-e)));return s*s*(3-2*s)};function we(e,o,t,s,i,r,h){let c=o,l=t,p=s;switch(e){case "pebble":{c=o*1.1+.04*t,l=t*.92*(.95+.05*Math.tanh(3*t))+.03*o,p=s*.94;break}case "blob":{let n=.97+.045*Math.sin(2.1*o+1.3*i)*Math.sin(2.4*t+1.7*i+1)+.035*Math.sin(2.7*s+2.3*o+1.1*i);c=o*n,l=t*n,p=s*n;break}case "drop":{let n=Math.max(t,0),a=1-.8*Math.pow(n,1.6);c=o*a*.98+.1*n*n*n,l=(t<0?t*.9:t*1.32)-.12,p=s*a*.98;break}case "bean":{let n=t*.78+.42*Math.tanh(t*3);c=(o*.8+.14*(n*n/1.44)-.07)*.9,l=n*.88,p=s*.8*.9;break}case "cube":{let a=Math.pow(Math.pow(Math.abs(o),4.5)+Math.pow(Math.abs(t),4.5)+Math.pow(Math.abs(s),4.5),-0.2222222222222222);c=o*a*.84,l=t*a*.82,p=s*a*.84;break}case "ghost":{if(t>=0)c=o,l=t,p=s;else {let a=(Math.acos(Math.max(-1,t))-Math.PI/2)/(Math.PI/2),u=Math.hypot(o,s)||1,w=o/u,A=s/u,k=Math.atan2(s,o),S=.085*Math.sin(5*k+2.2*i),E=1.04,C=.13,O=-1.05+S,B;if(a<.45){let m=a/.45;B=1+.04*m,l=-m*(1.05-C)+S*ft(.45,1,m);}else if(a<.75){let m=(a-.45)/.3*(Math.PI/2);B=E-C+C*Math.cos(m),l=O+C-C*Math.sin(m);}else {let m=(a-.75)/.25;B=(E-C)*(1-m),l=O+.22*m*m*(3-2*m);}c=w*B,p=A*B;}c*=.9,l=(l+.1)*.9,p*=.9;break}}r[h]=c,r[h+1]=l,r[h+2]=p;}var I=null,re=null;function bt(){if(typeof window>"u"||!("AudioContext"in window))return null;if(gt(),!I){I=new AudioContext;let e=I.createBiquadFilter();e.type="lowpass",e.frequency.value=3200,re=I.createGain(),re.gain.value=.55,re.connect(e).connect(I.destination);}return I.state==="suspended"&&I.resume(),I}function F(e,{freq:o,to:t,at:s=0,dur:i=.12,type:r="sine",gain:h=.1,wobble:c=0}){let l=e.currentTime+s,p=e.createOscillator(),n=e.createGain();if(p.type=r,p.frequency.setValueAtTime(o,l),t&&p.frequency.exponentialRampToValueAtTime(t,l+i),c){let a=e.createOscillator(),u=e.createGain();a.frequency.value=7,u.gain.value=c,a.connect(u).connect(p.frequency),a.start(l),a.stop(l+i+.05);}n.gain.setValueAtTime(1e-4,l),n.gain.exponentialRampToValueAtTime(h,l+Math.min(.012,i/4)),n.gain.exponentialRampToValueAtTime(1e-4,l+i),p.connect(n).connect(re),p.start(l),p.stop(l+i+.02);}var ke={idle:e=>F(e,{freq:520,to:780,dur:.09,gain:.08}),thinking:e=>[660,760,900].forEach((o,t)=>F(e,{freq:o,to:o*1.08,at:t*.1,dur:.07,type:"triangle",gain:.06})),loading:e=>[0,.14,.28].forEach(o=>F(e,{freq:940,at:o,dur:.04,gain:.05})),listening:e=>{F(e,{freq:523,dur:.12,type:"triangle",gain:.07}),F(e,{freq:784,at:.1,dur:.22,type:"triangle",gain:.07});},speaking:e=>[380,460,340,520,420].forEach((o,t)=>F(e,{freq:o,to:o*1.15,at:t*.075,dur:.06,type:"triangle",gain:.06})),happy:e=>[523,659,784,1047].forEach((o,t)=>F(e,{freq:o,at:t*.07,dur:.14,type:"triangle",gain:.07})),surprised:e=>F(e,{freq:280,to:980,dur:.18,gain:.09,wobble:30}),sad:e=>{F(e,{freq:560,to:300,dur:.5,gain:.07}),F(e,{freq:420,to:240,at:.12,dur:.5,gain:.04});},error:e=>[0,.14].forEach(o=>F(e,{freq:170,to:150,at:o,dur:.1,type:"sawtooth",gain:.045})),sleeping:e=>{F(e,{freq:150,to:115,dur:.6,gain:.07,wobble:6}),F(e,{freq:300,to:230,at:.05,dur:.5,gain:.02});},love:e=>[1319,1568,2093,1760].forEach((o,t)=>F(e,{freq:o,at:t*.08,dur:.22,gain:.04}))},Y=null,Ve=false;function gt(){if(Ve)return;Ve=true;let e=()=>{I&&I.resume().then(()=>{Y&&performance.now()<Y.until&&ke[Y.mood](I),Y=null;});};for(let o of ["pointerdown","touchend","keydown"])addEventListener(o,e,{passive:true});}function ae(e,o=false){let t=bt();if(t){if(t.state==="running"){Y=null,ke[e](t);return}Y={mood:e,until:performance.now()+(o?6e3:1200)},t.resume().then(()=>{Y&&performance.now()<Y.until&&ke[Y.mood](t),Y=null;});}}var Ae=-0.85,He=new Map;function Tt(e){let o=He.get(e);if(!o){let{geometry:t,dirs:s}=Ne(e);o={dirs:s,index:new Uint32Array(t.getIndex().array)},t.dispose(),He.set(e,o);}return o}var K=(e,o)=>1-Math.exp(-e*o),je=e=>1-Math.pow(1-e,3),he=class{constructor(){this.scene=new three.Scene;this.camera=new three.PerspectiveCamera(30,1,.1,50);this.group=new three.Group;this.v=new three.Vector3;this.geometry=new three.BufferGeometry;this.faceCanvas=document.createElement("canvas");this.uniforms={uFace:{value:null},uFaceY:{value:0},uFaceScale:{value:1.5}};this.state={...M};this.visible=false;this.connected=false;this.w=0;this.h=0;this.t=Math.random()*100;this.shapeFrom=M.shape;this.shapeTo=M.shape;this.morph=1;this.shapeDirty=true;this.shown=M.mood;this.prevShown=M.mood;this.tau=10;this.prevTau=10;this.blend=1;this.reaction=null;this.pose=ie();this.poseA=ie();this.poseB=ie();this.face=be(te.idle);this.target=be(te.idle);this.drawn=new Float32Array(U.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=X(M.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=Q(this.body);this.faceCT=this.faceC;this.cheek=ee(this.body);this.cheekT=this.cheek;this.look={x:0,y:0};this.wander={x:0,y:0,next:0};this.squash=0;this.squashV=0;this.pop=.6;this.popV=0;this.hover=0;this.hovering=false;this.overlay={dots:0,zz:0,hearts:0,waves:0,bang:0};this.needsFrame=true;this.ready=false;this.css=[0,0];this.quality=1;this.lowPowerApplied=false;this.cost=0;this.costFrames=0;this.lastFrame=[];this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d"),this.faceCtx=this.faceCanvas.getContext("2d"),this.faceCanvas.width=this.faceCanvas.height=256,this.faceTex=new three.CanvasTexture(this.faceCanvas),this.faceTex.colorSpace=three.SRGBColorSpace,this.faceTex.minFilter=three.LinearMipmapLinearFilter,this.faceTex.anisotropy=4,this.uniforms.uFace.value=this.faceTex,this.setSegments(24),this.material=this.createMaterial(),this.mesh=new three.Mesh(this.geometry,this.material),this.mesh.frustumCulled=false,this.mesh.position.y=-Ae,this.group.add(this.mesh),this.scene.add(this.group),this.scene.add(new three.HemisphereLight(16774120,7234140,1.7));let o=new three.DirectionalLight(16777215,1.9);o.position.set(2.5,3.5,4);let t=new three.DirectionalLight(16770770,1.4);t.position.set(-3.5,2,-3);let s=new three.DirectionalLight(16773350,.45);s.position.set(-2,-1,3),this.scene.add(o,t,s),this.camera.position.set(0,.3,6.6),this.camera.lookAt(0,.12,0);}get active(){return this.visible&&this.connected&&this.w>0}createMaterial(){let o=new three.MeshStandardMaterial({roughness:.62,metalness:0});return o.color.setRGB(...this.body,three.SRGBColorSpace),o.customProgramCacheKey=()=>"moodi-1",o.onBeforeCompile=t=>{Object.assign(t.uniforms,this.uniforms),t.vertexShader=t.vertexShader.replace("#include <common>",`#include <common>
1
+ 'use strict';var three=require('three');var Ue=["ghost","pebble","blob","drop","bean","cube"],Je=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],M={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:true,interactive:true,shadow:true,sound:false,settle:0},Bt={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},Ot={idle:{label:"Idle",description:"Breathing softly. Blinks now and then."},thinking:{label:"Thinking",description:"Eyes drift up. Little thoughts pop."},loading:{label:"Loading",description:"Half-lidded, wobbling patiently."},listening:{label:"Listening",description:"All eyes. Nods along to every word."},speaking:{label:"Speaking",description:"Chatty mouth, bouncing to the rhythm."},happy:{label:"Happy",description:"Bounces with joy. Cheeks glow."},surprised:{label:"Surprised",description:"Wide eyes, tiny o. Did not see that coming."},sad:{label:"Sad",description:"Slumps a little. Needs a minute."},error:{label:"Error",description:"Something broke. Shakes it off."},sleeping:{label:"Sleeping",description:"Deep, slow breaths. Do not disturb."},love:{label:"Love",description:"Heart eyes, floating on air."}},Lt={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},de=e=>Ue.includes(e),ue=e=>Je.includes(e);var O=null;function X(e,o){if(!e)return o;let t=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(e.trim());if(t)return Fe(t[1]);if(O||(O=document.createElement("canvas").getContext("2d")),!O)return o;O.fillStyle="#010203",O.fillStyle=e;let s=String(O.fillStyle);if(s==="#010203"&&e.trim()!=="#010203")return o;if(s.startsWith("#"))return Fe(s.slice(1));O.canvas.width=O.canvas.height=1,O.clearRect(0,0,1,1),O.fillRect(0,0,1,1);let i=O.getImageData(0,0,1,1).data;return [i[0]/255,i[1]/255,i[2]/255]}function Fe(e){e.length===3&&(e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]);let o=parseInt(e,16);return [(o>>16&255)/255,(o>>8&255)/255,(o&255)/255]}var L=e=>`rgb(${Math.round(e[0]*255)},${Math.round(e[1]*255)},${Math.round(e[2]*255)})`,x=(e,o,t)=>[e[0]+(o[0]-e[0])*t,e[1]+(o[1]-e[1])*t,e[2]+(o[2]-e[2])*t],pe=e=>e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4),me=e=>.2126*pe(e[0])+.7152*pe(e[1])+.0722*pe(e[2]),Ze=[.07,.06,.08],Qe=[1,.97,.93],et=[1,.42,.48],ee=e=>me(e)>.06?x(Ze,e,.06):Qe,te=e=>x(e,et,.78);var U=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],G={eyeOpen:1,eyeSize:1,lookX:0,lookY:0,lidCover:0,lidTilt:0,happyEye:0,closedEye:0,xEye:0,heartEye:0,mouthW:.1,mouthCurve:.35,mouthOpen:0,mouthRound:0,mouthSkew:0,mouthWave:0,blush:.45},oe={idle:{...G},thinking:{...G,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...G,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...G,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...G,mouthW:.09,mouthCurve:.25},happy:{...G,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...G,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...G,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...G,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...G,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...G,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Be={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},ge=e=>({...e}),tt=.31,ot=-0.04,st=.098,fe=.128,it=.2,be=.05;function Oe(e,o,t,s,i,r,d,a,l,u){let n=e.canvas.width;e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,n,n),e.setTransform(n/2,0,0,n/2,n/2,n/2),e.lineCap="round",e.lineJoin="round";let h=o.lookX*.075,p=o.lookY*.06;if(o.blush>.01){let m=n*.018;e.save(),"filter"in e&&(e.filter=`blur(${m}px)`);for(let y of [-1,1]){let v=y*.52+h,f=.16+p,c=e.createRadialGradient(v,f,0,v,f,.16);c.addColorStop(0,i),c.addColorStop(1,"rgba(0,0,0,0)"),e.globalAlpha=Math.min(1,o.blush)*.62,e.fillStyle=c,e.beginPath(),e.ellipse(v,f,.17,.12,0,0,Math.PI*2),e.fill();}e.restore();let g=Math.min(1,Math.max(0,(o.blush-.6)/.4));if(g>.01){e.save(),e.globalAlpha=g*.55,e.strokeStyle=i,e.lineWidth=.016,e.lineCap="round";for(let y of [-1,1]){let v=y*.52+h,f=.16+p;for(let c=-1;c<=1;c++){let T=v+c*.045;e.beginPath(),e.moveTo(T+.018,f-.035),e.lineTo(T-.018,f+.035),e.stroke();}}e.restore();}}e.fillStyle=s,e.strokeStyle=s,e.lineWidth=be;let b=Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye),E=1-b;for(let m of [-1,1]){let g=m*tt+h,y=ot+p,v=st*o.eyeSize;if(E>.01){e.globalAlpha=E;let f=Math.max(.06,o.eyeOpen*t),c=fe*o.eyeSize*f;if(e.save(),o.lidCover>.01||o.lidTilt>.01){let W=y-fe*o.eyeSize+2*fe*o.eyeSize*o.lidCover;e.translate(g,W),e.rotate(m*o.lidTilt*.6),e.beginPath(),e.rect(-1,0,2,2),e.clip(),e.rotate(-m*o.lidTilt*.6),e.translate(-g,-W);}r>.5&&ze(e,g,y,v*2.1,c*1.9,s,.35*E);let T=e.createRadialGradient(g,y-c*.2,0,g,y,Math.max(v,c));if(T.addColorStop(0,d),T.addColorStop(1,s),e.fillStyle=T,e.beginPath(),e.ellipse(g,y,v,c,0,0,Math.PI*2),e.fill(),e.fillStyle=s,f>.35){let R=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),ce=a==="#fff"?.3:.34;e.globalAlpha=E*Math.min(1,(f-.35)*3),e.fillStyle=a,e.beginPath(),e.arc(g-v*.1+R*v*.5,y-c*.16+W*c*.42,v*ce,0,Math.PI*2),e.fill(),e.fillStyle=s;}e.restore();}if(r>.5&&b>.01&&ze(e,g,y,v*2,v*1.8,s,.3*b),e.fillStyle=s,o.happyEye>.01){let f=.6+.4*o.happyEye;e.globalAlpha=Math.min(1,o.happyEye),e.beginPath(),e.arc(g,y+.045,v*1.15*f,Math.PI*1.12,Math.PI*1.88),e.stroke();}if(o.closedEye>.01){let f=.6+.4*o.closedEye;e.globalAlpha=Math.min(1,o.closedEye),e.beginPath(),e.moveTo(g-v*1.15*f,y+.01),e.quadraticCurveTo(g,y+.07*f,g+v*1.15*f,y+.01),e.stroke();}if(o.xEye>.01){let f=v*.95*(.6+.4*o.xEye);e.globalAlpha=Math.min(1,o.xEye),e.beginPath(),e.moveTo(g-f,y-f),e.lineTo(g+f,y+f),e.moveTo(g+f,y-f),e.lineTo(g-f,y+f),e.stroke();}o.heartEye>.01&&(e.globalAlpha=Math.min(1,o.heartEye),ye(e,g,y,v*1.35*(.5+.5*o.heartEye)),e.fill());}let S=h+o.mouthSkew*.07,k=it+p,C=Math.min(1,Math.max(0,o.mouthRound)),A=o.mouthW,B=e.createLinearGradient(S-A-.02,k,S+A+.02,k);B.addColorStop(0,u),B.addColorStop(.5,d),B.addColorStop(1,u);let q=(m,g,y,v)=>{e.fillStyle=l,m(),e.fill(),g>.02&&(e.save(),m(),e.clip(),e.fillStyle=i,e.globalAlpha*=Math.min(1,(g-.02)*12),e.beginPath(),e.ellipse(S,y,v,g*.55,0,0,Math.PI*2),e.fill(),e.restore()),e.strokeStyle=B;let f=e.lineWidth;e.lineWidth=f*.6,m(),e.stroke(),e.lineWidth=f;};if(C<.99){e.globalAlpha=1-C;let m=18,g=c=>o.mouthCurve*.075*(1-c*c),y=(c,T)=>{let R=c/m*2-1,W=S+R*A,ce=k+g(R)+o.mouthSkew*R*.03+o.mouthWave*.018*Math.sin((R+1)*Math.PI*2)+T*(1-R*R);return [W,ce]},v=Math.max(0,o.mouthOpen),f=()=>{for(let c=0;c<=m;c++){let[T,R]=y(c,-v*.15);c?e.lineTo(T,R):e.moveTo(T,R);}};v>.003?q(()=>{e.beginPath(),f();for(let T=m;T>=0;T--){let[R,W]=y(T,v);e.lineTo(R,W);}e.closePath();},v,y(m/2,v)[1],A*.55):(e.strokeStyle=B,e.beginPath(),f(),e.stroke());}if(C>.01){e.globalAlpha=C;let m=Math.max(.02,o.mouthOpen*.55+.01),g=()=>{e.beginPath(),e.ellipse(S,k+.01,m*.85,m,0,0,Math.PI*2);};e.lineWidth=be*.8,q(g,o.mouthOpen,k+.01+m*.8,m*.6),e.lineWidth=be;}e.globalAlpha=1;}function ze(e,o,t,s,i,r,d){let a=e.createRadialGradient(o,t,0,o,t,1);a.addColorStop(0,r),a.addColorStop(1,"rgba(0,0,0,0)"),e.save(),e.globalAlpha=d,e.translate(o,t),e.scale(s,i),e.translate(-o,-t),e.fillStyle=a,e.beginPath(),e.arc(o,t,1,0,Math.PI*2),e.fill(),e.restore();}function ye(e,o,t,s){e.beginPath(),e.moveTo(o,t+s*.85),e.bezierCurveTo(o-s*1.25,t+s*.05,o-s*.75,t-s*.95,o,t-s*.35),e.bezierCurveTo(o+s*.75,t-s*.95,o+s*1.25,t+s*.05,o,t+s*.85),e.closePath();}var J=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:w,cos:nt,abs:rt,max:se,exp:at,PI:ie}=Math;function ne(e,o,t,s){switch(s.y=s.rx=s.ry=s.rz=s.st=s.mouth=s.lookX=s.lookY=0,e){case "idle":{let i=w(o*1.6);s.y=.015*i,s.st=.018*i;break}case "thinking":{s.rz=.07*w(o*1.1),s.y=.02*w(o*2.2),s.ry=.15,s.rx=-0.06;break}case "loading":{let i=o*2.4;s.rx=.12*w(i),s.rz=.12*nt(i),s.ry=.22*w(i),s.y=.02*w(i*2),s.lookX=.55*w(i);break}case "listening":{let i=se(0,w(o*3.2));s.rx=.09*i*i;let r=rt(w(o*7.1)*.5+w(o*11.3)*.3+w(o*3.7)*.2);s.st=.03*r;break}case "speaking":{let i=se(0,w(o*9)*.6+w(o*14.3)*.4)*(.65+.35*w(o*1.7));s.mouth=.11*i,s.st=.04*i,s.rz=.03*w(o*2.1);break}case "happy":{let i=o*1.35%1;s.y=.2*w(ie*i);let r=se(0,1-i/.14)+se(0,(i-.9)/.1);s.st=.08*w(ie*i)-.14*r,s.rz=.05*w(o*1.35*ie);break}case "surprised":{if(t<.45){let i=w(ie*t/.45);s.y=.16*i,s.st=.12*i;}s.rz=.01*w(o*38)*at(-t*2),s.rx=-0.06;break}case "sad":{let i=w(o*1.1);s.y=-0.06,s.rx=.2,s.st=-0.04+.015*i,s.rz=-0.04;break}case "error":{let i=t%2.8;if(i<.55){let r=1-i/.55;s.ry=.26*w(i*42)*r,s.rz=.05*w(i*42+1)*r;}s.st=-0.02;break}case "sleeping":{let i=w(o*1);s.st=.04*i,s.y=-0.03,s.rz=.1,s.rx=.1;break}case "love":{s.y=.07+.06*w(o*1.6),s.rz=.05*w(o*.8),s.st=.015*w(o*1.6+1);break}}return s}function ve(e,o,t,s){for(let i of Object.keys(s))s[i]=e[i]+(o[i]-e[i])*t;return s}var Le={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var j=new Set,P=null,$=0,V=0,re=0,we,Ge=false,H="idle",D={x:0,y:0,lastMove:-1e9,known:false},z={reducedMotion:false,lowPower:false};function lt(){if(Ge)return;Ge=true,addEventListener("pointermove",o=>{D.x=o.clientX,D.y=o.clientY,D.lastMove=performance.now(),D.known=true;},{passive:true}),document.addEventListener("visibilitychange",_);let e=matchMedia("(prefers-reduced-motion: reduce)");z.reducedMotion=e.matches,e.addEventListener?.("change",o=>z.reducedMotion=o.matches);}function qe(e,o){return P||(P=new three.WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),P.setPixelRatio(1),P.setClearColor(0,0),z.lowPower=ct(P.getContext()),P.setScissorTest(true),P.domElement.addEventListener("webglcontextlost",t=>{t.preventDefault(),P?.dispose(),P=null,$=V=0,H="idle";})),(e>$||o>V)&&($=Math.max($,e),V=Math.max(V,o),P.setSize($,V,false)),P}var Ye=()=>V;function ct(e){try{let o=e.getExtension("WEBGL_debug_renderer_info"),t=String(e.getParameter(o?o.UNMASKED_RENDERER_WEBGL:e.RENDERER));return /swiftshader|llvmpipe|softpipe|software|basic render/i.test(t)}catch{return false}}function We(e,o){if(H==="ready")return true;if(H==="idle"&&P){H="compiling";let t=P;t.compileAsync(e,o).catch(()=>{}).then(()=>{t===P&&(H="ready"),_();});}return false}function De(e){lt(),clearTimeout(we),j.add(e),_();}function _e(e){j.delete(e),j.size===0&&(clearTimeout(we),we=setTimeout(()=>{j.size||!P||(P.dispose(),P.forceContextLoss(),P=null,$=V=0,H="idle");},1500));}function _(){if(!re){for(let e of j)if(e.active){re=requestAnimationFrame(Ne);return}}}var dt=10,Ie=new WeakMap,Me=0;function Ne(e){re=0;let o=[...j].filter(r=>r.active);if(!o.length)return;let t=performance.now(),s=o.length,i=0;for(;i<s;i++){let r=o[(Me+i)%s],d=Ie.get(r)??e-16;if(Ie.set(r,e),r.tick(Math.min(.05,Math.max(0,(e-d)/1e3)),e),performance.now()-t>dt){i++;break}}Me=(Me+i)%s,re=requestAnimationFrame(Ne);}var Z={pebble:{faceY:.02,faceScale:1.3,animated:false},blob:{faceY:.02,faceScale:1.28,animated:true},drop:{faceY:-0.24,faceScale:1.22,animated:false},bean:{faceY:.14,faceScale:.92,animated:false},cube:{faceY:0,faceScale:1.22,animated:false},ghost:{faceY:.22,faceScale:1.24,animated:true}};function Ve(e){let o=new three.SphereGeometry(1,e*2,Math.round(e*1.5)),t=o.getAttribute("position"),s=o.getIndex(),i=new Map,r=[],d=new Uint32Array(t.count);for(let n=0;n<t.count;n++){let h=t.getX(n),p=t.getY(n),b=t.getZ(n),E=`${Math.round(h*1e4)},${Math.round(p*1e4)},${Math.round(b*1e4)}`,S=i.get(E);if(S===void 0){S=r.length/3,i.set(E,S);let k=Math.hypot(h,p,b)||1;r.push(h/k,p/k,b/k);}d[n]=S;}let a=[];for(let n=0;n<s.count;n+=3){let h=d[s.getX(n)],p=d[s.getX(n+1)],b=d[s.getX(n+2)];h!==p&&p!==b&&h!==b&&a.push(h,p,b);}o.dispose();let l=new three.BufferGeometry,u=new Float32Array(r);return l.setAttribute("position",new three.BufferAttribute(new Float32Array(u),3)),l.setIndex(a),{geometry:l,dirs:u}}var ft=(e,o,t)=>{let s=Math.min(1,Math.max(0,(t-e)/(o-e)));return s*s*(3-2*s)};function ke(e,o,t,s,i,r,d){let a=o,l=t,u=s;switch(e){case "pebble":{a=o*1.1+.04*t,l=t*.92*(.95+.05*Math.tanh(3*t))+.03*o,u=s*.94;break}case "blob":{let n=.97+.045*Math.sin(2.1*o+1.3*i)*Math.sin(2.4*t+1.7*i+1)+.035*Math.sin(2.7*s+2.3*o+1.1*i);a=o*n,l=t*n,u=s*n;break}case "drop":{let n=Math.max(t,0),h=1-.8*Math.pow(n,1.6);a=o*h*.98+.1*n*n*n,l=(t<0?t*.9:t*1.32)-.12,u=s*h*.98;break}case "bean":{let n=t*.78+.42*Math.tanh(t*3);a=(o*.8+.14*(n*n/1.44)-.07)*.9,l=n*.88,u=s*.8*.9;break}case "cube":{let h=Math.pow(Math.pow(Math.abs(o),4.5)+Math.pow(Math.abs(t),4.5)+Math.pow(Math.abs(s),4.5),-0.2222222222222222);a=o*h*.84,l=t*h*.82,u=s*h*.84;break}case "ghost":{if(t>=0)a=o,l=t,u=s;else {let h=(Math.acos(Math.max(-1,t))-Math.PI/2)/(Math.PI/2),p=Math.hypot(o,s)||1,b=o/p,E=s/p,S=Math.atan2(s,o),k=.085*Math.sin(5*S+2.2*i),C=1.04,A=.13,B=-1.05+k,q;if(h<.45){let m=h/.45;q=1+.04*m,l=-m*(1.05-A)+k*ft(.45,1,m);}else if(h<.75){let m=(h-.45)/.3*(Math.PI/2);q=C-A+A*Math.cos(m),l=B+A-A*Math.sin(m);}else {let m=(h-.75)/.25;q=(C-A)*(1-m),l=B+.22*m*m*(3-2*m);}a=b*q,u=E*q;}a*=.9,l=(l+.1)*.9,u*=.9;break}}r[d]=a,r[d+1]=l,r[d+2]=u;}var I=null,ae=null;function bt(){if(typeof window>"u"||!("AudioContext"in window))return null;if(gt(),!I){I=new AudioContext;let e=I.createBiquadFilter();e.type="lowpass",e.frequency.value=3200,ae=I.createGain(),ae.gain.value=.55,ae.connect(e).connect(I.destination);}return I.state==="suspended"&&I.resume(),I}function F(e,{freq:o,to:t,at:s=0,dur:i=.12,type:r="sine",gain:d=.1,wobble:a=0}){let l=e.currentTime+s,u=e.createOscillator(),n=e.createGain();if(u.type=r,u.frequency.setValueAtTime(o,l),t&&u.frequency.exponentialRampToValueAtTime(t,l+i),a){let h=e.createOscillator(),p=e.createGain();h.frequency.value=7,p.gain.value=a,h.connect(p).connect(u.frequency),h.start(l),h.stop(l+i+.05);}n.gain.setValueAtTime(1e-4,l),n.gain.exponentialRampToValueAtTime(d,l+Math.min(.012,i/4)),n.gain.exponentialRampToValueAtTime(1e-4,l+i),u.connect(n).connect(ae),u.start(l),u.stop(l+i+.02);}var Se={idle:e=>F(e,{freq:520,to:780,dur:.09,gain:.08}),thinking:e=>[660,760,900].forEach((o,t)=>F(e,{freq:o,to:o*1.08,at:t*.1,dur:.07,type:"triangle",gain:.06})),loading:e=>[0,.14,.28].forEach(o=>F(e,{freq:940,at:o,dur:.04,gain:.05})),listening:e=>{F(e,{freq:523,dur:.12,type:"triangle",gain:.07}),F(e,{freq:784,at:.1,dur:.22,type:"triangle",gain:.07});},speaking:e=>[380,460,340,520,420].forEach((o,t)=>F(e,{freq:o,to:o*1.15,at:t*.075,dur:.06,type:"triangle",gain:.06})),happy:e=>[523,659,784,1047].forEach((o,t)=>F(e,{freq:o,at:t*.07,dur:.14,type:"triangle",gain:.07})),surprised:e=>F(e,{freq:280,to:980,dur:.18,gain:.09,wobble:30}),sad:e=>{F(e,{freq:560,to:300,dur:.5,gain:.07}),F(e,{freq:420,to:240,at:.12,dur:.5,gain:.04});},error:e=>[0,.14].forEach(o=>F(e,{freq:170,to:150,at:o,dur:.1,type:"sawtooth",gain:.045})),sleeping:e=>{F(e,{freq:150,to:115,dur:.6,gain:.07,wobble:6}),F(e,{freq:300,to:230,at:.05,dur:.5,gain:.02});},love:e=>[1319,1568,2093,1760].forEach((o,t)=>F(e,{freq:o,at:t*.08,dur:.22,gain:.04}))},Y=null,Xe=false;function gt(){if(Xe)return;Xe=true;let e=()=>{I&&I.resume().then(()=>{Y&&performance.now()<Y.until&&Se[Y.mood](I),Y=null;});};for(let o of ["pointerdown","touchend","keydown"])addEventListener(o,e,{passive:true});}function he(e,o=false){let t=bt();if(t){if(t.state==="running"){Y=null,Se[e](t);return}Y={mood:e,until:performance.now()+(o?6e3:1200)},t.resume().then(()=>{Y&&performance.now()<Y.until&&Se[Y.mood](t),Y=null;});}}var Ee=-0.85,je=new Map;function Tt(e){let o=je.get(e);if(!o){let{geometry:t,dirs:s}=Ve(e);o={dirs:s,index:new Uint32Array(t.getIndex().array)},t.dispose(),je.set(e,o);}return o}var K=(e,o)=>1-Math.exp(-e*o),Te=e=>1-Math.pow(1-e,3),le=class{constructor(){this.scene=new three.Scene;this.camera=new three.PerspectiveCamera(30,1,.1,50);this.group=new three.Group;this.v=new three.Vector3;this.geometry=new three.BufferGeometry;this.faceCanvas=document.createElement("canvas");this.uniforms={uFace:{value:null},uFaceY:{value:0},uFaceScale:{value:1.5}};this.state={...M};this.visible=false;this.connected=false;this.w=0;this.h=0;this.t=Math.random()*100;this.shapeFrom=M.shape;this.shapeTo=M.shape;this.morph=1;this.shapeDirty=true;this.shown=M.mood;this.prevShown=M.mood;this.tau=10;this.prevTau=10;this.blend=1;this.reaction=null;this.pose=J();this.poseIdle=J();this.moodTime=0;this.poseA=J();this.poseB=J();this.face=ge(oe.idle);this.target=ge(oe.idle);this.drawn=new Float32Array(U.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=X(M.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=ee(this.body);this.faceCT=this.faceC;this.cheek=te(this.body);this.cheekT=this.cheek;this.look={x:0,y:0};this.wander={x:0,y:0,next:0};this.squash=0;this.squashV=0;this.pop=.6;this.popV=0;this.hover=0;this.hovering=false;this.overlay={dots:0,zz:0,hearts:0,waves:0,bang:0};this.needsFrame=true;this.ready=false;this.css=[0,0];this.quality=1;this.lowPowerApplied=false;this.cost=0;this.costFrames=0;this.lastFrame=[];this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d"),this.faceCtx=this.faceCanvas.getContext("2d"),this.faceCanvas.width=this.faceCanvas.height=256,this.faceTex=new three.CanvasTexture(this.faceCanvas),this.faceTex.colorSpace=three.SRGBColorSpace,this.faceTex.minFilter=three.LinearMipmapLinearFilter,this.faceTex.anisotropy=4,this.uniforms.uFace.value=this.faceTex,this.setSegments(24),this.material=this.createMaterial(),this.mesh=new three.Mesh(this.geometry,this.material),this.mesh.frustumCulled=false,this.mesh.position.y=-Ee,this.group.add(this.mesh),this.scene.add(this.group),this.scene.add(new three.HemisphereLight(16774120,7234140,1.7));let o=new three.DirectionalLight(16777215,1.9);o.position.set(2.5,3.5,4);let t=new three.DirectionalLight(16770770,1.4);t.position.set(-3.5,2,-3);let s=new three.DirectionalLight(16773350,.45);s.position.set(-2,-1,3),this.scene.add(o,t,s),this.camera.position.set(0,.3,6.6),this.camera.lookAt(0,.12,0);}get active(){return this.visible&&this.connected&&this.w>0}createMaterial(){let o=new three.MeshStandardMaterial({roughness:.62,metalness:0});return o.color.setRGB(...this.body,three.SRGBColorSpace),o.customProgramCacheKey=()=>"moodi-1",o.onBeforeCompile=t=>{Object.assign(t.uniforms,this.uniforms),t.vertexShader=t.vertexShader.replace("#include <common>",`#include <common>
2
2
  varying vec3 vFaceP;
3
3
  varying vec3 vFaceN;`).replace("#include <begin_vertex>",`#include <begin_vertex>
4
4
  vFaceP = position;
@@ -20,9 +20,9 @@ outgoingLight *= mix(0.62, 1.0, smoothstep(-0.95, 0.35, normalize(vFaceN).y));
20
20
  // eyes and mouth glow a little: mostly unlit, so they read as light, not paint
21
21
  float emis = smoothstep(0.75, 1.0, fc.a) * inb * smoothstep(0.12, 0.45, normalize(vFaceN).z);
22
22
  outgoingLight = mix(outgoingLight, diffuseColor.rgb * 1.12, emis * 0.65);
23
- #include <opaque_fragment>`);},o}setSegments(o){let t=Tt(o);this.dirs!==t.dirs&&(this.dirs=t.dirs,this.geometry.dispose(),this.geometry=new three.BufferGeometry,this.geometry.setAttribute("position",new three.BufferAttribute(new Float32Array(t.dirs.length),3)),this.geometry.setIndex(new three.BufferAttribute(t.index,1)),this.mesh&&(this.mesh.geometry=this.geometry),this.shapeDirty=true);}connect(){this.connected=true,We(this);}disconnect(){this.connected=false,De(this);}setVisible(o){this.visible=o,o&&_();}resize(o,t){this.css=[o,t];let s=(z.lowPower?1:Math.min(2,devicePixelRatio||1))*this.quality,i=Math.max(1,Math.round(o*s)),r=Math.max(1,Math.round(t*s));if(i===this.w&&r===this.h)return;this.w=this.canvas.width=i,this.h=this.canvas.height=r;let h=Math.min(i,r),c=Math.min(o,t);this.camera.position.set(0,.3,c<90?5.1:6.1),this.camera.lookAt(0,c<90?.02:.12,0),this.camera.aspect=i/r,this.camera.updateProjectionMatrix(),this.setSegments(h<160?16:h<400?24:32);let l=h<220?256:512;this.faceCanvas.width!==l&&(this.faceCanvas.width=this.faceCanvas.height=l,this.faceTex.dispose(),this.drawn.fill(NaN)),this.needsFrame=true,_();}update(o){let t=this.state;this.state={...t,...o};let s=this.state;s.shape!==this.shapeTo&&(this.shapeFrom=this.shapeTo,this.shapeTo=s.shape,this.morph=0,z.reducedMotion||(this.squashV-=2.2)),(s.color!==t.color||s.faceColor!==t.faceColor||s.cheekColor!==t.cheekColor)&&(this.bodyT=X(s.color,X(M.color,this.bodyT)),this.faceCT=s.faceColor?X(s.faceColor,Q(this.bodyT)):Q(this.bodyT),this.cheekT=s.cheekColor?X(s.cheekColor,ee(this.bodyT)):ee(this.bodyT)),s.mood!==t.mood&&!this.reaction&&this.show(s.mood),s.sound&&!t.sound&&this.ready&&ae(this.shown),this.needsFrame=true,_();}bump(o=1){z.reducedMotion||(this.squashV-=1.6*o,this.needsFrame=true,_());}react(o,t=1200){this.reaction={mood:o,left:t/1e3},this.show(o);}get currentMood(){return this.shown}dispose(){this.disconnect(),this.geometry.dispose(),this.material.dispose(),this.faceTex.dispose(),this.onMoodChange=void 0;}show(o,t=true){if(o===this.shown)return;t&&this.state.sound&&this.ready&&ae(o);let s=this.shown;this.prevShown=s,this.prevTau=this.tau,this.shown=o,this.tau=0,this.blend=0,z.reducedMotion||(this.squashV-=1.1),this.onMoodChange?.({mood:o,previous:s,reaction:!!this.reaction});}tick(o,t){let s=this.state,i=z.reducedMotion,r=z.lowPower?0:o*Math.max(0,s.speed);this.t+=r,this.tau+=r,this.prevTau+=r,this.reaction&&(this.reaction.left-=o,this.reaction.left<=0&&(this.reaction=null,this.show(s.mood,false))),ye(this.shown,this.t,this.tau,this.poseB),this.blend<1?(this.blend=Math.min(1,this.blend+o/.45),ye(this.prevShown,this.t,this.prevTau,this.poseA),Oe(this.poseA,this.poseB,je(this.blend),this.pose)):Object.assign(this.pose,this.poseB);let h=this.pose;i&&(h.y=h.rx=h.ry=h.rz=h.st=h.lookX=0);let c=s.followCursor&&!i&&s.speed>0?Fe[this.shown]:0,l=0,p=0;if(c>0&&D.known){let d=this.canvas.getBoundingClientRect(),y=Math.max(200,d.width*.85);l=Math.max(-1,Math.min(1,(D.x-(d.left+d.width/2))/y)),p=Math.max(-1,Math.min(1,(D.y-(d.top+d.height*.45))/y)),l*=c,p*=c;}else if(this.shown==="idle"&&!i&&s.speed>0){if(this.t>this.wander.next){let d=Math.random()<.4;this.wander.x=d?0:(Math.random()*2-1)*.6,this.wander.y=d?0:(Math.random()*2-1)*.35,this.wander.next=this.t+1.8+Math.random()*2.6;}l=this.wander.x,p=this.wander.y;}let n=K(7,o);this.look.x+=(l-this.look.x)*n,this.look.y+=(p-this.look.y)*n;let a=te[this.shown],u=this.target;for(let d of U)u[d]=a[d];u.lookX+=this.look.x+h.lookX,u.lookY+=this.look.y+h.lookY,u.mouthOpen+=h.mouth,u.eyeOpen+=this.hover*.08;let w=K(11,o);for(let d of U)this.face[d]+=(u[d]-this.face[d])*w;let A=K(16,o);if(this.face.lookX+=(u.lookX-this.face.lookX)*A,this.face.lookY+=(u.lookY-this.face.lookY)*A,this.face.mouthOpen+=(u.mouthOpen-this.face.mouthOpen)*A,this.nextBlink-=r,this.nextBlink<=0&&this.blinkT<0&&(this.blinkT=0,this.nextBlink=Math.random()<.2?.25:2.2+Math.random()*3.8),this.blinkT>=0){this.blinkT+=r;let d=this.blinkT/.16;this.blink=d>=1?1:1-Math.sin(Math.PI*d),d>=1&&(this.blinkT=-1);}let k=Math.min(o,1/30);this.squashV+=(-170*this.squash-11*this.squashV)*k,this.squash+=this.squashV*k,i&&(this.squash=this.squashV=0),i?this.pop=1:(this.popV+=(-140*(this.pop-1)-13*this.popV)*k,this.pop+=this.popV*k),this.hover+=((this.hovering&&s.interactive?1:0)-this.hover)*K(12,o);let S=K(9,o);this.body=x(this.body,this.bodyT,S),this.faceC=x(this.faceC,this.faceCT,S),this.cheek=x(this.cheek,this.cheekT,S),this.material.color.setRGB(this.body[0],this.body[1],this.body[2],three.SRGBColorSpace);let E=Be[this.shown],C=K(6,o);for(let d of Object.keys(this.overlay))this.overlay[d]+=((E===d?1:0)-this.overlay[d])*C;this.morph<1&&(this.morph=Math.min(1,this.morph+o/(i?.2:.38)),this.shapeDirty=true);let O=J[this.shapeTo].animated||this.morph<1&&J[this.shapeFrom].animated;(this.shapeDirty||O&&r>0)&&this.rebuild();let B=this.group,m=(1+h.st+this.squash)*(1+this.hover*.04),f=1/Math.sqrt(Math.max(.3,1+h.st+this.squash))*(1+this.hover*.04);B.position.y=Ae+h.y,B.rotation.set(h.rx+this.look.y*.22*(c>0?1:0),h.ry+this.look.x*.42,h.rz),B.scale.set(f*this.pop,m*this.pop,f*this.pop);let g=this.drawFaceIfNeeded();if((s.speed===0||z.lowPower)&&!g&&!this.needsFrame&&this.settled())return;let b=performance.now();this.render(h)&&(this.needsFrame=false,this.ready||(this.ready=true,this.state.sound&&ae(this.shown,true),this.onReady?.(),this.onReady=void 0),this.adapt(performance.now()-b));}adapt(o){this.cost=this.costFrames?this.cost*.9+o*.1:o,!(++this.costFrames<20||this.cost<9||this.quality<=.5)&&(this.quality=Math.max(.5,this.quality-.25),this.costFrames=0,this.resize(this.css[0],this.css[1]));}settled(){let o=this.group,t=this.overlay,s=[t.bang,o.position.y,o.rotation.x,o.rotation.y,o.rotation.z,o.scale.x,o.scale.y,...this.body,this.morph,t.dots,t.zz,t.hearts,t.waves],i=this.lastFrame,r=i.length===s.length;for(let h=0;r&&h<s.length;h++)r=Math.abs(s[h]-i[h])<1e-4;return this.lastFrame=s,r}rebuild(){let o=this.geometry.getAttribute("position"),t=o.array,s=this.dirs,i=je(this.morph),r=this.shapeFrom,h=this.shapeTo,c=new Float32Array(3);for(let n=0;n<s.length;n+=3)we(h,s[n],s[n+1],s[n+2],this.t,t,n),i<1&&(we(r,s[n],s[n+1],s[n+2],this.t,c,0),t[n]=c[0]+(t[n]-c[0])*i,t[n+1]=c[1]+(t[n+1]-c[1])*i,t[n+2]=c[2]+(t[n+2]-c[2])*i);o.needsUpdate=true,this.geometry.computeVertexNormals();let l=J[r],p=J[h];this.uniforms.uFaceY.value=l.faceY+(p.faceY-l.faceY)*i,this.uniforms.uFaceScale.value=l.faceScale+(p.faceScale-l.faceScale)*i,this.shapeDirty=false;}drawFaceIfNeeded(){let o=this.face,t=this.drawn,s=false,i=0;for(let c of U)Math.abs(t[i]-o[c])<.002||(s=true,t[i]=o[c]),i++;let r=[this.blink,...this.faceC,...this.cheek];for(let c of r)Math.abs(t[i]-c)<.002||(s=true,t[i]=c),i++;if(!s)return false;let h=pe(this.faceC)>.45;return ze(this.faceCtx,o,this.blink,G(this.faceC),G(this.cheek),h?1:.3,G(x(this.faceC,[1,1,1],h?.6:.16)),h?G(x(this.body,[.04,.03,.06],.86)):"#fff",G(h?x(this.body,[.03,.02,.05],.78):x(this.faceC,[0,0,0],.35)),G(this.faceC)),this.faceTex.needsUpdate=true,true}project(o,t,s){let i=this.v.set(o,t,s).applyMatrix4(this.group.matrixWorld).project(this.camera);return [(i.x*.5+.5)*this.w,(-i.y*.5+.5)*this.h]}render(o){let{w:t,h:s,ctx:i}=this,r=qe(t,s);if(z.lowPower&&!this.lowPowerApplied){this.lowPowerApplied=true;let[h,c]=this.css;return this.w=this.h=0,this.resize(h,c),false}if(!Ye(this.scene,this.camera))return false;if(i.clearRect(0,0,t,s),this.group.updateMatrixWorld(),this.state.shadow){let h=this.v.set(0,Ae-.08,0).project(this.camera),c=(h.x*.5+.5)*t,l=(-h.y*.5+.5)*s,p=Math.max(0,o.y)*3,n=Math.min(t,s)*.26*(1-p*.25)*this.pop,a=i.createRadialGradient(c,l,0,c,l,n),u=x(this.body,[.05,.03,.02],.75);a.addColorStop(0,`rgba(${u[0]*255|0},${u[1]*255|0},${u[2]*255|0},${.42*(1-p*.4)})`),a.addColorStop(1,"rgba(0,0,0,0)"),i.fillStyle=a,i.save(),i.translate(c,l),i.scale(1,.22),i.translate(-c,-l),i.beginPath(),i.arc(c,l,n,0,Math.PI*2),i.fill(),i.restore();}return r.setViewport(0,0,t,s),r.setScissor(0,0,t,s),r.render(this.scene,this.camera),i.drawImage(r.domElement,0,Ie()-s,t,s,0,0,t,s),this.drawOverlays(),true}drawOverlays(){let o=this.overlay;if(o.dots+o.zz+o.hearts+o.waves+o.bang<.01)return;let{ctx:t}=this,s=this.t,i=z.reducedMotion,r=Math.min(this.w,this.h),h=x(this.body,[1,1,1],.22),c=G(h);if(t.save(),o.dots>.01){let[l,p]=this.project(.6,.92,.2),n=i?2:s*.55%1.6,a=[.014,.022,.052];for(let u=0;u<3;u++){let w=i?1:Pe(Ee((n-u*.18)/.32)),A=i?0:Math.sin(s*2.2+u)*r*.006,k=l+[0,.05,.12][u]*r,S=p-[0,.05,.12][u]*r+A,E=a[u]*r*w;if(!(E<=.2)&&(t.globalAlpha=o.dots*.95,t.fillStyle=c,t.beginPath(),u<2?t.arc(k,S,E,0,Math.PI*2):Pt(t,k,S,E),t.fill(),u===2&&w>.6)){t.fillStyle=G(x(this.body,[0,0,0],.35));for(let C=0;C<3;C++){let O=i?0:Math.max(0,Math.sin(s*7-C*.9))*E*.16;t.beginPath(),t.arc(k+(C-1)*E*.34,S-O,E*.1,0,Math.PI*2),t.fill();}}}}if(o.zz>.01){let[l,p]=this.project(.45,.85,.2);t.textAlign="center",t.textBaseline="middle",t.fillStyle=c;for(let n=0;n<3;n++){let a=i?.25+n*.25:(s*.33+n/3)%1,u=l+a*r*.17+Math.sin(a*7+n)*r*.018,w=p-Te(a)*r*.26;t.globalAlpha=o.zz*Math.min(1,a*5)*(1-a)*1.3,t.save(),t.translate(u,w),t.rotate(-0.25+Math.sin(a*5+n)*.22),t.font=`800 ${Math.round(r*(.05+.06*a))}px ui-rounded, system-ui, sans-serif`,t.fillText("z",0,0),t.restore();}}if(o.hearts>.01){let[l,p]=this.project(0,.8,.2),n=x(this.cheek,[1,.3,.45],.55);for(let a=0;a<4;a++){let u=i?.4:(s*.3+a/4)%1,w=a%2?1:-1,A=l+w*r*(.16+.07*(a>>1))+Math.sin(u*6+a)*r*.025,k=p-r*.02-Te(u)*r*.28,S=i?1:Pe(Ee(u/.22)),E=r*(.026+.008*(a%3))*S;t.globalAlpha=o.hearts*(1-Math.max(0,(u-.6)/.4)),t.save(),t.translate(A,k),t.rotate(Math.sin(s*3+a)*.25);let C=t.createLinearGradient(0,-E,0,E);C.addColorStop(0,G(x(n,[1,1,1],.35))),C.addColorStop(1,G(n)),t.fillStyle=C,ge(t,0,0,E),t.fill(),t.restore();}}if(o.waves>.01){t.lineCap="round",t.strokeStyle=c;for(let l of [-1,1]){let[p,n]=this.project(l*1,.12,0);for(let a=0;a<3;a++){let u=i?.3+a*.2:(s*.8+a/3)%1,w=Te(u);t.globalAlpha=o.waves*Math.sin(Math.PI*u)*.9,t.lineWidth=r*(.016-.008*u),t.beginPath();let A=r*(.03+.11*w);l>0?t.arc(p-r*.03,n,A,-0.55,.55):t.arc(p+r*.03,n,A,Math.PI-.55,Math.PI+.55),t.stroke();}}}if(o.bang>.01){let[l,p]=this.project(.55,1,.2),n=i?1:Pe(Ee(this.tau/.35));t.globalAlpha=o.bang,t.fillStyle=c,t.save(),t.translate(l,p),t.rotate(.18),t.scale(n,n),t.beginPath(),t.roundRect(-r*.016,-r*.1,r*.032,r*.07,r*.016),t.arc(0,r*.005,r*.018,0,Math.PI*2),t.fill(),t.restore();}t.restore();}},Ee=e=>e<0?0:e>1?1:e,Te=e=>1-(1-e)*(1-e),Pe=e=>1+(1.9+1)*Math.pow(e-1,3)+1.9*Math.pow(e-1,2);function Pt(e,o,t,s){e.ellipse(o,t,s,s*.72,0,0,Math.PI*2),e.moveTo(o-s*.35+s*.45,t-s*.38),e.arc(o-s*.35,t-s*.38,s*.45,0,Math.PI*2),e.moveTo(o+s*.3+s*.5,t-s*.32),e.arc(o+s*.3,t-s*.32,s*.5,0,Math.PI*2);}var xt=["shape","mood","color","face-color","cheek-color","size","speed","follow-cursor","interactive","shadow","sound"],N=(e,o)=>e===null?o:e!=="false"&&e!=="0",Rt=e=>e==null||e===""?`${M.size}px`:typeof e=="number"||/^\d+(\.\d+)?$/.test(String(e))?`${e}px`:String(e),Ft=`:host{display:inline-block;position:relative;cursor:pointer;vertical-align:middle;contain:layout paint style;-webkit-tap-highlight-color:transparent}
23
+ #include <opaque_fragment>`);},o}setSegments(o){let t=Tt(o);this.dirs!==t.dirs&&(this.dirs=t.dirs,this.geometry.dispose(),this.geometry=new three.BufferGeometry,this.geometry.setAttribute("position",new three.BufferAttribute(new Float32Array(t.dirs.length),3)),this.geometry.setIndex(new three.BufferAttribute(t.index,1)),this.mesh&&(this.mesh.geometry=this.geometry),this.shapeDirty=true);}connect(){this.connected=true,De(this);}disconnect(){this.connected=false,_e(this);}setVisible(o){this.visible=o,o&&_();}resize(o,t){this.css=[o,t];let s=(z.lowPower?1:Math.min(2,devicePixelRatio||1))*this.quality,i=Math.max(1,Math.round(o*s)),r=Math.max(1,Math.round(t*s));if(i===this.w&&r===this.h)return;this.w=this.canvas.width=i,this.h=this.canvas.height=r;let d=Math.min(i,r),a=Math.min(o,t);this.camera.position.set(0,.3,a<90?5.1:6.1),this.camera.lookAt(0,a<90?.02:.12,0),this.camera.aspect=i/r,this.camera.updateProjectionMatrix(),this.setSegments(d<160?16:d<400?24:32);let l=d<220?256:512;this.faceCanvas.width!==l&&(this.faceCanvas.width=this.faceCanvas.height=l,this.faceTex.dispose(),this.drawn.fill(NaN)),this.needsFrame=true,_();}update(o){let t=this.state;this.state={...t,...o};let s=this.state;s.shape!==this.shapeTo&&(this.shapeFrom=this.shapeTo,this.shapeTo=s.shape,this.morph=0,z.reducedMotion||(this.squashV-=2.2)),(s.color!==t.color||s.faceColor!==t.faceColor||s.cheekColor!==t.cheekColor)&&(this.bodyT=X(s.color,X(M.color,this.bodyT)),this.faceCT=s.faceColor?X(s.faceColor,ee(this.bodyT)):ee(this.bodyT),this.cheekT=s.cheekColor?X(s.cheekColor,te(this.bodyT)):te(this.bodyT)),s.mood!==t.mood&&!this.reaction&&this.show(s.mood),s.sound&&!t.sound&&this.ready&&he(this.shown),this.needsFrame=true,_();}bump(o=1){z.reducedMotion||(this.squashV-=1.6*o,this.needsFrame=true,_());}react(o,t=1200){this.reaction={mood:o,left:t/1e3},this.show(o);}get currentMood(){return this.shown}dispose(){this.disconnect(),this.geometry.dispose(),this.material.dispose(),this.faceTex.dispose(),this.onMoodChange=void 0;}show(o,t=true){if(o===this.shown)return;t&&this.state.sound&&this.ready&&he(o);let s=this.shown;this.prevShown=s,this.prevTau=this.tau,this.shown=o,this.tau=0,this.moodTime=0,this.blend=0,z.reducedMotion||(this.squashV-=1.1),this.onMoodChange?.({mood:o,previous:s,reaction:!!this.reaction});}tick(o,t){let s=this.state,i=z.reducedMotion,r=z.lowPower?0:o*Math.max(0,s.speed);this.t+=r,this.tau+=r,this.prevTau+=r,this.reaction&&(this.reaction.left-=o,this.reaction.left<=0&&(this.reaction=null,this.show(s.mood,false))),this.moodTime+=o;let d=s.settle>0&&this.shown!=="idle"?Te(Math.min(1,Math.max(0,(this.moodTime-s.settle/1e3)/.7))):0;ne(this.shown,this.t,this.tau,this.poseB),d>0&&ve(this.poseB,ne("idle",this.t,this.tau,this.poseIdle),d,this.poseB),this.blend<1?(this.blend=Math.min(1,this.blend+o/.45),ne(this.prevShown,this.t,this.prevTau,this.poseA),ve(this.poseA,this.poseB,Te(this.blend),this.pose)):Object.assign(this.pose,this.poseB);let a=this.pose;i&&(a.y=a.rx=a.ry=a.rz=a.st=a.lookX=0);let l=s.followCursor&&!i&&s.speed>0?Be[this.shown]:0,u=0,n=0;if(l>0&&D.known){let c=this.canvas.getBoundingClientRect(),T=Math.max(200,c.width*.85);u=Math.max(-1,Math.min(1,(D.x-(c.left+c.width/2))/T)),n=Math.max(-1,Math.min(1,(D.y-(c.top+c.height*.45))/T)),u*=l,n*=l;}else if(this.shown==="idle"&&!i&&s.speed>0){if(this.t>this.wander.next){let c=Math.random()<.4;this.wander.x=c?0:(Math.random()*2-1)*.6,this.wander.y=c?0:(Math.random()*2-1)*.35,this.wander.next=this.t+1.8+Math.random()*2.6;}u=this.wander.x,n=this.wander.y;}let h=K(7,o);this.look.x+=(u-this.look.x)*h,this.look.y+=(n-this.look.y)*h;let p=oe[this.shown],b=this.target;for(let c of U)b[c]=p[c];b.lookX+=this.look.x+a.lookX,b.lookY+=this.look.y+a.lookY,b.mouthOpen+=a.mouth,b.eyeOpen+=this.hover*.08;let E=K(11,o);for(let c of U)this.face[c]+=(b[c]-this.face[c])*E;let S=K(16,o);if(this.face.lookX+=(b.lookX-this.face.lookX)*S,this.face.lookY+=(b.lookY-this.face.lookY)*S,this.face.mouthOpen+=(b.mouthOpen-this.face.mouthOpen)*S,this.nextBlink-=r,this.nextBlink<=0&&this.blinkT<0&&(this.blinkT=0,this.nextBlink=Math.random()<.2?.25:2.2+Math.random()*3.8),this.blinkT>=0){this.blinkT+=r;let c=this.blinkT/.16;this.blink=c>=1?1:1-Math.sin(Math.PI*c),c>=1&&(this.blinkT=-1);}let k=Math.min(o,1/30);this.squashV+=(-170*this.squash-11*this.squashV)*k,this.squash+=this.squashV*k,i&&(this.squash=this.squashV=0),i?this.pop=1:(this.popV+=(-140*(this.pop-1)-13*this.popV)*k,this.pop+=this.popV*k),this.hover+=((this.hovering&&s.interactive?1:0)-this.hover)*K(12,o);let C=K(9,o);this.body=x(this.body,this.bodyT,C),this.faceC=x(this.faceC,this.faceCT,C),this.cheek=x(this.cheek,this.cheekT,C),this.material.color.setRGB(this.body[0],this.body[1],this.body[2],three.SRGBColorSpace);let A=Le[this.shown],B=K(6,o);for(let c of Object.keys(this.overlay))this.overlay[c]+=((A===c?1-d:0)-this.overlay[c])*B;this.morph<1&&(this.morph=Math.min(1,this.morph+o/(i?.2:.38)),this.shapeDirty=true);let q=Z[this.shapeTo].animated||this.morph<1&&Z[this.shapeFrom].animated;(this.shapeDirty||q&&r>0)&&this.rebuild();let m=this.group,g=(1+a.st+this.squash)*(1+this.hover*.04),y=1/Math.sqrt(Math.max(.3,1+a.st+this.squash))*(1+this.hover*.04);m.position.y=Ee+a.y,m.rotation.set(a.rx+this.look.y*.22*(l>0?1:0),a.ry+this.look.x*.42,a.rz),m.scale.set(y*this.pop,g*this.pop,y*this.pop);let v=this.drawFaceIfNeeded();if((s.speed===0||z.lowPower)&&!v&&!this.needsFrame&&this.settled())return;let f=performance.now();this.render(a)&&(this.needsFrame=false,this.ready||(this.ready=true,this.state.sound&&he(this.shown,true),this.onReady?.(),this.onReady=void 0),this.adapt(performance.now()-f));}adapt(o){this.cost=this.costFrames?this.cost*.9+o*.1:o,!(++this.costFrames<20||this.cost<9||this.quality<=.5)&&(this.quality=Math.max(.5,this.quality-.25),this.costFrames=0,this.resize(this.css[0],this.css[1]));}settled(){let o=this.group,t=this.overlay,s=[t.bang,o.position.y,o.rotation.x,o.rotation.y,o.rotation.z,o.scale.x,o.scale.y,...this.body,this.morph,t.dots,t.zz,t.hearts,t.waves],i=this.lastFrame,r=i.length===s.length;for(let d=0;r&&d<s.length;d++)r=Math.abs(s[d]-i[d])<1e-4;return this.lastFrame=s,r}rebuild(){let o=this.geometry.getAttribute("position"),t=o.array,s=this.dirs,i=Te(this.morph),r=this.shapeFrom,d=this.shapeTo,a=new Float32Array(3);for(let n=0;n<s.length;n+=3)ke(d,s[n],s[n+1],s[n+2],this.t,t,n),i<1&&(ke(r,s[n],s[n+1],s[n+2],this.t,a,0),t[n]=a[0]+(t[n]-a[0])*i,t[n+1]=a[1]+(t[n+1]-a[1])*i,t[n+2]=a[2]+(t[n+2]-a[2])*i);o.needsUpdate=true,this.geometry.computeVertexNormals();let l=Z[r],u=Z[d];this.uniforms.uFaceY.value=l.faceY+(u.faceY-l.faceY)*i,this.uniforms.uFaceScale.value=l.faceScale+(u.faceScale-l.faceScale)*i,this.shapeDirty=false;}drawFaceIfNeeded(){let o=this.face,t=this.drawn,s=false,i=0;for(let a of U)Math.abs(t[i]-o[a])<.002||(s=true,t[i]=o[a]),i++;let r=[this.blink,...this.faceC,...this.cheek];for(let a of r)Math.abs(t[i]-a)<.002||(s=true,t[i]=a),i++;if(!s)return false;let d=me(this.faceC)>.45;return Oe(this.faceCtx,o,this.blink,L(this.faceC),L(this.cheek),d?1:.3,L(x(this.faceC,[1,1,1],d?.6:.16)),d?L(x(this.body,[.04,.03,.06],.86)):"#fff",L(d?x(this.body,[.03,.02,.05],.78):x(this.faceC,[0,0,0],.35)),L(this.faceC)),this.faceTex.needsUpdate=true,true}project(o,t,s){let i=this.v.set(o,t,s).applyMatrix4(this.group.matrixWorld).project(this.camera);return [(i.x*.5+.5)*this.w,(-i.y*.5+.5)*this.h]}render(o){let{w:t,h:s,ctx:i}=this,r=qe(t,s);if(z.lowPower&&!this.lowPowerApplied){this.lowPowerApplied=true;let[d,a]=this.css;return this.w=this.h=0,this.resize(d,a),false}if(!We(this.scene,this.camera))return false;if(i.clearRect(0,0,t,s),this.group.updateMatrixWorld(),this.state.shadow){let d=this.v.set(0,Ee-.08,0).project(this.camera),a=(d.x*.5+.5)*t,l=(-d.y*.5+.5)*s,u=Math.max(0,o.y)*3,n=Math.min(t,s)*.26*(1-u*.25)*this.pop,h=i.createRadialGradient(a,l,0,a,l,n),p=x(this.body,[.05,.03,.02],.75);h.addColorStop(0,`rgba(${p[0]*255|0},${p[1]*255|0},${p[2]*255|0},${.42*(1-u*.4)})`),h.addColorStop(1,"rgba(0,0,0,0)"),i.fillStyle=h,i.save(),i.translate(a,l),i.scale(1,.22),i.translate(-a,-l),i.beginPath(),i.arc(a,l,n,0,Math.PI*2),i.fill(),i.restore();}return r.setViewport(0,0,t,s),r.setScissor(0,0,t,s),r.render(this.scene,this.camera),i.drawImage(r.domElement,0,Ye()-s,t,s,0,0,t,s),this.drawOverlays(),true}drawOverlays(){let o=this.overlay;if(o.dots+o.zz+o.hearts+o.waves+o.bang<.01)return;let{ctx:t}=this,s=this.t,i=z.reducedMotion,r=Math.min(this.w,this.h),d=x(this.body,[1,1,1],.22),a=L(d);if(t.save(),o.dots>.01){let[l,u]=this.project(.6,.92,.2),n=i?2:s*.55%1.6,h=[.014,.022,.052];for(let p=0;p<3;p++){let b=i?1:Re(Pe((n-p*.18)/.32)),E=i?0:Math.sin(s*2.2+p)*r*.006,S=l+[0,.05,.12][p]*r,k=u-[0,.05,.12][p]*r+E,C=h[p]*r*b;if(!(C<=.2)&&(t.globalAlpha=o.dots*.95,t.fillStyle=a,t.beginPath(),p<2?t.arc(S,k,C,0,Math.PI*2):Pt(t,S,k,C),t.fill(),p===2&&b>.6)){t.fillStyle=L(x(this.body,[0,0,0],.35));for(let A=0;A<3;A++){let B=i?0:Math.max(0,Math.sin(s*7-A*.9))*C*.16;t.beginPath(),t.arc(S+(A-1)*C*.34,k-B,C*.1,0,Math.PI*2),t.fill();}}}}if(o.zz>.01){let[l,u]=this.project(.45,.85,.2);t.textAlign="center",t.textBaseline="middle",t.fillStyle=a;for(let n=0;n<3;n++){let h=i?.25+n*.25:(s*.33+n/3)%1,p=l+h*r*.17+Math.sin(h*7+n)*r*.018,b=u-xe(h)*r*.26;t.globalAlpha=o.zz*Math.min(1,h*5)*(1-h)*1.3,t.save(),t.translate(p,b),t.rotate(-0.25+Math.sin(h*5+n)*.22),t.font=`800 ${Math.round(r*(.05+.06*h))}px ui-rounded, system-ui, sans-serif`,t.fillText("z",0,0),t.restore();}}if(o.hearts>.01){let[l,u]=this.project(0,.8,.2),n=x(this.cheek,[1,.3,.45],.55);for(let h=0;h<4;h++){let p=i?.4:(s*.3+h/4)%1,b=h%2?1:-1,E=l+b*r*(.16+.07*(h>>1))+Math.sin(p*6+h)*r*.025,S=u-r*.02-xe(p)*r*.28,k=i?1:Re(Pe(p/.22)),C=r*(.026+.008*(h%3))*k;t.globalAlpha=o.hearts*(1-Math.max(0,(p-.6)/.4)),t.save(),t.translate(E,S),t.rotate(Math.sin(s*3+h)*.25);let A=t.createLinearGradient(0,-C,0,C);A.addColorStop(0,L(x(n,[1,1,1],.35))),A.addColorStop(1,L(n)),t.fillStyle=A,ye(t,0,0,C),t.fill(),t.restore();}}if(o.waves>.01){t.lineCap="round",t.strokeStyle=a;for(let l of [-1,1]){let[u,n]=this.project(l*1,.12,0);for(let h=0;h<3;h++){let p=i?.3+h*.2:(s*.8+h/3)%1,b=xe(p);t.globalAlpha=o.waves*Math.sin(Math.PI*p)*.9,t.lineWidth=r*(.016-.008*p),t.beginPath();let E=r*(.03+.11*b);l>0?t.arc(u-r*.03,n,E,-0.55,.55):t.arc(u+r*.03,n,E,Math.PI-.55,Math.PI+.55),t.stroke();}}}if(o.bang>.01){let[l,u]=this.project(.55,1,.2),n=i?1:Re(Pe(this.tau/.35));t.globalAlpha=o.bang,t.fillStyle=a,t.save(),t.translate(l,u),t.rotate(.18),t.scale(n,n),t.beginPath(),t.roundRect(-r*.016,-r*.1,r*.032,r*.07,r*.016),t.arc(0,r*.005,r*.018,0,Math.PI*2),t.fill(),t.restore();}t.restore();}},Pe=e=>e<0?0:e>1?1:e,xe=e=>1-(1-e)*(1-e),Re=e=>1+(1.9+1)*Math.pow(e-1,3)+1.9*Math.pow(e-1,2);function Pt(e,o,t,s){e.ellipse(o,t,s,s*.72,0,0,Math.PI*2),e.moveTo(o-s*.35+s*.45,t-s*.38),e.arc(o-s*.35,t-s*.38,s*.45,0,Math.PI*2),e.moveTo(o+s*.3+s*.5,t-s*.32),e.arc(o+s*.3,t-s*.32,s*.5,0,Math.PI*2);}var xt=["shape","mood","color","face-color","cheek-color","size","speed","follow-cursor","interactive","shadow","sound","settle"],N=(e,o)=>e===null?o:e!=="false"&&e!=="0",Rt=e=>e==null||e===""?`${M.size}px`:typeof e=="number"||/^\d+(\.\d+)?$/.test(String(e))?`${e}px`:String(e),Ft=`:host{display:inline-block;position:relative;cursor:pointer;vertical-align:middle;contain:layout paint style;-webkit-tap-highlight-color:transparent}
24
24
  :host([interactive="false"]){pointer-events:none}
25
25
  canvas{position:absolute;inset:0;display:block;width:100%;height:100%}
26
26
  slot{display:block;width:100%;height:100%}
27
27
  ::slotted(img){display:block;width:100%;height:100%;object-fit:contain}
28
- :host([ready]) slot{display:none}`,zt=typeof HTMLElement>"u"?class{}:HTMLElement,Z=class extends zt{constructor(){super();this.buddy=null;this.sizeStyle=document.createElement("style");this.onEnter=()=>{this.buddy&&(this.buddy.hovering=true);};this.onLeave=()=>{this.buddy&&(this.buddy.hovering=false);};this.onClick=()=>{this.buddy?.state.interactive&&this.react(Math.random()<.5?"happy":"surprised",{duration:1100});};this.root=this.attachShadow({mode:"open"});let t=document.createElement("style");t.textContent=Ft,this.root.append(t,document.createElement("slot")),this.root.append(this.sizeStyle),this.applySize(this.getAttribute("size"));}connectedCallback(){if(this.hasAttribute("role")||this.setAttribute("role","img"),!this.buddy){let t=this.buddy=new he;t.onMoodChange=s=>{this.syncLabel(s.mood),this.dispatchEvent(new CustomEvent("moodchange",{detail:s,bubbles:true}));},t.onReady=()=>{this.setAttribute("ready",""),this.dispatchEvent(new Event("ready"));},this.root.append(t.canvas),t.update(this.readState()),this.addEventListener("pointerenter",this.onEnter),this.addEventListener("pointerleave",this.onLeave),this.addEventListener("click",this.onClick);}this.syncLabel(this.buddy.currentMood),this.buddy.connect(),this.io=new IntersectionObserver(([t])=>this.buddy?.setVisible(t.isIntersecting),{rootMargin:"64px"}),this.io.observe(this),this.ro=new ResizeObserver(([t])=>{let s=t.contentRect;this.buddy?.resize(s.width,s.height);}),this.ro.observe(this);}disconnectedCallback(){this.io?.disconnect(),this.ro?.disconnect(),this.buddy&&(this.buddy.canvas.remove(),this.buddy.dispose(),this.buddy=null),this.removeEventListener("pointerenter",this.onEnter),this.removeEventListener("pointerleave",this.onLeave),this.removeEventListener("click",this.onClick);}attributeChangedCallback(t,s,i){s!==i&&(t==="size"&&(this.applySize(i),s!==null&&this.buddy?.bump()),t!=="aria-label"&&(this.buddy?.update(this.readState()),t==="mood"&&this.buddy&&this.syncLabel(this.buddy.currentMood)));}applySize(t){let s=Rt(t);this.sizeStyle.textContent=`:host{width:${s};height:${s}}`;}readState(){let t=this.getAttribute("shape"),s=this.getAttribute("mood"),i=parseFloat(this.getAttribute("speed")??"");return {shape:ce(t)?t:M.shape,mood:de(s)?s:M.mood,color:this.getAttribute("color")||M.color,faceColor:this.getAttribute("face-color")||void 0,cheekColor:this.getAttribute("cheek-color")||void 0,speed:Number.isFinite(i)?Math.max(0,i):M.speed,followCursor:N(this.getAttribute("follow-cursor"),M.followCursor),interactive:N(this.getAttribute("interactive"),M.interactive),shadow:N(this.getAttribute("shadow"),M.shadow),sound:N(this.getAttribute("sound"),M.sound)}}syncLabel(t){let s=this.getAttribute("aria-label");s&&!s.startsWith("moodi: ")||this.setAttribute("aria-label",`moodi: ${t}`);}react(t,s={}){this.buddy?.react(t,s.duration??1200);}get shape(){let t=this.getAttribute("shape");return ce(t)?t:M.shape}set shape(t){this.setAttribute("shape",t);}get mood(){let t=this.getAttribute("mood");return de(t)?t:M.mood}set mood(t){this.setAttribute("mood",t);}get color(){return this.getAttribute("color")||M.color}set color(t){this.setAttribute("color",t);}get faceColor(){return this.getAttribute("face-color")||void 0}set faceColor(t){t?this.setAttribute("face-color",t):this.removeAttribute("face-color");}get cheekColor(){return this.getAttribute("cheek-color")||void 0}set cheekColor(t){t?this.setAttribute("cheek-color",t):this.removeAttribute("cheek-color");}get size(){let t=this.getAttribute("size");return t?/^\d+(\.\d+)?$/.test(t)?Number(t):t:M.size}set size(t){this.setAttribute("size",String(t));}get speed(){return this.readState().speed}set speed(t){this.setAttribute("speed",String(t));}get followCursor(){return N(this.getAttribute("follow-cursor"),M.followCursor)}set followCursor(t){this.setAttribute("follow-cursor",String(t));}get interactive(){return N(this.getAttribute("interactive"),M.interactive)}set interactive(t){this.setAttribute("interactive",String(t));}get shadow(){return N(this.getAttribute("shadow"),M.shadow)}set shadow(t){this.setAttribute("shadow",String(t));}get sound(){return N(this.getAttribute("sound"),M.sound)}set sound(t){this.setAttribute("sound",String(t));}};Z.observedAttributes=xt;function Ke(e="moodi-buddy"){typeof window>"u"||!("customElements"in window)||customElements.get(e)||customElements.define(e,class extends Z{});}Ke();exports.DEFAULTS=M;exports.MOODS=Je;exports.MOOD_INFO=Bt;exports.MoodiBuddyElement=Z;exports.PALETTES=Ot;exports.SHAPES=Ue;exports.SHAPE_LABELS=Lt;exports.defineMoodi=Ke;exports.isMood=de;exports.isShape=ce;
28
+ :host([ready]) slot{display:none}`,zt=typeof HTMLElement>"u"?class{}:HTMLElement,Q=class extends zt{constructor(){super();this.buddy=null;this.sizeStyle=document.createElement("style");this.onEnter=()=>{this.buddy&&(this.buddy.hovering=true);};this.onLeave=()=>{this.buddy&&(this.buddy.hovering=false);};this.onClick=()=>{this.buddy?.state.interactive&&this.react(Math.random()<.5?"happy":"surprised",{duration:1100});};this.root=this.attachShadow({mode:"open"});let t=document.createElement("style");t.textContent=Ft,this.root.append(t,document.createElement("slot")),this.root.append(this.sizeStyle),this.applySize(this.getAttribute("size"));}connectedCallback(){if(this.hasAttribute("role")||this.setAttribute("role","img"),!this.buddy){let t=this.buddy=new le;t.onMoodChange=s=>{this.syncLabel(s.mood),this.dispatchEvent(new CustomEvent("moodchange",{detail:s,bubbles:true}));},t.onReady=()=>{this.setAttribute("ready",""),this.dispatchEvent(new Event("ready"));},this.root.append(t.canvas),t.update(this.readState()),this.addEventListener("pointerenter",this.onEnter),this.addEventListener("pointerleave",this.onLeave),this.addEventListener("click",this.onClick);}this.syncLabel(this.buddy.currentMood),this.buddy.connect(),this.io=new IntersectionObserver(([t])=>this.buddy?.setVisible(t.isIntersecting),{rootMargin:"64px"}),this.io.observe(this),this.ro=new ResizeObserver(([t])=>{let s=t.contentRect;this.buddy?.resize(s.width,s.height);}),this.ro.observe(this);}disconnectedCallback(){this.io?.disconnect(),this.ro?.disconnect(),this.buddy&&(this.buddy.canvas.remove(),this.buddy.dispose(),this.buddy=null),this.removeEventListener("pointerenter",this.onEnter),this.removeEventListener("pointerleave",this.onLeave),this.removeEventListener("click",this.onClick);}attributeChangedCallback(t,s,i){s!==i&&(t==="size"&&(this.applySize(i),s!==null&&this.buddy?.bump()),t!=="aria-label"&&(this.buddy?.update(this.readState()),t==="mood"&&this.buddy&&this.syncLabel(this.buddy.currentMood)));}applySize(t){let s=Rt(t);this.sizeStyle.textContent=`:host{width:${s};height:${s}}`;}readState(){let t=this.getAttribute("shape"),s=this.getAttribute("mood"),i=parseFloat(this.getAttribute("speed")??""),r=parseFloat(this.getAttribute("settle")??"");return {shape:de(t)?t:M.shape,mood:ue(s)?s:M.mood,color:this.getAttribute("color")||M.color,faceColor:this.getAttribute("face-color")||void 0,cheekColor:this.getAttribute("cheek-color")||void 0,speed:Number.isFinite(i)?Math.max(0,i):M.speed,followCursor:N(this.getAttribute("follow-cursor"),M.followCursor),interactive:N(this.getAttribute("interactive"),M.interactive),shadow:N(this.getAttribute("shadow"),M.shadow),sound:N(this.getAttribute("sound"),M.sound),settle:Number.isFinite(r)?Math.max(0,r):M.settle}}syncLabel(t){let s=this.getAttribute("aria-label");s&&!s.startsWith("moodi: ")||this.setAttribute("aria-label",`moodi: ${t}`);}react(t,s={}){this.buddy?.react(t,s.duration??1200);}get shape(){let t=this.getAttribute("shape");return de(t)?t:M.shape}set shape(t){this.setAttribute("shape",t);}get mood(){let t=this.getAttribute("mood");return ue(t)?t:M.mood}set mood(t){this.setAttribute("mood",t);}get color(){return this.getAttribute("color")||M.color}set color(t){this.setAttribute("color",t);}get faceColor(){return this.getAttribute("face-color")||void 0}set faceColor(t){t?this.setAttribute("face-color",t):this.removeAttribute("face-color");}get cheekColor(){return this.getAttribute("cheek-color")||void 0}set cheekColor(t){t?this.setAttribute("cheek-color",t):this.removeAttribute("cheek-color");}get size(){let t=this.getAttribute("size");return t?/^\d+(\.\d+)?$/.test(t)?Number(t):t:M.size}set size(t){this.setAttribute("size",String(t));}get speed(){return this.readState().speed}set speed(t){this.setAttribute("speed",String(t));}get followCursor(){return N(this.getAttribute("follow-cursor"),M.followCursor)}set followCursor(t){this.setAttribute("follow-cursor",String(t));}get interactive(){return N(this.getAttribute("interactive"),M.interactive)}set interactive(t){this.setAttribute("interactive",String(t));}get shadow(){return N(this.getAttribute("shadow"),M.shadow)}set shadow(t){this.setAttribute("shadow",String(t));}get settle(){return this.readState().settle}set settle(t){this.setAttribute("settle",String(t));}get sound(){return N(this.getAttribute("sound"),M.sound)}set sound(t){this.setAttribute("sound",String(t));}};Q.observedAttributes=xt;function Ke(e="moodi-buddy"){typeof window>"u"||!("customElements"in window)||customElements.get(e)||customElements.define(e,class extends Q{});}Ke();exports.DEFAULTS=M;exports.MOODS=Je;exports.MOOD_INFO=Ot;exports.MoodiBuddyElement=Q;exports.PALETTES=Bt;exports.SHAPES=Ue;exports.SHAPE_LABELS=Lt;exports.defineMoodi=Ke;exports.isMood=ue;exports.isShape=de;
package/dist/index.d.cts CHANGED
@@ -23,6 +23,11 @@ interface MoodiProps {
23
23
  shadow?: boolean;
24
24
  /** A short sound effect on every mood change (and when it first appears). Default `false`. */
25
25
  sound?: boolean;
26
+ /**
27
+ * Milliseconds after a mood change when the body calms down to gentle idle breathing
28
+ * (hopping, shaking, hearts… stop; the facial expression stays). `0` = never. Default `0`.
29
+ */
30
+ settle?: number;
26
31
  }
27
32
  interface MoodChangeDetail {
28
33
  mood: Mood;
@@ -42,6 +47,7 @@ declare const DEFAULTS: {
42
47
  interactive: boolean;
43
48
  shadow: boolean;
44
49
  sound: boolean;
50
+ settle: number;
45
51
  };
46
52
  declare const PALETTES: Readonly<Record<string, string>>;
47
53
  declare const MOOD_INFO: Readonly<Record<Mood, {
@@ -54,7 +60,7 @@ declare const isMood: (v: unknown) => v is Mood;
54
60
 
55
61
  declare const Base: typeof HTMLElement;
56
62
  declare class MoodiBuddyElement extends Base {
57
- static observedAttributes: readonly ["shape", "mood", "color", "face-color", "cheek-color", "size", "speed", "follow-cursor", "interactive", "shadow", "sound"];
63
+ static observedAttributes: readonly ["shape", "mood", "color", "face-color", "cheek-color", "size", "speed", "follow-cursor", "interactive", "shadow", "sound", "settle"];
58
64
  private buddy;
59
65
  private io?;
60
66
  private ro?;
@@ -95,6 +101,8 @@ declare class MoodiBuddyElement extends Base {
95
101
  set interactive(v: boolean);
96
102
  get shadow(): boolean;
97
103
  set shadow(v: boolean);
104
+ get settle(): number;
105
+ set settle(v: number);
98
106
  get sound(): boolean;
99
107
  set sound(v: boolean);
100
108
  }
package/dist/index.d.ts CHANGED
@@ -23,6 +23,11 @@ interface MoodiProps {
23
23
  shadow?: boolean;
24
24
  /** A short sound effect on every mood change (and when it first appears). Default `false`. */
25
25
  sound?: boolean;
26
+ /**
27
+ * Milliseconds after a mood change when the body calms down to gentle idle breathing
28
+ * (hopping, shaking, hearts… stop; the facial expression stays). `0` = never. Default `0`.
29
+ */
30
+ settle?: number;
26
31
  }
27
32
  interface MoodChangeDetail {
28
33
  mood: Mood;
@@ -42,6 +47,7 @@ declare const DEFAULTS: {
42
47
  interactive: boolean;
43
48
  shadow: boolean;
44
49
  sound: boolean;
50
+ settle: number;
45
51
  };
46
52
  declare const PALETTES: Readonly<Record<string, string>>;
47
53
  declare const MOOD_INFO: Readonly<Record<Mood, {
@@ -54,7 +60,7 @@ declare const isMood: (v: unknown) => v is Mood;
54
60
 
55
61
  declare const Base: typeof HTMLElement;
56
62
  declare class MoodiBuddyElement extends Base {
57
- static observedAttributes: readonly ["shape", "mood", "color", "face-color", "cheek-color", "size", "speed", "follow-cursor", "interactive", "shadow", "sound"];
63
+ static observedAttributes: readonly ["shape", "mood", "color", "face-color", "cheek-color", "size", "speed", "follow-cursor", "interactive", "shadow", "sound", "settle"];
58
64
  private buddy;
59
65
  private io?;
60
66
  private ro?;
@@ -95,6 +101,8 @@ declare class MoodiBuddyElement extends Base {
95
101
  set interactive(v: boolean);
96
102
  get shadow(): boolean;
97
103
  set shadow(v: boolean);
104
+ get settle(): number;
105
+ set settle(v: number);
98
106
  get sound(): boolean;
99
107
  set sound(v: boolean);
100
108
  }
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import {Scene,PerspectiveCamera,Group,Vector3,BufferGeometry,CanvasTexture,SRGBColorSpace,LinearMipmapLinearFilter,Mesh,HemisphereLight,DirectionalLight,MeshStandardMaterial,BufferAttribute,WebGLRenderer,SphereGeometry}from'three';var Ue=["ghost","pebble","blob","drop","bean","cube"],Je=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],M={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:true,interactive:true,shadow:true,sound:false},Ot={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},Bt={idle:{label:"Idle",description:"Breathing softly. Blinks now and then."},thinking:{label:"Thinking",description:"Eyes drift up. Little thoughts pop."},loading:{label:"Loading",description:"Half-lidded, wobbling patiently."},listening:{label:"Listening",description:"All eyes. Nods along to every word."},speaking:{label:"Speaking",description:"Chatty mouth, bouncing to the rhythm."},happy:{label:"Happy",description:"Bounces with joy. Cheeks glow."},surprised:{label:"Surprised",description:"Wide eyes, tiny o. Did not see that coming."},sad:{label:"Sad",description:"Slumps a little. Needs a minute."},error:{label:"Error",description:"Something broke. Shakes it off."},sleeping:{label:"Sleeping",description:"Deep, slow breaths. Do not disturb."},love:{label:"Love",description:"Heart eyes, floating on air."}},Lt={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},ce=e=>Ue.includes(e),de=e=>Je.includes(e);var L=null;function X(e,o){if(!e)return o;let t=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(e.trim());if(t)return xe(t[1]);if(L||(L=document.createElement("canvas").getContext("2d")),!L)return o;L.fillStyle="#010203",L.fillStyle=e;let s=String(L.fillStyle);if(s==="#010203"&&e.trim()!=="#010203")return o;if(s.startsWith("#"))return xe(s.slice(1));L.canvas.width=L.canvas.height=1,L.clearRect(0,0,1,1),L.fillRect(0,0,1,1);let i=L.getImageData(0,0,1,1).data;return [i[0]/255,i[1]/255,i[2]/255]}function xe(e){e.length===3&&(e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]);let o=parseInt(e,16);return [(o>>16&255)/255,(o>>8&255)/255,(o&255)/255]}var G=e=>`rgb(${Math.round(e[0]*255)},${Math.round(e[1]*255)},${Math.round(e[2]*255)})`,x=(e,o,t)=>[e[0]+(o[0]-e[0])*t,e[1]+(o[1]-e[1])*t,e[2]+(o[2]-e[2])*t],ue=e=>e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4),pe=e=>.2126*ue(e[0])+.7152*ue(e[1])+.0722*ue(e[2]),Ze=[.07,.06,.08],Qe=[1,.97,.93],et=[1,.42,.48],Q=e=>pe(e)>.06?x(Ze,e,.06):Qe,ee=e=>x(e,et,.78);var U=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],q={eyeOpen:1,eyeSize:1,lookX:0,lookY:0,lidCover:0,lidTilt:0,happyEye:0,closedEye:0,xEye:0,heartEye:0,mouthW:.1,mouthCurve:.35,mouthOpen:0,mouthRound:0,mouthSkew:0,mouthWave:0,blush:.45},te={idle:{...q},thinking:{...q,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...q,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...q,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...q,mouthW:.09,mouthCurve:.25},happy:{...q,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...q,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...q,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...q,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...q,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...q,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Fe={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},be=e=>({...e}),tt=.31,ot=-0.04,st=.098,me=.128,it=.2,fe=.05;function ze(e,o,t,s,i,r,h,c,l,p){let n=e.canvas.width;e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,n,n),e.setTransform(n/2,0,0,n/2,n/2,n/2),e.lineCap="round",e.lineJoin="round";let a=o.lookX*.075,u=o.lookY*.06;if(o.blush>.01){let m=n*.018;e.save(),"filter"in e&&(e.filter=`blur(${m}px)`);for(let g of [-1,1]){let b=g*.52+a,d=.16+u,y=e.createRadialGradient(b,d,0,b,d,.16);y.addColorStop(0,i),y.addColorStop(1,"rgba(0,0,0,0)"),e.globalAlpha=Math.min(1,o.blush)*.62,e.fillStyle=y,e.beginPath(),e.ellipse(b,d,.17,.12,0,0,Math.PI*2),e.fill();}e.restore();let f=Math.min(1,Math.max(0,(o.blush-.6)/.4));if(f>.01){e.save(),e.globalAlpha=f*.55,e.strokeStyle=i,e.lineWidth=.016,e.lineCap="round";for(let g of [-1,1]){let b=g*.52+a,d=.16+u;for(let y=-1;y<=1;y++){let P=b+y*.045;e.beginPath(),e.moveTo(P+.018,d-.035),e.lineTo(P-.018,d+.035),e.stroke();}}e.restore();}}e.fillStyle=s,e.strokeStyle=s,e.lineWidth=fe;let w=Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye),A=1-w;for(let m of [-1,1]){let f=m*tt+a,g=ot+u,b=st*o.eyeSize;if(A>.01){e.globalAlpha=A;let d=Math.max(.06,o.eyeOpen*t),y=me*o.eyeSize*d;if(e.save(),o.lidCover>.01||o.lidTilt>.01){let W=g-me*o.eyeSize+2*me*o.eyeSize*o.lidCover;e.translate(f,W),e.rotate(m*o.lidTilt*.6),e.beginPath(),e.rect(-1,0,2,2),e.clip(),e.rotate(-m*o.lidTilt*.6),e.translate(-f,-W);}r>.5&&Re(e,f,g,b*2.1,y*1.9,s,.35*A);let P=e.createRadialGradient(f,g-y*.2,0,f,g,Math.max(b,y));if(P.addColorStop(0,h),P.addColorStop(1,s),e.fillStyle=P,e.beginPath(),e.ellipse(f,g,b,y,0,0,Math.PI*2),e.fill(),e.fillStyle=s,d>.35){let R=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),le=c==="#fff"?.3:.34;e.globalAlpha=A*Math.min(1,(d-.35)*3),e.fillStyle=c,e.beginPath(),e.arc(f-b*.1+R*b*.5,g-y*.16+W*y*.42,b*le,0,Math.PI*2),e.fill(),e.fillStyle=s;}e.restore();}if(r>.5&&w>.01&&Re(e,f,g,b*2,b*1.8,s,.3*w),e.fillStyle=s,o.happyEye>.01){let d=.6+.4*o.happyEye;e.globalAlpha=Math.min(1,o.happyEye),e.beginPath(),e.arc(f,g+.045,b*1.15*d,Math.PI*1.12,Math.PI*1.88),e.stroke();}if(o.closedEye>.01){let d=.6+.4*o.closedEye;e.globalAlpha=Math.min(1,o.closedEye),e.beginPath(),e.moveTo(f-b*1.15*d,g+.01),e.quadraticCurveTo(f,g+.07*d,f+b*1.15*d,g+.01),e.stroke();}if(o.xEye>.01){let d=b*.95*(.6+.4*o.xEye);e.globalAlpha=Math.min(1,o.xEye),e.beginPath(),e.moveTo(f-d,g-d),e.lineTo(f+d,g+d),e.moveTo(f+d,g-d),e.lineTo(f-d,g+d),e.stroke();}o.heartEye>.01&&(e.globalAlpha=Math.min(1,o.heartEye),ge(e,f,g,b*1.35*(.5+.5*o.heartEye)),e.fill());}let k=a+o.mouthSkew*.07,S=it+u,E=Math.min(1,Math.max(0,o.mouthRound)),C=o.mouthW,O=e.createLinearGradient(k-C-.02,S,k+C+.02,S);O.addColorStop(0,p),O.addColorStop(.5,h),O.addColorStop(1,p);let B=(m,f,g,b)=>{e.fillStyle=l,m(),e.fill(),f>.02&&(e.save(),m(),e.clip(),e.fillStyle=i,e.globalAlpha*=Math.min(1,(f-.02)*12),e.beginPath(),e.ellipse(k,g,b,f*.55,0,0,Math.PI*2),e.fill(),e.restore()),e.strokeStyle=O;let d=e.lineWidth;e.lineWidth=d*.6,m(),e.stroke(),e.lineWidth=d;};if(E<.99){e.globalAlpha=1-E;let m=18,f=y=>o.mouthCurve*.075*(1-y*y),g=(y,P)=>{let R=y/m*2-1,W=k+R*C,le=S+f(R)+o.mouthSkew*R*.03+o.mouthWave*.018*Math.sin((R+1)*Math.PI*2)+P*(1-R*R);return [W,le]},b=Math.max(0,o.mouthOpen),d=()=>{for(let y=0;y<=m;y++){let[P,R]=g(y,-b*.15);y?e.lineTo(P,R):e.moveTo(P,R);}};b>.003?B(()=>{e.beginPath(),d();for(let P=m;P>=0;P--){let[R,W]=g(P,b);e.lineTo(R,W);}e.closePath();},b,g(m/2,b)[1],C*.55):(e.strokeStyle=O,e.beginPath(),d(),e.stroke());}if(E>.01){e.globalAlpha=E;let m=Math.max(.02,o.mouthOpen*.55+.01),f=()=>{e.beginPath(),e.ellipse(k,S+.01,m*.85,m,0,0,Math.PI*2);};e.lineWidth=fe*.8,B(f,o.mouthOpen,S+.01+m*.8,m*.6),e.lineWidth=fe;}e.globalAlpha=1;}function Re(e,o,t,s,i,r,h){let c=e.createRadialGradient(o,t,0,o,t,1);c.addColorStop(0,r),c.addColorStop(1,"rgba(0,0,0,0)"),e.save(),e.globalAlpha=h,e.translate(o,t),e.scale(s,i),e.translate(-o,-t),e.fillStyle=c,e.beginPath(),e.arc(o,t,1,0,Math.PI*2),e.fill(),e.restore();}function ge(e,o,t,s){e.beginPath(),e.moveTo(o,t+s*.85),e.bezierCurveTo(o-s*1.25,t+s*.05,o-s*.75,t-s*.95,o,t-s*.35),e.bezierCurveTo(o+s*.75,t-s*.95,o+s*1.25,t+s*.05,o,t+s*.85),e.closePath();}var ie=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:v,cos:nt,abs:rt,max:oe,exp:at,PI:se}=Math;function ye(e,o,t,s){switch(s.y=s.rx=s.ry=s.rz=s.st=s.mouth=s.lookX=s.lookY=0,e){case "idle":{let i=v(o*1.6);s.y=.015*i,s.st=.018*i;break}case "thinking":{s.rz=.07*v(o*1.1),s.y=.02*v(o*2.2),s.ry=.15,s.rx=-0.06;break}case "loading":{let i=o*2.4;s.rx=.12*v(i),s.rz=.12*nt(i),s.ry=.22*v(i),s.y=.02*v(i*2),s.lookX=.55*v(i);break}case "listening":{let i=oe(0,v(o*3.2));s.rx=.09*i*i;let r=rt(v(o*7.1)*.5+v(o*11.3)*.3+v(o*3.7)*.2);s.st=.03*r;break}case "speaking":{let i=oe(0,v(o*9)*.6+v(o*14.3)*.4)*(.65+.35*v(o*1.7));s.mouth=.11*i,s.st=.04*i,s.rz=.03*v(o*2.1);break}case "happy":{let i=o*1.35%1;s.y=.2*v(se*i);let r=oe(0,1-i/.14)+oe(0,(i-.9)/.1);s.st=.08*v(se*i)-.14*r,s.rz=.05*v(o*1.35*se);break}case "surprised":{if(t<.45){let i=v(se*t/.45);s.y=.16*i,s.st=.12*i;}s.rz=.01*v(o*38)*at(-t*2),s.rx=-0.06;break}case "sad":{let i=v(o*1.1);s.y=-0.06,s.rx=.2,s.st=-0.04+.015*i,s.rz=-0.04;break}case "error":{let i=t%2.8;if(i<.55){let r=1-i/.55;s.ry=.26*v(i*42)*r,s.rz=.05*v(i*42+1)*r;}s.st=-0.02;break}case "sleeping":{let i=v(o*1);s.st=.04*i,s.y=-0.03,s.rz=.1,s.rx=.1;break}case "love":{s.y=.07+.06*v(o*1.6),s.rz=.05*v(o*.8),s.st=.015*v(o*1.6+1);break}}return s}function Oe(e,o,t,s){for(let i of Object.keys(s))s[i]=e[i]+(o[i]-e[i])*t;return s}var Be={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var j=new Set,T=null,$=0,V=0,ne=0,Me,Le=false,H="idle",D={x:0,y:0,lastMove:-1e9,known:false},z={reducedMotion:false,lowPower:false};function lt(){if(Le)return;Le=true,addEventListener("pointermove",o=>{D.x=o.clientX,D.y=o.clientY,D.lastMove=performance.now(),D.known=true;},{passive:true}),document.addEventListener("visibilitychange",_);let e=matchMedia("(prefers-reduced-motion: reduce)");z.reducedMotion=e.matches,e.addEventListener?.("change",o=>z.reducedMotion=o.matches);}function qe(e,o){return T||(T=new WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),T.setPixelRatio(1),T.setClearColor(0,0),z.lowPower=ct(T.getContext()),T.setScissorTest(true),T.domElement.addEventListener("webglcontextlost",t=>{t.preventDefault(),T?.dispose(),T=null,$=V=0,H="idle";})),(e>$||o>V)&&($=Math.max($,e),V=Math.max(V,o),T.setSize($,V,false)),T}var Ie=()=>V;function ct(e){try{let o=e.getExtension("WEBGL_debug_renderer_info"),t=String(e.getParameter(o?o.UNMASKED_RENDERER_WEBGL:e.RENDERER));return /swiftshader|llvmpipe|softpipe|software|basic render/i.test(t)}catch{return false}}function Ye(e,o){if(H==="ready")return true;if(H==="idle"&&T){H="compiling";let t=T;t.compileAsync(e,o).catch(()=>{}).then(()=>{t===T&&(H="ready"),_();});}return false}function We(e){lt(),clearTimeout(Me),j.add(e),_();}function De(e){j.delete(e),j.size===0&&(clearTimeout(Me),Me=setTimeout(()=>{j.size||!T||(T.dispose(),T.forceContextLoss(),T=null,$=V=0,H="idle");},1500));}function _(){if(!ne){for(let e of j)if(e.active){ne=requestAnimationFrame(_e);return}}}var dt=10,Ge=new WeakMap,ve=0;function _e(e){ne=0;let o=[...j].filter(r=>r.active);if(!o.length)return;let t=performance.now(),s=o.length,i=0;for(;i<s;i++){let r=o[(ve+i)%s],h=Ge.get(r)??e-16;if(Ge.set(r,e),r.tick(Math.min(.05,Math.max(0,(e-h)/1e3)),e),performance.now()-t>dt){i++;break}}ve=(ve+i)%s,ne=requestAnimationFrame(_e);}var J={pebble:{faceY:.02,faceScale:1.3,animated:false},blob:{faceY:.02,faceScale:1.28,animated:true},drop:{faceY:-0.24,faceScale:1.22,animated:false},bean:{faceY:.14,faceScale:.92,animated:false},cube:{faceY:0,faceScale:1.22,animated:false},ghost:{faceY:.22,faceScale:1.24,animated:true}};function Ne(e){let o=new SphereGeometry(1,e*2,Math.round(e*1.5)),t=o.getAttribute("position"),s=o.getIndex(),i=new Map,r=[],h=new Uint32Array(t.count);for(let n=0;n<t.count;n++){let a=t.getX(n),u=t.getY(n),w=t.getZ(n),A=`${Math.round(a*1e4)},${Math.round(u*1e4)},${Math.round(w*1e4)}`,k=i.get(A);if(k===void 0){k=r.length/3,i.set(A,k);let S=Math.hypot(a,u,w)||1;r.push(a/S,u/S,w/S);}h[n]=k;}let c=[];for(let n=0;n<s.count;n+=3){let a=h[s.getX(n)],u=h[s.getX(n+1)],w=h[s.getX(n+2)];a!==u&&u!==w&&a!==w&&c.push(a,u,w);}o.dispose();let l=new BufferGeometry,p=new Float32Array(r);return l.setAttribute("position",new BufferAttribute(new Float32Array(p),3)),l.setIndex(c),{geometry:l,dirs:p}}var ft=(e,o,t)=>{let s=Math.min(1,Math.max(0,(t-e)/(o-e)));return s*s*(3-2*s)};function we(e,o,t,s,i,r,h){let c=o,l=t,p=s;switch(e){case "pebble":{c=o*1.1+.04*t,l=t*.92*(.95+.05*Math.tanh(3*t))+.03*o,p=s*.94;break}case "blob":{let n=.97+.045*Math.sin(2.1*o+1.3*i)*Math.sin(2.4*t+1.7*i+1)+.035*Math.sin(2.7*s+2.3*o+1.1*i);c=o*n,l=t*n,p=s*n;break}case "drop":{let n=Math.max(t,0),a=1-.8*Math.pow(n,1.6);c=o*a*.98+.1*n*n*n,l=(t<0?t*.9:t*1.32)-.12,p=s*a*.98;break}case "bean":{let n=t*.78+.42*Math.tanh(t*3);c=(o*.8+.14*(n*n/1.44)-.07)*.9,l=n*.88,p=s*.8*.9;break}case "cube":{let a=Math.pow(Math.pow(Math.abs(o),4.5)+Math.pow(Math.abs(t),4.5)+Math.pow(Math.abs(s),4.5),-0.2222222222222222);c=o*a*.84,l=t*a*.82,p=s*a*.84;break}case "ghost":{if(t>=0)c=o,l=t,p=s;else {let a=(Math.acos(Math.max(-1,t))-Math.PI/2)/(Math.PI/2),u=Math.hypot(o,s)||1,w=o/u,A=s/u,k=Math.atan2(s,o),S=.085*Math.sin(5*k+2.2*i),E=1.04,C=.13,O=-1.05+S,B;if(a<.45){let m=a/.45;B=1+.04*m,l=-m*(1.05-C)+S*ft(.45,1,m);}else if(a<.75){let m=(a-.45)/.3*(Math.PI/2);B=E-C+C*Math.cos(m),l=O+C-C*Math.sin(m);}else {let m=(a-.75)/.25;B=(E-C)*(1-m),l=O+.22*m*m*(3-2*m);}c=w*B,p=A*B;}c*=.9,l=(l+.1)*.9,p*=.9;break}}r[h]=c,r[h+1]=l,r[h+2]=p;}var I=null,re=null;function bt(){if(typeof window>"u"||!("AudioContext"in window))return null;if(gt(),!I){I=new AudioContext;let e=I.createBiquadFilter();e.type="lowpass",e.frequency.value=3200,re=I.createGain(),re.gain.value=.55,re.connect(e).connect(I.destination);}return I.state==="suspended"&&I.resume(),I}function F(e,{freq:o,to:t,at:s=0,dur:i=.12,type:r="sine",gain:h=.1,wobble:c=0}){let l=e.currentTime+s,p=e.createOscillator(),n=e.createGain();if(p.type=r,p.frequency.setValueAtTime(o,l),t&&p.frequency.exponentialRampToValueAtTime(t,l+i),c){let a=e.createOscillator(),u=e.createGain();a.frequency.value=7,u.gain.value=c,a.connect(u).connect(p.frequency),a.start(l),a.stop(l+i+.05);}n.gain.setValueAtTime(1e-4,l),n.gain.exponentialRampToValueAtTime(h,l+Math.min(.012,i/4)),n.gain.exponentialRampToValueAtTime(1e-4,l+i),p.connect(n).connect(re),p.start(l),p.stop(l+i+.02);}var ke={idle:e=>F(e,{freq:520,to:780,dur:.09,gain:.08}),thinking:e=>[660,760,900].forEach((o,t)=>F(e,{freq:o,to:o*1.08,at:t*.1,dur:.07,type:"triangle",gain:.06})),loading:e=>[0,.14,.28].forEach(o=>F(e,{freq:940,at:o,dur:.04,gain:.05})),listening:e=>{F(e,{freq:523,dur:.12,type:"triangle",gain:.07}),F(e,{freq:784,at:.1,dur:.22,type:"triangle",gain:.07});},speaking:e=>[380,460,340,520,420].forEach((o,t)=>F(e,{freq:o,to:o*1.15,at:t*.075,dur:.06,type:"triangle",gain:.06})),happy:e=>[523,659,784,1047].forEach((o,t)=>F(e,{freq:o,at:t*.07,dur:.14,type:"triangle",gain:.07})),surprised:e=>F(e,{freq:280,to:980,dur:.18,gain:.09,wobble:30}),sad:e=>{F(e,{freq:560,to:300,dur:.5,gain:.07}),F(e,{freq:420,to:240,at:.12,dur:.5,gain:.04});},error:e=>[0,.14].forEach(o=>F(e,{freq:170,to:150,at:o,dur:.1,type:"sawtooth",gain:.045})),sleeping:e=>{F(e,{freq:150,to:115,dur:.6,gain:.07,wobble:6}),F(e,{freq:300,to:230,at:.05,dur:.5,gain:.02});},love:e=>[1319,1568,2093,1760].forEach((o,t)=>F(e,{freq:o,at:t*.08,dur:.22,gain:.04}))},Y=null,Ve=false;function gt(){if(Ve)return;Ve=true;let e=()=>{I&&I.resume().then(()=>{Y&&performance.now()<Y.until&&ke[Y.mood](I),Y=null;});};for(let o of ["pointerdown","touchend","keydown"])addEventListener(o,e,{passive:true});}function ae(e,o=false){let t=bt();if(t){if(t.state==="running"){Y=null,ke[e](t);return}Y={mood:e,until:performance.now()+(o?6e3:1200)},t.resume().then(()=>{Y&&performance.now()<Y.until&&ke[Y.mood](t),Y=null;});}}var Ae=-0.85,He=new Map;function Tt(e){let o=He.get(e);if(!o){let{geometry:t,dirs:s}=Ne(e);o={dirs:s,index:new Uint32Array(t.getIndex().array)},t.dispose(),He.set(e,o);}return o}var K=(e,o)=>1-Math.exp(-e*o),je=e=>1-Math.pow(1-e,3),he=class{constructor(){this.scene=new Scene;this.camera=new PerspectiveCamera(30,1,.1,50);this.group=new Group;this.v=new Vector3;this.geometry=new BufferGeometry;this.faceCanvas=document.createElement("canvas");this.uniforms={uFace:{value:null},uFaceY:{value:0},uFaceScale:{value:1.5}};this.state={...M};this.visible=false;this.connected=false;this.w=0;this.h=0;this.t=Math.random()*100;this.shapeFrom=M.shape;this.shapeTo=M.shape;this.morph=1;this.shapeDirty=true;this.shown=M.mood;this.prevShown=M.mood;this.tau=10;this.prevTau=10;this.blend=1;this.reaction=null;this.pose=ie();this.poseA=ie();this.poseB=ie();this.face=be(te.idle);this.target=be(te.idle);this.drawn=new Float32Array(U.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=X(M.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=Q(this.body);this.faceCT=this.faceC;this.cheek=ee(this.body);this.cheekT=this.cheek;this.look={x:0,y:0};this.wander={x:0,y:0,next:0};this.squash=0;this.squashV=0;this.pop=.6;this.popV=0;this.hover=0;this.hovering=false;this.overlay={dots:0,zz:0,hearts:0,waves:0,bang:0};this.needsFrame=true;this.ready=false;this.css=[0,0];this.quality=1;this.lowPowerApplied=false;this.cost=0;this.costFrames=0;this.lastFrame=[];this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d"),this.faceCtx=this.faceCanvas.getContext("2d"),this.faceCanvas.width=this.faceCanvas.height=256,this.faceTex=new CanvasTexture(this.faceCanvas),this.faceTex.colorSpace=SRGBColorSpace,this.faceTex.minFilter=LinearMipmapLinearFilter,this.faceTex.anisotropy=4,this.uniforms.uFace.value=this.faceTex,this.setSegments(24),this.material=this.createMaterial(),this.mesh=new Mesh(this.geometry,this.material),this.mesh.frustumCulled=false,this.mesh.position.y=-Ae,this.group.add(this.mesh),this.scene.add(this.group),this.scene.add(new HemisphereLight(16774120,7234140,1.7));let o=new DirectionalLight(16777215,1.9);o.position.set(2.5,3.5,4);let t=new DirectionalLight(16770770,1.4);t.position.set(-3.5,2,-3);let s=new DirectionalLight(16773350,.45);s.position.set(-2,-1,3),this.scene.add(o,t,s),this.camera.position.set(0,.3,6.6),this.camera.lookAt(0,.12,0);}get active(){return this.visible&&this.connected&&this.w>0}createMaterial(){let o=new MeshStandardMaterial({roughness:.62,metalness:0});return o.color.setRGB(...this.body,SRGBColorSpace),o.customProgramCacheKey=()=>"moodi-1",o.onBeforeCompile=t=>{Object.assign(t.uniforms,this.uniforms),t.vertexShader=t.vertexShader.replace("#include <common>",`#include <common>
1
+ import {Scene,PerspectiveCamera,Group,Vector3,BufferGeometry,CanvasTexture,SRGBColorSpace,LinearMipmapLinearFilter,Mesh,HemisphereLight,DirectionalLight,MeshStandardMaterial,BufferAttribute,WebGLRenderer,SphereGeometry}from'three';var Ue=["ghost","pebble","blob","drop","bean","cube"],Je=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],M={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:true,interactive:true,shadow:true,sound:false,settle:0},Bt={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},Ot={idle:{label:"Idle",description:"Breathing softly. Blinks now and then."},thinking:{label:"Thinking",description:"Eyes drift up. Little thoughts pop."},loading:{label:"Loading",description:"Half-lidded, wobbling patiently."},listening:{label:"Listening",description:"All eyes. Nods along to every word."},speaking:{label:"Speaking",description:"Chatty mouth, bouncing to the rhythm."},happy:{label:"Happy",description:"Bounces with joy. Cheeks glow."},surprised:{label:"Surprised",description:"Wide eyes, tiny o. Did not see that coming."},sad:{label:"Sad",description:"Slumps a little. Needs a minute."},error:{label:"Error",description:"Something broke. Shakes it off."},sleeping:{label:"Sleeping",description:"Deep, slow breaths. Do not disturb."},love:{label:"Love",description:"Heart eyes, floating on air."}},Lt={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},de=e=>Ue.includes(e),ue=e=>Je.includes(e);var O=null;function X(e,o){if(!e)return o;let t=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(e.trim());if(t)return Fe(t[1]);if(O||(O=document.createElement("canvas").getContext("2d")),!O)return o;O.fillStyle="#010203",O.fillStyle=e;let s=String(O.fillStyle);if(s==="#010203"&&e.trim()!=="#010203")return o;if(s.startsWith("#"))return Fe(s.slice(1));O.canvas.width=O.canvas.height=1,O.clearRect(0,0,1,1),O.fillRect(0,0,1,1);let i=O.getImageData(0,0,1,1).data;return [i[0]/255,i[1]/255,i[2]/255]}function Fe(e){e.length===3&&(e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]);let o=parseInt(e,16);return [(o>>16&255)/255,(o>>8&255)/255,(o&255)/255]}var L=e=>`rgb(${Math.round(e[0]*255)},${Math.round(e[1]*255)},${Math.round(e[2]*255)})`,x=(e,o,t)=>[e[0]+(o[0]-e[0])*t,e[1]+(o[1]-e[1])*t,e[2]+(o[2]-e[2])*t],pe=e=>e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4),me=e=>.2126*pe(e[0])+.7152*pe(e[1])+.0722*pe(e[2]),Ze=[.07,.06,.08],Qe=[1,.97,.93],et=[1,.42,.48],ee=e=>me(e)>.06?x(Ze,e,.06):Qe,te=e=>x(e,et,.78);var U=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],G={eyeOpen:1,eyeSize:1,lookX:0,lookY:0,lidCover:0,lidTilt:0,happyEye:0,closedEye:0,xEye:0,heartEye:0,mouthW:.1,mouthCurve:.35,mouthOpen:0,mouthRound:0,mouthSkew:0,mouthWave:0,blush:.45},oe={idle:{...G},thinking:{...G,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...G,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...G,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...G,mouthW:.09,mouthCurve:.25},happy:{...G,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...G,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...G,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...G,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...G,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...G,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Be={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},ge=e=>({...e}),tt=.31,ot=-0.04,st=.098,fe=.128,it=.2,be=.05;function Oe(e,o,t,s,i,r,d,a,l,u){let n=e.canvas.width;e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,n,n),e.setTransform(n/2,0,0,n/2,n/2,n/2),e.lineCap="round",e.lineJoin="round";let h=o.lookX*.075,p=o.lookY*.06;if(o.blush>.01){let m=n*.018;e.save(),"filter"in e&&(e.filter=`blur(${m}px)`);for(let y of [-1,1]){let v=y*.52+h,f=.16+p,c=e.createRadialGradient(v,f,0,v,f,.16);c.addColorStop(0,i),c.addColorStop(1,"rgba(0,0,0,0)"),e.globalAlpha=Math.min(1,o.blush)*.62,e.fillStyle=c,e.beginPath(),e.ellipse(v,f,.17,.12,0,0,Math.PI*2),e.fill();}e.restore();let g=Math.min(1,Math.max(0,(o.blush-.6)/.4));if(g>.01){e.save(),e.globalAlpha=g*.55,e.strokeStyle=i,e.lineWidth=.016,e.lineCap="round";for(let y of [-1,1]){let v=y*.52+h,f=.16+p;for(let c=-1;c<=1;c++){let T=v+c*.045;e.beginPath(),e.moveTo(T+.018,f-.035),e.lineTo(T-.018,f+.035),e.stroke();}}e.restore();}}e.fillStyle=s,e.strokeStyle=s,e.lineWidth=be;let b=Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye),E=1-b;for(let m of [-1,1]){let g=m*tt+h,y=ot+p,v=st*o.eyeSize;if(E>.01){e.globalAlpha=E;let f=Math.max(.06,o.eyeOpen*t),c=fe*o.eyeSize*f;if(e.save(),o.lidCover>.01||o.lidTilt>.01){let W=y-fe*o.eyeSize+2*fe*o.eyeSize*o.lidCover;e.translate(g,W),e.rotate(m*o.lidTilt*.6),e.beginPath(),e.rect(-1,0,2,2),e.clip(),e.rotate(-m*o.lidTilt*.6),e.translate(-g,-W);}r>.5&&ze(e,g,y,v*2.1,c*1.9,s,.35*E);let T=e.createRadialGradient(g,y-c*.2,0,g,y,Math.max(v,c));if(T.addColorStop(0,d),T.addColorStop(1,s),e.fillStyle=T,e.beginPath(),e.ellipse(g,y,v,c,0,0,Math.PI*2),e.fill(),e.fillStyle=s,f>.35){let R=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),ce=a==="#fff"?.3:.34;e.globalAlpha=E*Math.min(1,(f-.35)*3),e.fillStyle=a,e.beginPath(),e.arc(g-v*.1+R*v*.5,y-c*.16+W*c*.42,v*ce,0,Math.PI*2),e.fill(),e.fillStyle=s;}e.restore();}if(r>.5&&b>.01&&ze(e,g,y,v*2,v*1.8,s,.3*b),e.fillStyle=s,o.happyEye>.01){let f=.6+.4*o.happyEye;e.globalAlpha=Math.min(1,o.happyEye),e.beginPath(),e.arc(g,y+.045,v*1.15*f,Math.PI*1.12,Math.PI*1.88),e.stroke();}if(o.closedEye>.01){let f=.6+.4*o.closedEye;e.globalAlpha=Math.min(1,o.closedEye),e.beginPath(),e.moveTo(g-v*1.15*f,y+.01),e.quadraticCurveTo(g,y+.07*f,g+v*1.15*f,y+.01),e.stroke();}if(o.xEye>.01){let f=v*.95*(.6+.4*o.xEye);e.globalAlpha=Math.min(1,o.xEye),e.beginPath(),e.moveTo(g-f,y-f),e.lineTo(g+f,y+f),e.moveTo(g+f,y-f),e.lineTo(g-f,y+f),e.stroke();}o.heartEye>.01&&(e.globalAlpha=Math.min(1,o.heartEye),ye(e,g,y,v*1.35*(.5+.5*o.heartEye)),e.fill());}let S=h+o.mouthSkew*.07,k=it+p,C=Math.min(1,Math.max(0,o.mouthRound)),A=o.mouthW,B=e.createLinearGradient(S-A-.02,k,S+A+.02,k);B.addColorStop(0,u),B.addColorStop(.5,d),B.addColorStop(1,u);let q=(m,g,y,v)=>{e.fillStyle=l,m(),e.fill(),g>.02&&(e.save(),m(),e.clip(),e.fillStyle=i,e.globalAlpha*=Math.min(1,(g-.02)*12),e.beginPath(),e.ellipse(S,y,v,g*.55,0,0,Math.PI*2),e.fill(),e.restore()),e.strokeStyle=B;let f=e.lineWidth;e.lineWidth=f*.6,m(),e.stroke(),e.lineWidth=f;};if(C<.99){e.globalAlpha=1-C;let m=18,g=c=>o.mouthCurve*.075*(1-c*c),y=(c,T)=>{let R=c/m*2-1,W=S+R*A,ce=k+g(R)+o.mouthSkew*R*.03+o.mouthWave*.018*Math.sin((R+1)*Math.PI*2)+T*(1-R*R);return [W,ce]},v=Math.max(0,o.mouthOpen),f=()=>{for(let c=0;c<=m;c++){let[T,R]=y(c,-v*.15);c?e.lineTo(T,R):e.moveTo(T,R);}};v>.003?q(()=>{e.beginPath(),f();for(let T=m;T>=0;T--){let[R,W]=y(T,v);e.lineTo(R,W);}e.closePath();},v,y(m/2,v)[1],A*.55):(e.strokeStyle=B,e.beginPath(),f(),e.stroke());}if(C>.01){e.globalAlpha=C;let m=Math.max(.02,o.mouthOpen*.55+.01),g=()=>{e.beginPath(),e.ellipse(S,k+.01,m*.85,m,0,0,Math.PI*2);};e.lineWidth=be*.8,q(g,o.mouthOpen,k+.01+m*.8,m*.6),e.lineWidth=be;}e.globalAlpha=1;}function ze(e,o,t,s,i,r,d){let a=e.createRadialGradient(o,t,0,o,t,1);a.addColorStop(0,r),a.addColorStop(1,"rgba(0,0,0,0)"),e.save(),e.globalAlpha=d,e.translate(o,t),e.scale(s,i),e.translate(-o,-t),e.fillStyle=a,e.beginPath(),e.arc(o,t,1,0,Math.PI*2),e.fill(),e.restore();}function ye(e,o,t,s){e.beginPath(),e.moveTo(o,t+s*.85),e.bezierCurveTo(o-s*1.25,t+s*.05,o-s*.75,t-s*.95,o,t-s*.35),e.bezierCurveTo(o+s*.75,t-s*.95,o+s*1.25,t+s*.05,o,t+s*.85),e.closePath();}var J=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:w,cos:nt,abs:rt,max:se,exp:at,PI:ie}=Math;function ne(e,o,t,s){switch(s.y=s.rx=s.ry=s.rz=s.st=s.mouth=s.lookX=s.lookY=0,e){case "idle":{let i=w(o*1.6);s.y=.015*i,s.st=.018*i;break}case "thinking":{s.rz=.07*w(o*1.1),s.y=.02*w(o*2.2),s.ry=.15,s.rx=-0.06;break}case "loading":{let i=o*2.4;s.rx=.12*w(i),s.rz=.12*nt(i),s.ry=.22*w(i),s.y=.02*w(i*2),s.lookX=.55*w(i);break}case "listening":{let i=se(0,w(o*3.2));s.rx=.09*i*i;let r=rt(w(o*7.1)*.5+w(o*11.3)*.3+w(o*3.7)*.2);s.st=.03*r;break}case "speaking":{let i=se(0,w(o*9)*.6+w(o*14.3)*.4)*(.65+.35*w(o*1.7));s.mouth=.11*i,s.st=.04*i,s.rz=.03*w(o*2.1);break}case "happy":{let i=o*1.35%1;s.y=.2*w(ie*i);let r=se(0,1-i/.14)+se(0,(i-.9)/.1);s.st=.08*w(ie*i)-.14*r,s.rz=.05*w(o*1.35*ie);break}case "surprised":{if(t<.45){let i=w(ie*t/.45);s.y=.16*i,s.st=.12*i;}s.rz=.01*w(o*38)*at(-t*2),s.rx=-0.06;break}case "sad":{let i=w(o*1.1);s.y=-0.06,s.rx=.2,s.st=-0.04+.015*i,s.rz=-0.04;break}case "error":{let i=t%2.8;if(i<.55){let r=1-i/.55;s.ry=.26*w(i*42)*r,s.rz=.05*w(i*42+1)*r;}s.st=-0.02;break}case "sleeping":{let i=w(o*1);s.st=.04*i,s.y=-0.03,s.rz=.1,s.rx=.1;break}case "love":{s.y=.07+.06*w(o*1.6),s.rz=.05*w(o*.8),s.st=.015*w(o*1.6+1);break}}return s}function ve(e,o,t,s){for(let i of Object.keys(s))s[i]=e[i]+(o[i]-e[i])*t;return s}var Le={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var j=new Set,P=null,$=0,V=0,re=0,we,Ge=false,H="idle",D={x:0,y:0,lastMove:-1e9,known:false},z={reducedMotion:false,lowPower:false};function lt(){if(Ge)return;Ge=true,addEventListener("pointermove",o=>{D.x=o.clientX,D.y=o.clientY,D.lastMove=performance.now(),D.known=true;},{passive:true}),document.addEventListener("visibilitychange",_);let e=matchMedia("(prefers-reduced-motion: reduce)");z.reducedMotion=e.matches,e.addEventListener?.("change",o=>z.reducedMotion=o.matches);}function qe(e,o){return P||(P=new WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),P.setPixelRatio(1),P.setClearColor(0,0),z.lowPower=ct(P.getContext()),P.setScissorTest(true),P.domElement.addEventListener("webglcontextlost",t=>{t.preventDefault(),P?.dispose(),P=null,$=V=0,H="idle";})),(e>$||o>V)&&($=Math.max($,e),V=Math.max(V,o),P.setSize($,V,false)),P}var Ye=()=>V;function ct(e){try{let o=e.getExtension("WEBGL_debug_renderer_info"),t=String(e.getParameter(o?o.UNMASKED_RENDERER_WEBGL:e.RENDERER));return /swiftshader|llvmpipe|softpipe|software|basic render/i.test(t)}catch{return false}}function We(e,o){if(H==="ready")return true;if(H==="idle"&&P){H="compiling";let t=P;t.compileAsync(e,o).catch(()=>{}).then(()=>{t===P&&(H="ready"),_();});}return false}function De(e){lt(),clearTimeout(we),j.add(e),_();}function _e(e){j.delete(e),j.size===0&&(clearTimeout(we),we=setTimeout(()=>{j.size||!P||(P.dispose(),P.forceContextLoss(),P=null,$=V=0,H="idle");},1500));}function _(){if(!re){for(let e of j)if(e.active){re=requestAnimationFrame(Ne);return}}}var dt=10,Ie=new WeakMap,Me=0;function Ne(e){re=0;let o=[...j].filter(r=>r.active);if(!o.length)return;let t=performance.now(),s=o.length,i=0;for(;i<s;i++){let r=o[(Me+i)%s],d=Ie.get(r)??e-16;if(Ie.set(r,e),r.tick(Math.min(.05,Math.max(0,(e-d)/1e3)),e),performance.now()-t>dt){i++;break}}Me=(Me+i)%s,re=requestAnimationFrame(Ne);}var Z={pebble:{faceY:.02,faceScale:1.3,animated:false},blob:{faceY:.02,faceScale:1.28,animated:true},drop:{faceY:-0.24,faceScale:1.22,animated:false},bean:{faceY:.14,faceScale:.92,animated:false},cube:{faceY:0,faceScale:1.22,animated:false},ghost:{faceY:.22,faceScale:1.24,animated:true}};function Ve(e){let o=new SphereGeometry(1,e*2,Math.round(e*1.5)),t=o.getAttribute("position"),s=o.getIndex(),i=new Map,r=[],d=new Uint32Array(t.count);for(let n=0;n<t.count;n++){let h=t.getX(n),p=t.getY(n),b=t.getZ(n),E=`${Math.round(h*1e4)},${Math.round(p*1e4)},${Math.round(b*1e4)}`,S=i.get(E);if(S===void 0){S=r.length/3,i.set(E,S);let k=Math.hypot(h,p,b)||1;r.push(h/k,p/k,b/k);}d[n]=S;}let a=[];for(let n=0;n<s.count;n+=3){let h=d[s.getX(n)],p=d[s.getX(n+1)],b=d[s.getX(n+2)];h!==p&&p!==b&&h!==b&&a.push(h,p,b);}o.dispose();let l=new BufferGeometry,u=new Float32Array(r);return l.setAttribute("position",new BufferAttribute(new Float32Array(u),3)),l.setIndex(a),{geometry:l,dirs:u}}var ft=(e,o,t)=>{let s=Math.min(1,Math.max(0,(t-e)/(o-e)));return s*s*(3-2*s)};function ke(e,o,t,s,i,r,d){let a=o,l=t,u=s;switch(e){case "pebble":{a=o*1.1+.04*t,l=t*.92*(.95+.05*Math.tanh(3*t))+.03*o,u=s*.94;break}case "blob":{let n=.97+.045*Math.sin(2.1*o+1.3*i)*Math.sin(2.4*t+1.7*i+1)+.035*Math.sin(2.7*s+2.3*o+1.1*i);a=o*n,l=t*n,u=s*n;break}case "drop":{let n=Math.max(t,0),h=1-.8*Math.pow(n,1.6);a=o*h*.98+.1*n*n*n,l=(t<0?t*.9:t*1.32)-.12,u=s*h*.98;break}case "bean":{let n=t*.78+.42*Math.tanh(t*3);a=(o*.8+.14*(n*n/1.44)-.07)*.9,l=n*.88,u=s*.8*.9;break}case "cube":{let h=Math.pow(Math.pow(Math.abs(o),4.5)+Math.pow(Math.abs(t),4.5)+Math.pow(Math.abs(s),4.5),-0.2222222222222222);a=o*h*.84,l=t*h*.82,u=s*h*.84;break}case "ghost":{if(t>=0)a=o,l=t,u=s;else {let h=(Math.acos(Math.max(-1,t))-Math.PI/2)/(Math.PI/2),p=Math.hypot(o,s)||1,b=o/p,E=s/p,S=Math.atan2(s,o),k=.085*Math.sin(5*S+2.2*i),C=1.04,A=.13,B=-1.05+k,q;if(h<.45){let m=h/.45;q=1+.04*m,l=-m*(1.05-A)+k*ft(.45,1,m);}else if(h<.75){let m=(h-.45)/.3*(Math.PI/2);q=C-A+A*Math.cos(m),l=B+A-A*Math.sin(m);}else {let m=(h-.75)/.25;q=(C-A)*(1-m),l=B+.22*m*m*(3-2*m);}a=b*q,u=E*q;}a*=.9,l=(l+.1)*.9,u*=.9;break}}r[d]=a,r[d+1]=l,r[d+2]=u;}var I=null,ae=null;function bt(){if(typeof window>"u"||!("AudioContext"in window))return null;if(gt(),!I){I=new AudioContext;let e=I.createBiquadFilter();e.type="lowpass",e.frequency.value=3200,ae=I.createGain(),ae.gain.value=.55,ae.connect(e).connect(I.destination);}return I.state==="suspended"&&I.resume(),I}function F(e,{freq:o,to:t,at:s=0,dur:i=.12,type:r="sine",gain:d=.1,wobble:a=0}){let l=e.currentTime+s,u=e.createOscillator(),n=e.createGain();if(u.type=r,u.frequency.setValueAtTime(o,l),t&&u.frequency.exponentialRampToValueAtTime(t,l+i),a){let h=e.createOscillator(),p=e.createGain();h.frequency.value=7,p.gain.value=a,h.connect(p).connect(u.frequency),h.start(l),h.stop(l+i+.05);}n.gain.setValueAtTime(1e-4,l),n.gain.exponentialRampToValueAtTime(d,l+Math.min(.012,i/4)),n.gain.exponentialRampToValueAtTime(1e-4,l+i),u.connect(n).connect(ae),u.start(l),u.stop(l+i+.02);}var Se={idle:e=>F(e,{freq:520,to:780,dur:.09,gain:.08}),thinking:e=>[660,760,900].forEach((o,t)=>F(e,{freq:o,to:o*1.08,at:t*.1,dur:.07,type:"triangle",gain:.06})),loading:e=>[0,.14,.28].forEach(o=>F(e,{freq:940,at:o,dur:.04,gain:.05})),listening:e=>{F(e,{freq:523,dur:.12,type:"triangle",gain:.07}),F(e,{freq:784,at:.1,dur:.22,type:"triangle",gain:.07});},speaking:e=>[380,460,340,520,420].forEach((o,t)=>F(e,{freq:o,to:o*1.15,at:t*.075,dur:.06,type:"triangle",gain:.06})),happy:e=>[523,659,784,1047].forEach((o,t)=>F(e,{freq:o,at:t*.07,dur:.14,type:"triangle",gain:.07})),surprised:e=>F(e,{freq:280,to:980,dur:.18,gain:.09,wobble:30}),sad:e=>{F(e,{freq:560,to:300,dur:.5,gain:.07}),F(e,{freq:420,to:240,at:.12,dur:.5,gain:.04});},error:e=>[0,.14].forEach(o=>F(e,{freq:170,to:150,at:o,dur:.1,type:"sawtooth",gain:.045})),sleeping:e=>{F(e,{freq:150,to:115,dur:.6,gain:.07,wobble:6}),F(e,{freq:300,to:230,at:.05,dur:.5,gain:.02});},love:e=>[1319,1568,2093,1760].forEach((o,t)=>F(e,{freq:o,at:t*.08,dur:.22,gain:.04}))},Y=null,Xe=false;function gt(){if(Xe)return;Xe=true;let e=()=>{I&&I.resume().then(()=>{Y&&performance.now()<Y.until&&Se[Y.mood](I),Y=null;});};for(let o of ["pointerdown","touchend","keydown"])addEventListener(o,e,{passive:true});}function he(e,o=false){let t=bt();if(t){if(t.state==="running"){Y=null,Se[e](t);return}Y={mood:e,until:performance.now()+(o?6e3:1200)},t.resume().then(()=>{Y&&performance.now()<Y.until&&Se[Y.mood](t),Y=null;});}}var Ee=-0.85,je=new Map;function Tt(e){let o=je.get(e);if(!o){let{geometry:t,dirs:s}=Ve(e);o={dirs:s,index:new Uint32Array(t.getIndex().array)},t.dispose(),je.set(e,o);}return o}var K=(e,o)=>1-Math.exp(-e*o),Te=e=>1-Math.pow(1-e,3),le=class{constructor(){this.scene=new Scene;this.camera=new PerspectiveCamera(30,1,.1,50);this.group=new Group;this.v=new Vector3;this.geometry=new BufferGeometry;this.faceCanvas=document.createElement("canvas");this.uniforms={uFace:{value:null},uFaceY:{value:0},uFaceScale:{value:1.5}};this.state={...M};this.visible=false;this.connected=false;this.w=0;this.h=0;this.t=Math.random()*100;this.shapeFrom=M.shape;this.shapeTo=M.shape;this.morph=1;this.shapeDirty=true;this.shown=M.mood;this.prevShown=M.mood;this.tau=10;this.prevTau=10;this.blend=1;this.reaction=null;this.pose=J();this.poseIdle=J();this.moodTime=0;this.poseA=J();this.poseB=J();this.face=ge(oe.idle);this.target=ge(oe.idle);this.drawn=new Float32Array(U.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=X(M.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=ee(this.body);this.faceCT=this.faceC;this.cheek=te(this.body);this.cheekT=this.cheek;this.look={x:0,y:0};this.wander={x:0,y:0,next:0};this.squash=0;this.squashV=0;this.pop=.6;this.popV=0;this.hover=0;this.hovering=false;this.overlay={dots:0,zz:0,hearts:0,waves:0,bang:0};this.needsFrame=true;this.ready=false;this.css=[0,0];this.quality=1;this.lowPowerApplied=false;this.cost=0;this.costFrames=0;this.lastFrame=[];this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d"),this.faceCtx=this.faceCanvas.getContext("2d"),this.faceCanvas.width=this.faceCanvas.height=256,this.faceTex=new CanvasTexture(this.faceCanvas),this.faceTex.colorSpace=SRGBColorSpace,this.faceTex.minFilter=LinearMipmapLinearFilter,this.faceTex.anisotropy=4,this.uniforms.uFace.value=this.faceTex,this.setSegments(24),this.material=this.createMaterial(),this.mesh=new Mesh(this.geometry,this.material),this.mesh.frustumCulled=false,this.mesh.position.y=-Ee,this.group.add(this.mesh),this.scene.add(this.group),this.scene.add(new HemisphereLight(16774120,7234140,1.7));let o=new DirectionalLight(16777215,1.9);o.position.set(2.5,3.5,4);let t=new DirectionalLight(16770770,1.4);t.position.set(-3.5,2,-3);let s=new DirectionalLight(16773350,.45);s.position.set(-2,-1,3),this.scene.add(o,t,s),this.camera.position.set(0,.3,6.6),this.camera.lookAt(0,.12,0);}get active(){return this.visible&&this.connected&&this.w>0}createMaterial(){let o=new MeshStandardMaterial({roughness:.62,metalness:0});return o.color.setRGB(...this.body,SRGBColorSpace),o.customProgramCacheKey=()=>"moodi-1",o.onBeforeCompile=t=>{Object.assign(t.uniforms,this.uniforms),t.vertexShader=t.vertexShader.replace("#include <common>",`#include <common>
2
2
  varying vec3 vFaceP;
3
3
  varying vec3 vFaceN;`).replace("#include <begin_vertex>",`#include <begin_vertex>
4
4
  vFaceP = position;
@@ -20,9 +20,9 @@ outgoingLight *= mix(0.62, 1.0, smoothstep(-0.95, 0.35, normalize(vFaceN).y));
20
20
  // eyes and mouth glow a little: mostly unlit, so they read as light, not paint
21
21
  float emis = smoothstep(0.75, 1.0, fc.a) * inb * smoothstep(0.12, 0.45, normalize(vFaceN).z);
22
22
  outgoingLight = mix(outgoingLight, diffuseColor.rgb * 1.12, emis * 0.65);
23
- #include <opaque_fragment>`);},o}setSegments(o){let t=Tt(o);this.dirs!==t.dirs&&(this.dirs=t.dirs,this.geometry.dispose(),this.geometry=new BufferGeometry,this.geometry.setAttribute("position",new BufferAttribute(new Float32Array(t.dirs.length),3)),this.geometry.setIndex(new BufferAttribute(t.index,1)),this.mesh&&(this.mesh.geometry=this.geometry),this.shapeDirty=true);}connect(){this.connected=true,We(this);}disconnect(){this.connected=false,De(this);}setVisible(o){this.visible=o,o&&_();}resize(o,t){this.css=[o,t];let s=(z.lowPower?1:Math.min(2,devicePixelRatio||1))*this.quality,i=Math.max(1,Math.round(o*s)),r=Math.max(1,Math.round(t*s));if(i===this.w&&r===this.h)return;this.w=this.canvas.width=i,this.h=this.canvas.height=r;let h=Math.min(i,r),c=Math.min(o,t);this.camera.position.set(0,.3,c<90?5.1:6.1),this.camera.lookAt(0,c<90?.02:.12,0),this.camera.aspect=i/r,this.camera.updateProjectionMatrix(),this.setSegments(h<160?16:h<400?24:32);let l=h<220?256:512;this.faceCanvas.width!==l&&(this.faceCanvas.width=this.faceCanvas.height=l,this.faceTex.dispose(),this.drawn.fill(NaN)),this.needsFrame=true,_();}update(o){let t=this.state;this.state={...t,...o};let s=this.state;s.shape!==this.shapeTo&&(this.shapeFrom=this.shapeTo,this.shapeTo=s.shape,this.morph=0,z.reducedMotion||(this.squashV-=2.2)),(s.color!==t.color||s.faceColor!==t.faceColor||s.cheekColor!==t.cheekColor)&&(this.bodyT=X(s.color,X(M.color,this.bodyT)),this.faceCT=s.faceColor?X(s.faceColor,Q(this.bodyT)):Q(this.bodyT),this.cheekT=s.cheekColor?X(s.cheekColor,ee(this.bodyT)):ee(this.bodyT)),s.mood!==t.mood&&!this.reaction&&this.show(s.mood),s.sound&&!t.sound&&this.ready&&ae(this.shown),this.needsFrame=true,_();}bump(o=1){z.reducedMotion||(this.squashV-=1.6*o,this.needsFrame=true,_());}react(o,t=1200){this.reaction={mood:o,left:t/1e3},this.show(o);}get currentMood(){return this.shown}dispose(){this.disconnect(),this.geometry.dispose(),this.material.dispose(),this.faceTex.dispose(),this.onMoodChange=void 0;}show(o,t=true){if(o===this.shown)return;t&&this.state.sound&&this.ready&&ae(o);let s=this.shown;this.prevShown=s,this.prevTau=this.tau,this.shown=o,this.tau=0,this.blend=0,z.reducedMotion||(this.squashV-=1.1),this.onMoodChange?.({mood:o,previous:s,reaction:!!this.reaction});}tick(o,t){let s=this.state,i=z.reducedMotion,r=z.lowPower?0:o*Math.max(0,s.speed);this.t+=r,this.tau+=r,this.prevTau+=r,this.reaction&&(this.reaction.left-=o,this.reaction.left<=0&&(this.reaction=null,this.show(s.mood,false))),ye(this.shown,this.t,this.tau,this.poseB),this.blend<1?(this.blend=Math.min(1,this.blend+o/.45),ye(this.prevShown,this.t,this.prevTau,this.poseA),Oe(this.poseA,this.poseB,je(this.blend),this.pose)):Object.assign(this.pose,this.poseB);let h=this.pose;i&&(h.y=h.rx=h.ry=h.rz=h.st=h.lookX=0);let c=s.followCursor&&!i&&s.speed>0?Fe[this.shown]:0,l=0,p=0;if(c>0&&D.known){let d=this.canvas.getBoundingClientRect(),y=Math.max(200,d.width*.85);l=Math.max(-1,Math.min(1,(D.x-(d.left+d.width/2))/y)),p=Math.max(-1,Math.min(1,(D.y-(d.top+d.height*.45))/y)),l*=c,p*=c;}else if(this.shown==="idle"&&!i&&s.speed>0){if(this.t>this.wander.next){let d=Math.random()<.4;this.wander.x=d?0:(Math.random()*2-1)*.6,this.wander.y=d?0:(Math.random()*2-1)*.35,this.wander.next=this.t+1.8+Math.random()*2.6;}l=this.wander.x,p=this.wander.y;}let n=K(7,o);this.look.x+=(l-this.look.x)*n,this.look.y+=(p-this.look.y)*n;let a=te[this.shown],u=this.target;for(let d of U)u[d]=a[d];u.lookX+=this.look.x+h.lookX,u.lookY+=this.look.y+h.lookY,u.mouthOpen+=h.mouth,u.eyeOpen+=this.hover*.08;let w=K(11,o);for(let d of U)this.face[d]+=(u[d]-this.face[d])*w;let A=K(16,o);if(this.face.lookX+=(u.lookX-this.face.lookX)*A,this.face.lookY+=(u.lookY-this.face.lookY)*A,this.face.mouthOpen+=(u.mouthOpen-this.face.mouthOpen)*A,this.nextBlink-=r,this.nextBlink<=0&&this.blinkT<0&&(this.blinkT=0,this.nextBlink=Math.random()<.2?.25:2.2+Math.random()*3.8),this.blinkT>=0){this.blinkT+=r;let d=this.blinkT/.16;this.blink=d>=1?1:1-Math.sin(Math.PI*d),d>=1&&(this.blinkT=-1);}let k=Math.min(o,1/30);this.squashV+=(-170*this.squash-11*this.squashV)*k,this.squash+=this.squashV*k,i&&(this.squash=this.squashV=0),i?this.pop=1:(this.popV+=(-140*(this.pop-1)-13*this.popV)*k,this.pop+=this.popV*k),this.hover+=((this.hovering&&s.interactive?1:0)-this.hover)*K(12,o);let S=K(9,o);this.body=x(this.body,this.bodyT,S),this.faceC=x(this.faceC,this.faceCT,S),this.cheek=x(this.cheek,this.cheekT,S),this.material.color.setRGB(this.body[0],this.body[1],this.body[2],SRGBColorSpace);let E=Be[this.shown],C=K(6,o);for(let d of Object.keys(this.overlay))this.overlay[d]+=((E===d?1:0)-this.overlay[d])*C;this.morph<1&&(this.morph=Math.min(1,this.morph+o/(i?.2:.38)),this.shapeDirty=true);let O=J[this.shapeTo].animated||this.morph<1&&J[this.shapeFrom].animated;(this.shapeDirty||O&&r>0)&&this.rebuild();let B=this.group,m=(1+h.st+this.squash)*(1+this.hover*.04),f=1/Math.sqrt(Math.max(.3,1+h.st+this.squash))*(1+this.hover*.04);B.position.y=Ae+h.y,B.rotation.set(h.rx+this.look.y*.22*(c>0?1:0),h.ry+this.look.x*.42,h.rz),B.scale.set(f*this.pop,m*this.pop,f*this.pop);let g=this.drawFaceIfNeeded();if((s.speed===0||z.lowPower)&&!g&&!this.needsFrame&&this.settled())return;let b=performance.now();this.render(h)&&(this.needsFrame=false,this.ready||(this.ready=true,this.state.sound&&ae(this.shown,true),this.onReady?.(),this.onReady=void 0),this.adapt(performance.now()-b));}adapt(o){this.cost=this.costFrames?this.cost*.9+o*.1:o,!(++this.costFrames<20||this.cost<9||this.quality<=.5)&&(this.quality=Math.max(.5,this.quality-.25),this.costFrames=0,this.resize(this.css[0],this.css[1]));}settled(){let o=this.group,t=this.overlay,s=[t.bang,o.position.y,o.rotation.x,o.rotation.y,o.rotation.z,o.scale.x,o.scale.y,...this.body,this.morph,t.dots,t.zz,t.hearts,t.waves],i=this.lastFrame,r=i.length===s.length;for(let h=0;r&&h<s.length;h++)r=Math.abs(s[h]-i[h])<1e-4;return this.lastFrame=s,r}rebuild(){let o=this.geometry.getAttribute("position"),t=o.array,s=this.dirs,i=je(this.morph),r=this.shapeFrom,h=this.shapeTo,c=new Float32Array(3);for(let n=0;n<s.length;n+=3)we(h,s[n],s[n+1],s[n+2],this.t,t,n),i<1&&(we(r,s[n],s[n+1],s[n+2],this.t,c,0),t[n]=c[0]+(t[n]-c[0])*i,t[n+1]=c[1]+(t[n+1]-c[1])*i,t[n+2]=c[2]+(t[n+2]-c[2])*i);o.needsUpdate=true,this.geometry.computeVertexNormals();let l=J[r],p=J[h];this.uniforms.uFaceY.value=l.faceY+(p.faceY-l.faceY)*i,this.uniforms.uFaceScale.value=l.faceScale+(p.faceScale-l.faceScale)*i,this.shapeDirty=false;}drawFaceIfNeeded(){let o=this.face,t=this.drawn,s=false,i=0;for(let c of U)Math.abs(t[i]-o[c])<.002||(s=true,t[i]=o[c]),i++;let r=[this.blink,...this.faceC,...this.cheek];for(let c of r)Math.abs(t[i]-c)<.002||(s=true,t[i]=c),i++;if(!s)return false;let h=pe(this.faceC)>.45;return ze(this.faceCtx,o,this.blink,G(this.faceC),G(this.cheek),h?1:.3,G(x(this.faceC,[1,1,1],h?.6:.16)),h?G(x(this.body,[.04,.03,.06],.86)):"#fff",G(h?x(this.body,[.03,.02,.05],.78):x(this.faceC,[0,0,0],.35)),G(this.faceC)),this.faceTex.needsUpdate=true,true}project(o,t,s){let i=this.v.set(o,t,s).applyMatrix4(this.group.matrixWorld).project(this.camera);return [(i.x*.5+.5)*this.w,(-i.y*.5+.5)*this.h]}render(o){let{w:t,h:s,ctx:i}=this,r=qe(t,s);if(z.lowPower&&!this.lowPowerApplied){this.lowPowerApplied=true;let[h,c]=this.css;return this.w=this.h=0,this.resize(h,c),false}if(!Ye(this.scene,this.camera))return false;if(i.clearRect(0,0,t,s),this.group.updateMatrixWorld(),this.state.shadow){let h=this.v.set(0,Ae-.08,0).project(this.camera),c=(h.x*.5+.5)*t,l=(-h.y*.5+.5)*s,p=Math.max(0,o.y)*3,n=Math.min(t,s)*.26*(1-p*.25)*this.pop,a=i.createRadialGradient(c,l,0,c,l,n),u=x(this.body,[.05,.03,.02],.75);a.addColorStop(0,`rgba(${u[0]*255|0},${u[1]*255|0},${u[2]*255|0},${.42*(1-p*.4)})`),a.addColorStop(1,"rgba(0,0,0,0)"),i.fillStyle=a,i.save(),i.translate(c,l),i.scale(1,.22),i.translate(-c,-l),i.beginPath(),i.arc(c,l,n,0,Math.PI*2),i.fill(),i.restore();}return r.setViewport(0,0,t,s),r.setScissor(0,0,t,s),r.render(this.scene,this.camera),i.drawImage(r.domElement,0,Ie()-s,t,s,0,0,t,s),this.drawOverlays(),true}drawOverlays(){let o=this.overlay;if(o.dots+o.zz+o.hearts+o.waves+o.bang<.01)return;let{ctx:t}=this,s=this.t,i=z.reducedMotion,r=Math.min(this.w,this.h),h=x(this.body,[1,1,1],.22),c=G(h);if(t.save(),o.dots>.01){let[l,p]=this.project(.6,.92,.2),n=i?2:s*.55%1.6,a=[.014,.022,.052];for(let u=0;u<3;u++){let w=i?1:Pe(Ee((n-u*.18)/.32)),A=i?0:Math.sin(s*2.2+u)*r*.006,k=l+[0,.05,.12][u]*r,S=p-[0,.05,.12][u]*r+A,E=a[u]*r*w;if(!(E<=.2)&&(t.globalAlpha=o.dots*.95,t.fillStyle=c,t.beginPath(),u<2?t.arc(k,S,E,0,Math.PI*2):Pt(t,k,S,E),t.fill(),u===2&&w>.6)){t.fillStyle=G(x(this.body,[0,0,0],.35));for(let C=0;C<3;C++){let O=i?0:Math.max(0,Math.sin(s*7-C*.9))*E*.16;t.beginPath(),t.arc(k+(C-1)*E*.34,S-O,E*.1,0,Math.PI*2),t.fill();}}}}if(o.zz>.01){let[l,p]=this.project(.45,.85,.2);t.textAlign="center",t.textBaseline="middle",t.fillStyle=c;for(let n=0;n<3;n++){let a=i?.25+n*.25:(s*.33+n/3)%1,u=l+a*r*.17+Math.sin(a*7+n)*r*.018,w=p-Te(a)*r*.26;t.globalAlpha=o.zz*Math.min(1,a*5)*(1-a)*1.3,t.save(),t.translate(u,w),t.rotate(-0.25+Math.sin(a*5+n)*.22),t.font=`800 ${Math.round(r*(.05+.06*a))}px ui-rounded, system-ui, sans-serif`,t.fillText("z",0,0),t.restore();}}if(o.hearts>.01){let[l,p]=this.project(0,.8,.2),n=x(this.cheek,[1,.3,.45],.55);for(let a=0;a<4;a++){let u=i?.4:(s*.3+a/4)%1,w=a%2?1:-1,A=l+w*r*(.16+.07*(a>>1))+Math.sin(u*6+a)*r*.025,k=p-r*.02-Te(u)*r*.28,S=i?1:Pe(Ee(u/.22)),E=r*(.026+.008*(a%3))*S;t.globalAlpha=o.hearts*(1-Math.max(0,(u-.6)/.4)),t.save(),t.translate(A,k),t.rotate(Math.sin(s*3+a)*.25);let C=t.createLinearGradient(0,-E,0,E);C.addColorStop(0,G(x(n,[1,1,1],.35))),C.addColorStop(1,G(n)),t.fillStyle=C,ge(t,0,0,E),t.fill(),t.restore();}}if(o.waves>.01){t.lineCap="round",t.strokeStyle=c;for(let l of [-1,1]){let[p,n]=this.project(l*1,.12,0);for(let a=0;a<3;a++){let u=i?.3+a*.2:(s*.8+a/3)%1,w=Te(u);t.globalAlpha=o.waves*Math.sin(Math.PI*u)*.9,t.lineWidth=r*(.016-.008*u),t.beginPath();let A=r*(.03+.11*w);l>0?t.arc(p-r*.03,n,A,-0.55,.55):t.arc(p+r*.03,n,A,Math.PI-.55,Math.PI+.55),t.stroke();}}}if(o.bang>.01){let[l,p]=this.project(.55,1,.2),n=i?1:Pe(Ee(this.tau/.35));t.globalAlpha=o.bang,t.fillStyle=c,t.save(),t.translate(l,p),t.rotate(.18),t.scale(n,n),t.beginPath(),t.roundRect(-r*.016,-r*.1,r*.032,r*.07,r*.016),t.arc(0,r*.005,r*.018,0,Math.PI*2),t.fill(),t.restore();}t.restore();}},Ee=e=>e<0?0:e>1?1:e,Te=e=>1-(1-e)*(1-e),Pe=e=>1+(1.9+1)*Math.pow(e-1,3)+1.9*Math.pow(e-1,2);function Pt(e,o,t,s){e.ellipse(o,t,s,s*.72,0,0,Math.PI*2),e.moveTo(o-s*.35+s*.45,t-s*.38),e.arc(o-s*.35,t-s*.38,s*.45,0,Math.PI*2),e.moveTo(o+s*.3+s*.5,t-s*.32),e.arc(o+s*.3,t-s*.32,s*.5,0,Math.PI*2);}var xt=["shape","mood","color","face-color","cheek-color","size","speed","follow-cursor","interactive","shadow","sound"],N=(e,o)=>e===null?o:e!=="false"&&e!=="0",Rt=e=>e==null||e===""?`${M.size}px`:typeof e=="number"||/^\d+(\.\d+)?$/.test(String(e))?`${e}px`:String(e),Ft=`:host{display:inline-block;position:relative;cursor:pointer;vertical-align:middle;contain:layout paint style;-webkit-tap-highlight-color:transparent}
23
+ #include <opaque_fragment>`);},o}setSegments(o){let t=Tt(o);this.dirs!==t.dirs&&(this.dirs=t.dirs,this.geometry.dispose(),this.geometry=new BufferGeometry,this.geometry.setAttribute("position",new BufferAttribute(new Float32Array(t.dirs.length),3)),this.geometry.setIndex(new BufferAttribute(t.index,1)),this.mesh&&(this.mesh.geometry=this.geometry),this.shapeDirty=true);}connect(){this.connected=true,De(this);}disconnect(){this.connected=false,_e(this);}setVisible(o){this.visible=o,o&&_();}resize(o,t){this.css=[o,t];let s=(z.lowPower?1:Math.min(2,devicePixelRatio||1))*this.quality,i=Math.max(1,Math.round(o*s)),r=Math.max(1,Math.round(t*s));if(i===this.w&&r===this.h)return;this.w=this.canvas.width=i,this.h=this.canvas.height=r;let d=Math.min(i,r),a=Math.min(o,t);this.camera.position.set(0,.3,a<90?5.1:6.1),this.camera.lookAt(0,a<90?.02:.12,0),this.camera.aspect=i/r,this.camera.updateProjectionMatrix(),this.setSegments(d<160?16:d<400?24:32);let l=d<220?256:512;this.faceCanvas.width!==l&&(this.faceCanvas.width=this.faceCanvas.height=l,this.faceTex.dispose(),this.drawn.fill(NaN)),this.needsFrame=true,_();}update(o){let t=this.state;this.state={...t,...o};let s=this.state;s.shape!==this.shapeTo&&(this.shapeFrom=this.shapeTo,this.shapeTo=s.shape,this.morph=0,z.reducedMotion||(this.squashV-=2.2)),(s.color!==t.color||s.faceColor!==t.faceColor||s.cheekColor!==t.cheekColor)&&(this.bodyT=X(s.color,X(M.color,this.bodyT)),this.faceCT=s.faceColor?X(s.faceColor,ee(this.bodyT)):ee(this.bodyT),this.cheekT=s.cheekColor?X(s.cheekColor,te(this.bodyT)):te(this.bodyT)),s.mood!==t.mood&&!this.reaction&&this.show(s.mood),s.sound&&!t.sound&&this.ready&&he(this.shown),this.needsFrame=true,_();}bump(o=1){z.reducedMotion||(this.squashV-=1.6*o,this.needsFrame=true,_());}react(o,t=1200){this.reaction={mood:o,left:t/1e3},this.show(o);}get currentMood(){return this.shown}dispose(){this.disconnect(),this.geometry.dispose(),this.material.dispose(),this.faceTex.dispose(),this.onMoodChange=void 0;}show(o,t=true){if(o===this.shown)return;t&&this.state.sound&&this.ready&&he(o);let s=this.shown;this.prevShown=s,this.prevTau=this.tau,this.shown=o,this.tau=0,this.moodTime=0,this.blend=0,z.reducedMotion||(this.squashV-=1.1),this.onMoodChange?.({mood:o,previous:s,reaction:!!this.reaction});}tick(o,t){let s=this.state,i=z.reducedMotion,r=z.lowPower?0:o*Math.max(0,s.speed);this.t+=r,this.tau+=r,this.prevTau+=r,this.reaction&&(this.reaction.left-=o,this.reaction.left<=0&&(this.reaction=null,this.show(s.mood,false))),this.moodTime+=o;let d=s.settle>0&&this.shown!=="idle"?Te(Math.min(1,Math.max(0,(this.moodTime-s.settle/1e3)/.7))):0;ne(this.shown,this.t,this.tau,this.poseB),d>0&&ve(this.poseB,ne("idle",this.t,this.tau,this.poseIdle),d,this.poseB),this.blend<1?(this.blend=Math.min(1,this.blend+o/.45),ne(this.prevShown,this.t,this.prevTau,this.poseA),ve(this.poseA,this.poseB,Te(this.blend),this.pose)):Object.assign(this.pose,this.poseB);let a=this.pose;i&&(a.y=a.rx=a.ry=a.rz=a.st=a.lookX=0);let l=s.followCursor&&!i&&s.speed>0?Be[this.shown]:0,u=0,n=0;if(l>0&&D.known){let c=this.canvas.getBoundingClientRect(),T=Math.max(200,c.width*.85);u=Math.max(-1,Math.min(1,(D.x-(c.left+c.width/2))/T)),n=Math.max(-1,Math.min(1,(D.y-(c.top+c.height*.45))/T)),u*=l,n*=l;}else if(this.shown==="idle"&&!i&&s.speed>0){if(this.t>this.wander.next){let c=Math.random()<.4;this.wander.x=c?0:(Math.random()*2-1)*.6,this.wander.y=c?0:(Math.random()*2-1)*.35,this.wander.next=this.t+1.8+Math.random()*2.6;}u=this.wander.x,n=this.wander.y;}let h=K(7,o);this.look.x+=(u-this.look.x)*h,this.look.y+=(n-this.look.y)*h;let p=oe[this.shown],b=this.target;for(let c of U)b[c]=p[c];b.lookX+=this.look.x+a.lookX,b.lookY+=this.look.y+a.lookY,b.mouthOpen+=a.mouth,b.eyeOpen+=this.hover*.08;let E=K(11,o);for(let c of U)this.face[c]+=(b[c]-this.face[c])*E;let S=K(16,o);if(this.face.lookX+=(b.lookX-this.face.lookX)*S,this.face.lookY+=(b.lookY-this.face.lookY)*S,this.face.mouthOpen+=(b.mouthOpen-this.face.mouthOpen)*S,this.nextBlink-=r,this.nextBlink<=0&&this.blinkT<0&&(this.blinkT=0,this.nextBlink=Math.random()<.2?.25:2.2+Math.random()*3.8),this.blinkT>=0){this.blinkT+=r;let c=this.blinkT/.16;this.blink=c>=1?1:1-Math.sin(Math.PI*c),c>=1&&(this.blinkT=-1);}let k=Math.min(o,1/30);this.squashV+=(-170*this.squash-11*this.squashV)*k,this.squash+=this.squashV*k,i&&(this.squash=this.squashV=0),i?this.pop=1:(this.popV+=(-140*(this.pop-1)-13*this.popV)*k,this.pop+=this.popV*k),this.hover+=((this.hovering&&s.interactive?1:0)-this.hover)*K(12,o);let C=K(9,o);this.body=x(this.body,this.bodyT,C),this.faceC=x(this.faceC,this.faceCT,C),this.cheek=x(this.cheek,this.cheekT,C),this.material.color.setRGB(this.body[0],this.body[1],this.body[2],SRGBColorSpace);let A=Le[this.shown],B=K(6,o);for(let c of Object.keys(this.overlay))this.overlay[c]+=((A===c?1-d:0)-this.overlay[c])*B;this.morph<1&&(this.morph=Math.min(1,this.morph+o/(i?.2:.38)),this.shapeDirty=true);let q=Z[this.shapeTo].animated||this.morph<1&&Z[this.shapeFrom].animated;(this.shapeDirty||q&&r>0)&&this.rebuild();let m=this.group,g=(1+a.st+this.squash)*(1+this.hover*.04),y=1/Math.sqrt(Math.max(.3,1+a.st+this.squash))*(1+this.hover*.04);m.position.y=Ee+a.y,m.rotation.set(a.rx+this.look.y*.22*(l>0?1:0),a.ry+this.look.x*.42,a.rz),m.scale.set(y*this.pop,g*this.pop,y*this.pop);let v=this.drawFaceIfNeeded();if((s.speed===0||z.lowPower)&&!v&&!this.needsFrame&&this.settled())return;let f=performance.now();this.render(a)&&(this.needsFrame=false,this.ready||(this.ready=true,this.state.sound&&he(this.shown,true),this.onReady?.(),this.onReady=void 0),this.adapt(performance.now()-f));}adapt(o){this.cost=this.costFrames?this.cost*.9+o*.1:o,!(++this.costFrames<20||this.cost<9||this.quality<=.5)&&(this.quality=Math.max(.5,this.quality-.25),this.costFrames=0,this.resize(this.css[0],this.css[1]));}settled(){let o=this.group,t=this.overlay,s=[t.bang,o.position.y,o.rotation.x,o.rotation.y,o.rotation.z,o.scale.x,o.scale.y,...this.body,this.morph,t.dots,t.zz,t.hearts,t.waves],i=this.lastFrame,r=i.length===s.length;for(let d=0;r&&d<s.length;d++)r=Math.abs(s[d]-i[d])<1e-4;return this.lastFrame=s,r}rebuild(){let o=this.geometry.getAttribute("position"),t=o.array,s=this.dirs,i=Te(this.morph),r=this.shapeFrom,d=this.shapeTo,a=new Float32Array(3);for(let n=0;n<s.length;n+=3)ke(d,s[n],s[n+1],s[n+2],this.t,t,n),i<1&&(ke(r,s[n],s[n+1],s[n+2],this.t,a,0),t[n]=a[0]+(t[n]-a[0])*i,t[n+1]=a[1]+(t[n+1]-a[1])*i,t[n+2]=a[2]+(t[n+2]-a[2])*i);o.needsUpdate=true,this.geometry.computeVertexNormals();let l=Z[r],u=Z[d];this.uniforms.uFaceY.value=l.faceY+(u.faceY-l.faceY)*i,this.uniforms.uFaceScale.value=l.faceScale+(u.faceScale-l.faceScale)*i,this.shapeDirty=false;}drawFaceIfNeeded(){let o=this.face,t=this.drawn,s=false,i=0;for(let a of U)Math.abs(t[i]-o[a])<.002||(s=true,t[i]=o[a]),i++;let r=[this.blink,...this.faceC,...this.cheek];for(let a of r)Math.abs(t[i]-a)<.002||(s=true,t[i]=a),i++;if(!s)return false;let d=me(this.faceC)>.45;return Oe(this.faceCtx,o,this.blink,L(this.faceC),L(this.cheek),d?1:.3,L(x(this.faceC,[1,1,1],d?.6:.16)),d?L(x(this.body,[.04,.03,.06],.86)):"#fff",L(d?x(this.body,[.03,.02,.05],.78):x(this.faceC,[0,0,0],.35)),L(this.faceC)),this.faceTex.needsUpdate=true,true}project(o,t,s){let i=this.v.set(o,t,s).applyMatrix4(this.group.matrixWorld).project(this.camera);return [(i.x*.5+.5)*this.w,(-i.y*.5+.5)*this.h]}render(o){let{w:t,h:s,ctx:i}=this,r=qe(t,s);if(z.lowPower&&!this.lowPowerApplied){this.lowPowerApplied=true;let[d,a]=this.css;return this.w=this.h=0,this.resize(d,a),false}if(!We(this.scene,this.camera))return false;if(i.clearRect(0,0,t,s),this.group.updateMatrixWorld(),this.state.shadow){let d=this.v.set(0,Ee-.08,0).project(this.camera),a=(d.x*.5+.5)*t,l=(-d.y*.5+.5)*s,u=Math.max(0,o.y)*3,n=Math.min(t,s)*.26*(1-u*.25)*this.pop,h=i.createRadialGradient(a,l,0,a,l,n),p=x(this.body,[.05,.03,.02],.75);h.addColorStop(0,`rgba(${p[0]*255|0},${p[1]*255|0},${p[2]*255|0},${.42*(1-u*.4)})`),h.addColorStop(1,"rgba(0,0,0,0)"),i.fillStyle=h,i.save(),i.translate(a,l),i.scale(1,.22),i.translate(-a,-l),i.beginPath(),i.arc(a,l,n,0,Math.PI*2),i.fill(),i.restore();}return r.setViewport(0,0,t,s),r.setScissor(0,0,t,s),r.render(this.scene,this.camera),i.drawImage(r.domElement,0,Ye()-s,t,s,0,0,t,s),this.drawOverlays(),true}drawOverlays(){let o=this.overlay;if(o.dots+o.zz+o.hearts+o.waves+o.bang<.01)return;let{ctx:t}=this,s=this.t,i=z.reducedMotion,r=Math.min(this.w,this.h),d=x(this.body,[1,1,1],.22),a=L(d);if(t.save(),o.dots>.01){let[l,u]=this.project(.6,.92,.2),n=i?2:s*.55%1.6,h=[.014,.022,.052];for(let p=0;p<3;p++){let b=i?1:Re(Pe((n-p*.18)/.32)),E=i?0:Math.sin(s*2.2+p)*r*.006,S=l+[0,.05,.12][p]*r,k=u-[0,.05,.12][p]*r+E,C=h[p]*r*b;if(!(C<=.2)&&(t.globalAlpha=o.dots*.95,t.fillStyle=a,t.beginPath(),p<2?t.arc(S,k,C,0,Math.PI*2):Pt(t,S,k,C),t.fill(),p===2&&b>.6)){t.fillStyle=L(x(this.body,[0,0,0],.35));for(let A=0;A<3;A++){let B=i?0:Math.max(0,Math.sin(s*7-A*.9))*C*.16;t.beginPath(),t.arc(S+(A-1)*C*.34,k-B,C*.1,0,Math.PI*2),t.fill();}}}}if(o.zz>.01){let[l,u]=this.project(.45,.85,.2);t.textAlign="center",t.textBaseline="middle",t.fillStyle=a;for(let n=0;n<3;n++){let h=i?.25+n*.25:(s*.33+n/3)%1,p=l+h*r*.17+Math.sin(h*7+n)*r*.018,b=u-xe(h)*r*.26;t.globalAlpha=o.zz*Math.min(1,h*5)*(1-h)*1.3,t.save(),t.translate(p,b),t.rotate(-0.25+Math.sin(h*5+n)*.22),t.font=`800 ${Math.round(r*(.05+.06*h))}px ui-rounded, system-ui, sans-serif`,t.fillText("z",0,0),t.restore();}}if(o.hearts>.01){let[l,u]=this.project(0,.8,.2),n=x(this.cheek,[1,.3,.45],.55);for(let h=0;h<4;h++){let p=i?.4:(s*.3+h/4)%1,b=h%2?1:-1,E=l+b*r*(.16+.07*(h>>1))+Math.sin(p*6+h)*r*.025,S=u-r*.02-xe(p)*r*.28,k=i?1:Re(Pe(p/.22)),C=r*(.026+.008*(h%3))*k;t.globalAlpha=o.hearts*(1-Math.max(0,(p-.6)/.4)),t.save(),t.translate(E,S),t.rotate(Math.sin(s*3+h)*.25);let A=t.createLinearGradient(0,-C,0,C);A.addColorStop(0,L(x(n,[1,1,1],.35))),A.addColorStop(1,L(n)),t.fillStyle=A,ye(t,0,0,C),t.fill(),t.restore();}}if(o.waves>.01){t.lineCap="round",t.strokeStyle=a;for(let l of [-1,1]){let[u,n]=this.project(l*1,.12,0);for(let h=0;h<3;h++){let p=i?.3+h*.2:(s*.8+h/3)%1,b=xe(p);t.globalAlpha=o.waves*Math.sin(Math.PI*p)*.9,t.lineWidth=r*(.016-.008*p),t.beginPath();let E=r*(.03+.11*b);l>0?t.arc(u-r*.03,n,E,-0.55,.55):t.arc(u+r*.03,n,E,Math.PI-.55,Math.PI+.55),t.stroke();}}}if(o.bang>.01){let[l,u]=this.project(.55,1,.2),n=i?1:Re(Pe(this.tau/.35));t.globalAlpha=o.bang,t.fillStyle=a,t.save(),t.translate(l,u),t.rotate(.18),t.scale(n,n),t.beginPath(),t.roundRect(-r*.016,-r*.1,r*.032,r*.07,r*.016),t.arc(0,r*.005,r*.018,0,Math.PI*2),t.fill(),t.restore();}t.restore();}},Pe=e=>e<0?0:e>1?1:e,xe=e=>1-(1-e)*(1-e),Re=e=>1+(1.9+1)*Math.pow(e-1,3)+1.9*Math.pow(e-1,2);function Pt(e,o,t,s){e.ellipse(o,t,s,s*.72,0,0,Math.PI*2),e.moveTo(o-s*.35+s*.45,t-s*.38),e.arc(o-s*.35,t-s*.38,s*.45,0,Math.PI*2),e.moveTo(o+s*.3+s*.5,t-s*.32),e.arc(o+s*.3,t-s*.32,s*.5,0,Math.PI*2);}var xt=["shape","mood","color","face-color","cheek-color","size","speed","follow-cursor","interactive","shadow","sound","settle"],N=(e,o)=>e===null?o:e!=="false"&&e!=="0",Rt=e=>e==null||e===""?`${M.size}px`:typeof e=="number"||/^\d+(\.\d+)?$/.test(String(e))?`${e}px`:String(e),Ft=`:host{display:inline-block;position:relative;cursor:pointer;vertical-align:middle;contain:layout paint style;-webkit-tap-highlight-color:transparent}
24
24
  :host([interactive="false"]){pointer-events:none}
25
25
  canvas{position:absolute;inset:0;display:block;width:100%;height:100%}
26
26
  slot{display:block;width:100%;height:100%}
27
27
  ::slotted(img){display:block;width:100%;height:100%;object-fit:contain}
28
- :host([ready]) slot{display:none}`,zt=typeof HTMLElement>"u"?class{}:HTMLElement,Z=class extends zt{constructor(){super();this.buddy=null;this.sizeStyle=document.createElement("style");this.onEnter=()=>{this.buddy&&(this.buddy.hovering=true);};this.onLeave=()=>{this.buddy&&(this.buddy.hovering=false);};this.onClick=()=>{this.buddy?.state.interactive&&this.react(Math.random()<.5?"happy":"surprised",{duration:1100});};this.root=this.attachShadow({mode:"open"});let t=document.createElement("style");t.textContent=Ft,this.root.append(t,document.createElement("slot")),this.root.append(this.sizeStyle),this.applySize(this.getAttribute("size"));}connectedCallback(){if(this.hasAttribute("role")||this.setAttribute("role","img"),!this.buddy){let t=this.buddy=new he;t.onMoodChange=s=>{this.syncLabel(s.mood),this.dispatchEvent(new CustomEvent("moodchange",{detail:s,bubbles:true}));},t.onReady=()=>{this.setAttribute("ready",""),this.dispatchEvent(new Event("ready"));},this.root.append(t.canvas),t.update(this.readState()),this.addEventListener("pointerenter",this.onEnter),this.addEventListener("pointerleave",this.onLeave),this.addEventListener("click",this.onClick);}this.syncLabel(this.buddy.currentMood),this.buddy.connect(),this.io=new IntersectionObserver(([t])=>this.buddy?.setVisible(t.isIntersecting),{rootMargin:"64px"}),this.io.observe(this),this.ro=new ResizeObserver(([t])=>{let s=t.contentRect;this.buddy?.resize(s.width,s.height);}),this.ro.observe(this);}disconnectedCallback(){this.io?.disconnect(),this.ro?.disconnect(),this.buddy&&(this.buddy.canvas.remove(),this.buddy.dispose(),this.buddy=null),this.removeEventListener("pointerenter",this.onEnter),this.removeEventListener("pointerleave",this.onLeave),this.removeEventListener("click",this.onClick);}attributeChangedCallback(t,s,i){s!==i&&(t==="size"&&(this.applySize(i),s!==null&&this.buddy?.bump()),t!=="aria-label"&&(this.buddy?.update(this.readState()),t==="mood"&&this.buddy&&this.syncLabel(this.buddy.currentMood)));}applySize(t){let s=Rt(t);this.sizeStyle.textContent=`:host{width:${s};height:${s}}`;}readState(){let t=this.getAttribute("shape"),s=this.getAttribute("mood"),i=parseFloat(this.getAttribute("speed")??"");return {shape:ce(t)?t:M.shape,mood:de(s)?s:M.mood,color:this.getAttribute("color")||M.color,faceColor:this.getAttribute("face-color")||void 0,cheekColor:this.getAttribute("cheek-color")||void 0,speed:Number.isFinite(i)?Math.max(0,i):M.speed,followCursor:N(this.getAttribute("follow-cursor"),M.followCursor),interactive:N(this.getAttribute("interactive"),M.interactive),shadow:N(this.getAttribute("shadow"),M.shadow),sound:N(this.getAttribute("sound"),M.sound)}}syncLabel(t){let s=this.getAttribute("aria-label");s&&!s.startsWith("moodi: ")||this.setAttribute("aria-label",`moodi: ${t}`);}react(t,s={}){this.buddy?.react(t,s.duration??1200);}get shape(){let t=this.getAttribute("shape");return ce(t)?t:M.shape}set shape(t){this.setAttribute("shape",t);}get mood(){let t=this.getAttribute("mood");return de(t)?t:M.mood}set mood(t){this.setAttribute("mood",t);}get color(){return this.getAttribute("color")||M.color}set color(t){this.setAttribute("color",t);}get faceColor(){return this.getAttribute("face-color")||void 0}set faceColor(t){t?this.setAttribute("face-color",t):this.removeAttribute("face-color");}get cheekColor(){return this.getAttribute("cheek-color")||void 0}set cheekColor(t){t?this.setAttribute("cheek-color",t):this.removeAttribute("cheek-color");}get size(){let t=this.getAttribute("size");return t?/^\d+(\.\d+)?$/.test(t)?Number(t):t:M.size}set size(t){this.setAttribute("size",String(t));}get speed(){return this.readState().speed}set speed(t){this.setAttribute("speed",String(t));}get followCursor(){return N(this.getAttribute("follow-cursor"),M.followCursor)}set followCursor(t){this.setAttribute("follow-cursor",String(t));}get interactive(){return N(this.getAttribute("interactive"),M.interactive)}set interactive(t){this.setAttribute("interactive",String(t));}get shadow(){return N(this.getAttribute("shadow"),M.shadow)}set shadow(t){this.setAttribute("shadow",String(t));}get sound(){return N(this.getAttribute("sound"),M.sound)}set sound(t){this.setAttribute("sound",String(t));}};Z.observedAttributes=xt;function Ke(e="moodi-buddy"){typeof window>"u"||!("customElements"in window)||customElements.get(e)||customElements.define(e,class extends Z{});}Ke();export{M as DEFAULTS,Je as MOODS,Bt as MOOD_INFO,Z as MoodiBuddyElement,Ot as PALETTES,Ue as SHAPES,Lt as SHAPE_LABELS,Ke as defineMoodi,de as isMood,ce as isShape};
28
+ :host([ready]) slot{display:none}`,zt=typeof HTMLElement>"u"?class{}:HTMLElement,Q=class extends zt{constructor(){super();this.buddy=null;this.sizeStyle=document.createElement("style");this.onEnter=()=>{this.buddy&&(this.buddy.hovering=true);};this.onLeave=()=>{this.buddy&&(this.buddy.hovering=false);};this.onClick=()=>{this.buddy?.state.interactive&&this.react(Math.random()<.5?"happy":"surprised",{duration:1100});};this.root=this.attachShadow({mode:"open"});let t=document.createElement("style");t.textContent=Ft,this.root.append(t,document.createElement("slot")),this.root.append(this.sizeStyle),this.applySize(this.getAttribute("size"));}connectedCallback(){if(this.hasAttribute("role")||this.setAttribute("role","img"),!this.buddy){let t=this.buddy=new le;t.onMoodChange=s=>{this.syncLabel(s.mood),this.dispatchEvent(new CustomEvent("moodchange",{detail:s,bubbles:true}));},t.onReady=()=>{this.setAttribute("ready",""),this.dispatchEvent(new Event("ready"));},this.root.append(t.canvas),t.update(this.readState()),this.addEventListener("pointerenter",this.onEnter),this.addEventListener("pointerleave",this.onLeave),this.addEventListener("click",this.onClick);}this.syncLabel(this.buddy.currentMood),this.buddy.connect(),this.io=new IntersectionObserver(([t])=>this.buddy?.setVisible(t.isIntersecting),{rootMargin:"64px"}),this.io.observe(this),this.ro=new ResizeObserver(([t])=>{let s=t.contentRect;this.buddy?.resize(s.width,s.height);}),this.ro.observe(this);}disconnectedCallback(){this.io?.disconnect(),this.ro?.disconnect(),this.buddy&&(this.buddy.canvas.remove(),this.buddy.dispose(),this.buddy=null),this.removeEventListener("pointerenter",this.onEnter),this.removeEventListener("pointerleave",this.onLeave),this.removeEventListener("click",this.onClick);}attributeChangedCallback(t,s,i){s!==i&&(t==="size"&&(this.applySize(i),s!==null&&this.buddy?.bump()),t!=="aria-label"&&(this.buddy?.update(this.readState()),t==="mood"&&this.buddy&&this.syncLabel(this.buddy.currentMood)));}applySize(t){let s=Rt(t);this.sizeStyle.textContent=`:host{width:${s};height:${s}}`;}readState(){let t=this.getAttribute("shape"),s=this.getAttribute("mood"),i=parseFloat(this.getAttribute("speed")??""),r=parseFloat(this.getAttribute("settle")??"");return {shape:de(t)?t:M.shape,mood:ue(s)?s:M.mood,color:this.getAttribute("color")||M.color,faceColor:this.getAttribute("face-color")||void 0,cheekColor:this.getAttribute("cheek-color")||void 0,speed:Number.isFinite(i)?Math.max(0,i):M.speed,followCursor:N(this.getAttribute("follow-cursor"),M.followCursor),interactive:N(this.getAttribute("interactive"),M.interactive),shadow:N(this.getAttribute("shadow"),M.shadow),sound:N(this.getAttribute("sound"),M.sound),settle:Number.isFinite(r)?Math.max(0,r):M.settle}}syncLabel(t){let s=this.getAttribute("aria-label");s&&!s.startsWith("moodi: ")||this.setAttribute("aria-label",`moodi: ${t}`);}react(t,s={}){this.buddy?.react(t,s.duration??1200);}get shape(){let t=this.getAttribute("shape");return de(t)?t:M.shape}set shape(t){this.setAttribute("shape",t);}get mood(){let t=this.getAttribute("mood");return ue(t)?t:M.mood}set mood(t){this.setAttribute("mood",t);}get color(){return this.getAttribute("color")||M.color}set color(t){this.setAttribute("color",t);}get faceColor(){return this.getAttribute("face-color")||void 0}set faceColor(t){t?this.setAttribute("face-color",t):this.removeAttribute("face-color");}get cheekColor(){return this.getAttribute("cheek-color")||void 0}set cheekColor(t){t?this.setAttribute("cheek-color",t):this.removeAttribute("cheek-color");}get size(){let t=this.getAttribute("size");return t?/^\d+(\.\d+)?$/.test(t)?Number(t):t:M.size}set size(t){this.setAttribute("size",String(t));}get speed(){return this.readState().speed}set speed(t){this.setAttribute("speed",String(t));}get followCursor(){return N(this.getAttribute("follow-cursor"),M.followCursor)}set followCursor(t){this.setAttribute("follow-cursor",String(t));}get interactive(){return N(this.getAttribute("interactive"),M.interactive)}set interactive(t){this.setAttribute("interactive",String(t));}get shadow(){return N(this.getAttribute("shadow"),M.shadow)}set shadow(t){this.setAttribute("shadow",String(t));}get settle(){return this.readState().settle}set settle(t){this.setAttribute("settle",String(t));}get sound(){return N(this.getAttribute("sound"),M.sound)}set sound(t){this.setAttribute("sound",String(t));}};Q.observedAttributes=xt;function Ke(e="moodi-buddy"){typeof window>"u"||!("customElements"in window)||customElements.get(e)||customElements.define(e,class extends Q{});}Ke();export{M as DEFAULTS,Je as MOODS,Ot as MOOD_INFO,Q as MoodiBuddyElement,Bt as PALETTES,Ue as SHAPES,Lt as SHAPE_LABELS,Ke as defineMoodi,ue as isMood,de as isShape};
package/dist/react.cjs CHANGED
@@ -1,2 +1,2 @@
1
1
  "use client";
2
- "use strict";var R=Object.create;var c=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var N=Object.getPrototypeOf,j=Object.prototype.hasOwnProperty;var q=(e,o)=>{for(var t in o)c(e,t,{get:o[t],enumerable:!0})},y=(e,o,t,i)=>{if(o&&typeof o=="object"||typeof o=="function")for(let d of F(o))!j.call(e,d)&&d!==t&&c(e,d,{get:()=>o[d],enumerable:!(i=O(o,d))||i.enumerable});return e};var W=(e,o,t)=>(t=e!=null?R(N(e)):{},y(o||!e||!e.__esModule?c(t,"default",{value:e,enumerable:!0}):t,e)),_=e=>y(c({},"__esModule",{value:!0}),e);var X={};q(X,{DEFAULTS:()=>$,MOODS:()=>S,MOOD_INFO:()=>G,Moodi:()=>V,PALETTES:()=>z,SHAPES:()=>M,SHAPE_LABELS:()=>U,isMood:()=>K,isShape:()=>J,loadMoodi:()=>l});module.exports=_(X);var n=require("react");var M=["ghost","pebble","blob","drop","bean","cube"],S=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],$={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:!0,interactive:!0,shadow:!0,sound:!1},z={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},G={idle:{label:"Idle",description:"Breathing softly. Blinks now and then."},thinking:{label:"Thinking",description:"Eyes drift up. Little thoughts pop."},loading:{label:"Loading",description:"Half-lidded, wobbling patiently."},listening:{label:"Listening",description:"All eyes. Nods along to every word."},speaking:{label:"Speaking",description:"Chatty mouth, bouncing to the rhythm."},happy:{label:"Happy",description:"Bounces with joy. Cheeks glow."},surprised:{label:"Surprised",description:"Wide eyes, tiny o. Did not see that coming."},sad:{label:"Sad",description:"Slumps a little. Needs a minute."},error:{label:"Error",description:"Something broke. Shakes it off."},sleeping:{label:"Sleeping",description:"Deep, slow breaths. Do not disturb."},love:{label:"Love",description:"Heart eyes, floating on air."}},U={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},J=e=>M.includes(e),K=e=>S.includes(e);var E=null,v=!1,l=()=>E??(E=import("moodi").then(e=>e.defineMoodi()));function Q(e){if(E||e==="eager")return void l();if(e==="idle"){typeof requestIdleCallback=="function"?requestIdleCallback(()=>l(),{timeout:1200}):setTimeout(l,1);return}if(v)return;v=!0;let o=["pointermove","pointerdown","touchstart","keydown","wheel","scroll"],t=()=>{o.forEach(i=>removeEventListener(i,t)),l()};o.forEach(i=>addEventListener(i,t,{passive:!0,once:!0}))}var u=e=>e===void 0?void 0:String(e),k=e=>e===void 0?"160px":typeof e=="number"||/^\d+(\.\d+)?$/.test(e)?`${e}px`:e,V=(0,n.forwardRef)(function({shape:o,mood:t,color:i,faceColor:d,cheekColor:w,size:a,speed:b,followCursor:x,interactive:L,shadow:C,sound:P,onMoodChange:g,onReady:s,poster:h,loading:f="idle",className:B,style:D,...A},H){let p=(0,n.useRef)(null);(0,n.useImperativeHandle)(H,()=>p.current,[]),(0,n.useEffect)(()=>{Q(f)},[f]),(0,n.useEffect)(()=>{let r=p.current;if(!(!r||!s))return r.hasAttribute("ready")?void s():(r.addEventListener("ready",s,{once:!0}),()=>r.removeEventListener("ready",s))},[s]),(0,n.useEffect)(()=>{let r=p.current;if(!r||!g)return;let m=I=>g(I.detail);return r.addEventListener("moodchange",m),()=>r.removeEventListener("moodchange",m)},[g]);let T=h?(0,n.createElement)("img",{src:h,alt:"","aria-hidden":!0,decoding:"async",draggable:!1,style:{display:"block",width:"100%",height:"100%",objectFit:"contain"}}):null;return(0,n.createElement)("moodi-buddy",{ref:p,className:B,shape:o,mood:t,color:i,"face-color":d,"cheek-color":w,size:a===void 0?void 0:String(a),speed:b===void 0?void 0:String(b),"follow-cursor":u(x),interactive:u(L),shadow:u(C),sound:u(P),style:{display:"inline-block",width:k(a),height:k(a),verticalAlign:"middle",...D},suppressHydrationWarning:!0,...A},T)});0&&(module.exports={DEFAULTS,MOODS,MOOD_INFO,Moodi,PALETTES,SHAPES,SHAPE_LABELS,isMood,isShape,loadMoodi});
2
+ "use strict";var O=Object.create;var c=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var j=Object.getPrototypeOf,q=Object.prototype.hasOwnProperty;var W=(e,o)=>{for(var t in o)c(e,t,{get:o[t],enumerable:!0})},M=(e,o,t,i)=>{if(o&&typeof o=="object"||typeof o=="function")for(let d of N(o))!q.call(e,d)&&d!==t&&c(e,d,{get:()=>o[d],enumerable:!(i=F(o,d))||i.enumerable});return e};var _=(e,o,t)=>(t=e!=null?O(j(e)):{},M(o||!e||!e.__esModule?c(t,"default",{value:e,enumerable:!0}):t,e)),$=e=>M(c({},"__esModule",{value:!0}),e);var Y={};W(Y,{DEFAULTS:()=>z,MOODS:()=>v,MOOD_INFO:()=>U,Moodi:()=>X,PALETTES:()=>G,SHAPES:()=>S,SHAPE_LABELS:()=>J,isMood:()=>Q,isShape:()=>K,loadMoodi:()=>l});module.exports=$(Y);var n=require("react");var S=["ghost","pebble","blob","drop","bean","cube"],v=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],z={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:!0,interactive:!0,shadow:!0,sound:!1,settle:0},G={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},U={idle:{label:"Idle",description:"Breathing softly. Blinks now and then."},thinking:{label:"Thinking",description:"Eyes drift up. Little thoughts pop."},loading:{label:"Loading",description:"Half-lidded, wobbling patiently."},listening:{label:"Listening",description:"All eyes. Nods along to every word."},speaking:{label:"Speaking",description:"Chatty mouth, bouncing to the rhythm."},happy:{label:"Happy",description:"Bounces with joy. Cheeks glow."},surprised:{label:"Surprised",description:"Wide eyes, tiny o. Did not see that coming."},sad:{label:"Sad",description:"Slumps a little. Needs a minute."},error:{label:"Error",description:"Something broke. Shakes it off."},sleeping:{label:"Sleeping",description:"Deep, slow breaths. Do not disturb."},love:{label:"Love",description:"Heart eyes, floating on air."}},J={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},K=e=>S.includes(e),Q=e=>v.includes(e);var w=null,k=!1,l=()=>w??(w=import("moodi").then(e=>e.defineMoodi()));function V(e){if(w||e==="eager")return void l();if(e==="idle"){typeof requestIdleCallback=="function"?requestIdleCallback(()=>l(),{timeout:1200}):setTimeout(l,1);return}if(k)return;k=!0;let o=["pointermove","pointerdown","touchstart","keydown","wheel","scroll"],t=()=>{o.forEach(i=>removeEventListener(i,t)),l()};o.forEach(i=>addEventListener(i,t,{passive:!0,once:!0}))}var u=e=>e===void 0?void 0:String(e),E=e=>e===void 0?"160px":typeof e=="number"||/^\d+(\.\d+)?$/.test(e)?`${e}px`:e,X=(0,n.forwardRef)(function({shape:o,mood:t,color:i,faceColor:d,cheekColor:x,size:a,speed:b,followCursor:L,interactive:C,shadow:P,sound:B,settle:h,onMoodChange:g,onReady:s,poster:f,loading:m="idle",className:D,style:A,...H},T){let p=(0,n.useRef)(null);(0,n.useImperativeHandle)(T,()=>p.current,[]),(0,n.useEffect)(()=>{V(m)},[m]),(0,n.useEffect)(()=>{let r=p.current;if(!(!r||!s))return r.hasAttribute("ready")?void s():(r.addEventListener("ready",s,{once:!0}),()=>r.removeEventListener("ready",s))},[s]),(0,n.useEffect)(()=>{let r=p.current;if(!r||!g)return;let y=R=>g(R.detail);return r.addEventListener("moodchange",y),()=>r.removeEventListener("moodchange",y)},[g]);let I=f?(0,n.createElement)("img",{src:f,alt:"","aria-hidden":!0,decoding:"async",draggable:!1,style:{display:"block",width:"100%",height:"100%",objectFit:"contain"}}):null;return(0,n.createElement)("moodi-buddy",{ref:p,className:D,shape:o,mood:t,color:i,"face-color":d,"cheek-color":x,size:a===void 0?void 0:String(a),speed:b===void 0?void 0:String(b),"follow-cursor":u(L),interactive:u(C),shadow:u(P),sound:u(B),settle:h===void 0?void 0:String(h),style:{display:"inline-block",width:E(a),height:E(a),verticalAlign:"middle",...A},suppressHydrationWarning:!0,...H},I)});0&&(module.exports={DEFAULTS,MOODS,MOOD_INFO,Moodi,PALETTES,SHAPES,SHAPE_LABELS,isMood,isShape,loadMoodi});
package/dist/react.d.cts CHANGED
@@ -26,6 +26,11 @@ interface MoodiProps {
26
26
  shadow?: boolean;
27
27
  /** A short sound effect on every mood change (and when it first appears). Default `false`. */
28
28
  sound?: boolean;
29
+ /**
30
+ * Milliseconds after a mood change when the body calms down to gentle idle breathing
31
+ * (hopping, shaking, hearts… stop; the facial expression stays). `0` = never. Default `0`.
32
+ */
33
+ settle?: number;
29
34
  }
30
35
  interface MoodChangeDetail {
31
36
  mood: Mood;
@@ -45,6 +50,7 @@ declare const DEFAULTS: {
45
50
  interactive: boolean;
46
51
  shadow: boolean;
47
52
  sound: boolean;
53
+ settle: number;
48
54
  };
49
55
  declare const PALETTES: Readonly<Record<string, string>>;
50
56
  declare const MOOD_INFO: Readonly<Record<Mood, {
@@ -57,7 +63,7 @@ declare const isMood: (v: unknown) => v is Mood;
57
63
 
58
64
  declare const Base: typeof HTMLElement;
59
65
  declare class MoodiBuddyElement extends Base {
60
- static observedAttributes: readonly ["shape", "mood", "color", "face-color", "cheek-color", "size", "speed", "follow-cursor", "interactive", "shadow", "sound"];
66
+ static observedAttributes: readonly ["shape", "mood", "color", "face-color", "cheek-color", "size", "speed", "follow-cursor", "interactive", "shadow", "sound", "settle"];
61
67
  private buddy;
62
68
  private io?;
63
69
  private ro?;
@@ -98,6 +104,8 @@ declare class MoodiBuddyElement extends Base {
98
104
  set interactive(v: boolean);
99
105
  get shadow(): boolean;
100
106
  set shadow(v: boolean);
107
+ get settle(): number;
108
+ set settle(v: number);
101
109
  get sound(): boolean;
102
110
  set sound(v: boolean);
103
111
  }
package/dist/react.d.ts CHANGED
@@ -26,6 +26,11 @@ interface MoodiProps {
26
26
  shadow?: boolean;
27
27
  /** A short sound effect on every mood change (and when it first appears). Default `false`. */
28
28
  sound?: boolean;
29
+ /**
30
+ * Milliseconds after a mood change when the body calms down to gentle idle breathing
31
+ * (hopping, shaking, hearts… stop; the facial expression stays). `0` = never. Default `0`.
32
+ */
33
+ settle?: number;
29
34
  }
30
35
  interface MoodChangeDetail {
31
36
  mood: Mood;
@@ -45,6 +50,7 @@ declare const DEFAULTS: {
45
50
  interactive: boolean;
46
51
  shadow: boolean;
47
52
  sound: boolean;
53
+ settle: number;
48
54
  };
49
55
  declare const PALETTES: Readonly<Record<string, string>>;
50
56
  declare const MOOD_INFO: Readonly<Record<Mood, {
@@ -57,7 +63,7 @@ declare const isMood: (v: unknown) => v is Mood;
57
63
 
58
64
  declare const Base: typeof HTMLElement;
59
65
  declare class MoodiBuddyElement extends Base {
60
- static observedAttributes: readonly ["shape", "mood", "color", "face-color", "cheek-color", "size", "speed", "follow-cursor", "interactive", "shadow", "sound"];
66
+ static observedAttributes: readonly ["shape", "mood", "color", "face-color", "cheek-color", "size", "speed", "follow-cursor", "interactive", "shadow", "sound", "settle"];
61
67
  private buddy;
62
68
  private io?;
63
69
  private ro?;
@@ -98,6 +104,8 @@ declare class MoodiBuddyElement extends Base {
98
104
  set interactive(v: boolean);
99
105
  get shadow(): boolean;
100
106
  set shadow(v: boolean);
107
+ get settle(): number;
108
+ set settle(v: number);
101
109
  get sound(): boolean;
102
110
  set sound(v: boolean);
103
111
  }
package/dist/react.js CHANGED
@@ -1,2 +1,2 @@
1
1
  "use client";
2
- import{createElement as f,forwardRef as I,useEffect as c,useImperativeHandle as R,useRef as O}from"react";var H=["ghost","pebble","blob","drop","bean","cube"],T=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],N={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:!0,interactive:!0,shadow:!0,sound:!1},j={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},q={idle:{label:"Idle",description:"Breathing softly. Blinks now and then."},thinking:{label:"Thinking",description:"Eyes drift up. Little thoughts pop."},loading:{label:"Loading",description:"Half-lidded, wobbling patiently."},listening:{label:"Listening",description:"All eyes. Nods along to every word."},speaking:{label:"Speaking",description:"Chatty mouth, bouncing to the rhythm."},happy:{label:"Happy",description:"Bounces with joy. Cheeks glow."},surprised:{label:"Surprised",description:"Wide eyes, tiny o. Did not see that coming."},sad:{label:"Sad",description:"Slumps a little. Needs a minute."},error:{label:"Error",description:"Something broke. Shakes it off."},sleeping:{label:"Sleeping",description:"Deep, slow breaths. Do not disturb."},love:{label:"Love",description:"Heart eyes, floating on air."}},W={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},_=e=>H.includes(e),$=e=>T.includes(e);var M=null,m=!1,l=()=>M??(M=import("moodi").then(e=>e.defineMoodi()));function F(e){if(M||e==="eager")return void l();if(e==="idle"){typeof requestIdleCallback=="function"?requestIdleCallback(()=>l(),{timeout:1200}):setTimeout(l,1);return}if(m)return;m=!0;let i=["pointermove","pointerdown","touchstart","keydown","wheel","scroll"],r=()=>{i.forEach(t=>removeEventListener(t,r)),l()};i.forEach(t=>addEventListener(t,r,{passive:!0,once:!0}))}var a=e=>e===void 0?void 0:String(e),y=e=>e===void 0?"160px":typeof e=="number"||/^\d+(\.\d+)?$/.test(e)?`${e}px`:e,U=I(function({shape:i,mood:r,color:t,faceColor:S,cheekColor:v,size:d,speed:u,followCursor:k,interactive:E,shadow:w,sound:x,onMoodChange:p,onReady:n,poster:g,loading:b="idle",className:L,style:C,...P},B){let s=O(null);R(B,()=>s.current,[]),c(()=>{F(b)},[b]),c(()=>{let o=s.current;if(!(!o||!n))return o.hasAttribute("ready")?void n():(o.addEventListener("ready",n,{once:!0}),()=>o.removeEventListener("ready",n))},[n]),c(()=>{let o=s.current;if(!o||!p)return;let h=A=>p(A.detail);return o.addEventListener("moodchange",h),()=>o.removeEventListener("moodchange",h)},[p]);let D=g?f("img",{src:g,alt:"","aria-hidden":!0,decoding:"async",draggable:!1,style:{display:"block",width:"100%",height:"100%",objectFit:"contain"}}):null;return f("moodi-buddy",{ref:s,className:L,shape:i,mood:r,color:t,"face-color":S,"cheek-color":v,size:d===void 0?void 0:String(d),speed:u===void 0?void 0:String(u),"follow-cursor":a(k),interactive:a(E),shadow:a(w),sound:a(x),style:{display:"inline-block",width:y(d),height:y(d),verticalAlign:"middle",...C},suppressHydrationWarning:!0,...P},D)});export{N as DEFAULTS,T as MOODS,q as MOOD_INFO,U as Moodi,j as PALETTES,H as SHAPES,W as SHAPE_LABELS,$ as isMood,_ as isShape,l as loadMoodi};
2
+ import{createElement as m,forwardRef as R,useEffect as c,useImperativeHandle as O,useRef as F}from"react";var T=["ghost","pebble","blob","drop","bean","cube"],I=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],j={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:!0,interactive:!0,shadow:!0,sound:!1,settle:0},q={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},W={idle:{label:"Idle",description:"Breathing softly. Blinks now and then."},thinking:{label:"Thinking",description:"Eyes drift up. Little thoughts pop."},loading:{label:"Loading",description:"Half-lidded, wobbling patiently."},listening:{label:"Listening",description:"All eyes. Nods along to every word."},speaking:{label:"Speaking",description:"Chatty mouth, bouncing to the rhythm."},happy:{label:"Happy",description:"Bounces with joy. Cheeks glow."},surprised:{label:"Surprised",description:"Wide eyes, tiny o. Did not see that coming."},sad:{label:"Sad",description:"Slumps a little. Needs a minute."},error:{label:"Error",description:"Something broke. Shakes it off."},sleeping:{label:"Sleeping",description:"Deep, slow breaths. Do not disturb."},love:{label:"Love",description:"Heart eyes, floating on air."}},_={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},$=e=>T.includes(e),z=e=>I.includes(e);var S=null,y=!1,l=()=>S??(S=import("moodi").then(e=>e.defineMoodi()));function N(e){if(S||e==="eager")return void l();if(e==="idle"){typeof requestIdleCallback=="function"?requestIdleCallback(()=>l(),{timeout:1200}):setTimeout(l,1);return}if(y)return;y=!0;let i=["pointermove","pointerdown","touchstart","keydown","wheel","scroll"],r=()=>{i.forEach(t=>removeEventListener(t,r)),l()};i.forEach(t=>addEventListener(t,r,{passive:!0,once:!0}))}var a=e=>e===void 0?void 0:String(e),M=e=>e===void 0?"160px":typeof e=="number"||/^\d+(\.\d+)?$/.test(e)?`${e}px`:e,J=R(function({shape:i,mood:r,color:t,faceColor:v,cheekColor:k,size:d,speed:u,followCursor:E,interactive:w,shadow:x,sound:L,settle:g,onMoodChange:p,onReady:n,poster:b,loading:h="idle",className:C,style:P,...B},D){let s=F(null);O(D,()=>s.current,[]),c(()=>{N(h)},[h]),c(()=>{let o=s.current;if(!(!o||!n))return o.hasAttribute("ready")?void n():(o.addEventListener("ready",n,{once:!0}),()=>o.removeEventListener("ready",n))},[n]),c(()=>{let o=s.current;if(!o||!p)return;let f=H=>p(H.detail);return o.addEventListener("moodchange",f),()=>o.removeEventListener("moodchange",f)},[p]);let A=b?m("img",{src:b,alt:"","aria-hidden":!0,decoding:"async",draggable:!1,style:{display:"block",width:"100%",height:"100%",objectFit:"contain"}}):null;return m("moodi-buddy",{ref:s,className:C,shape:i,mood:r,color:t,"face-color":v,"cheek-color":k,size:d===void 0?void 0:String(d),speed:u===void 0?void 0:String(u),"follow-cursor":a(E),interactive:a(w),shadow:a(x),sound:a(L),settle:g===void 0?void 0:String(g),style:{display:"inline-block",width:M(d),height:M(d),verticalAlign:"middle",...P},suppressHydrationWarning:!0,...B},A)});export{j as DEFAULTS,I as MOODS,W as MOOD_INFO,J as Moodi,q as PALETTES,T as SHAPES,_ as SHAPE_LABELS,z as isMood,$ as isShape,l as loadMoodi};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moodi",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Tiny 3D mood buddies for your UI.",
5
5
  "license": "MIT",
6
6
  "type": "module",