moodi 0.4.0 → 0.5.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/CHANGELOG.md +4 -0
- package/README.md +2 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/react.cjs +1 -1
- package/dist/react.d.cts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ npm i moodi three
|
|
|
24
24
|
### Web Component
|
|
25
25
|
|
|
26
26
|
```html
|
|
27
|
-
<moodi-buddy shape="ghost" mood="happy" color="#
|
|
27
|
+
<moodi-buddy shape="ghost" mood="happy" color="#2b15d1" size="160"></moodi-buddy>
|
|
28
28
|
<script type="module">import 'moodi';</script>
|
|
29
29
|
```
|
|
30
30
|
|
|
@@ -65,7 +65,7 @@ With the Web Component, put an `<img>` inside `<moodi-buddy>`; it shows until th
|
|
|
65
65
|
| ----------------------------- | ------------------------------------------------------------ | ----------- | --------------------------------------------- |
|
|
66
66
|
| `shape` | `'ghost' \| 'pebble' \| 'blob' \| 'drop' \| 'bean' \| 'cube'` | `'ghost'` | Body shape |
|
|
67
67
|
| `mood` | `Mood` (see below) | `'idle'` | Changes are interpolated, never instant |
|
|
68
|
-
| `color` | any CSS color | `'#
|
|
68
|
+
| `color` | any CSS color | `'#2b15d1'` | Body color |
|
|
69
69
|
| `faceColor` (`face-color`) | any CSS color | auto | Eyes & mouth, auto-contrast with the body |
|
|
70
70
|
| `cheekColor` (`cheek-color`) | any CSS color | auto | Blush, derived from the body color |
|
|
71
71
|
| `size` | `number \| string` | `160` | px, or any CSS length (`"10rem"`, `"40vw"`) |
|
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
'use strict';var three=require('three');var Qe=["ghost","pebble","blob","drop","bean","cube"],et=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],y={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:true,interactive:true,shadow:true,sound:false,settle:0},Ot={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},Lt={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."}},Gt={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},pe=e=>Qe.includes(e),J=e=>et.includes(e);var G=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 Be(t[1]);if(G||(G=document.createElement("canvas").getContext("2d")),!G)return o;G.fillStyle="#010203",G.fillStyle=e;let i=String(G.fillStyle);if(i==="#010203"&&e.trim()!=="#010203")return o;if(i.startsWith("#"))return Be(i.slice(1));G.canvas.width=G.canvas.height=1,G.clearRect(0,0,1,1),G.fillRect(0,0,1,1);let s=G.getImageData(0,0,1,1).data;return [s[0]/255,s[1]/255,s[2]/255]}function Be(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 D=e=>`rgb(${Math.round(e[0]*255)},${Math.round(e[1]*255)},${Math.round(e[2]*255)})`,R=(e,o,t)=>[e[0]+(o[0]-e[0])*t,e[1]+(o[1]-e[1])*t,e[2]+(o[2]-e[2])*t],me=e=>e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4),fe=e=>.2126*me(e[0])+.7152*me(e[1])+.0722*me(e[2]),tt=[.07,.06,.08],ot=[209/255,190/255,173/255],it=[1,.42,.48],oe=e=>fe(e)>.06?R(tt,e,.06):ot,ie=e=>R(e,it,.78);var Z=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],Y={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},se={idle:{...Y},thinking:{...Y,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...Y,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...Y,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...Y,mouthW:.09,mouthCurve:.25},happy:{...Y,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...Y,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...Y,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...Y,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...Y,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...Y,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Le={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},ve=e=>({...e}),st=.31,nt=-0.04,at=.098,be=.128,rt=.2,ge=.05;function ye(e,o,t,i,s,n,u,r,l,d,a=false){let h=e.canvas.width;e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,h,h),e.setTransform(h/2,0,0,h/2,h/2,h/2),e.lineCap="round",e.lineJoin="round";let p=o.lookX*.075,m=o.lookY*.06;if(o.blush>.01&&!a){let T=h*.018;e.save(),"filter"in e&&(e.filter=`blur(${T}px)`);for(let g of [-1,1]){let b=g*.52+p,c=.16+m,v=e.createRadialGradient(b,c,0,b,c,.16);v.addColorStop(0,s),v.addColorStop(1,"rgba(0,0,0,0)"),e.globalAlpha=Math.min(1,o.blush)*.62,e.fillStyle=v,e.beginPath(),e.ellipse(b,c,.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=s,e.lineWidth=.016,e.lineCap="round";for(let g of [-1,1]){let b=g*.52+p,c=.16+m;for(let v=-1;v<=1;v++){let P=b+v*.045;e.beginPath(),e.moveTo(P+.018,c-.035),e.lineTo(P-.018,c+.035),e.stroke();}}e.restore();}}e.fillStyle=i,e.strokeStyle=i,e.lineWidth=ge;let E=Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye),S=1-E;for(let T of [-1,1]){let f=T*st+p,g=nt+m,b=at*o.eyeSize;if(S>.01){e.globalAlpha=S;let c=Math.max(.06,o.eyeOpen*t),v=be*o.eyeSize*c;if(e.save(),o.lidCover>.01||o.lidTilt>.01){let W=g-be*o.eyeSize+2*be*o.eyeSize*o.lidCover;e.translate(f,W),e.rotate(T*o.lidTilt*.6),e.beginPath(),e.rect(-1,0,2,2),e.clip(),e.rotate(-T*o.lidTilt*.6),e.translate(-f,-W);}n>.5&&!a&&Oe(e,f,g,b*2.1,v*1.9,i,.35*S);let P=e.createRadialGradient(f,g-v*.2,0,f,g,Math.max(b,v));if(P.addColorStop(0,u),P.addColorStop(1,i),e.fillStyle=P,e.beginPath(),e.ellipse(f,g,b,v,0,0,Math.PI*2),e.fill(),e.fillStyle=i,c>.35&&!a){let F=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),de=r==="#fff"?.3:.34;e.globalAlpha=S*Math.min(1,(c-.35)*3),e.fillStyle=r,e.beginPath(),e.arc(f-b*.1+F*b*.5,g-v*.16+W*v*.42,b*de,0,Math.PI*2),e.fill(),e.fillStyle=i;}e.restore();}if(n>.5&&!a&&E>.01&&Oe(e,f,g,b*2,b*1.8,i,.3*E),e.fillStyle=i,o.happyEye>.01){let c=.6+.4*o.happyEye;e.globalAlpha=Math.min(1,o.happyEye),e.beginPath(),e.arc(f,g+.045,b*1.15*c,Math.PI*1.12,Math.PI*1.88),e.stroke();}if(o.closedEye>.01){let c=.6+.4*o.closedEye;e.globalAlpha=Math.min(1,o.closedEye),e.beginPath(),e.moveTo(f-b*1.15*c,g+.01),e.quadraticCurveTo(f,g+.07*c,f+b*1.15*c,g+.01),e.stroke();}if(o.xEye>.01){let c=b*.95*(.6+.4*o.xEye);e.globalAlpha=Math.min(1,o.xEye),e.beginPath(),e.moveTo(f-c,g-c),e.lineTo(f+c,g+c),e.moveTo(f+c,g-c),e.lineTo(f-c,g+c),e.stroke();}o.heartEye>.01&&(e.globalAlpha=Math.min(1,o.heartEye),Me(e,f,g,b*1.35*(.5+.5*o.heartEye)),e.fill());}let w=p+o.mouthSkew*.07,k=rt+m,C=Math.min(1,Math.max(0,o.mouthRound)),L=o.mouthW,B=e.createLinearGradient(w-L-.02,k,w+L+.02,k);B.addColorStop(0,d),B.addColorStop(.5,u),B.addColorStop(1,d);let A=(T,f,g,b)=>{e.fillStyle=l,T(),e.fill(),f>.02&&!a&&(e.save(),T(),e.clip(),e.fillStyle=s,e.globalAlpha*=Math.min(1,(f-.02)*12),e.beginPath(),e.ellipse(w,g,b,f*.55,0,0,Math.PI*2),e.fill(),e.restore()),e.strokeStyle=B;let c=e.lineWidth;e.lineWidth=c*.6,T(),e.stroke(),e.lineWidth=c;};if(C<.99){e.globalAlpha=1-C;let T=18,f=v=>o.mouthCurve*.075*(1-v*v),g=(v,P)=>{let F=v/T*2-1,W=w+F*L,de=k+f(F)+o.mouthSkew*F*.03+o.mouthWave*.018*Math.sin((F+1)*Math.PI*2)+P*(1-F*F);return [W,de]},b=Math.max(0,o.mouthOpen),c=()=>{for(let v=0;v<=T;v++){let[P,F]=g(v,-b*.15);v?e.lineTo(P,F):e.moveTo(P,F);}};b>.003?A(()=>{e.beginPath(),c();for(let P=T;P>=0;P--){let[F,W]=g(P,b);e.lineTo(F,W);}e.closePath();},b,g(T/2,b)[1],L*.55):(e.strokeStyle=B,e.beginPath(),c(),e.stroke());}if(C>.01){e.globalAlpha=C;let T=Math.max(.02,o.mouthOpen*.55+.01),f=()=>{e.beginPath(),e.ellipse(w,k+.01,T*.85,T,0,0,Math.PI*2);};e.lineWidth=ge*.8,A(f,o.mouthOpen,k+.01+T*.8,T*.6),e.lineWidth=ge;}e.globalAlpha=1;}function Oe(e,o,t,i,s,n,u){let r=e.createRadialGradient(o,t,0,o,t,1);r.addColorStop(0,n),r.addColorStop(1,"rgba(0,0,0,0)"),e.save(),e.globalAlpha=u,e.translate(o,t),e.scale(i,s),e.translate(-o,-t),e.fillStyle=r,e.beginPath(),e.arc(o,t,1,0,Math.PI*2),e.fill(),e.restore();}function Me(e,o,t,i){e.beginPath(),e.moveTo(o,t+i*.85),e.bezierCurveTo(o-i*1.25,t+i*.05,o-i*.75,t-i*.95,o,t-i*.35),e.bezierCurveTo(o+i*.75,t-i*.95,o+i*1.25,t+i*.05,o,t+i*.85),e.closePath();}var Q=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:M,cos:ht,abs:lt,max:ne,exp:ct,PI:ae}=Math;function re(e,o,t,i){switch(i.y=i.rx=i.ry=i.rz=i.st=i.mouth=i.lookX=i.lookY=0,e){case "idle":{let s=M(o*1.6);i.y=.015*s,i.st=.018*s;break}case "thinking":{i.rz=.07*M(o*1.1),i.y=.02*M(o*2.2),i.ry=.15,i.rx=-0.06;break}case "loading":{let s=o*2.4;i.rx=.12*M(s),i.rz=.12*ht(s),i.ry=.22*M(s),i.y=.02*M(s*2),i.lookX=.55*M(s);break}case "listening":{let s=ne(0,M(o*3.2));i.rx=.09*s*s;let n=lt(M(o*7.1)*.5+M(o*11.3)*.3+M(o*3.7)*.2);i.st=.03*n;break}case "speaking":{let s=ne(0,M(o*9)*.6+M(o*14.3)*.4)*(.65+.35*M(o*1.7));i.mouth=.11*s,i.st=.04*s,i.rz=.03*M(o*2.1);break}case "happy":{let s=o*1.35%1;i.y=.2*M(ae*s);let n=ne(0,1-s/.14)+ne(0,(s-.9)/.1);i.st=.08*M(ae*s)-.14*n,i.rz=.05*M(o*1.35*ae);break}case "surprised":{if(t<.45){let s=M(ae*t/.45);i.y=.16*s,i.st=.12*s;}i.rz=.01*M(o*38)*ct(-t*2),i.rx=-0.06;break}case "sad":{let s=M(o*1.1);i.y=-0.06,i.rx=.2,i.st=-0.04+.015*s,i.rz=-0.04;break}case "error":{let s=t%2.8;if(s<.55){let n=1-s/.55;i.ry=.26*M(s*42)*n,i.rz=.05*M(s*42+1)*n;}i.st=-0.02;break}case "sleeping":{let s=M(o*1);i.st=.04*s,i.y=-0.03,i.rz=.1,i.rx=.1;break}case "love":{i.y=.07+.06*M(o*1.6),i.rz=.05*M(o*.8),i.st=.015*M(o*1.6+1);break}}return i}function we(e,o,t,i){for(let s of Object.keys(i))i[s]=e[s]+(o[s]-e[s])*t;return i}var Ge={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var K=new Set,x=null,$=0,V=0,he=0,Ce,De=false,j="idle",_={x:0,y:0,lastMove:-1e9,known:false},O={reducedMotion:false,lowPower:false};function dt(){if(De)return;De=true,addEventListener("pointermove",o=>{_.x=o.clientX,_.y=o.clientY,_.lastMove=performance.now(),_.known=true;},{passive:true}),document.addEventListener("visibilitychange",H);let e=matchMedia("(prefers-reduced-motion: reduce)");O.reducedMotion=e.matches,e.addEventListener?.("change",o=>O.reducedMotion=o.matches);}function Ie(e,o){return x||(x=new three.WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),x.setPixelRatio(1),x.setClearColor(0,0),O.lowPower=pt(x.getContext()),x.setScissorTest(true),x.domElement.addEventListener("webglcontextlost",t=>{t.preventDefault(),x?.dispose(),x=null,$=V=0,j="idle";})),(e>$||o>V)&&($=Math.max($,e),V=Math.max(V,o),x.setSize($,V,false)),x}var qe=()=>V;function pt(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(j==="ready")return true;if(j==="idle"&&x){j="compiling";let t=x;t.compileAsync(e,o).catch(()=>{}).then(()=>{t===x&&(j="ready"),H();});}return false}function _e(e){dt(),clearTimeout(Ce),K.add(e),H();}function He(e){K.delete(e),K.size===0&&(clearTimeout(Ce),Ce=setTimeout(()=>{K.size||!x||(x.dispose(),x.forceContextLoss(),x=null,$=V=0,j="idle");},1500));}function H(){if(!he){for(let e of K)if(e.active){he=requestAnimationFrame(Ne);return}}}var mt=10,Ye=new WeakMap,ke=0;function Ne(e){he=0;let o=[...K].filter(n=>n.active);if(!o.length)return;let t=performance.now(),i=o.length,s=0;for(;s<i;s++){let n=o[(ke+s)%i],u=Ye.get(n)??e-16;if(Ye.set(n,e),n.tick(Math.min(.05,Math.max(0,(e-u)/1e3)),e),performance.now()-t>mt){s++;break}}ke=(ke+s)%i,he=requestAnimationFrame(Ne);}var ee={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"),i=o.getIndex(),s=new Map,n=[],u=new Uint32Array(t.count);for(let a=0;a<t.count;a++){let h=t.getX(a),p=t.getY(a),m=t.getZ(a),E=`${Math.round(h*1e4)},${Math.round(p*1e4)},${Math.round(m*1e4)}`,S=s.get(E);if(S===void 0){S=n.length/3,s.set(E,S);let w=Math.hypot(h,p,m)||1;n.push(h/w,p/w,m/w);}u[a]=S;}let r=[];for(let a=0;a<i.count;a+=3){let h=u[i.getX(a)],p=u[i.getX(a+1)],m=u[i.getX(a+2)];h!==p&&p!==m&&h!==m&&r.push(h,p,m);}o.dispose();let l=new three.BufferGeometry,d=new Float32Array(n);return l.setAttribute("position",new three.BufferAttribute(new Float32Array(d),3)),l.setIndex(r),{geometry:l,dirs:d}}var vt=(e,o,t)=>{let i=Math.min(1,Math.max(0,(t-e)/(o-e)));return i*i*(3-2*i)};function Se(e,o,t,i,s,n,u){let r=o,l=t,d=i;switch(e){case "pebble":{r=o*1.1+.04*t,l=t*.92*(.95+.05*Math.tanh(3*t))+.03*o,d=i*.94;break}case "blob":{let a=.97+.045*Math.sin(2.1*o+1.3*s)*Math.sin(2.4*t+1.7*s+1)+.035*Math.sin(2.7*i+2.3*o+1.1*s);r=o*a,l=t*a,d=i*a;break}case "drop":{let a=Math.max(t,0),h=1-.8*Math.pow(a,1.6);r=o*h*.98+.1*a*a*a,l=(t<0?t*.9:t*1.32)-.12,d=i*h*.98;break}case "bean":{let a=t*.78+.42*Math.tanh(t*3);r=(o*.8+.14*(a*a/1.44)-.07)*.9,l=a*.88,d=i*.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(i),4.5),-0.2222222222222222);r=o*h*.84,l=t*h*.82,d=i*h*.84;break}case "ghost":{if(t>=0)r=o,l=t,d=i;else {let h=(Math.acos(Math.max(-1,t))-Math.PI/2)/(Math.PI/2),p=Math.hypot(o,i)||1,m=o/p,E=i/p,S=Math.atan2(i,o),w=.085*Math.sin(5*S+2.2*s),k=1.04,C=.13,L=-1.05+w,B;if(h<.45){let A=h/.45;B=1+.04*A,l=-A*(1.05-C)+w*vt(.45,1,A);}else if(h<.75){let A=(h-.45)/.3*(Math.PI/2);B=k-C+C*Math.cos(A),l=L+C-C*Math.sin(A);}else {let A=(h-.75)/.25;B=(k-C)*(1-A),l=L+.22*A*A*(3-2*A);}r=m*B,d=E*B;}r*=.9,l=(l+.1)*.9,d*=.9;break}}n[u]=r,n[u+1]=l,n[u+2]=d;}var I=null,le=null;function yt(){if(typeof window>"u"||!("AudioContext"in window))return null;if(Mt(),!I){I=new AudioContext;let e=I.createBiquadFilter();e.type="lowpass",e.frequency.value=3200,le=I.createGain(),le.gain.value=.55,le.connect(e).connect(I.destination);}return I.state==="suspended"&&I.resume(),I}function z(e,{freq:o,to:t,at:i=0,dur:s=.12,type:n="sine",gain:u=.1,wobble:r=0}){let l=e.currentTime+i,d=e.createOscillator(),a=e.createGain();if(d.type=n,d.frequency.setValueAtTime(o,l),t&&d.frequency.exponentialRampToValueAtTime(t,l+s),r){let h=e.createOscillator(),p=e.createGain();h.frequency.value=7,p.gain.value=r,h.connect(p).connect(d.frequency),h.start(l),h.stop(l+s+.05);}a.gain.setValueAtTime(1e-4,l),a.gain.exponentialRampToValueAtTime(u,l+Math.min(.012,s/4)),a.gain.exponentialRampToValueAtTime(1e-4,l+s),d.connect(a).connect(le),d.start(l),d.stop(l+s+.02);}var Te={idle:e=>z(e,{freq:520,to:780,dur:.09,gain:.08}),thinking:e=>[660,760,900].forEach((o,t)=>z(e,{freq:o,to:o*1.08,at:t*.1,dur:.07,type:"triangle",gain:.06})),loading:e=>[0,.14,.28].forEach(o=>z(e,{freq:940,at:o,dur:.04,gain:.05})),listening:e=>{z(e,{freq:523,dur:.12,type:"triangle",gain:.07}),z(e,{freq:784,at:.1,dur:.22,type:"triangle",gain:.07});},speaking:e=>[380,460,340,520,420].forEach((o,t)=>z(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)=>z(e,{freq:o,at:t*.07,dur:.14,type:"triangle",gain:.07})),surprised:e=>z(e,{freq:280,to:980,dur:.18,gain:.09,wobble:30}),sad:e=>{z(e,{freq:560,to:300,dur:.5,gain:.07}),z(e,{freq:420,to:240,at:.12,dur:.5,gain:.04});},error:e=>[0,.14].forEach(o=>z(e,{freq:170,to:150,at:o,dur:.1,type:"sawtooth",gain:.045})),sleeping:e=>{z(e,{freq:150,to:115,dur:.6,gain:.07,wobble:6}),z(e,{freq:300,to:230,at:.05,dur:.5,gain:.02});},love:e=>[1319,1568,2093,1760].forEach((o,t)=>z(e,{freq:o,at:t*.08,dur:.22,gain:.04}))},q=null,Xe=false;function Mt(){if(Xe)return;Xe=true;let e=()=>{I&&I.resume().then(()=>{q&&performance.now()<q.until&&Te[q.mood](I),q=null;});};for(let o of ["pointerdown","touchend","keydown"])addEventListener(o,e,{passive:true});}function ce(e,o=false){let t=yt();if(t){if(t.state==="running"){q=null,Te[e](t);return}q={mood:e,until:performance.now()+(o?6e3:1200)},t.resume().then(()=>{q&&performance.now()<q.until&&Te[q.mood](t),q=null;});}}var Ee=-0.85,Je=new Map;function Et(e){let o=Je.get(e);if(!o){let{geometry:t,dirs:i}=Ve(e);o={dirs:i,index:new Uint32Array(t.getIndex().array)},t.dispose(),Je.set(e,o);}return o}var U=(e,o)=>1-Math.exp(-e*o),Pe=e=>1-Math.pow(1-e,3),ue=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.heightCanvas=document.createElement("canvas");this.uniforms={uFace:{value:null},uHeight:{value:null},uFaceY:{value:0},uFaceScale:{value:1.5}};this.state={...y};this.visible=false;this.connected=false;this.w=0;this.h=0;this.t=Math.random()*100;this.shapeFrom=y.shape;this.shapeTo=y.shape;this.morph=1;this.shapeDirty=true;this.shown=y.mood;this.prevShown=y.mood;this.tau=10;this.prevTau=10;this.blend=1;this.reaction=null;this.pose=Q();this.poseIdle=Q();this.moodTime=0;this.poseA=Q();this.poseB=Q();this.face=ve(se.idle);this.target=ve(se.idle);this.drawn=new Float32Array(Z.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=X(y.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=oe(this.body);this.faceCT=this.faceC;this.cheek=ie(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.heightCtx=this.heightCanvas.getContext("2d"),this.heightCanvas.width=this.heightCanvas.height=256,this.heightTex=new three.CanvasTexture(this.heightCanvas),this.heightTex.minFilter=three.LinearMipmapLinearFilter,this.uniforms.uHeight.value=this.heightTex,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 i=new three.DirectionalLight(16773350,.45);i.position.set(-2,-1,3),this.scene.add(o,t,i),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 Qe=["ghost","pebble","blob","drop","bean","cube"],et=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],y={shape:"ghost",mood:"idle",color:"#2b15d1",size:160,speed:1,followCursor:true,interactive:true,shadow:true,sound:false,settle:0},Ot={Iris:"#2b15d1",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},Lt={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."}},Gt={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},pe=e=>Qe.includes(e),J=e=>et.includes(e);var G=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 Be(t[1]);if(G||(G=document.createElement("canvas").getContext("2d")),!G)return o;G.fillStyle="#010203",G.fillStyle=e;let i=String(G.fillStyle);if(i==="#010203"&&e.trim()!=="#010203")return o;if(i.startsWith("#"))return Be(i.slice(1));G.canvas.width=G.canvas.height=1,G.clearRect(0,0,1,1),G.fillRect(0,0,1,1);let s=G.getImageData(0,0,1,1).data;return [s[0]/255,s[1]/255,s[2]/255]}function Be(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 D=e=>`rgb(${Math.round(e[0]*255)},${Math.round(e[1]*255)},${Math.round(e[2]*255)})`,R=(e,o,t)=>[e[0]+(o[0]-e[0])*t,e[1]+(o[1]-e[1])*t,e[2]+(o[2]-e[2])*t],me=e=>e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4),fe=e=>.2126*me(e[0])+.7152*me(e[1])+.0722*me(e[2]),tt=[.07,.06,.08],ot=[209/255,190/255,173/255],it=[1,.42,.48],oe=e=>fe(e)>.042?R(tt,e,.06):ot,ie=e=>R(e,it,.78);var Z=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],Y={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},se={idle:{...Y},thinking:{...Y,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...Y,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...Y,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...Y,mouthW:.09,mouthCurve:.25},happy:{...Y,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...Y,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...Y,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...Y,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...Y,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...Y,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Le={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},ve=e=>({...e}),st=.31,nt=-0.04,at=.098,be=.128,rt=.2,ge=.05;function ye(e,o,t,i,s,n,u,r,l,d,a=false){let h=e.canvas.width;e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,h,h),e.setTransform(h/2,0,0,h/2,h/2,h/2),e.lineCap="round",e.lineJoin="round";let p=o.lookX*.075,m=o.lookY*.06;if(o.blush>.01&&!a){let T=h*.018;e.save(),"filter"in e&&(e.filter=`blur(${T}px)`);for(let g of [-1,1]){let b=g*.52+p,c=.16+m,v=e.createRadialGradient(b,c,0,b,c,.16);v.addColorStop(0,s),v.addColorStop(1,"rgba(0,0,0,0)"),e.globalAlpha=Math.min(1,o.blush)*.62,e.fillStyle=v,e.beginPath(),e.ellipse(b,c,.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=s,e.lineWidth=.016,e.lineCap="round";for(let g of [-1,1]){let b=g*.52+p,c=.16+m;for(let v=-1;v<=1;v++){let P=b+v*.045;e.beginPath(),e.moveTo(P+.018,c-.035),e.lineTo(P-.018,c+.035),e.stroke();}}e.restore();}}e.fillStyle=i,e.strokeStyle=i,e.lineWidth=ge;let E=Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye),S=1-E;for(let T of [-1,1]){let f=T*st+p,g=nt+m,b=at*o.eyeSize;if(S>.01){e.globalAlpha=S;let c=Math.max(.06,o.eyeOpen*t),v=be*o.eyeSize*c;if(e.save(),o.lidCover>.01||o.lidTilt>.01){let W=g-be*o.eyeSize+2*be*o.eyeSize*o.lidCover;e.translate(f,W),e.rotate(T*o.lidTilt*.6),e.beginPath(),e.rect(-1,0,2,2),e.clip(),e.rotate(-T*o.lidTilt*.6),e.translate(-f,-W);}n>.5&&!a&&Oe(e,f,g,b*2.1,v*1.9,i,.35*S);let P=e.createRadialGradient(f,g-v*.2,0,f,g,Math.max(b,v));if(P.addColorStop(0,u),P.addColorStop(1,i),e.fillStyle=P,e.beginPath(),e.ellipse(f,g,b,v,0,0,Math.PI*2),e.fill(),e.fillStyle=i,c>.35&&!a){let F=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),de=r==="#fff"?.3:.34;e.globalAlpha=S*Math.min(1,(c-.35)*3),e.fillStyle=r,e.beginPath(),e.arc(f-b*.1+F*b*.5,g-v*.16+W*v*.42,b*de,0,Math.PI*2),e.fill(),e.fillStyle=i;}e.restore();}if(n>.5&&!a&&E>.01&&Oe(e,f,g,b*2,b*1.8,i,.3*E),e.fillStyle=i,o.happyEye>.01){let c=.6+.4*o.happyEye;e.globalAlpha=Math.min(1,o.happyEye),e.beginPath(),e.arc(f,g+.045,b*1.15*c,Math.PI*1.12,Math.PI*1.88),e.stroke();}if(o.closedEye>.01){let c=.6+.4*o.closedEye;e.globalAlpha=Math.min(1,o.closedEye),e.beginPath(),e.moveTo(f-b*1.15*c,g+.01),e.quadraticCurveTo(f,g+.07*c,f+b*1.15*c,g+.01),e.stroke();}if(o.xEye>.01){let c=b*.95*(.6+.4*o.xEye);e.globalAlpha=Math.min(1,o.xEye),e.beginPath(),e.moveTo(f-c,g-c),e.lineTo(f+c,g+c),e.moveTo(f+c,g-c),e.lineTo(f-c,g+c),e.stroke();}o.heartEye>.01&&(e.globalAlpha=Math.min(1,o.heartEye),Me(e,f,g,b*1.35*(.5+.5*o.heartEye)),e.fill());}let w=p+o.mouthSkew*.07,k=rt+m,C=Math.min(1,Math.max(0,o.mouthRound)),L=o.mouthW,B=e.createLinearGradient(w-L-.02,k,w+L+.02,k);B.addColorStop(0,d),B.addColorStop(.5,u),B.addColorStop(1,d);let A=(T,f,g,b)=>{e.fillStyle=l,T(),e.fill(),f>.02&&!a&&(e.save(),T(),e.clip(),e.fillStyle=s,e.globalAlpha*=Math.min(1,(f-.02)*12),e.beginPath(),e.ellipse(w,g,b,f*.55,0,0,Math.PI*2),e.fill(),e.restore()),e.strokeStyle=B;let c=e.lineWidth;e.lineWidth=c*.6,T(),e.stroke(),e.lineWidth=c;};if(C<.99){e.globalAlpha=1-C;let T=18,f=v=>o.mouthCurve*.075*(1-v*v),g=(v,P)=>{let F=v/T*2-1,W=w+F*L,de=k+f(F)+o.mouthSkew*F*.03+o.mouthWave*.018*Math.sin((F+1)*Math.PI*2)+P*(1-F*F);return [W,de]},b=Math.max(0,o.mouthOpen),c=()=>{for(let v=0;v<=T;v++){let[P,F]=g(v,-b*.15);v?e.lineTo(P,F):e.moveTo(P,F);}};b>.003?A(()=>{e.beginPath(),c();for(let P=T;P>=0;P--){let[F,W]=g(P,b);e.lineTo(F,W);}e.closePath();},b,g(T/2,b)[1],L*.55):(e.strokeStyle=B,e.beginPath(),c(),e.stroke());}if(C>.01){e.globalAlpha=C;let T=Math.max(.02,o.mouthOpen*.55+.01),f=()=>{e.beginPath(),e.ellipse(w,k+.01,T*.85,T,0,0,Math.PI*2);};e.lineWidth=ge*.8,A(f,o.mouthOpen,k+.01+T*.8,T*.6),e.lineWidth=ge;}e.globalAlpha=1;}function Oe(e,o,t,i,s,n,u){let r=e.createRadialGradient(o,t,0,o,t,1);r.addColorStop(0,n),r.addColorStop(1,"rgba(0,0,0,0)"),e.save(),e.globalAlpha=u,e.translate(o,t),e.scale(i,s),e.translate(-o,-t),e.fillStyle=r,e.beginPath(),e.arc(o,t,1,0,Math.PI*2),e.fill(),e.restore();}function Me(e,o,t,i){e.beginPath(),e.moveTo(o,t+i*.85),e.bezierCurveTo(o-i*1.25,t+i*.05,o-i*.75,t-i*.95,o,t-i*.35),e.bezierCurveTo(o+i*.75,t-i*.95,o+i*1.25,t+i*.05,o,t+i*.85),e.closePath();}var Q=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:M,cos:ht,abs:lt,max:ne,exp:ct,PI:ae}=Math;function re(e,o,t,i){switch(i.y=i.rx=i.ry=i.rz=i.st=i.mouth=i.lookX=i.lookY=0,e){case "idle":{let s=M(o*1.6);i.y=.015*s,i.st=.018*s;break}case "thinking":{i.rz=.07*M(o*1.1),i.y=.02*M(o*2.2),i.ry=.15,i.rx=-0.06;break}case "loading":{let s=o*2.4;i.rx=.12*M(s),i.rz=.12*ht(s),i.ry=.22*M(s),i.y=.02*M(s*2),i.lookX=.55*M(s);break}case "listening":{let s=ne(0,M(o*3.2));i.rx=.09*s*s;let n=lt(M(o*7.1)*.5+M(o*11.3)*.3+M(o*3.7)*.2);i.st=.03*n;break}case "speaking":{let s=ne(0,M(o*9)*.6+M(o*14.3)*.4)*(.65+.35*M(o*1.7));i.mouth=.11*s,i.st=.04*s,i.rz=.03*M(o*2.1);break}case "happy":{let s=o*1.35%1;i.y=.2*M(ae*s);let n=ne(0,1-s/.14)+ne(0,(s-.9)/.1);i.st=.08*M(ae*s)-.14*n,i.rz=.05*M(o*1.35*ae);break}case "surprised":{if(t<.45){let s=M(ae*t/.45);i.y=.16*s,i.st=.12*s;}i.rz=.01*M(o*38)*ct(-t*2),i.rx=-0.06;break}case "sad":{let s=M(o*1.1);i.y=-0.06,i.rx=.2,i.st=-0.04+.015*s,i.rz=-0.04;break}case "error":{let s=t%2.8;if(s<.55){let n=1-s/.55;i.ry=.26*M(s*42)*n,i.rz=.05*M(s*42+1)*n;}i.st=-0.02;break}case "sleeping":{let s=M(o*1);i.st=.04*s,i.y=-0.03,i.rz=.1,i.rx=.1;break}case "love":{i.y=.07+.06*M(o*1.6),i.rz=.05*M(o*.8),i.st=.015*M(o*1.6+1);break}}return i}function we(e,o,t,i){for(let s of Object.keys(i))i[s]=e[s]+(o[s]-e[s])*t;return i}var Ge={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var K=new Set,x=null,$=0,V=0,he=0,Ce,De=false,j="idle",_={x:0,y:0,lastMove:-1e9,known:false},O={reducedMotion:false,lowPower:false};function dt(){if(De)return;De=true,addEventListener("pointermove",o=>{_.x=o.clientX,_.y=o.clientY,_.lastMove=performance.now(),_.known=true;},{passive:true}),document.addEventListener("visibilitychange",H);let e=matchMedia("(prefers-reduced-motion: reduce)");O.reducedMotion=e.matches,e.addEventListener?.("change",o=>O.reducedMotion=o.matches);}function Ie(e,o){return x||(x=new three.WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),x.setPixelRatio(1),x.setClearColor(0,0),O.lowPower=pt(x.getContext()),x.setScissorTest(true),x.domElement.addEventListener("webglcontextlost",t=>{t.preventDefault(),x?.dispose(),x=null,$=V=0,j="idle";})),(e>$||o>V)&&($=Math.max($,e),V=Math.max(V,o),x.setSize($,V,false)),x}var qe=()=>V;function pt(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(j==="ready")return true;if(j==="idle"&&x){j="compiling";let t=x;t.compileAsync(e,o).catch(()=>{}).then(()=>{t===x&&(j="ready"),H();});}return false}function _e(e){dt(),clearTimeout(Ce),K.add(e),H();}function He(e){K.delete(e),K.size===0&&(clearTimeout(Ce),Ce=setTimeout(()=>{K.size||!x||(x.dispose(),x.forceContextLoss(),x=null,$=V=0,j="idle");},1500));}function H(){if(!he){for(let e of K)if(e.active){he=requestAnimationFrame(Ne);return}}}var mt=10,Ye=new WeakMap,ke=0;function Ne(e){he=0;let o=[...K].filter(n=>n.active);if(!o.length)return;let t=performance.now(),i=o.length,s=0;for(;s<i;s++){let n=o[(ke+s)%i],u=Ye.get(n)??e-16;if(Ye.set(n,e),n.tick(Math.min(.05,Math.max(0,(e-u)/1e3)),e),performance.now()-t>mt){s++;break}}ke=(ke+s)%i,he=requestAnimationFrame(Ne);}var ee={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"),i=o.getIndex(),s=new Map,n=[],u=new Uint32Array(t.count);for(let a=0;a<t.count;a++){let h=t.getX(a),p=t.getY(a),m=t.getZ(a),E=`${Math.round(h*1e4)},${Math.round(p*1e4)},${Math.round(m*1e4)}`,S=s.get(E);if(S===void 0){S=n.length/3,s.set(E,S);let w=Math.hypot(h,p,m)||1;n.push(h/w,p/w,m/w);}u[a]=S;}let r=[];for(let a=0;a<i.count;a+=3){let h=u[i.getX(a)],p=u[i.getX(a+1)],m=u[i.getX(a+2)];h!==p&&p!==m&&h!==m&&r.push(h,p,m);}o.dispose();let l=new three.BufferGeometry,d=new Float32Array(n);return l.setAttribute("position",new three.BufferAttribute(new Float32Array(d),3)),l.setIndex(r),{geometry:l,dirs:d}}var vt=(e,o,t)=>{let i=Math.min(1,Math.max(0,(t-e)/(o-e)));return i*i*(3-2*i)};function Se(e,o,t,i,s,n,u){let r=o,l=t,d=i;switch(e){case "pebble":{r=o*1.1+.04*t,l=t*.92*(.95+.05*Math.tanh(3*t))+.03*o,d=i*.94;break}case "blob":{let a=.97+.045*Math.sin(2.1*o+1.3*s)*Math.sin(2.4*t+1.7*s+1)+.035*Math.sin(2.7*i+2.3*o+1.1*s);r=o*a,l=t*a,d=i*a;break}case "drop":{let a=Math.max(t,0),h=1-.8*Math.pow(a,1.6);r=o*h*.98+.1*a*a*a,l=(t<0?t*.9:t*1.32)-.12,d=i*h*.98;break}case "bean":{let a=t*.78+.42*Math.tanh(t*3);r=(o*.8+.14*(a*a/1.44)-.07)*.9,l=a*.88,d=i*.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(i),4.5),-0.2222222222222222);r=o*h*.84,l=t*h*.82,d=i*h*.84;break}case "ghost":{if(t>=0)r=o,l=t,d=i;else {let h=(Math.acos(Math.max(-1,t))-Math.PI/2)/(Math.PI/2),p=Math.hypot(o,i)||1,m=o/p,E=i/p,S=Math.atan2(i,o),w=.085*Math.sin(5*S+2.2*s),k=1.04,C=.13,L=-1.05+w,B;if(h<.45){let A=h/.45;B=1+.04*A,l=-A*(1.05-C)+w*vt(.45,1,A);}else if(h<.75){let A=(h-.45)/.3*(Math.PI/2);B=k-C+C*Math.cos(A),l=L+C-C*Math.sin(A);}else {let A=(h-.75)/.25;B=(k-C)*(1-A),l=L+.22*A*A*(3-2*A);}r=m*B,d=E*B;}r*=.9,l=(l+.1)*.9,d*=.9;break}}n[u]=r,n[u+1]=l,n[u+2]=d;}var I=null,le=null;function yt(){if(typeof window>"u"||!("AudioContext"in window))return null;if(Mt(),!I){I=new AudioContext;let e=I.createBiquadFilter();e.type="lowpass",e.frequency.value=3200,le=I.createGain(),le.gain.value=.55,le.connect(e).connect(I.destination);}return I.state==="suspended"&&I.resume(),I}function z(e,{freq:o,to:t,at:i=0,dur:s=.12,type:n="sine",gain:u=.1,wobble:r=0}){let l=e.currentTime+i,d=e.createOscillator(),a=e.createGain();if(d.type=n,d.frequency.setValueAtTime(o,l),t&&d.frequency.exponentialRampToValueAtTime(t,l+s),r){let h=e.createOscillator(),p=e.createGain();h.frequency.value=7,p.gain.value=r,h.connect(p).connect(d.frequency),h.start(l),h.stop(l+s+.05);}a.gain.setValueAtTime(1e-4,l),a.gain.exponentialRampToValueAtTime(u,l+Math.min(.012,s/4)),a.gain.exponentialRampToValueAtTime(1e-4,l+s),d.connect(a).connect(le),d.start(l),d.stop(l+s+.02);}var Te={idle:e=>z(e,{freq:520,to:780,dur:.09,gain:.08}),thinking:e=>[660,760,900].forEach((o,t)=>z(e,{freq:o,to:o*1.08,at:t*.1,dur:.07,type:"triangle",gain:.06})),loading:e=>[0,.14,.28].forEach(o=>z(e,{freq:940,at:o,dur:.04,gain:.05})),listening:e=>{z(e,{freq:523,dur:.12,type:"triangle",gain:.07}),z(e,{freq:784,at:.1,dur:.22,type:"triangle",gain:.07});},speaking:e=>[380,460,340,520,420].forEach((o,t)=>z(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)=>z(e,{freq:o,at:t*.07,dur:.14,type:"triangle",gain:.07})),surprised:e=>z(e,{freq:280,to:980,dur:.18,gain:.09,wobble:30}),sad:e=>{z(e,{freq:560,to:300,dur:.5,gain:.07}),z(e,{freq:420,to:240,at:.12,dur:.5,gain:.04});},error:e=>[0,.14].forEach(o=>z(e,{freq:170,to:150,at:o,dur:.1,type:"sawtooth",gain:.045})),sleeping:e=>{z(e,{freq:150,to:115,dur:.6,gain:.07,wobble:6}),z(e,{freq:300,to:230,at:.05,dur:.5,gain:.02});},love:e=>[1319,1568,2093,1760].forEach((o,t)=>z(e,{freq:o,at:t*.08,dur:.22,gain:.04}))},q=null,Xe=false;function Mt(){if(Xe)return;Xe=true;let e=()=>{I&&I.resume().then(()=>{q&&performance.now()<q.until&&Te[q.mood](I),q=null;});};for(let o of ["pointerdown","touchend","keydown"])addEventListener(o,e,{passive:true});}function ce(e,o=false){let t=yt();if(t){if(t.state==="running"){q=null,Te[e](t);return}q={mood:e,until:performance.now()+(o?6e3:1200)},t.resume().then(()=>{q&&performance.now()<q.until&&Te[q.mood](t),q=null;});}}var Ee=-0.85,Je=new Map;function Et(e){let o=Je.get(e);if(!o){let{geometry:t,dirs:i}=Ve(e);o={dirs:i,index:new Uint32Array(t.getIndex().array)},t.dispose(),Je.set(e,o);}return o}var U=(e,o)=>1-Math.exp(-e*o),Pe=e=>1-Math.pow(1-e,3),ue=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.heightCanvas=document.createElement("canvas");this.uniforms={uFace:{value:null},uHeight:{value:null},uFaceY:{value:0},uFaceScale:{value:1.5}};this.state={...y};this.visible=false;this.connected=false;this.w=0;this.h=0;this.t=Math.random()*100;this.shapeFrom=y.shape;this.shapeTo=y.shape;this.morph=1;this.shapeDirty=true;this.shown=y.mood;this.prevShown=y.mood;this.tau=10;this.prevTau=10;this.blend=1;this.reaction=null;this.pose=Q();this.poseIdle=Q();this.moodTime=0;this.poseA=Q();this.poseB=Q();this.face=ve(se.idle);this.target=ve(se.idle);this.drawn=new Float32Array(Z.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=X(y.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=oe(this.body);this.faceCT=this.faceC;this.cheek=ie(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.heightCtx=this.heightCanvas.getContext("2d"),this.heightCanvas.width=this.heightCanvas.height=256,this.heightTex=new three.CanvasTexture(this.heightCanvas),this.heightTex.minFilter=three.LinearMipmapLinearFilter,this.uniforms.uHeight.value=this.heightTex,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 i=new three.DirectionalLight(16773350,.45);i.position.set(-2,-1,3),this.scene.add(o,t,i),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;
|
|
4
4
|
varying vec3 vTanX;
|
package/dist/index.d.cts
CHANGED
|
@@ -5,7 +5,7 @@ interface MoodiProps {
|
|
|
5
5
|
shape?: Shape;
|
|
6
6
|
/** Current mood / state. Default `'idle'`. */
|
|
7
7
|
mood?: Mood;
|
|
8
|
-
/** Body color, any CSS color. Default `'#
|
|
8
|
+
/** Body color, any CSS color. Default `'#2b15d1'`. */
|
|
9
9
|
color?: string;
|
|
10
10
|
/** Eyes / mouth color. Auto-contrast by default. */
|
|
11
11
|
faceColor?: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ interface MoodiProps {
|
|
|
5
5
|
shape?: Shape;
|
|
6
6
|
/** Current mood / state. Default `'idle'`. */
|
|
7
7
|
mood?: Mood;
|
|
8
|
-
/** Body color, any CSS color. Default `'#
|
|
8
|
+
/** Body color, any CSS color. Default `'#2b15d1'`. */
|
|
9
9
|
color?: string;
|
|
10
10
|
/** Eyes / mouth color. Auto-contrast by default. */
|
|
11
11
|
faceColor?: string;
|
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 Qe=["ghost","pebble","blob","drop","bean","cube"],et=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],y={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:true,interactive:true,shadow:true,sound:false,settle:0},Ot={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},Lt={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."}},Gt={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},pe=e=>Qe.includes(e),J=e=>et.includes(e);var G=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 Be(t[1]);if(G||(G=document.createElement("canvas").getContext("2d")),!G)return o;G.fillStyle="#010203",G.fillStyle=e;let i=String(G.fillStyle);if(i==="#010203"&&e.trim()!=="#010203")return o;if(i.startsWith("#"))return Be(i.slice(1));G.canvas.width=G.canvas.height=1,G.clearRect(0,0,1,1),G.fillRect(0,0,1,1);let s=G.getImageData(0,0,1,1).data;return [s[0]/255,s[1]/255,s[2]/255]}function Be(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 D=e=>`rgb(${Math.round(e[0]*255)},${Math.round(e[1]*255)},${Math.round(e[2]*255)})`,R=(e,o,t)=>[e[0]+(o[0]-e[0])*t,e[1]+(o[1]-e[1])*t,e[2]+(o[2]-e[2])*t],me=e=>e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4),fe=e=>.2126*me(e[0])+.7152*me(e[1])+.0722*me(e[2]),tt=[.07,.06,.08],ot=[209/255,190/255,173/255],it=[1,.42,.48],oe=e=>fe(e)>.06?R(tt,e,.06):ot,ie=e=>R(e,it,.78);var Z=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],Y={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},se={idle:{...Y},thinking:{...Y,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...Y,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...Y,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...Y,mouthW:.09,mouthCurve:.25},happy:{...Y,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...Y,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...Y,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...Y,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...Y,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...Y,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Le={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},ve=e=>({...e}),st=.31,nt=-0.04,at=.098,be=.128,rt=.2,ge=.05;function ye(e,o,t,i,s,n,u,r,l,d,a=false){let h=e.canvas.width;e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,h,h),e.setTransform(h/2,0,0,h/2,h/2,h/2),e.lineCap="round",e.lineJoin="round";let p=o.lookX*.075,m=o.lookY*.06;if(o.blush>.01&&!a){let T=h*.018;e.save(),"filter"in e&&(e.filter=`blur(${T}px)`);for(let g of [-1,1]){let b=g*.52+p,c=.16+m,v=e.createRadialGradient(b,c,0,b,c,.16);v.addColorStop(0,s),v.addColorStop(1,"rgba(0,0,0,0)"),e.globalAlpha=Math.min(1,o.blush)*.62,e.fillStyle=v,e.beginPath(),e.ellipse(b,c,.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=s,e.lineWidth=.016,e.lineCap="round";for(let g of [-1,1]){let b=g*.52+p,c=.16+m;for(let v=-1;v<=1;v++){let P=b+v*.045;e.beginPath(),e.moveTo(P+.018,c-.035),e.lineTo(P-.018,c+.035),e.stroke();}}e.restore();}}e.fillStyle=i,e.strokeStyle=i,e.lineWidth=ge;let E=Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye),S=1-E;for(let T of [-1,1]){let f=T*st+p,g=nt+m,b=at*o.eyeSize;if(S>.01){e.globalAlpha=S;let c=Math.max(.06,o.eyeOpen*t),v=be*o.eyeSize*c;if(e.save(),o.lidCover>.01||o.lidTilt>.01){let W=g-be*o.eyeSize+2*be*o.eyeSize*o.lidCover;e.translate(f,W),e.rotate(T*o.lidTilt*.6),e.beginPath(),e.rect(-1,0,2,2),e.clip(),e.rotate(-T*o.lidTilt*.6),e.translate(-f,-W);}n>.5&&!a&&Oe(e,f,g,b*2.1,v*1.9,i,.35*S);let P=e.createRadialGradient(f,g-v*.2,0,f,g,Math.max(b,v));if(P.addColorStop(0,u),P.addColorStop(1,i),e.fillStyle=P,e.beginPath(),e.ellipse(f,g,b,v,0,0,Math.PI*2),e.fill(),e.fillStyle=i,c>.35&&!a){let F=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),de=r==="#fff"?.3:.34;e.globalAlpha=S*Math.min(1,(c-.35)*3),e.fillStyle=r,e.beginPath(),e.arc(f-b*.1+F*b*.5,g-v*.16+W*v*.42,b*de,0,Math.PI*2),e.fill(),e.fillStyle=i;}e.restore();}if(n>.5&&!a&&E>.01&&Oe(e,f,g,b*2,b*1.8,i,.3*E),e.fillStyle=i,o.happyEye>.01){let c=.6+.4*o.happyEye;e.globalAlpha=Math.min(1,o.happyEye),e.beginPath(),e.arc(f,g+.045,b*1.15*c,Math.PI*1.12,Math.PI*1.88),e.stroke();}if(o.closedEye>.01){let c=.6+.4*o.closedEye;e.globalAlpha=Math.min(1,o.closedEye),e.beginPath(),e.moveTo(f-b*1.15*c,g+.01),e.quadraticCurveTo(f,g+.07*c,f+b*1.15*c,g+.01),e.stroke();}if(o.xEye>.01){let c=b*.95*(.6+.4*o.xEye);e.globalAlpha=Math.min(1,o.xEye),e.beginPath(),e.moveTo(f-c,g-c),e.lineTo(f+c,g+c),e.moveTo(f+c,g-c),e.lineTo(f-c,g+c),e.stroke();}o.heartEye>.01&&(e.globalAlpha=Math.min(1,o.heartEye),Me(e,f,g,b*1.35*(.5+.5*o.heartEye)),e.fill());}let w=p+o.mouthSkew*.07,k=rt+m,C=Math.min(1,Math.max(0,o.mouthRound)),L=o.mouthW,B=e.createLinearGradient(w-L-.02,k,w+L+.02,k);B.addColorStop(0,d),B.addColorStop(.5,u),B.addColorStop(1,d);let A=(T,f,g,b)=>{e.fillStyle=l,T(),e.fill(),f>.02&&!a&&(e.save(),T(),e.clip(),e.fillStyle=s,e.globalAlpha*=Math.min(1,(f-.02)*12),e.beginPath(),e.ellipse(w,g,b,f*.55,0,0,Math.PI*2),e.fill(),e.restore()),e.strokeStyle=B;let c=e.lineWidth;e.lineWidth=c*.6,T(),e.stroke(),e.lineWidth=c;};if(C<.99){e.globalAlpha=1-C;let T=18,f=v=>o.mouthCurve*.075*(1-v*v),g=(v,P)=>{let F=v/T*2-1,W=w+F*L,de=k+f(F)+o.mouthSkew*F*.03+o.mouthWave*.018*Math.sin((F+1)*Math.PI*2)+P*(1-F*F);return [W,de]},b=Math.max(0,o.mouthOpen),c=()=>{for(let v=0;v<=T;v++){let[P,F]=g(v,-b*.15);v?e.lineTo(P,F):e.moveTo(P,F);}};b>.003?A(()=>{e.beginPath(),c();for(let P=T;P>=0;P--){let[F,W]=g(P,b);e.lineTo(F,W);}e.closePath();},b,g(T/2,b)[1],L*.55):(e.strokeStyle=B,e.beginPath(),c(),e.stroke());}if(C>.01){e.globalAlpha=C;let T=Math.max(.02,o.mouthOpen*.55+.01),f=()=>{e.beginPath(),e.ellipse(w,k+.01,T*.85,T,0,0,Math.PI*2);};e.lineWidth=ge*.8,A(f,o.mouthOpen,k+.01+T*.8,T*.6),e.lineWidth=ge;}e.globalAlpha=1;}function Oe(e,o,t,i,s,n,u){let r=e.createRadialGradient(o,t,0,o,t,1);r.addColorStop(0,n),r.addColorStop(1,"rgba(0,0,0,0)"),e.save(),e.globalAlpha=u,e.translate(o,t),e.scale(i,s),e.translate(-o,-t),e.fillStyle=r,e.beginPath(),e.arc(o,t,1,0,Math.PI*2),e.fill(),e.restore();}function Me(e,o,t,i){e.beginPath(),e.moveTo(o,t+i*.85),e.bezierCurveTo(o-i*1.25,t+i*.05,o-i*.75,t-i*.95,o,t-i*.35),e.bezierCurveTo(o+i*.75,t-i*.95,o+i*1.25,t+i*.05,o,t+i*.85),e.closePath();}var Q=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:M,cos:ht,abs:lt,max:ne,exp:ct,PI:ae}=Math;function re(e,o,t,i){switch(i.y=i.rx=i.ry=i.rz=i.st=i.mouth=i.lookX=i.lookY=0,e){case "idle":{let s=M(o*1.6);i.y=.015*s,i.st=.018*s;break}case "thinking":{i.rz=.07*M(o*1.1),i.y=.02*M(o*2.2),i.ry=.15,i.rx=-0.06;break}case "loading":{let s=o*2.4;i.rx=.12*M(s),i.rz=.12*ht(s),i.ry=.22*M(s),i.y=.02*M(s*2),i.lookX=.55*M(s);break}case "listening":{let s=ne(0,M(o*3.2));i.rx=.09*s*s;let n=lt(M(o*7.1)*.5+M(o*11.3)*.3+M(o*3.7)*.2);i.st=.03*n;break}case "speaking":{let s=ne(0,M(o*9)*.6+M(o*14.3)*.4)*(.65+.35*M(o*1.7));i.mouth=.11*s,i.st=.04*s,i.rz=.03*M(o*2.1);break}case "happy":{let s=o*1.35%1;i.y=.2*M(ae*s);let n=ne(0,1-s/.14)+ne(0,(s-.9)/.1);i.st=.08*M(ae*s)-.14*n,i.rz=.05*M(o*1.35*ae);break}case "surprised":{if(t<.45){let s=M(ae*t/.45);i.y=.16*s,i.st=.12*s;}i.rz=.01*M(o*38)*ct(-t*2),i.rx=-0.06;break}case "sad":{let s=M(o*1.1);i.y=-0.06,i.rx=.2,i.st=-0.04+.015*s,i.rz=-0.04;break}case "error":{let s=t%2.8;if(s<.55){let n=1-s/.55;i.ry=.26*M(s*42)*n,i.rz=.05*M(s*42+1)*n;}i.st=-0.02;break}case "sleeping":{let s=M(o*1);i.st=.04*s,i.y=-0.03,i.rz=.1,i.rx=.1;break}case "love":{i.y=.07+.06*M(o*1.6),i.rz=.05*M(o*.8),i.st=.015*M(o*1.6+1);break}}return i}function we(e,o,t,i){for(let s of Object.keys(i))i[s]=e[s]+(o[s]-e[s])*t;return i}var Ge={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var K=new Set,x=null,$=0,V=0,he=0,Ce,De=false,j="idle",_={x:0,y:0,lastMove:-1e9,known:false},O={reducedMotion:false,lowPower:false};function dt(){if(De)return;De=true,addEventListener("pointermove",o=>{_.x=o.clientX,_.y=o.clientY,_.lastMove=performance.now(),_.known=true;},{passive:true}),document.addEventListener("visibilitychange",H);let e=matchMedia("(prefers-reduced-motion: reduce)");O.reducedMotion=e.matches,e.addEventListener?.("change",o=>O.reducedMotion=o.matches);}function Ie(e,o){return x||(x=new WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),x.setPixelRatio(1),x.setClearColor(0,0),O.lowPower=pt(x.getContext()),x.setScissorTest(true),x.domElement.addEventListener("webglcontextlost",t=>{t.preventDefault(),x?.dispose(),x=null,$=V=0,j="idle";})),(e>$||o>V)&&($=Math.max($,e),V=Math.max(V,o),x.setSize($,V,false)),x}var qe=()=>V;function pt(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(j==="ready")return true;if(j==="idle"&&x){j="compiling";let t=x;t.compileAsync(e,o).catch(()=>{}).then(()=>{t===x&&(j="ready"),H();});}return false}function _e(e){dt(),clearTimeout(Ce),K.add(e),H();}function He(e){K.delete(e),K.size===0&&(clearTimeout(Ce),Ce=setTimeout(()=>{K.size||!x||(x.dispose(),x.forceContextLoss(),x=null,$=V=0,j="idle");},1500));}function H(){if(!he){for(let e of K)if(e.active){he=requestAnimationFrame(Ne);return}}}var mt=10,Ye=new WeakMap,ke=0;function Ne(e){he=0;let o=[...K].filter(n=>n.active);if(!o.length)return;let t=performance.now(),i=o.length,s=0;for(;s<i;s++){let n=o[(ke+s)%i],u=Ye.get(n)??e-16;if(Ye.set(n,e),n.tick(Math.min(.05,Math.max(0,(e-u)/1e3)),e),performance.now()-t>mt){s++;break}}ke=(ke+s)%i,he=requestAnimationFrame(Ne);}var ee={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"),i=o.getIndex(),s=new Map,n=[],u=new Uint32Array(t.count);for(let a=0;a<t.count;a++){let h=t.getX(a),p=t.getY(a),m=t.getZ(a),E=`${Math.round(h*1e4)},${Math.round(p*1e4)},${Math.round(m*1e4)}`,S=s.get(E);if(S===void 0){S=n.length/3,s.set(E,S);let w=Math.hypot(h,p,m)||1;n.push(h/w,p/w,m/w);}u[a]=S;}let r=[];for(let a=0;a<i.count;a+=3){let h=u[i.getX(a)],p=u[i.getX(a+1)],m=u[i.getX(a+2)];h!==p&&p!==m&&h!==m&&r.push(h,p,m);}o.dispose();let l=new BufferGeometry,d=new Float32Array(n);return l.setAttribute("position",new BufferAttribute(new Float32Array(d),3)),l.setIndex(r),{geometry:l,dirs:d}}var vt=(e,o,t)=>{let i=Math.min(1,Math.max(0,(t-e)/(o-e)));return i*i*(3-2*i)};function Se(e,o,t,i,s,n,u){let r=o,l=t,d=i;switch(e){case "pebble":{r=o*1.1+.04*t,l=t*.92*(.95+.05*Math.tanh(3*t))+.03*o,d=i*.94;break}case "blob":{let a=.97+.045*Math.sin(2.1*o+1.3*s)*Math.sin(2.4*t+1.7*s+1)+.035*Math.sin(2.7*i+2.3*o+1.1*s);r=o*a,l=t*a,d=i*a;break}case "drop":{let a=Math.max(t,0),h=1-.8*Math.pow(a,1.6);r=o*h*.98+.1*a*a*a,l=(t<0?t*.9:t*1.32)-.12,d=i*h*.98;break}case "bean":{let a=t*.78+.42*Math.tanh(t*3);r=(o*.8+.14*(a*a/1.44)-.07)*.9,l=a*.88,d=i*.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(i),4.5),-0.2222222222222222);r=o*h*.84,l=t*h*.82,d=i*h*.84;break}case "ghost":{if(t>=0)r=o,l=t,d=i;else {let h=(Math.acos(Math.max(-1,t))-Math.PI/2)/(Math.PI/2),p=Math.hypot(o,i)||1,m=o/p,E=i/p,S=Math.atan2(i,o),w=.085*Math.sin(5*S+2.2*s),k=1.04,C=.13,L=-1.05+w,B;if(h<.45){let A=h/.45;B=1+.04*A,l=-A*(1.05-C)+w*vt(.45,1,A);}else if(h<.75){let A=(h-.45)/.3*(Math.PI/2);B=k-C+C*Math.cos(A),l=L+C-C*Math.sin(A);}else {let A=(h-.75)/.25;B=(k-C)*(1-A),l=L+.22*A*A*(3-2*A);}r=m*B,d=E*B;}r*=.9,l=(l+.1)*.9,d*=.9;break}}n[u]=r,n[u+1]=l,n[u+2]=d;}var I=null,le=null;function yt(){if(typeof window>"u"||!("AudioContext"in window))return null;if(Mt(),!I){I=new AudioContext;let e=I.createBiquadFilter();e.type="lowpass",e.frequency.value=3200,le=I.createGain(),le.gain.value=.55,le.connect(e).connect(I.destination);}return I.state==="suspended"&&I.resume(),I}function z(e,{freq:o,to:t,at:i=0,dur:s=.12,type:n="sine",gain:u=.1,wobble:r=0}){let l=e.currentTime+i,d=e.createOscillator(),a=e.createGain();if(d.type=n,d.frequency.setValueAtTime(o,l),t&&d.frequency.exponentialRampToValueAtTime(t,l+s),r){let h=e.createOscillator(),p=e.createGain();h.frequency.value=7,p.gain.value=r,h.connect(p).connect(d.frequency),h.start(l),h.stop(l+s+.05);}a.gain.setValueAtTime(1e-4,l),a.gain.exponentialRampToValueAtTime(u,l+Math.min(.012,s/4)),a.gain.exponentialRampToValueAtTime(1e-4,l+s),d.connect(a).connect(le),d.start(l),d.stop(l+s+.02);}var Te={idle:e=>z(e,{freq:520,to:780,dur:.09,gain:.08}),thinking:e=>[660,760,900].forEach((o,t)=>z(e,{freq:o,to:o*1.08,at:t*.1,dur:.07,type:"triangle",gain:.06})),loading:e=>[0,.14,.28].forEach(o=>z(e,{freq:940,at:o,dur:.04,gain:.05})),listening:e=>{z(e,{freq:523,dur:.12,type:"triangle",gain:.07}),z(e,{freq:784,at:.1,dur:.22,type:"triangle",gain:.07});},speaking:e=>[380,460,340,520,420].forEach((o,t)=>z(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)=>z(e,{freq:o,at:t*.07,dur:.14,type:"triangle",gain:.07})),surprised:e=>z(e,{freq:280,to:980,dur:.18,gain:.09,wobble:30}),sad:e=>{z(e,{freq:560,to:300,dur:.5,gain:.07}),z(e,{freq:420,to:240,at:.12,dur:.5,gain:.04});},error:e=>[0,.14].forEach(o=>z(e,{freq:170,to:150,at:o,dur:.1,type:"sawtooth",gain:.045})),sleeping:e=>{z(e,{freq:150,to:115,dur:.6,gain:.07,wobble:6}),z(e,{freq:300,to:230,at:.05,dur:.5,gain:.02});},love:e=>[1319,1568,2093,1760].forEach((o,t)=>z(e,{freq:o,at:t*.08,dur:.22,gain:.04}))},q=null,Xe=false;function Mt(){if(Xe)return;Xe=true;let e=()=>{I&&I.resume().then(()=>{q&&performance.now()<q.until&&Te[q.mood](I),q=null;});};for(let o of ["pointerdown","touchend","keydown"])addEventListener(o,e,{passive:true});}function ce(e,o=false){let t=yt();if(t){if(t.state==="running"){q=null,Te[e](t);return}q={mood:e,until:performance.now()+(o?6e3:1200)},t.resume().then(()=>{q&&performance.now()<q.until&&Te[q.mood](t),q=null;});}}var Ee=-0.85,Je=new Map;function Et(e){let o=Je.get(e);if(!o){let{geometry:t,dirs:i}=Ve(e);o={dirs:i,index:new Uint32Array(t.getIndex().array)},t.dispose(),Je.set(e,o);}return o}var U=(e,o)=>1-Math.exp(-e*o),Pe=e=>1-Math.pow(1-e,3),ue=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.heightCanvas=document.createElement("canvas");this.uniforms={uFace:{value:null},uHeight:{value:null},uFaceY:{value:0},uFaceScale:{value:1.5}};this.state={...y};this.visible=false;this.connected=false;this.w=0;this.h=0;this.t=Math.random()*100;this.shapeFrom=y.shape;this.shapeTo=y.shape;this.morph=1;this.shapeDirty=true;this.shown=y.mood;this.prevShown=y.mood;this.tau=10;this.prevTau=10;this.blend=1;this.reaction=null;this.pose=Q();this.poseIdle=Q();this.moodTime=0;this.poseA=Q();this.poseB=Q();this.face=ve(se.idle);this.target=ve(se.idle);this.drawn=new Float32Array(Z.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=X(y.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=oe(this.body);this.faceCT=this.faceC;this.cheek=ie(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.heightCtx=this.heightCanvas.getContext("2d"),this.heightCanvas.width=this.heightCanvas.height=256,this.heightTex=new CanvasTexture(this.heightCanvas),this.heightTex.minFilter=LinearMipmapLinearFilter,this.uniforms.uHeight.value=this.heightTex,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 i=new DirectionalLight(16773350,.45);i.position.set(-2,-1,3),this.scene.add(o,t,i),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 Qe=["ghost","pebble","blob","drop","bean","cube"],et=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],y={shape:"ghost",mood:"idle",color:"#2b15d1",size:160,speed:1,followCursor:true,interactive:true,shadow:true,sound:false,settle:0},Ot={Iris:"#2b15d1",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},Lt={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."}},Gt={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},pe=e=>Qe.includes(e),J=e=>et.includes(e);var G=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 Be(t[1]);if(G||(G=document.createElement("canvas").getContext("2d")),!G)return o;G.fillStyle="#010203",G.fillStyle=e;let i=String(G.fillStyle);if(i==="#010203"&&e.trim()!=="#010203")return o;if(i.startsWith("#"))return Be(i.slice(1));G.canvas.width=G.canvas.height=1,G.clearRect(0,0,1,1),G.fillRect(0,0,1,1);let s=G.getImageData(0,0,1,1).data;return [s[0]/255,s[1]/255,s[2]/255]}function Be(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 D=e=>`rgb(${Math.round(e[0]*255)},${Math.round(e[1]*255)},${Math.round(e[2]*255)})`,R=(e,o,t)=>[e[0]+(o[0]-e[0])*t,e[1]+(o[1]-e[1])*t,e[2]+(o[2]-e[2])*t],me=e=>e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4),fe=e=>.2126*me(e[0])+.7152*me(e[1])+.0722*me(e[2]),tt=[.07,.06,.08],ot=[209/255,190/255,173/255],it=[1,.42,.48],oe=e=>fe(e)>.042?R(tt,e,.06):ot,ie=e=>R(e,it,.78);var Z=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],Y={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},se={idle:{...Y},thinking:{...Y,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...Y,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...Y,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...Y,mouthW:.09,mouthCurve:.25},happy:{...Y,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...Y,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...Y,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...Y,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...Y,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...Y,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Le={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},ve=e=>({...e}),st=.31,nt=-0.04,at=.098,be=.128,rt=.2,ge=.05;function ye(e,o,t,i,s,n,u,r,l,d,a=false){let h=e.canvas.width;e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,h,h),e.setTransform(h/2,0,0,h/2,h/2,h/2),e.lineCap="round",e.lineJoin="round";let p=o.lookX*.075,m=o.lookY*.06;if(o.blush>.01&&!a){let T=h*.018;e.save(),"filter"in e&&(e.filter=`blur(${T}px)`);for(let g of [-1,1]){let b=g*.52+p,c=.16+m,v=e.createRadialGradient(b,c,0,b,c,.16);v.addColorStop(0,s),v.addColorStop(1,"rgba(0,0,0,0)"),e.globalAlpha=Math.min(1,o.blush)*.62,e.fillStyle=v,e.beginPath(),e.ellipse(b,c,.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=s,e.lineWidth=.016,e.lineCap="round";for(let g of [-1,1]){let b=g*.52+p,c=.16+m;for(let v=-1;v<=1;v++){let P=b+v*.045;e.beginPath(),e.moveTo(P+.018,c-.035),e.lineTo(P-.018,c+.035),e.stroke();}}e.restore();}}e.fillStyle=i,e.strokeStyle=i,e.lineWidth=ge;let E=Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye),S=1-E;for(let T of [-1,1]){let f=T*st+p,g=nt+m,b=at*o.eyeSize;if(S>.01){e.globalAlpha=S;let c=Math.max(.06,o.eyeOpen*t),v=be*o.eyeSize*c;if(e.save(),o.lidCover>.01||o.lidTilt>.01){let W=g-be*o.eyeSize+2*be*o.eyeSize*o.lidCover;e.translate(f,W),e.rotate(T*o.lidTilt*.6),e.beginPath(),e.rect(-1,0,2,2),e.clip(),e.rotate(-T*o.lidTilt*.6),e.translate(-f,-W);}n>.5&&!a&&Oe(e,f,g,b*2.1,v*1.9,i,.35*S);let P=e.createRadialGradient(f,g-v*.2,0,f,g,Math.max(b,v));if(P.addColorStop(0,u),P.addColorStop(1,i),e.fillStyle=P,e.beginPath(),e.ellipse(f,g,b,v,0,0,Math.PI*2),e.fill(),e.fillStyle=i,c>.35&&!a){let F=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),de=r==="#fff"?.3:.34;e.globalAlpha=S*Math.min(1,(c-.35)*3),e.fillStyle=r,e.beginPath(),e.arc(f-b*.1+F*b*.5,g-v*.16+W*v*.42,b*de,0,Math.PI*2),e.fill(),e.fillStyle=i;}e.restore();}if(n>.5&&!a&&E>.01&&Oe(e,f,g,b*2,b*1.8,i,.3*E),e.fillStyle=i,o.happyEye>.01){let c=.6+.4*o.happyEye;e.globalAlpha=Math.min(1,o.happyEye),e.beginPath(),e.arc(f,g+.045,b*1.15*c,Math.PI*1.12,Math.PI*1.88),e.stroke();}if(o.closedEye>.01){let c=.6+.4*o.closedEye;e.globalAlpha=Math.min(1,o.closedEye),e.beginPath(),e.moveTo(f-b*1.15*c,g+.01),e.quadraticCurveTo(f,g+.07*c,f+b*1.15*c,g+.01),e.stroke();}if(o.xEye>.01){let c=b*.95*(.6+.4*o.xEye);e.globalAlpha=Math.min(1,o.xEye),e.beginPath(),e.moveTo(f-c,g-c),e.lineTo(f+c,g+c),e.moveTo(f+c,g-c),e.lineTo(f-c,g+c),e.stroke();}o.heartEye>.01&&(e.globalAlpha=Math.min(1,o.heartEye),Me(e,f,g,b*1.35*(.5+.5*o.heartEye)),e.fill());}let w=p+o.mouthSkew*.07,k=rt+m,C=Math.min(1,Math.max(0,o.mouthRound)),L=o.mouthW,B=e.createLinearGradient(w-L-.02,k,w+L+.02,k);B.addColorStop(0,d),B.addColorStop(.5,u),B.addColorStop(1,d);let A=(T,f,g,b)=>{e.fillStyle=l,T(),e.fill(),f>.02&&!a&&(e.save(),T(),e.clip(),e.fillStyle=s,e.globalAlpha*=Math.min(1,(f-.02)*12),e.beginPath(),e.ellipse(w,g,b,f*.55,0,0,Math.PI*2),e.fill(),e.restore()),e.strokeStyle=B;let c=e.lineWidth;e.lineWidth=c*.6,T(),e.stroke(),e.lineWidth=c;};if(C<.99){e.globalAlpha=1-C;let T=18,f=v=>o.mouthCurve*.075*(1-v*v),g=(v,P)=>{let F=v/T*2-1,W=w+F*L,de=k+f(F)+o.mouthSkew*F*.03+o.mouthWave*.018*Math.sin((F+1)*Math.PI*2)+P*(1-F*F);return [W,de]},b=Math.max(0,o.mouthOpen),c=()=>{for(let v=0;v<=T;v++){let[P,F]=g(v,-b*.15);v?e.lineTo(P,F):e.moveTo(P,F);}};b>.003?A(()=>{e.beginPath(),c();for(let P=T;P>=0;P--){let[F,W]=g(P,b);e.lineTo(F,W);}e.closePath();},b,g(T/2,b)[1],L*.55):(e.strokeStyle=B,e.beginPath(),c(),e.stroke());}if(C>.01){e.globalAlpha=C;let T=Math.max(.02,o.mouthOpen*.55+.01),f=()=>{e.beginPath(),e.ellipse(w,k+.01,T*.85,T,0,0,Math.PI*2);};e.lineWidth=ge*.8,A(f,o.mouthOpen,k+.01+T*.8,T*.6),e.lineWidth=ge;}e.globalAlpha=1;}function Oe(e,o,t,i,s,n,u){let r=e.createRadialGradient(o,t,0,o,t,1);r.addColorStop(0,n),r.addColorStop(1,"rgba(0,0,0,0)"),e.save(),e.globalAlpha=u,e.translate(o,t),e.scale(i,s),e.translate(-o,-t),e.fillStyle=r,e.beginPath(),e.arc(o,t,1,0,Math.PI*2),e.fill(),e.restore();}function Me(e,o,t,i){e.beginPath(),e.moveTo(o,t+i*.85),e.bezierCurveTo(o-i*1.25,t+i*.05,o-i*.75,t-i*.95,o,t-i*.35),e.bezierCurveTo(o+i*.75,t-i*.95,o+i*1.25,t+i*.05,o,t+i*.85),e.closePath();}var Q=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:M,cos:ht,abs:lt,max:ne,exp:ct,PI:ae}=Math;function re(e,o,t,i){switch(i.y=i.rx=i.ry=i.rz=i.st=i.mouth=i.lookX=i.lookY=0,e){case "idle":{let s=M(o*1.6);i.y=.015*s,i.st=.018*s;break}case "thinking":{i.rz=.07*M(o*1.1),i.y=.02*M(o*2.2),i.ry=.15,i.rx=-0.06;break}case "loading":{let s=o*2.4;i.rx=.12*M(s),i.rz=.12*ht(s),i.ry=.22*M(s),i.y=.02*M(s*2),i.lookX=.55*M(s);break}case "listening":{let s=ne(0,M(o*3.2));i.rx=.09*s*s;let n=lt(M(o*7.1)*.5+M(o*11.3)*.3+M(o*3.7)*.2);i.st=.03*n;break}case "speaking":{let s=ne(0,M(o*9)*.6+M(o*14.3)*.4)*(.65+.35*M(o*1.7));i.mouth=.11*s,i.st=.04*s,i.rz=.03*M(o*2.1);break}case "happy":{let s=o*1.35%1;i.y=.2*M(ae*s);let n=ne(0,1-s/.14)+ne(0,(s-.9)/.1);i.st=.08*M(ae*s)-.14*n,i.rz=.05*M(o*1.35*ae);break}case "surprised":{if(t<.45){let s=M(ae*t/.45);i.y=.16*s,i.st=.12*s;}i.rz=.01*M(o*38)*ct(-t*2),i.rx=-0.06;break}case "sad":{let s=M(o*1.1);i.y=-0.06,i.rx=.2,i.st=-0.04+.015*s,i.rz=-0.04;break}case "error":{let s=t%2.8;if(s<.55){let n=1-s/.55;i.ry=.26*M(s*42)*n,i.rz=.05*M(s*42+1)*n;}i.st=-0.02;break}case "sleeping":{let s=M(o*1);i.st=.04*s,i.y=-0.03,i.rz=.1,i.rx=.1;break}case "love":{i.y=.07+.06*M(o*1.6),i.rz=.05*M(o*.8),i.st=.015*M(o*1.6+1);break}}return i}function we(e,o,t,i){for(let s of Object.keys(i))i[s]=e[s]+(o[s]-e[s])*t;return i}var Ge={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var K=new Set,x=null,$=0,V=0,he=0,Ce,De=false,j="idle",_={x:0,y:0,lastMove:-1e9,known:false},O={reducedMotion:false,lowPower:false};function dt(){if(De)return;De=true,addEventListener("pointermove",o=>{_.x=o.clientX,_.y=o.clientY,_.lastMove=performance.now(),_.known=true;},{passive:true}),document.addEventListener("visibilitychange",H);let e=matchMedia("(prefers-reduced-motion: reduce)");O.reducedMotion=e.matches,e.addEventListener?.("change",o=>O.reducedMotion=o.matches);}function Ie(e,o){return x||(x=new WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),x.setPixelRatio(1),x.setClearColor(0,0),O.lowPower=pt(x.getContext()),x.setScissorTest(true),x.domElement.addEventListener("webglcontextlost",t=>{t.preventDefault(),x?.dispose(),x=null,$=V=0,j="idle";})),(e>$||o>V)&&($=Math.max($,e),V=Math.max(V,o),x.setSize($,V,false)),x}var qe=()=>V;function pt(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(j==="ready")return true;if(j==="idle"&&x){j="compiling";let t=x;t.compileAsync(e,o).catch(()=>{}).then(()=>{t===x&&(j="ready"),H();});}return false}function _e(e){dt(),clearTimeout(Ce),K.add(e),H();}function He(e){K.delete(e),K.size===0&&(clearTimeout(Ce),Ce=setTimeout(()=>{K.size||!x||(x.dispose(),x.forceContextLoss(),x=null,$=V=0,j="idle");},1500));}function H(){if(!he){for(let e of K)if(e.active){he=requestAnimationFrame(Ne);return}}}var mt=10,Ye=new WeakMap,ke=0;function Ne(e){he=0;let o=[...K].filter(n=>n.active);if(!o.length)return;let t=performance.now(),i=o.length,s=0;for(;s<i;s++){let n=o[(ke+s)%i],u=Ye.get(n)??e-16;if(Ye.set(n,e),n.tick(Math.min(.05,Math.max(0,(e-u)/1e3)),e),performance.now()-t>mt){s++;break}}ke=(ke+s)%i,he=requestAnimationFrame(Ne);}var ee={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"),i=o.getIndex(),s=new Map,n=[],u=new Uint32Array(t.count);for(let a=0;a<t.count;a++){let h=t.getX(a),p=t.getY(a),m=t.getZ(a),E=`${Math.round(h*1e4)},${Math.round(p*1e4)},${Math.round(m*1e4)}`,S=s.get(E);if(S===void 0){S=n.length/3,s.set(E,S);let w=Math.hypot(h,p,m)||1;n.push(h/w,p/w,m/w);}u[a]=S;}let r=[];for(let a=0;a<i.count;a+=3){let h=u[i.getX(a)],p=u[i.getX(a+1)],m=u[i.getX(a+2)];h!==p&&p!==m&&h!==m&&r.push(h,p,m);}o.dispose();let l=new BufferGeometry,d=new Float32Array(n);return l.setAttribute("position",new BufferAttribute(new Float32Array(d),3)),l.setIndex(r),{geometry:l,dirs:d}}var vt=(e,o,t)=>{let i=Math.min(1,Math.max(0,(t-e)/(o-e)));return i*i*(3-2*i)};function Se(e,o,t,i,s,n,u){let r=o,l=t,d=i;switch(e){case "pebble":{r=o*1.1+.04*t,l=t*.92*(.95+.05*Math.tanh(3*t))+.03*o,d=i*.94;break}case "blob":{let a=.97+.045*Math.sin(2.1*o+1.3*s)*Math.sin(2.4*t+1.7*s+1)+.035*Math.sin(2.7*i+2.3*o+1.1*s);r=o*a,l=t*a,d=i*a;break}case "drop":{let a=Math.max(t,0),h=1-.8*Math.pow(a,1.6);r=o*h*.98+.1*a*a*a,l=(t<0?t*.9:t*1.32)-.12,d=i*h*.98;break}case "bean":{let a=t*.78+.42*Math.tanh(t*3);r=(o*.8+.14*(a*a/1.44)-.07)*.9,l=a*.88,d=i*.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(i),4.5),-0.2222222222222222);r=o*h*.84,l=t*h*.82,d=i*h*.84;break}case "ghost":{if(t>=0)r=o,l=t,d=i;else {let h=(Math.acos(Math.max(-1,t))-Math.PI/2)/(Math.PI/2),p=Math.hypot(o,i)||1,m=o/p,E=i/p,S=Math.atan2(i,o),w=.085*Math.sin(5*S+2.2*s),k=1.04,C=.13,L=-1.05+w,B;if(h<.45){let A=h/.45;B=1+.04*A,l=-A*(1.05-C)+w*vt(.45,1,A);}else if(h<.75){let A=(h-.45)/.3*(Math.PI/2);B=k-C+C*Math.cos(A),l=L+C-C*Math.sin(A);}else {let A=(h-.75)/.25;B=(k-C)*(1-A),l=L+.22*A*A*(3-2*A);}r=m*B,d=E*B;}r*=.9,l=(l+.1)*.9,d*=.9;break}}n[u]=r,n[u+1]=l,n[u+2]=d;}var I=null,le=null;function yt(){if(typeof window>"u"||!("AudioContext"in window))return null;if(Mt(),!I){I=new AudioContext;let e=I.createBiquadFilter();e.type="lowpass",e.frequency.value=3200,le=I.createGain(),le.gain.value=.55,le.connect(e).connect(I.destination);}return I.state==="suspended"&&I.resume(),I}function z(e,{freq:o,to:t,at:i=0,dur:s=.12,type:n="sine",gain:u=.1,wobble:r=0}){let l=e.currentTime+i,d=e.createOscillator(),a=e.createGain();if(d.type=n,d.frequency.setValueAtTime(o,l),t&&d.frequency.exponentialRampToValueAtTime(t,l+s),r){let h=e.createOscillator(),p=e.createGain();h.frequency.value=7,p.gain.value=r,h.connect(p).connect(d.frequency),h.start(l),h.stop(l+s+.05);}a.gain.setValueAtTime(1e-4,l),a.gain.exponentialRampToValueAtTime(u,l+Math.min(.012,s/4)),a.gain.exponentialRampToValueAtTime(1e-4,l+s),d.connect(a).connect(le),d.start(l),d.stop(l+s+.02);}var Te={idle:e=>z(e,{freq:520,to:780,dur:.09,gain:.08}),thinking:e=>[660,760,900].forEach((o,t)=>z(e,{freq:o,to:o*1.08,at:t*.1,dur:.07,type:"triangle",gain:.06})),loading:e=>[0,.14,.28].forEach(o=>z(e,{freq:940,at:o,dur:.04,gain:.05})),listening:e=>{z(e,{freq:523,dur:.12,type:"triangle",gain:.07}),z(e,{freq:784,at:.1,dur:.22,type:"triangle",gain:.07});},speaking:e=>[380,460,340,520,420].forEach((o,t)=>z(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)=>z(e,{freq:o,at:t*.07,dur:.14,type:"triangle",gain:.07})),surprised:e=>z(e,{freq:280,to:980,dur:.18,gain:.09,wobble:30}),sad:e=>{z(e,{freq:560,to:300,dur:.5,gain:.07}),z(e,{freq:420,to:240,at:.12,dur:.5,gain:.04});},error:e=>[0,.14].forEach(o=>z(e,{freq:170,to:150,at:o,dur:.1,type:"sawtooth",gain:.045})),sleeping:e=>{z(e,{freq:150,to:115,dur:.6,gain:.07,wobble:6}),z(e,{freq:300,to:230,at:.05,dur:.5,gain:.02});},love:e=>[1319,1568,2093,1760].forEach((o,t)=>z(e,{freq:o,at:t*.08,dur:.22,gain:.04}))},q=null,Xe=false;function Mt(){if(Xe)return;Xe=true;let e=()=>{I&&I.resume().then(()=>{q&&performance.now()<q.until&&Te[q.mood](I),q=null;});};for(let o of ["pointerdown","touchend","keydown"])addEventListener(o,e,{passive:true});}function ce(e,o=false){let t=yt();if(t){if(t.state==="running"){q=null,Te[e](t);return}q={mood:e,until:performance.now()+(o?6e3:1200)},t.resume().then(()=>{q&&performance.now()<q.until&&Te[q.mood](t),q=null;});}}var Ee=-0.85,Je=new Map;function Et(e){let o=Je.get(e);if(!o){let{geometry:t,dirs:i}=Ve(e);o={dirs:i,index:new Uint32Array(t.getIndex().array)},t.dispose(),Je.set(e,o);}return o}var U=(e,o)=>1-Math.exp(-e*o),Pe=e=>1-Math.pow(1-e,3),ue=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.heightCanvas=document.createElement("canvas");this.uniforms={uFace:{value:null},uHeight:{value:null},uFaceY:{value:0},uFaceScale:{value:1.5}};this.state={...y};this.visible=false;this.connected=false;this.w=0;this.h=0;this.t=Math.random()*100;this.shapeFrom=y.shape;this.shapeTo=y.shape;this.morph=1;this.shapeDirty=true;this.shown=y.mood;this.prevShown=y.mood;this.tau=10;this.prevTau=10;this.blend=1;this.reaction=null;this.pose=Q();this.poseIdle=Q();this.moodTime=0;this.poseA=Q();this.poseB=Q();this.face=ve(se.idle);this.target=ve(se.idle);this.drawn=new Float32Array(Z.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=X(y.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=oe(this.body);this.faceCT=this.faceC;this.cheek=ie(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.heightCtx=this.heightCanvas.getContext("2d"),this.heightCanvas.width=this.heightCanvas.height=256,this.heightTex=new CanvasTexture(this.heightCanvas),this.heightTex.minFilter=LinearMipmapLinearFilter,this.uniforms.uHeight.value=this.heightTex,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 i=new DirectionalLight(16773350,.45);i.position.set(-2,-1,3),this.scene.add(o,t,i),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;
|
|
4
4
|
varying vec3 vTanX;
|
package/dist/react.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";var F=Object.create;var c=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty;var _=(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 j(o))!W.call(e,d)&&d!==t&&c(e,d,{get:()=>o[d],enumerable:!(i=N(o,d))||i.enumerable});return e};var $=(e,o,t)=>(t=e!=null?F(q(e)):{},M(o||!e||!e.__esModule?c(t,"default",{value:e,enumerable:!0}):t,e)),z=e=>M(c({},"__esModule",{value:!0}),e);var Z={};_(Z,{DEFAULTS:()=>G,MOODS:()=>v,MOOD_INFO:()=>J,Moodi:()=>Y,PALETTES:()=>U,SHAPES:()=>S,SHAPE_LABELS:()=>K,isMood:()=>V,isShape:()=>Q,loadMoodi:()=>l});module.exports=z(Z);var n=require("react");var S=["ghost","pebble","blob","drop","bean","cube"],v=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],G={shape:"ghost",mood:"idle",color:"#
|
|
2
|
+
"use strict";var F=Object.create;var c=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty;var _=(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 j(o))!W.call(e,d)&&d!==t&&c(e,d,{get:()=>o[d],enumerable:!(i=N(o,d))||i.enumerable});return e};var $=(e,o,t)=>(t=e!=null?F(q(e)):{},M(o||!e||!e.__esModule?c(t,"default",{value:e,enumerable:!0}):t,e)),z=e=>M(c({},"__esModule",{value:!0}),e);var Z={};_(Z,{DEFAULTS:()=>G,MOODS:()=>v,MOOD_INFO:()=>J,Moodi:()=>Y,PALETTES:()=>U,SHAPES:()=>S,SHAPE_LABELS:()=>K,isMood:()=>V,isShape:()=>Q,loadMoodi:()=>l});module.exports=z(Z);var n=require("react");var S=["ghost","pebble","blob","drop","bean","cube"],v=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],G={shape:"ghost",mood:"idle",color:"#2b15d1",size:160,speed:1,followCursor:!0,interactive:!0,shadow:!0,sound:!1,settle:0},U={Iris:"#2b15d1",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},J={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."}},K={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},Q=e=>S.includes(e),V=e=>v.includes(e);var w=null,k=!1,l=()=>w??(w=import("moodi").then(e=>e.defineMoodi()));function X(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,Y=(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,settleMood:D,onMoodChange:g,onReady:s,poster:f,loading:m="idle",className:A,style:H,...T},I){let p=(0,n.useRef)(null);(0,n.useImperativeHandle)(I,()=>p.current,[]),(0,n.useEffect)(()=>{X(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=O=>g(O.detail);return r.addEventListener("moodchange",y),()=>r.removeEventListener("moodchange",y)},[g]);let R=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:A,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),"settle-mood":D,style:{display:"inline-block",width:E(a),height:E(a),verticalAlign:"middle",...H},suppressHydrationWarning:!0,...T},R)});0&&(module.exports={DEFAULTS,MOODS,MOOD_INFO,Moodi,PALETTES,SHAPES,SHAPE_LABELS,isMood,isShape,loadMoodi});
|
package/dist/react.d.cts
CHANGED
|
@@ -8,7 +8,7 @@ interface MoodiProps {
|
|
|
8
8
|
shape?: Shape;
|
|
9
9
|
/** Current mood / state. Default `'idle'`. */
|
|
10
10
|
mood?: Mood;
|
|
11
|
-
/** Body color, any CSS color. Default `'#
|
|
11
|
+
/** Body color, any CSS color. Default `'#2b15d1'`. */
|
|
12
12
|
color?: string;
|
|
13
13
|
/** Eyes / mouth color. Auto-contrast by default. */
|
|
14
14
|
faceColor?: string;
|
package/dist/react.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ interface MoodiProps {
|
|
|
8
8
|
shape?: Shape;
|
|
9
9
|
/** Current mood / state. Default `'idle'`. */
|
|
10
10
|
mood?: Mood;
|
|
11
|
-
/** Body color, any CSS color. Default `'#
|
|
11
|
+
/** Body color, any CSS color. Default `'#2b15d1'`. */
|
|
12
12
|
color?: string;
|
|
13
13
|
/** Eyes / mouth color. Auto-contrast by default. */
|
|
14
14
|
faceColor?: string;
|
package/dist/react.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{createElement as m,forwardRef as O,useEffect as c,useImperativeHandle as F,useRef as N}from"react";var I=["ghost","pebble","blob","drop","bean","cube"],R=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],q={shape:"ghost",mood:"idle",color:"#
|
|
2
|
+
import{createElement as m,forwardRef as O,useEffect as c,useImperativeHandle as F,useRef as N}from"react";var I=["ghost","pebble","blob","drop","bean","cube"],R=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],q={shape:"ghost",mood:"idle",color:"#2b15d1",size:160,speed:1,followCursor:!0,interactive:!0,shadow:!0,sound:!1,settle:0},W={Iris:"#2b15d1",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},_={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"},z=e=>I.includes(e),G=e=>R.includes(e);var S=null,y=!1,l=()=>S??(S=import("moodi").then(e=>e.defineMoodi()));function j(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,K=O(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,settleMood:C,onMoodChange:p,onReady:n,poster:b,loading:h="idle",className:P,style:B,...D},A){let s=N(null);F(A,()=>s.current,[]),c(()=>{j(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=T=>p(T.detail);return o.addEventListener("moodchange",f),()=>o.removeEventListener("moodchange",f)},[p]);let H=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:P,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),"settle-mood":C,style:{display:"inline-block",width:M(d),height:M(d),verticalAlign:"middle",...B},suppressHydrationWarning:!0,...D},H)});export{q as DEFAULTS,R as MOODS,_ as MOOD_INFO,K as Moodi,W as PALETTES,I as SHAPES,$ as SHAPE_LABELS,G as isMood,z as isShape,l as loadMoodi};
|