UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

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