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