UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

4 lines (3 loc) 13.5 kB
(()=>{var Ft=Object.defineProperty;var St=(e,t)=>{for(var s in t)Ft(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 W={};St(W,{ANCHOR_BOT_LEFT:()=>he,ANCHOR_BOT_RIGHT:()=>ae,ANCHOR_CENTER:()=>ie,ANCHOR_TOP_LEFT:()=>rt,ANCHOR_TOP_RIGHT:()=>oe,Actor:()=>C,BACK_IN:()=>ue,BACK_IN_OUT:()=>xe,BACK_OUT:()=>fe,BOUNCE_IN:()=>_t,BOUNCE_IN_OUT:()=>ye,BOUNCE_OUT:()=>Z,Camera:()=>g,DOWN:()=>se,EASE_IN:()=>le,EASE_IN_OUT:()=>pe,EASE_OUT:()=>ce,ELASTIC_IN:()=>_e,ELASTIC_IN_OUT:()=>de,ELASTIC_OUT:()=>me,Grid:()=>w,LEFT:()=>re,LINEAR:()=>xt,Noise:()=>H,ONE:()=>vt,RIGHT:()=>ee,TypedGrid:()=>L,UP:()=>te,Vector:()=>b,ZERO:()=>st,advance:()=>ht,assert:()=>V,choose:()=>Et,colcirc:()=>q,colrect:()=>j,colrectcirc:()=>E,dd:()=>Lt,diff:()=>it,dist:()=>nt,flipImage:()=>mt,formatTime:()=>Rt,fract:()=>ot,head:()=>wt,intersection:()=>I,is:()=>A,last:()=>Tt,lerpAngle:()=>ut,log:()=>N,lpad:()=>Nt,mag:()=>lt,makeCircle:()=>gt,makeRectangle:()=>bt,mean:()=>ct,median:()=>pt,mod:()=>at,percent:()=>ft,range:()=>Mt,resolverect:()=>D,rpad:()=>At,scaleImage:()=>dt,shuffle:()=>It,sum:()=>P,tail:()=>Pt,tintImage:()=>yt,tween:()=>ne,vec:()=>n,vecAbs:()=>Dt,vecAdd:()=>F,vecAngle:()=>Ut,vecAngleBetween:()=>Ht,vecCeil:()=>Vt,vecClamp:()=>zt,vecCross:()=>Wt,vecDist:()=>Yt,vecDist2:()=>Zt,vecDiv:()=>k,vecDot:()=>et,vecEq:()=>B,vecFloor:()=>jt,vecHeading:()=>tt,vecIsZero:()=>Gt,vecLerp:()=>Xt,vecLimit:()=>Ct,vecMag:()=>Q,vecMag2:()=>v,vecMove:()=>$t,vecMult:()=>T,vecNorm:()=>O,vecRand:()=>Qt,vecReflect:()=>Bt,vecRem:()=>Kt,vecRotate:()=>Ot,vecRound:()=>qt,vecSet:()=>G,vecSetMag:()=>J,vecSub:()=>S,vecToArray:()=>Jt});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,o=null){this._engine=t||globalThis,this.ox=s,this.oy=r,this.resize(i||this._engine.W-s,o||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),o=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-o*s+this.x,r.y=o*t+i*s+this.y,r}getCameraPoint(t,s,r={}){let i=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=t-this.x,s=s-this.y,t=i*t-o*s,s=o*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,o,h,a)=>{let l=Math.max(e,i),_=Math.min(e+s,i+h)-l,p=Math.max(t,o),m=Math.min(t+r,o+a)-p;return[l,p,_,m]};var D=(e,t,s,r,i,o,h,a)=>{let[l,_,p,m]=I(e,t,s,r,i,o,h,a),x="",y=e,c=t;return p<m?e<i?(x="right",y=i-s):(x="left",y=i+h):t<o?(x="bottom",c=o-r):(x="top",c=o+a),{dir:x,x:y,y:c}};var V=(e,t="Assertion failed")=>{if(!e)throw new Error(t)};var j=(e,t,s,r,i,o,h,a)=>e<i+h&&e+s>i&&t<o+a&&t+r>o;var q=(e,t,s,r,i,o)=>(r-e)*(r-e)+(i-t)*(i-t)<=(s+o)*(s+o);var E=(e,t,s,r,i,o,h)=>{let a=i-Math.max(e,Math.min(i,e+s)),l=o-Math.max(t,Math.min(o,t+r));return a*a+l*l<=h*h};var w=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,o=s?-1:1;for(;r!==i;){let h=this.indexToPointX(r),a=this.indexToPointY(r),l=this._c[r];if(t(h,a,l,this)===!1)break;r+=o}}fill(t){this.forEach((s,r)=>{this.set(s,r,t)})}clampX(t){return z(t,0,this._w-1)}clampY(t){return z(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,o,h)=>{r[o]=r[o]||"",r[o]+=h+t}),r.join(` `)}},L=class e extends w{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 z(e,t,s){return e<t?t:e>s?s:e}var K=Math.cos,$=Math.sin,kt=2*Math.PI,b=class{constructor(t=0,s=t){this.x=parseFloat(t)||0,this.y=parseFloat(s)||0}toString(){return`Vector (${this.x}, ${this.y})`}},d=e=>e instanceof b,n=(e=0,t=e)=>(d(e)&&(t=e.y,e=e.x),new b(e,t)),G=(e,t,s=t)=>(d(t)?G(e,t.x,t.y):(e.x=t,e.y=s),e),F=(e,t,s=t)=>d(t)?F(e,t.x,t.y):(e.x+=t,e.y+=s,e),S=(e,t,s=t)=>d(t)?S(e,t.x,t.y):(e.x-=t,e.y-=s,e),T=(e,t,s=t)=>d(t)?T(e,t.x,t.y):(e.x*=t,e.y*=s,e),k=(e,t,s=t)=>d(t)?k(e,t.x,t.y):(e.x/=t||1,e.y/=s||1,e),Ot=(e,t)=>{let s=K(t),r=$(t);return e.x=s*e.x-r*e.y,e.y=r*e.x+s*e.y,e},Bt=(e,t)=>{let s=O(n(t));return S(e,T(s,2*et(e,s)))},J=(e,t)=>(O(e),T(e,t),e),Q=e=>Math.hypot(e.x,e.y),v=e=>e.x*e.x+e.y*e.y,O=e=>{let t=Q(e);return t>0&&k(e,t),e},Ct=(e,t=1)=>(v(e)>t*t&&J(e,t),e),Yt=(e,t)=>Math.hypot(t.x-e.x,t.y-e.y),Zt=(e,t)=>{let s=e.x-t.x,r=e.y-t.y;return s*s+r*r},tt=e=>Math.atan2(e.y,e.x),Ut=e=>tt(e),Ht=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),et=(e,t)=>e.x*t.x+e.y*t.y,Wt=(e,t)=>e.x*t.y-e.y*t.x,Xt=(e,t,s)=>(e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0,e),Dt=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),Vt=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),jt=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),qt=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),zt=(e,t,s)=>(e.x<t.x&&(e.x=t.x),e.x>s.x&&(e.x=s.x),e.y<t.y&&(e.y=t.y),e.y>s.y&&(e.y=s.y),e),Kt=(e,t)=>(e.x%=t,e.y%=t,e),$t=(e,t,s=1)=>F(e,t.x*s,t.y*s),B=(e,t,s=t)=>d(t)?B(e,t.x,t.y):e.x===t&&e.y===s,Gt=e=>B(e,st),Jt=e=>[e.x,e.y],Qt=(e=1,t=e,s=globalThis.rand||Math.random)=>{let r=s()*kt,i=s()*(t-e)+e;return n(K(r)*i,$(r)*i)},st=n(0,0),vt=n(1,1),te=n(0,-1),ee=n(1,0),se=n(0,1),re=n(-1,0);var ie=n(.5,.5),rt=n(0,0),oe=n(1,0),he=n(0,1),ae=n(1,1),C=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,r=rt){this.sprite=t||{width:0,height:0},this.pos=s||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 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,o=this.pos.y-r*this.anchor.y;return[i,o,s,r]}in(t,s,r=1){return E(...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),o=-this.sprite.height*(this.flipY?1-r.y:r.y);s&&t.alpha(this.opacity),t.image(i,o,this.sprite)}};var it=(e,t)=>Math.abs(t-e)||0;var ot=e=>e%1||0;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 at=(e,t)=>(t+e%t)%t;var nt=(e,t,s,r)=>Math.hypot(s-e,r-t);var lt=(e,t)=>Math.hypot(e,t);var P=e=>{let t=0;for(let s=0;s<e.length;s++)t+=e[s];return t};var ct=e=>P(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 ut=(e,t,s)=>{let r=(t-e)%360;return r>180?r-=360:r<-180&&(r+=360),e+r*s};var ft=(e,t=0,s=1)=>s-t?(e-t)/(s-t):0;var R=Math.PI/2,ne=(e,t,s,r=1,i=xt)=>new Y(e,t,s,r,i),xt=e=>e,le=e=>e*e,ce=e=>-e*(e-2),pe=e=>e<.5?2*e*e:-2*e*e+4*e-1,ue=e=>e*e*e-e*Math.sin(e*Math.PI),fe=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},xe=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},_e=e=>Math.sin(13*R*e)*Math.pow(2,10*(e-1)),me=e=>Math.sin(-13*R*(e+1))*Math.pow(2,-10*e)+1,de=e=>{if(e<.5){let r=Math.sin(13*R*(2*e)),i=Math.pow(2,10*(2*e-1));return .5*r*i}let t=Math.sin(-13*R*(2*e-1+1)),s=Math.pow(2,-10*(2*e-1));return .5*(t*s+2)},_t=e=>1-Z(1-e),Z=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,ye=e=>e<.5?.5*_t(e*2):.5*Z(e*2-1)+.5,Y=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,o){this._o=t,this._p=s,this._x=r,this._d=i,this._e=o,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 o=this._t-this._w;this._o[this._p]=this._lc.lerp(s,r,this._e(o/this._d)),this._t+=i,o>=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 U=e=>.5*(1-Math.cos(e*Math.PI)),H=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),o=Math.floor(s),h=Math.floor(r),a=t-i,l=s-o,_=r-h,p,m,x=0,y=.5,c,u,M;for(let X=0;X<this._po;X++){let f=i+(o<<4)+(h<<8);p=U(a),m=U(l),c=this._p[f&4095],c+=p*(this._p[f+1&4095]-c),u=this._p[f+16&4095],u+=p*(this._p[f+16+1&4095]-u),c+=m*(u-c),f+=256,u=this._p[f&4095],u+=p*(this._p[f+1&4095]-u),M=this._p[f+16&4095],M+=p*(this._p[f+16+1&4095]-M),u+=m*(M-u),c+=U(_)*(u-c),x+=c*y,y*=this._pf,i<<=1,a*=2,o<<=1,l*=2,h<<=1,_*=2,a>=1&&(i++,a--),l>=1&&(o++,l--),_>=1&&(h++,_--)}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 dt=(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 yt=(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 gt=(e,t,{borderWidth:s=0,borderColor:r=0,engine:i=globalThis}={})=>{let o=e*2+s;return i.paint(o,o,()=>{i.circfill(o/2,o/2,e,t),s>0&&(i.linewidth(s),i.stroke(r))})};var bt=(e,t,s,{borderWidth:r=0,borderColor:i=0,engine:o=globalThis}={})=>{let h=e+r*2,a=t+r*2;return o.paint(h,a,()=>{let l=r>0;l&&o.cls(i),o.rectfill(l?r:0,l?r:0,e,t,s)})};var Mt=(e,t=0,s=1)=>[...Array(e|0).keys()].map(r=>t+s*r);var It=(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 Et=(e,t=globalThis.rand||Math.random)=>e[Math.floor(t()*e.length)];var wt=e=>e[0];var Tt=e=>e[e.length-1];var Pt=e=>e.slice(1);var Rt=e=>~~(e/60)+":"+(e%60<10?"0":"")+~~(e%60);var At=(e,t,s="0")=>(e+"").padEnd(t,s);var Nt=(e,t,s="0")=>(e+"").padStart(t,s);var A=(e,t)=>typeof t=="function"?e instanceof t:typeof e===t;var N=(e,t,s=globalThis)=>s.text(16,16,(t?`${t}: `:"")+(A(e,"object")?JSON.stringify(e,null,4):e));var Lt=(e,t,s=globalThis)=>{s.pal(["blue","#fff"]),s.cls(0),s.ctx().resetTransform(),s.textfont("monospace"),s.textsize(16),s.textalign("start","top"),N(e,t||"dd() output",s),s.quit()};globalThis.utils=Object.assign(globalThis.utils||{},W);})(); /*! @litecanvas/utils by Luiz Bills | MIT Licensed */