UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

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