UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

4 lines (3 loc) 11.3 kB
(()=>{var ut=Object.defineProperty;var ft=(e,t)=>{for(var s in t)ut(e,s,{get:t[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let e in globalThis.utils)e!=="global"&&(globalThis[e]=globalThis.utils[e])};var W={};ft(W,{ANCHOR_BOT_LEFT:()=>Bt,ANCHOR_BOT_RIGHT:()=>Wt,ANCHOR_CENTER:()=>Yt,ANCHOR_TOP_LEFT:()=>J,ANCHOR_TOP_RIGHT:()=>Nt,Actor:()=>R,BACK_IN:()=>jt,BACK_IN_OUT:()=>qt,BACK_OUT:()=>$t,BOUNCE_IN:()=>ot,BOUNCE_IN_OUT:()=>Jt,BOUNCE_OUT:()=>Y,Camera:()=>w,DOWN:()=>Rt,EASE_IN:()=>Ft,EASE_IN_OUT:()=>Gt,EASE_OUT:()=>Vt,ELASTIC_IN:()=>Zt,ELASTIC_IN_OUT:()=>Kt,ELASTIC_OUT:()=>zt,Grid:()=>b,LEFT:()=>Xt,LINEAR:()=>rt,Noise:()=>B,ONE:()=>Lt,RIGHT:()=>Dt,TypedGrid:()=>S,UP:()=>Pt,Vector:()=>T,ZERO:()=>K,advance:()=>st,diff:()=>Q,flipImage:()=>lt,fract:()=>tt,intersection:()=>I,mod:()=>it,range:()=>et,resolve:()=>F,scaleImage:()=>ct,tint:()=>at,tween:()=>Ut,vec:()=>n,vecAbs:()=>bt,vecAdd:()=>L,vecAngle:()=>wt,vecAngleBetween:()=>Tt,vecCeil:()=>At,vecClamp:()=>St,vecCross:()=>Et,vecDist:()=>yt,vecDist2:()=>mt,vecDiv:()=>A,vecDot:()=>z,vecEq:()=>C,vecFloor:()=>Ht,vecIsZero:()=>Ct,vecLerp:()=>Mt,vecLimit:()=>gt,vecMag:()=>q,vecMag2:()=>Z,vecMove:()=>kt,vecMult:()=>E,vecNorm:()=>D,vecRand:()=>It,vecReflect:()=>dt,vecRotate:()=>_t,vecRound:()=>Ot,vecSet:()=>$,vecSetMag:()=>xt,vecSub:()=>P,wave:()=>v});var I=(e,t,s,i,r,o,h,a)=>{let c=Math.max(e,r),x=Math.min(e+s,r+h)-c,u=Math.max(t,o),g=Math.min(t+i,o+a)-u;return[c,u,x,g]};var F=(e,t,s,i,r,o,h,a)=>{let[c,x,u,g]=I(e,t,s,i,r,o,h,a),d="",m=e,l=t;return u<g?e<r?(d="right",m=r-s):(d="left",m=r+h):t<o?(d="bottom",l=o-i):(d="top",l=o+a),{direction:d,x:m,y:l}};var w=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,i=0,r=null,o=null){this._engine=t||globalThis,this.ox=s,this.oy=i,this.resize(r||this._engine.WIDTH-s,o||this._engine.HEIGHT-i),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 r=path();r.rect(this.ox,this.oy,this.width,this.height),this._engine.clip(r)}let s=this.ox+this.width/2,i=this.oy+this.height/2;this._engine.translate(s,i),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,i={}){let r=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,i.x=r*t-o*s+this.x,i.y=o*t+r*s+this.y,i}getCameraPoint(t,s,i={}){let r=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=t-this.x,s=s-this.y,t=r*t-o*s,s=o*t+r*s,i.x=t*this.scale+this.width/2+this.ox,i.y=s*this.scale+this.height/2+this.oy,i}getBounds(){return[this.ox,this.oy,this.width,this.height]}viewing(t,s,i,r){let o=this.width/2-this.x,h=this.height/2-this.y,a=this.width/this.scale,c=this.height/this.scale;return this._engine.colrect(t,s,i,r,o,h,a,c)}shake(t=1,s=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",i=>{this._shake.x=this._engine.randi(-t,t),this._shake.y=this._engine.randi(-t,t),s-=i,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 b=class e{_w;_h;_c;constructor(t,s,i=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~s),this._c=i}[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,i){this._c[this.pointToIndex(t,s)]=i}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 i=s?this.length-1:0,r=s?-1:this.length,o=s?-1:1;for(;i!==r;){let h=this.indexToPointX(i),a=this.indexToPointY(i),c=this._c[i];if(t(h,a,c,this)===!1)break;i+=o}}fill(t){this.forEach((s,i)=>{this.set(s,i,t)})}clampX(t){return V(t,0,this._w-1)}clampY(t){return V(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",s=!0){if(!s)return this._c.join(t);let i=[];return this.forEach((r,o,h)=>{i[o]=i[o]||"",i[o]+=h+t}),i.join(` `)}},S=class e extends b{constructor(t,s,i=Uint8Array){super(t,s,null),this._c=new i(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,i,r)=>{t.set(s,i,r)}),t}};function V(e,t,s){return e<t?t:e>s?s:e}var k=Math.sqrt,G=Math.cos,j=Math.sin,pt=2*Math.PI,T=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},y=e=>e instanceof T,n=(e=0,t=e)=>(y(e)&&(t=e.y,e=e.x),new T(e,t)),C=(e,t,s=t)=>y(t)?C(e,t.x,t.y):e.x===t&&e.y===s,$=(e,t,s=t)=>(y(t)?$(e,t.x,t.y):(e.x=t,e.y=s),e),L=(e,t,s=t)=>y(t)?L(e,t.x,t.y):(e.x+=t,e.y+=s,e),P=(e,t,s=t)=>y(t)?P(e,t.x,t.y):(e.x-=t,e.y-=s,e),E=(e,t,s=t)=>y(t)?E(e,t.x,t.y):(e.x*=t,e.y*=s,e),A=(e,t,s=t)=>y(t)?A(e,t.x,t.y):(e.x/=t||1,e.y/=s||1,e),_t=(e,t)=>{let s=G(t),i=j(t);return e.x=s*e.x-i*e.y,e.y=i*e.x+s*e.y,e},dt=(e,t)=>{let s=D(n(t));return P(e,E(s,2*z(e,s)))},xt=(e,t)=>(D(e),E(e,t),e),q=e=>k(e.x*e.x+e.y*e.y),Z=e=>e.x*e.x+e.y*e.y,D=e=>{let t=q(e);return t>0&&A(e,t),e},gt=(e,t=1)=>{let s=Z(e);return s>t*t&&(A(e,k(s)),E(e,t)),e},yt=(e,t)=>{let s=e.x-t.x,i=e.y-t.y;return k(s*s+i*i)},mt=(e,t)=>{let s=e.x-t.x,i=e.y-t.y;return s*s+i*i},wt=e=>Math.atan2(e.y,e.x),Tt=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),z=(e,t)=>e.x*t.x+e.y*t.y,Et=(e,t)=>e.x*t.y-e.y*t.x,Mt=(e,t,s)=>(e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0,e),It=(e=1,t=e,s=globalThis.rand||Math.random)=>{let i=s()*pt,r=s()*(t-e)+e;return n(G(i)*r,j(i)*r)},bt=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),At=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),Ht=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),Ot=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),St=(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,s=1)=>L(e,t.x*s,t.y*s),Ct=e=>C(e,K),K=n(0,0),Lt=n(1,1),Pt=n(0,-1),Dt=n(1,0),Rt=n(0,1),Xt=n(-1,0);var Yt=n(.5,.5),J=n(0,0),Nt=n(1,0),Bt=n(0,1),Wt=n(1,1),R=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,i=J){this.sprite=t,this.pos=s||n(0),this._o=n(i),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),i=this.sprite.height*(t?this._s.y:1),r=this.pos.x-s*this.anchor.x,o=this.pos.y-i*this.anchor.y;return[r,o,s,i]}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 i=this.anchor,r=-this.sprite.width*(this.flipX?1-i.x:i.x),o=-this.sprite.height*(this.flipY?1-i.y:i.y);s&&t.alpha(this.opacity),t.image(r,o,this.sprite)}};var Q=(e,t)=>Math.abs(t-e)||0;var v=(e,t,s,i=Math.sin)=>e+(i(s)+1)/2*(t-e);var tt=e=>e%1||0;var et=e=>Array.from(Array(e).keys());var st=advance=(e,t,s,i=1)=>{s&&(t.x+=s.x*i,t.y+=s.y*i),e.x+=t.x*i,e.y+=t.y*i};var it=(e,t)=>(t+e%t)%t;var H=Math.PI/2,Ut=(e,t,s,i=1,r=rt)=>new X(e,t,s,i,r),rt=e=>e,Ft=e=>e*e,Vt=e=>-e*(e-2),Gt=e=>e<.5?2*e*e:-2*e*e+4*e-1,jt=e=>e*e*e-e*Math.sin(e*Math.PI),$t=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},qt=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},Zt=e=>Math.sin(13*H*e)*Math.pow(2,10*(e-1)),zt=e=>Math.sin(-13*H*(e+1))*Math.pow(2,-10*e)+1,Kt=e=>{if(e<.5){let i=Math.sin(13*H*(2*e)),r=Math.pow(2,10*(2*e-1));return .5*i*r}let t=Math.sin(-13*H*(2*e-1+1)),s=Math.pow(2,-10*(2*e-1));return .5*(t*s+2)},ot=e=>1-Y(1-e),Y=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,Jt=e=>e<.5?.5*ot(e*2):.5*Y(e*2-1)+.5,X=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(t,s,i,r,o){this._o=t,this._p=s,this._x=i,this._d=r,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,i=this._rel?s+this._x:this._x;return this._lc=this._lc||t||globalThis,this._u=this._lc.listen("update",r=>{if(this._t<=this._w){this._t+=r;return}let o=this._t-this._w;this._o[this._p]=this._lc.lerp(s,i,this._e(o/this._d)),this._t+=r,o>=this._d&&(this._o[this._p]=i,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 nt=4,O=1<<nt,ht=8,Qt=1<<ht,_=4095,N=e=>.5*(1-Math.cos(e*Math.PI)),B=class{_p=[];_po=4;_pf=.5;_e=null;constructor(t){this._e=t||globalThis,this.noiseSeed()}noise(t,s=0,i=0){t<0&&(t=-t),s<0&&(s=-s),i<0&&(i=-i);let r=Math.floor(t),o=Math.floor(s),h=Math.floor(i),a=t-r,c=s-o,x=i-h,u,g,d=0,m=.5,l,f,M;for(let U=0;U<this._po;U++){let p=r+(o<<nt)+(h<<ht);u=N(a),g=N(c),l=this._p[p&_],l+=u*(this._p[p+1&_]-l),f=this._p[p+O&_],f+=u*(this._p[p+O+1&_]-f),l+=g*(f-l),p+=Qt,f=this._p[p&_],f+=u*(this._p[p+1&_]-f),M=this._p[p+O&_],M+=u*(this._p[p+O+1&_]-M),f+=g*(M-f),l+=N(x)*(f-l),d+=l*m,m*=this._pf,r<<=1,a*=2,o<<=1,c*=2,h<<=1,x*=2,a>=1&&(r++,a--),c>=1&&(o++,c--),x>=1&&(h++,x--)}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 i=0;i<_+1;i++)this._p[i]=s()}};var at=(e,t,s=1,i=globalThis)=>i.paint(e.width,e.height,r=>{i.push(),i.alpha(s),i.rectfill(0,0,e.width,e.height,t),r.globalCompositeOperation="destination-atop",i.alpha(1),i.image(0,0,e),i.pop()});var ct=(e,t,s=!0,i=globalThis)=>i.paint(e.width*t,e.height*t,r=>{i.push(),r.imageSmoothingEnabled=!s,i.scale(t),i.image(0,0,e),i.pop()});var lt=(e,t=!0,s=!1,i=globalThis)=>i.paint(e.width,e.height,r=>{i.push(),i.scale(t?-1:1,s?-1:1),i.image(t?-e.width:0,s?-e.height:0,e),i.pop()});globalThis.utils=Object.assign(globalThis.utils||{},W);})(); /*! @litecanvas/utils by Luiz Bills | MIT Licensed */