UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

2 lines (1 loc) 2.29 kB
(()=>{var E=Object.defineProperty;var F=(t,e)=>{for(var o in e)E(t,o,{get:e[o],enumerable:!0})};var h={};F(h,{DOWN:()=>X,LEFT:()=>Y,ONE:()=>J,RIGHT:()=>Q,UP:()=>K,Vector:()=>s,ZERO:()=>C,vec:()=>r,vecAbs:()=>U,vecAdd:()=>y,vecAngle:()=>N,vecAngleBetween:()=>P,vecCeil:()=>Z,vecClamp:()=>B,vecCross:()=>w,vecDist:()=>q,vecDist2:()=>L,vecDiv:()=>u,vecDot:()=>D,vecEq:()=>a,vecFloor:()=>$,vecHeading:()=>A,vecIsZero:()=>W,vecLerp:()=>H,vecLimit:()=>b,vecMag:()=>R,vecMag2:()=>T,vecMove:()=>V,vecMult:()=>x,vecNorm:()=>i,vecRand:()=>z,vecReflect:()=>S,vecRem:()=>G,vecRotate:()=>O,vecRound:()=>j,vecSet:()=>d,vecSetMag:()=>g,vecSub:()=>p,vecToArray:()=>k});var M=Math.cos,f=Math.sin,I=2*Math.PI,s=class{constructor(e=0,o=e){this.x=parseFloat(e)||0,this.y=parseFloat(o)||0}toString(){return`Vector (${this.x}, ${this.y})`}},n=t=>t instanceof s,r=(t=0,e=t)=>(n(t)&&(e=t.y,t=t.x),new s(t,e)),d=(t,e,o=e)=>(n(e)?d(t,e.x,e.y):(t.x=e,t.y=o),t),y=(t,e,o=e)=>n(e)?y(t,e.x,e.y):(t.x+=e,t.y+=o,t),p=(t,e,o=e)=>n(e)?p(t,e.x,e.y):(t.x-=e,t.y-=o,t),x=(t,e,o=e)=>n(e)?x(t,e.x,e.y):(t.x*=e,t.y*=o,t),u=(t,e,o=e)=>n(e)?u(t,e.x,e.y):(t.x/=e||1,t.y/=o||1,t),O=(t,e)=>{let o=M(e),c=f(e);return t.x=o*t.x-c*t.y,t.y=c*t.x+o*t.y,t},S=(t,e)=>{let o=i(r(e));return p(t,x(o,2*D(t,o)))},g=(t,e)=>(i(t),x(t,e),t),R=t=>Math.hypot(t.x,t.y),T=t=>t.x*t.x+t.y*t.y,i=t=>{let e=R(t);return e>0&&u(t,e),t},b=(t,e=1)=>(T(t)>e*e&&g(t,e),t),q=(t,e)=>Math.hypot(e.x-t.x,e.y-t.y),L=(t,e)=>{let o=t.x-e.x,c=t.y-e.y;return o*o+c*c},A=t=>Math.atan2(t.y,t.x),N=t=>A(t),P=(t,e)=>Math.atan2(e.y-t.y,e.x-t.x),D=(t,e)=>t.x*e.x+t.y*e.y,w=(t,e)=>t.x*e.y-t.y*e.x,H=(t,e,o)=>(t.x+=(e.x-t.x)*o||0,t.y+=(e.y-t.y)*o||0,t),U=t=>(t.x=Math.abs(t.x),t.y=Math.abs(t.y),t),Z=t=>(t.x=Math.ceil(t.x),t.y=Math.ceil(t.y),t),$=t=>(t.x=Math.floor(t.x),t.y=Math.floor(t.y),t),j=t=>(t.x=Math.round(t.x),t.y=Math.round(t.y),t),B=(t,e,o)=>(t.x<e.x&&(t.x=e.x),t.x>o.x&&(t.x=o.x),t.y<e.y&&(t.y=e.y),t.y>o.y&&(t.y=o.y),t),G=(t,e)=>(t.x%=e,t.y%=e,t),V=(t,e,o=1)=>y(t,e.x*o,e.y*o),a=(t,e,o=e)=>n(e)?a(t,e.x,e.y):t.x===e&&t.y===o,W=t=>a(t,C),k=t=>[t.x,t.y],z=(t=1,e=t,o=globalThis.rand||Math.random)=>{let c=o()*I,l=o()*(e-t)+t;return r(M(c)*l,f(c)*l)},C=r(0,0),J=r(1,1),K=r(0,-1),Q=r(1,0),X=r(0,1),Y=r(-1,0);globalThis.utils=Object.assign(globalThis.utils||{},h);})();