UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

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