moodie
Version:
Randomly generated avatars with a mood
7 lines (4 loc) • 4.21 kB
JavaScript
import g, { memo, useMemo } from 'react';
import { jsxs, jsx } from 'react/jsx-runtime';
var S=e=>{let t=0;for(let a of e)t=(t<<5)-t+a.charCodeAt(0),t&=t;return Math.abs(t)},p=e=>{let t=e.slice(1),[a,s,n]=[0,2,4].map(y=>parseInt(t.slice(y,y+2),16));return (a*299+s*587+n*114)/1e3>=128?"#000000":"#FFFFFF"},h=class e{constructor(t){this._seed=t;(this._seed<=0||this._seed===Number.MAX_VALUE)&&(this._seed=1);}nextDouble(){let t=Math.floor(this._seed/e.q),a=this._seed%e.q;return this._seed=e.a*a-e.r*t,this._seed<=0&&(this._seed+=e.m),Number(this._seed)/e.m}nextInt(t,a){let s=Math.round(a)-Math.round(t);return t+Math.round(s*this.nextDouble())}nextNumber(t,a){let s=a-t;return t+s*this.nextDouble()}nextBoolean(){return this.nextDouble()>=.5}nextChoice(t){if(t.length===0)return;let a=this.nextInt(0,t.length-1);return t[a]}nextUnit=(t,a)=>this.nextNumber(a?-t:0,t);static a=16807;static m=2147483647;static q=127773;static r=2836};var o=36,k=["#92A1C6","#146A7C","#F0AB3D","#C271B4","#C20D90"],c=(e,t)=>({rightEye:e,leftEye:t??e}),f={normal:c(e=>jsx("rect",{x:e.x+e.eyeSpread,y:e.y,width:e.eyeSize,height:2,rx:1,fill:e.eyeColor})),happy:c(e=>jsx("path",{d:`M${e.x+e.eyeSpread-e.eyeSize},${e.y+e.eyeSize} Q${e.x+e.eyeSpread},${e.y} ${e.x+e.eyeSpread+e.eyeSize},${e.y+e.eyeSize}`,fill:"none",stroke:e.eyeColor,strokeWidth:1,strokeLinecap:"round"})),sleepy:c(e=>jsx("path",{d:`M${e.x+e.eyeSpread-e.eyeSize},${e.y} Q${e.x+e.eyeSpread},${e.y+e.eyeSize} ${e.x+e.eyeSpread+e.eyeSize},${e.y}`,fill:"none",stroke:e.eyeColor,strokeWidth:1,strokeLinecap:"round"})),mischief:c(e=>jsx("path",{d:`M${e.x+e.eyeSpread},${e.y} l${e.eyeSize},${e.eyeSize} l-${e.eyeSize},${e.eyeSize}`,fill:"none",stroke:e.eyeColor,strokeWidth:1,strokeLinecap:"round"}),e=>jsx("path",{d:`M${e.x+e.eyeSpread},${e.y} l-${e.eyeSize},${e.eyeSize} l${e.eyeSize},${e.eyeSize}`,fill:"none",stroke:e.eyeColor,strokeWidth:1,strokeLinecap:"round"}))},x={smile:e=>jsx("path",{d:`M13,${19+e.mouthSpread} a1,0.75 0 0,0 10,0`,fill:e.mouthColor}),open:e=>jsx("path",{d:`M15 ${19+e.mouthSpread}c2 1 4 1 6 0`,stroke:e.mouthColor,fill:"none",strokeLinecap:"round"}),surprise:e=>jsx("circle",{cx:20,cy:19+e.mouthSpread,r:e.mouthSize,fill:e.mouthColor}),unhappy:e=>jsx("path",{d:`M15 ${19+e.mouthSpread}c2 -1 4 -1 6 0`,stroke:e.mouthColor,fill:"none",strokeLinecap:"round"})};function z(e,t=k,a={}){let s=S(e??crypto.randomUUID()),n=new h(s),l=n.nextChoice(t),y=n.nextUnit(10,!0),r=y<5?y+o/9:y,u=n.nextUnit(10,!0),m=u<5?u+o/9:u,b=a.eye??n.nextChoice(Object.keys(f)),C=a.mouth??n.nextChoice(Object.keys(x));return {wrapperColor:l,faceColor:p(l),backgroundColor:n.nextChoice(t),wrapperTranslateX:r,wrapperTranslateY:m,wrapperRotate:n.nextUnit(360,!1),wrapperScale:1+n.nextUnit(o/12,!1)/10,isCircle:n.nextBoolean(),eyeSpread:n.nextUnit(5,!1),eyeSize:1.5+n.nextUnit(1,!1),mouthSpread:n.nextUnit(5,!1),mouthSize:1.5+n.nextUnit(1,!0),faceRotate:n.nextUnit(10,!0),faceTranslateX:r>o/6?r/2:n.nextUnit(8,!0),faceTranslateY:m>o/6?m/2:n.nextUnit(7,!0),eyeType:b,mouthType:C}}var w=memo(({name:e,colors:t,size:a,title:s,square:n,expression:l,...y})=>{let r=useMemo(()=>z(e,t,l),[e,t,l]),u=g.useId();return jsxs("svg",{viewBox:`0 0 ${o} ${o}`,fill:"none",role:"img",xmlns:"http://www.w3.org/2000/svg",width:a,height:a,...y,children:[s&&jsx("title",{children:s}),jsx("mask",{id:u,maskUnits:"userSpaceOnUse",x:0,y:0,width:o,height:o,children:jsx("rect",{width:o,height:o,rx:n?void 0:o*2,fill:"#FFFFFF"})}),jsxs("g",{mask:`url(#${u})`,children:[jsx("rect",{width:o,height:o,fill:r.backgroundColor}),jsx("rect",{x:"0",y:"0",width:o,height:o,transform:`translate(${r.wrapperTranslateX} ${r.wrapperTranslateY}) rotate(${r.wrapperRotate} ${o/2} ${o/2}) scale(${r.wrapperScale})`,fill:r.wrapperColor,rx:r.isCircle?o:o/6}),jsxs("g",{transform:`translate(${r.faceTranslateX} ${r.faceTranslateY}) rotate(${r.faceRotate} ${o/2} ${o/2})`,children:[f[r.eyeType].leftEye({eyeSize:r.eyeSize,eyeSpread:r.eyeSpread,eyeColor:r.faceColor,x:20,y:14}),f[r.eyeType].rightEye({eyeSize:r.eyeSize,eyeSpread:-r.eyeSpread,eyeColor:r.faceColor,x:14,y:14}),x[r.mouthType]({mouthSpread:r.mouthSpread,mouthSize:r.mouthSize,mouthColor:r.faceColor})]})]})]})});w.displayName="Moodie";
export { w as Moodie };