@litecanvas/utils
Version:
Utilities to help build litecanvas games
4 lines (3 loc) • 14.8 kB
JavaScript
(()=>{var Dt=Object.defineProperty;var jt=(s,t)=>{for(var e in t)Dt(s,e,{get:t[e],enumerable:!0})};window.utils=window.utils||{};window.utils.global=(s=!0)=>{for(let t in window.utils)t!=="global"&&(s||globalThis[t]===void 0)&&(globalThis[t]=window.utils[t])};var X={};jt(X,{ANCHOR_BOT_LEFT:()=>gs,ANCHOR_BOT_RIGHT:()=>Ns,ANCHOR_CENTER:()=>_s,ANCHOR_TOP_LEFT:()=>ot,ANCHOR_TOP_RIGHT:()=>ys,Actor:()=>Z,BACK_IN:()=>Fs,BACK_IN_OUT:()=>vs,BACK_OUT:()=>Ps,BOUNCE_IN:()=>It,BOUNCE_IN_OUT:()=>Ls,BOUNCE_OUT:()=>V,Camera:()=>g,DOWN:()=>xs,EASE_IN:()=>Ms,EASE_IN_OUT:()=>Es,EASE_OUT:()=>Is,ELASTIC_IN:()=>Rs,ELASTIC_IN_OUT:()=>Ts,ELASTIC_OUT:()=>As,Grid:()=>E,LEFT:()=>bs,LINEAR:()=>Mt,Noise:()=>W,ONE:()=>ps,RIGHT:()=>ds,TypedGrid:()=>L,UP:()=>fs,Vector:()=>zt,ZERO:()=>at,advance:()=>ut,almost:()=>_t,assert:()=>q,choose:()=>Lt,clamp:()=>p,colcirc:()=>K,colrect:()=>z,colrectcirc:()=>I,copysign:()=>gt,dd:()=>Xt,diff:()=>v,dist:()=>lt,dot:()=>wt,flipImage:()=>Et,formatTime:()=>Yt,fract:()=>nt,head:()=>St,includes:()=>Ct,intersection:()=>M,is:()=>R,last:()=>kt,length:()=>Bt,lerpAngle:()=>ft,lower:()=>Ht,lpad:()=>Ut,mag:()=>ct,makeCircle:()=>vt,makeRectangle:()=>Rt,mean:()=>mt,median:()=>pt,mod:()=>ht,move:()=>bt,percent:()=>dt,pingpong:()=>Nt,range:()=>At,resolverect:()=>j,rpad:()=>Zt,scaleImage:()=>Ft,score:()=>Wt,shuffle:()=>Tt,smoothstep:()=>yt,sum:()=>A,tail:()=>Ot,tintImage:()=>Pt,tween:()=>ws,upper:()=>Vt,vec:()=>n,vecAbs:()=>rs,vecAdd:()=>tt,vecAngle:()=>Qt,vecAngleBetween:()=>ts,vecCeil:()=>is,vecClamp:()=>ns,vecCross:()=>ss,vecDist:()=>rt,vecDist2:()=>Jt,vecDiv:()=>O,vecDot:()=>C,vecEq:()=>Y,vecFloor:()=>as,vecHeading:()=>it,vecIsZero:()=>ls,vecLerp:()=>es,vecLimit:()=>Gt,vecMag:()=>F,vecMag2:()=>et,vecMove:()=>hs,vecMult:()=>P,vecNorm:()=>B,vecRand:()=>ms,vecReflect:()=>$t,vecRem:()=>us,vecRotate:()=>Kt,vecRound:()=>os,vecSet:()=>S,vecSetMag:()=>st,vecSub:()=>k,vecToArray:()=>cs,wave:()=>xt});var g=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;constructor(t=null,e=0,r=0,i=null,a=null){this._engine=t||globalThis,this.ox=e,this.oy=r,this.resize(i||this._engine.W-e,a||this._engine.H-r),this.x=this.width/2,this.y=this.height/2,this._shake={x:0,y:0,removeListener:null}}resize(t,e){this.width=t,this.height=e,this._engine.emit("camera-resized",this)}start(t=!1){this._engine.push(),t&&this._engine.clip(i=>{i.rect(this.ox,this.oy,this.width,this.height)});let e=this.ox+this.width/2,r=this.oy+this.height/2;this._engine.translate(e,r),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y)}end(){this._engine.pop()}lookAt(t,e){this.x=t,this.y=e}move(t,e){this.x+=t,this.y+=e}zoom(t){this.scale*=t}zoomTo(t){this.scale=t}rotate(t){this.rotation+=t}rotateTo(t){this.rotation=t}getWorldPoint(t,e,r={}){let i=Math.cos(-this.rotation),a=Math.sin(-this.rotation);return t=(t-this.width/2-this.ox)/this.scale,e=(e-this.height/2-this.oy)/this.scale,r.x=i*t-a*e+this.x,r.y=a*t+i*e+this.y,r}getCameraPoint(t,e,r={}){let i=Math.cos(-this.rotation),a=Math.sin(-this.rotation);return t=t-this.x,e=e-this.y,t=i*t-a*e,e=a*t+i*e,r.x=t*this.scale+this.width/2+this.ox,r.y=e*this.scale+this.height/2+this.oy,r}getBounds(){return[this.ox,this.oy,this.width,this.height]}shake(t=1,e=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",r=>{this._shake.x=this._engine.randi(-t,t),this._shake.y=this._engine.randi(-t,t),e-=r,e<=0&&this.unshake()}))}unshake(){this.shaking&&(this._shake.removeListener(),this._shake.removeListener=null,this._shake.x=this._shake.y=0)}get shaking(){return this._shake.removeListener!==null}};var M=(s,t,e,r,i,a,o,u)=>{let h=Math.max(s,i),x=Math.min(s+e,i+o)-h,c=Math.max(t,a),b=Math.min(t+r,a+u)-c;return[h,c,x,b]};var j=(s,t,e,r,i,a,o,u)=>{let[h,x,c,b]=M(s,t,e,r,i,a,o,u),d="",y=s,l=t;return c<b?s<i?(d="right",y=i-e):(d="left",y=i+o):t<a?(d="bottom",l=a-r):(d="top",l=a+u),{dir:d,x:y,y:l}};var q=(s,t="Assertion failed")=>{if(!s)throw new Error(t)};var z=(s,t,e,r,i,a,o,u)=>s<i+o&&s+e>i&&t<a+u&&t+r>a;var K=(s,t,e,r,i,a)=>(r-s)*(r-s)+(i-t)*(i-t)<=(e+a)*(e+a);var I=(s,t,e,r,i,a,o)=>{let u=i-Math.max(s,Math.min(i,s+e)),h=a-Math.max(t,Math.min(a,t+r));return u*u+h*h<=o*o};var E=class s{_w;_h;_c;constructor(t,e,r=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~e),this._c=r}[Symbol.iterator](){let t=0;return{next:()=>({value:[this.indexToPointX(t),this.indexToPointY(t),this._c[t++]],done:t>this._c.length})}}clone(){return new s(this._w,this._h,this._c)}clear(){this.forEach((t,e)=>this.set(t,e,void 0))}get width(){return this._w}get height(){return this._h}set(t,e,r){this._c[this.pointToIndex(t,e)]=r}get(t,e){return this._c[this.pointToIndex(t,e)]}has(t,e){return this.get(t,e)!=null}check(t,e){return t>=0&&t<this._w&&e>=0&&e<this._h}get length(){return this._w*this._h}pointToIndex(t,e){return this.clampX(~~t)+this.clampY(~~e)*this._w}indexToPointX(t){return t%this._w}indexToPointY(t){return Math.floor(t/this._w)}forEach(t,e=!1){let r=e?this.length-1:0,i=e?-1:this.length,a=e?-1:1;for(;r!==i;){let o=this.indexToPointX(r),u=this.indexToPointY(r),h=this._c[r];if(t(o,u,h,this)===!1)break;r+=a}}fill(t){this.forEach((e,r)=>{this.set(e,r,t)})}clampX(t){return $(t,0,this._w-1)}clampY(t){return $(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",e=!0){if(!e)return this._c.join(t);let r=[];return this.forEach((i,a,o)=>{r[a]=r[a]||"",r[a]+=o+t}),r.join(`
`)}},L=class s extends E{constructor(t,e,r=Uint8Array){super(t,e,null),this._c=new r(this._w*this._h)}has(t,e){return this.get(t,e)!==0}clone(){let t=new s(this._w,this._h,this._c.constructor);return this.forEach((e,r,i)=>{t.set(e,r,i)}),t}};function $(s,t,e){return s<t?t:s>e?e:s}var p=(s,t,e)=>s<t?t:s>e?e:s;var J=Math.cos,Q=Math.sin,qt=2*Math.PI,G=parseFloat,N=class{constructor(t=0,e=t){this.x=G(t)||0,this.y=G(e)||0}toString(){return`Vector (${this.x}, ${this.y})`}},zt=N,_=s=>s instanceof N,n=(s=0,t=s)=>(_(s)&&(t=s.y,s=s.x),new N(s,t)),S=(s,t,e=t)=>(_(t)?S(s,t.x,t.y):(s.x=t,s.y=e),s),tt=(s,t,e=t)=>_(t)?tt(s,t.x,t.y):(s.x+=t,s.y+=e,s),k=(s,t,e=t)=>_(t)?k(s,t.x,t.y):(s.x-=t,s.y-=e,s),P=(s,t,e=t)=>_(t)?P(s,t.x,t.y):(s.x*=t,s.y*=e,s),O=(s,t,e=t)=>_(t)?O(s,t.x,t.y):(s.x/=t||1,s.y/=e||1,s),Kt=(s,t)=>{let e=J(t),r=Q(t);return s.x=e*s.x-r*s.y,s.y=r*s.x+e*s.y,s},$t=(s,t)=>{let e=B(n(t));return k(s,P(e,2*C(s,e)))},st=(s,t)=>P(B(s),t),F=s=>Math.hypot(s.x,s.y),et=s=>s.x*s.x+s.y*s.y,B=s=>{let t=F(s);return t>0&&O(s,t),s},Gt=(s,t=1)=>(et(s)>t*t&&st(s,t),s),rt=(s,t)=>Math.hypot(t.x-s.x,t.y-s.y),Jt=(s,t)=>{let e=s.x-t.x,r=s.y-t.y;return e*e+r*r},it=s=>Math.atan2(s.y,s.x),Qt=s=>it(s),C=(s,t)=>s.x*t.x+s.y*t.y,ts=(s,t)=>{let e=F(s),r=F(t);return r-e?Math.acos(p(C(s,t)/(e*r),-1,1)):0},ss=(s,t)=>s.x*t.y-s.y*t.x,es=(s,t,e)=>(s.x+=(t.x-s.x)*e||0,s.y+=(t.y-s.y)*e||0,s),rs=s=>(s.x=Math.abs(s.x),s.y=Math.abs(s.y),s),is=s=>(s.x=Math.ceil(s.x),s.y=Math.ceil(s.y),s),as=s=>(s.x=Math.floor(s.x),s.y=Math.floor(s.y),s),os=s=>(s.x=Math.round(s.x),s.y=Math.round(s.y),s),ns=(s,t,e)=>S(s,p(s.x,t,e),p(s.y,t,e)),us=(s,t)=>(s.x%=t,s.y%=t,s),hs=(s,t,e=1)=>{let r=t.x-s.x,i=t.y-s.y,a=Math.hypot(r,i);if(e=Math.abs(e),a<=e||a===0)s.x=t.x,s.y=t.y;else{let o=e/a;s.x=s.x+r*o,s.y=s.y+i*o}return s},Y=(s,t,e=t,r=1e-5)=>_(t)?Y(s,t.x,t.y,r):rt(s,t,e)<=r,ls=s=>Y(s,at),cs=s=>[s.x,s.y],ms=(s=1,t=s,e=window.rand||Math.random)=>{let r=e()*qt,i=e()*(t-s)+s;return n(J(r)*i,Q(r)*i)},at=n(0,0),ps=n(1,1),fs=n(0,-1),ds=n(1,0),xs=n(0,1),bs=n(-1,0);var _s=n(.5,.5),ot=n(0,0),ys=n(1,0),gs=n(0,1),Ns=n(1,1),Z=class{sprite;pos;spd;acc;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,e,r=ot){this.sprite=t||{width:0,height:0},this.pos=e||n(0),this.spd=n(0),this.acc=n(0),this._o=n(r),this._s=n(1,1)}set x(t){this.pos.x=t}get x(){return this.pos.x}set y(t){this.pos.y=t}get y(){return this.pos.y}set dx(t){this.spd.x=t}get dx(){return this.spd.x}set dy(t){this.spd.y=t}get dy(){return this.spd.y}set ddx(t){this.acc.x=t}get ddx(){return this.acc.x}set ddy(t){this.acc.y=t}get ddy(){return this.acc.y}set anchor(t){this._o.x=t.x,this._o.y=t.y}get anchor(){return this._o}get width(){return this.sprite.width*this._s.x}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}scaleTo(t,e=t){this._s.x=t,this._s.y=e}scaleBy(t,e=t){this._s.x*=t,this._s.y*=e}getBounds(t=!0){let e=this.sprite.width*(t?this._s.x:1),r=this.sprite.height*(t?this._s.y:1),i=this.pos.x-e*this.anchor.x,a=this.pos.y-r*this.anchor.y;return[i,a,e,r]}in(t,e,r=1){return I(...this.getBounds(),t,e,r)}col(t){return colrect(...this.getBounds(),...t.getBounds())}update(t=1){this.spd.x+=this.acc.x*t,this.spd.y+=this.acc.y*t,this.pos.x+=this.spd.x*t,this.pos.y+=this.spd.y*t}draw(t=globalThis,e=!0){e&&t.push(),this.transform(t),this.sprite.width&&this.sprite.height&&!this.hidden&&this.opacity>0&&this.drawImage(t),e&&t.pop()}transform(t=globalThis){t.translate(this.pos.x,this.pos.y),t.rotate(t.deg2rad(this.angle)),t.scale((this.flipX?-1:1)*this._s.x,(this.flipY?-1:1)*this._s.y)}drawImage(t=globalThis,e=!0){let r=this.anchor,i=-this.sprite.width*(this.flipX?1-r.x:r.x),a=-this.sprite.height*(this.flipY?1-r.y:r.y);e&&t.alpha(this.opacity),t.image(i,a,this.sprite)}};var v=(s,t)=>Math.abs(t-s);var nt=s=>s%1;var R=(s,t,e)=>{switch(t){case"function":return typeof s=="function";case"array":return Array.isArray(s);case"int":return Number.isInteger(s);case"number":return typeof s=="number"&&!Number.isNaN(s);case"infinity":return typeof s=="number"&&Math.abs(s)===1/0;default:return typeof s===t}};var ut=(s,t,e,r=1)=>{e&&(t.x+=e.x*r,t.y+=e.y*r),s.x+=t.x*r,s.y+=t.y*r};var ht=(s,t)=>(s%t+t)%t||0;var lt=(s,t,e,r)=>Math.hypot(e-s,r-t)||0;var ct=(s,t)=>Math.hypot(s,t);var A=s=>{let t=0;for(let e=0;e<s.length;e++)t+=s[e];return t};var mt=s=>A(s)/s.length;var pt=s=>{let t=s.sort((r,i)=>r-i),e=Math.floor(t.length/2);return t.length%2===0?(t[e-1]+t[e])/2:t[e]};var ft=(s,t,e)=>{let r=(t-s)%360;return r>180?r-=360:r<-180&&(r+=360),s+r*e};var dt=(s,t,e)=>p((s-t)/(e-t),0,1);var xt=(s,t,e,r=Math.sin)=>s+(r(e)+1)/2*(t-s);var bt=(s,t,e)=>Math.abs(t-s)<=e?t:s+Math.sign(t-s)*e;var _t=(s,t,e=1e-5)=>v(s,t)<=e;var yt=(s,t,e)=>{let r=p((e-s)/(t-s),0,1);return r*r*(3-2*r)};var gt=(s,t)=>Math.sign(t)*Math.abs(s);var Nt=(s,t=1)=>t-Math.abs(s%(t*2)-t);var wt=(s,t,e,r)=>s*e+t*r;var T=Math.PI/2,ws=(s,t,e,r=1,i=Mt)=>new U(s,t,e,r,i),Mt=s=>s,Ms=s=>s*s,Is=s=>-s*(s-2),Es=s=>s<.5?2*s*s:-2*s*s+4*s-1,Fs=s=>s*s*s-s*Math.sin(s*Math.PI),Ps=s=>{let t=1-s;return 1-(t*t*t-t*Math.sin(t*Math.PI))},vs=s=>{if(s<.5){let e=2*s;return .5*(e*e*e-e*Math.sin(e*Math.PI))}let t=1-(2*s-1);return .5*(1-(t*t*t-t*Math.sin(s*Math.PI)))+.5},Rs=s=>Math.sin(13*T*s)*Math.pow(2,10*(s-1)),As=s=>Math.sin(-13*T*(s+1))*Math.pow(2,-10*s)+1,Ts=s=>{if(s<.5){let r=Math.sin(13*T*(2*s)),i=Math.pow(2,10*(2*s-1));return .5*r*i}let t=Math.sin(-13*T*(2*s-1+1)),e=Math.pow(2,-10*(2*s-1));return .5*(t*e+2)},It=s=>1-V(1-s),V=s=>s<4/11?121*s*s/16:s<8/11?363/40*s*s-99/10*s+17/5:s<9/10?4356/361*s*s-35442/1805*s+16061/1805:54/5*s*s-513/25*s+268/25,Ls=s=>s<.5?.5*It(s*2):.5*V(s*2-1)+.5,U=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(t,e,r,i,a){this._o=t,this._p=e,this._x=r,this._d=i,this._e=a,this._w=0}start(t){if(this.running)return this;this._cu.stop(!1),this._ch=this._cu=this,this.running=!0;let e=this._o[this._p]||0,r=this._rel?e+this._x:this._x;return this._lc=this._lc||t||globalThis,this._u=this._lc.listen("update",i=>{if(this._t<=this._w){this._t+=i;return}let a=this._t-this._w;this._o[this._p]=this._lc.lerp(e,r,this._e(a/this._d)),this._t+=i,a>=this._d&&(this._o[this._p]=r,this.stop())}),this}stop(t=!0){if(!this._u)return this;if(this.running=!1,this._u(),this._t=0,t)for(let e of this._cb)e(this._o);return this}restart(t=null,e=!1){return this.stop(e).restart(t)}onEnd(t){return this._cb.push(t),this}chain(t){return this._ch.onEnd(()=>{this._cu=t.start(this._lc)}),this._ch=t,this}reset(){return this._cb.length=0,this.stop()}relative(t=!0){return this._rel=t,this}delay(t){return this._w=t,this}get current(){return this._cu}get progress(){return this.running&&this._t>this._w?(this._t-this._w)/this._d:0}};var H=s=>.5*(1-Math.cos(s*Math.PI)),W=class{_p=[];_po=4;_pf=.5;_e=null;constructor(t){this._e=t||globalThis,this.noiseSeed()}noise(t,e=0,r=0){t<0&&(t=-t),e<0&&(e=-e),r<0&&(r=-r);let i=Math.floor(t),a=Math.floor(e),o=Math.floor(r),u=t-i,h=e-a,x=r-o,c,b,d=0,y=.5,l,m,w;for(let D=0;D<this._po;D++){let f=i+(a<<4)+(o<<8);c=H(u),b=H(h),l=this._p[f&4095],l+=c*(this._p[f+1&4095]-l),m=this._p[f+16&4095],m+=c*(this._p[f+16+1&4095]-m),l+=b*(m-l),f+=256,m=this._p[f&4095],m+=c*(this._p[f+1&4095]-m),w=this._p[f+16&4095],w+=c*(this._p[f+16+1&4095]-w),m+=b*(w-m),l+=H(x)*(m-l),d+=l*y,y*=this._pf,i<<=1,u*=2,a<<=1,h*=2,o<<=1,x*=2,u>=1&&(i++,u--),h>=1&&(a++,h--),x>=1&&(o++,x--)}return d}noiseDetail(t,e){t>0&&(this._po=t),e>0&&(this._pf=e)}noiseSeed(t=null){t!=null&&this._e.rseed(t);let e=this._e.rand||Math.random;for(let r=0;r<4096;r++)this._p[r]=e()}};var Et=(s,t=!0,e=!1,r=globalThis)=>r.paint(s.width,s.height,i=>{r.push(),r.scale(t?-1:1,e?-1:1),r.image(t?-s.width:0,e?-s.height:0,s),r.pop()});var Ft=(s,t,e=!0,r=globalThis)=>r.paint(s.width*t,s.height*t,i=>{r.push(),i.imageSmoothingEnabled=!e,r.scale(t),r.image(0,0,s),r.pop()});var Pt=(s,t,e=1,r=globalThis)=>r.paint(s.width,s.height,i=>{r.push(),r.alpha(e),r.rectfill(0,0,s.width,s.height,t),i.globalCompositeOperation="destination-atop",r.alpha(1),r.image(0,0,s),r.pop()});var vt=(s,t,{borderWidth:e=0,borderColor:r=0,engine:i=globalThis}={})=>{let a=s*2+e;return i.paint(a,a,()=>{i.circfill(a/2,a/2,s,t),e>0&&(i.linewidth(e),i.stroke(r))})};var Rt=(s,t,e,{borderWidth:r=0,borderColor:i=0,engine:a=globalThis}={})=>{let o=s+r*2,u=t+r*2;return a.paint(o,u,()=>{let h=r>0;h&&a.cls(i),a.rectfill(h?r:0,h?r:0,s,t,e)})};var At=(s,t=0,e=1)=>[...Array(s|0).keys()].map(r=>t+e*r);var Tt=(s,t=window.rand||Math.random)=>{s=[...s];for(let e=s.length-1;e>0;e--){let r=Math.floor(t()*(e+1)),i=s[e];s[e]=s[r],s[r]=i}return s};var Lt=(s,t=window.rand||Math.random)=>s[Math.floor(t()*s.length)];var St=s=>s[0];var kt=s=>s[s.length-1];var Ot=s=>s.slice(1);var Bt=s=>~~s?.length;var Ct=(s,t,e=0)=>s?.includes(t,e);var Yt=s=>~~(s/60)+":"+(s%60<10?"0":"")+~~(s%60);var Zt=(s,t,e="0")=>(s+"").padEnd(t,e);var Ut=(s,t,e="0")=>(s+"").padStart(t,e);var Vt=s=>(s+"").toUpperCase();var Ht=s=>(s+"").toLowerCase();var Wt=(s,t=6)=>(s+"").padStart(t,"0");var Xt=(s,t,e=globalThis)=>{e.pal(["blue","#fff"]),e.cls(0),e.ctx().resetTransform(),e.textfont("monospace"),e.textsize(16),e.textalign("start","top"),e.text(16,16,`${t??"dd() output"}: `+(R(s,"object")?JSON.stringify(s,null,4):s)),e.quit()};window.utils=Object.assign(window.utils||{},X);})();
/*! @litecanvas/utils by Luiz Bills | MIT Licensed */