@litecanvas/utils
Version:
Utilities to help build litecanvas games
4 lines (3 loc) • 12.7 kB
JavaScript
(()=>{var Mt=Object.defineProperty;var It=(e,t)=>{for(var s in t)Mt(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 z={};It(z,{ANCHOR_BOT_LEFT:()=>te,ANCHOR_BOT_RIGHT:()=>ee,ANCHOR_CENTER:()=>Qt,ANCHOR_TOP_LEFT:()=>J,ANCHOR_TOP_RIGHT:()=>Jt,Actor:()=>C,BACK_IN:()=>ne,BACK_IN_OUT:()=>le,BACK_OUT:()=>oe,BOUNCE_IN:()=>ct,BOUNCE_IN_OUT:()=>me,BOUNCE_OUT:()=>v,Camera:()=>y,DOWN:()=>Zt,EASE_IN:()=>re,EASE_IN_OUT:()=>ae,EASE_OUT:()=>ie,ELASTIC_IN:()=>he,ELASTIC_IN_OUT:()=>ce,ELASTIC_OUT:()=>ue,Grid:()=>M,LEFT:()=>Kt,LINEAR:()=>ut,Noise:()=>Y,ONE:()=>qt,RIGHT:()=>$t,TypedGrid:()=>P,UP:()=>Vt,Vector:()=>F,ZERO:()=>Q,advance:()=>rt,choose:()=>Ft,colcirc:()=>W,colrect:()=>B,diff:()=>tt,dist:()=>at,flipImage:()=>ft,fract:()=>st,head:()=>wt,intersection:()=>E,last:()=>Tt,lerpAngle:()=>ht,mag:()=>nt,makeCircle:()=>xt,makeRectangle:()=>_t,mean:()=>ot,median:()=>lt,mod:()=>it,range:()=>gt,resolverect:()=>U,scaleImage:()=>dt,shuffle:()=>yt,sum:()=>A,tail:()=>Et,tintImage:()=>bt,tween:()=>se,vec:()=>o,vecAbs:()=>zt,vecAdd:()=>L,vecAngle:()=>Ct,vecAngleBetween:()=>Rt,vecCeil:()=>Xt,vecClamp:()=>Wt,vecCross:()=>vt,vecDist:()=>Ot,vecDist2:()=>St,vecDiv:()=>I,vecDot:()=>K,vecEq:()=>D,vecFloor:()=>Ut,vecIsZero:()=>jt,vecLerp:()=>Nt,vecLimit:()=>Lt,vecMag:()=>$,vecMag2:()=>Z,vecMove:()=>Gt,vecMult:()=>w,vecNorm:()=>S,vecRand:()=>Yt,vecReflect:()=>Pt,vecRotate:()=>Ht,vecRound:()=>Bt,vecSet:()=>V,vecSetMag:()=>Dt,vecSub:()=>O,wave:()=>et});var y=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};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.WIDTH-s,a||this._engine.HEIGHT-r),this.x=this.width/2,this.y=this.height/2}resize(t,s){this.width=t,this.height=s,this._engine.emit("camera-resized",this)}start(t=!1){if(this._engine.push(),t){let i=path();i.rect(this.ox,this.oy,this.width,this.height),this._engine.clip(i)}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]}viewing(t,s,r,i){let a=this.width/2-this.x,n=this.height/2-this.y,l=this.width/this.scale,h=this.height/this.scale;return this._engine.colrect(t,s,r,i,a,n,l,h)}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 E=(e,t,s,r,i,a,n,l)=>{let h=Math.max(e,i),b=Math.min(e+s,i+n)-h,c=Math.max(t,a),x=Math.min(t+r,a+l)-c;return[h,c,b,x]};var U=(e,t,s,r,i,a,n,l)=>{let[h,b,c,x]=E(e,t,s,r,i,a,n,l),d="",g=e,u=t;return c<x?e<i?(d="right",g=i-s):(d="left",g=i+n):t<a?(d="bottom",u=a-r):(d="top",u=a+l),{direction:d,x:g,y:u}};var B=(e,t,s,r,i,a,n,l)=>e<i+n&&e+s>i&&t<a+l&&t+r>a;var W=(e,t,s,r,i,a)=>(r-e)*(r-e)+(i-t)*(i-t)<=(s+a)*(s+a);var M=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 n=this.indexToPointX(r),l=this.indexToPointY(r),h=this._c[r];if(t(n,l,h,this)===!1)break;r+=a}}fill(t){this.forEach((s,r)=>{this.set(s,r,t)})}clampX(t){return G(t,0,this._w-1)}clampY(t){return G(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,n)=>{r[a]=r[a]||"",r[a]+=n+t}),r.join(`
`)}},P=class e extends M{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 G(e,t,s){return e<t?t:e>s?s:e}var At=Math.sqrt,j=Math.cos,q=Math.sin,kt=2*Math.PI,F=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},_=e=>e instanceof F,o=(e=0,t=e)=>(_(e)&&(t=e.y,e=e.x),new F(e,t)),D=(e,t,s=t)=>_(t)?D(e,t.x,t.y):e.x===t&&e.y===s,V=(e,t,s=t)=>(_(t)?V(e,t.x,t.y):(e.x=t,e.y=s),e),L=(e,t,s=t)=>_(t)?L(e,t.x,t.y):(e.x+=t,e.y+=s,e),O=(e,t,s=t)=>_(t)?O(e,t.x,t.y):(e.x-=t,e.y-=s,e),w=(e,t,s=t)=>_(t)?w(e,t.x,t.y):(e.x*=t,e.y*=s,e),I=(e,t,s=t)=>_(t)?I(e,t.x,t.y):(e.x/=t||1,e.y/=s||1,e),Ht=(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},Pt=(e,t)=>{let s=S(o(t));return O(e,w(s,2*K(e,s)))},Dt=(e,t)=>(S(e),w(e,t),e),$=e=>Math.hypot(e.x,e.y),Z=e=>e.x*e.x+e.y*e.y,S=e=>{let t=$(e);return t>0&&I(e,t),e},Lt=(e,t=1)=>{let s=Z(e);return s>t*t&&(I(e,At(s)),w(e,t)),e},Ot=(e,t)=>Math.hypot(t.x-e.x,t.y-e.y),St=(e,t)=>{let s=e.x-t.x,r=e.y-t.y;return s*s+r*r},Ct=e=>Math.atan2(e.y,e.x),Rt=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),K=(e,t)=>e.x*t.x+e.y*t.y,vt=(e,t)=>e.x*t.y-e.y*t.x,Nt=(e,t,s)=>(e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0,e),Yt=(e=1,t=e,s=globalThis.rand||Math.random)=>{let r=s()*kt,i=s()*(t-e)+e;return o(j(r)*i,q(r)*i)},zt=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),Xt=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),Ut=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),Bt=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),Wt=(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),Gt=(e,t,s=1)=>L(e,t.x*s,t.y*s),jt=e=>D(e,Q),Q=o(0,0),qt=o(1,1),Vt=o(0,-1),$t=o(1,0),Zt=o(0,1),Kt=o(-1,0);var Qt=o(.5,.5),J=o(0,0),Jt=o(1,0),te=o(0,1),ee=o(1,1),C=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,r=J){this.sprite=t,this.pos=s||o(0),this._o=o(r),this._s=o(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]}draw(t=globalThis,s=!0){this.hidden||this.opacity<=0||(s&&t.push(),this.transform(t),this.drawImage(t),s&&t.pop())}transform(t){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,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 tt=(e,t)=>Math.abs(t-e)||0;var et=(e,t,s,r=Math.sin)=>e+(r(s)+1)/2*(t-e);var st=e=>e%1||0;var rt=advance=(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 it=(e,t)=>(t+e%t)%t;var at=(e,t,s,r)=>Math.hypot(s-e,r-t);var nt=(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 ot=e=>A(e)/e.length;var lt=(...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 ht=(e,t,s)=>{let r=(t-e)%360;return r>180?r-=360:r<-180&&(r+=360),e+r*s};var k=Math.PI/2,se=(e,t,s,r=1,i=ut)=>new R(e,t,s,r,i),ut=e=>e,re=e=>e*e,ie=e=>-e*(e-2),ae=e=>e<.5?2*e*e:-2*e*e+4*e-1,ne=e=>e*e*e-e*Math.sin(e*Math.PI),oe=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},le=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},he=e=>Math.sin(13*k*e)*Math.pow(2,10*(e-1)),ue=e=>Math.sin(-13*k*(e+1))*Math.pow(2,-10*e)+1,ce=e=>{if(e<.5){let r=Math.sin(13*k*(2*e)),i=Math.pow(2,10*(2*e-1));return .5*r*i}let t=Math.sin(-13*k*(2*e-1+1)),s=Math.pow(2,-10*(2*e-1));return .5*(t*s+2)},ct=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,me=e=>e<.5?.5*ct(e*2):.5*v(e*2-1)+.5,R=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 mt=4,H=1<<mt,pt=8,pe=1<<pt,f=4095,N=e=>.5*(1-Math.cos(e*Math.PI)),Y=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),n=Math.floor(r),l=t-i,h=s-a,b=r-n,c,x,d=0,g=.5,u,m,T;for(let X=0;X<this._po;X++){let p=i+(a<<mt)+(n<<pt);c=N(l),x=N(h),u=this._p[p&f],u+=c*(this._p[p+1&f]-u),m=this._p[p+H&f],m+=c*(this._p[p+H+1&f]-m),u+=x*(m-u),p+=pe,m=this._p[p&f],m+=c*(this._p[p+1&f]-m),T=this._p[p+H&f],T+=c*(this._p[p+H+1&f]-T),m+=x*(T-m),u+=N(b)*(m-u),d+=u*g,g*=this._pf,i<<=1,l*=2,a<<=1,h*=2,n<<=1,b*=2,l>=1&&(i++,l--),h>=1&&(a++,h--),b>=1&&(n++,b--)}return d}noiseDetail(t,s){t>0&&(this._po=t),s>0&&(this._pf=s)}noiseSeed(t=null){t!=null&&this._e.seed(t);let s=this._e.rand||Math.random;for(let r=0;r<f+1;r++)this._p[r]=s()}};var ft=(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 bt=(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 xt=(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 _t=(e,t,s,{borderWidth:r=0,borderColor:i=0,engine:a=globalThis}={})=>{let n=e+r*2,l=t+r*2;return a.paint(n,l,()=>{a.rectfill(r>0?r:0,r>0?r:0,e,t,s),r>0&&(a.linewidth(r),a.stroke(i))})};var gt=(e,t=0,s=1)=>[...new Array(e).keys()].map(r=>t+s*r);var yt=(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 Ft=(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 Et=e=>e.slice(1);globalThis.utils=Object.assign(globalThis.utils||{},z);})();
/*! @litecanvas/utils by Luiz Bills | MIT Licensed */