UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

2 lines (1 loc) 2.16 kB
(()=>{var b=Object.defineProperty;var d=(e,t)=>{for(var s in t)b(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 h={};d(h,{ANCHOR_BOT_LEFT:()=>_,ANCHOR_BOT_RIGHT:()=>T,ANCHOR_CENTER:()=>g,ANCHOR_TOP_LEFT:()=>m,ANCHOR_TOP_RIGHT:()=>M,Actor:()=>a});var E=2*Math.PI,x=parseFloat,n=class{constructor(t=0,s=t){this.x=x(t)||0,this.y=x(s)||0}toString(){return`Vector (${this.x}, ${this.y})`}};var f=e=>e instanceof n,o=(e=0,t=e)=>(f(e)&&(t=e.y,e=e.x),new n(e,t));var y=(e,t,s,r,i,c,l)=>{let u=i-Math.max(e,Math.min(i,e+s)),p=c-Math.max(t,Math.min(c,t+r));return u*u+p*p<=l*l};var g=o(.5,.5),m=o(0,0),M=o(1,0),_=o(0,1),T=o(1,1),a=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,r=m){this.sprite=t||{width:0,height:0},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,c=this.pos.y-r*this.anchor.y;return[i,c,s,r]}in(t,s,r=1){return y(...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),c=-this.sprite.height*(this.flipY?1-r.y:r.y);s&&t.alpha(this.opacity),t.image(i,c,this.sprite)}};globalThis.utils=Object.assign(globalThis.utils||{},h);})();