@litecanvas/utils 0.25.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/all.js CHANGED
@@ -49,12 +49,13 @@
49
49
  ZERO: () => ZERO,
50
50
  advance: () => advance_default,
51
51
  diff: () => diff_default,
52
+ flipImage: () => flip_default,
52
53
  fract: () => fract_default,
53
54
  intersection: () => intersection_default,
54
55
  mod: () => mod_default,
55
56
  range: () => range_default,
56
57
  resolve: () => resolve_default,
57
- scaleImage: () => scale_image_default,
58
+ scaleImage: () => scale_default,
58
59
  tint: () => tint_default,
59
60
  tween: () => tween,
60
61
  vec: () => vec,
@@ -1273,8 +1274,8 @@
1273
1274
  });
1274
1275
  };
1275
1276
 
1276
- // src/image/scale-image.js
1277
- var scale_image_default = (img, factor, pixelart = true, engine = globalThis) => {
1277
+ // src/image/scale.js
1278
+ var scale_default = (img, factor, pixelart = true, engine = globalThis) => {
1278
1279
  return engine.paint(img.width * factor, img.height * factor, (ctx) => {
1279
1280
  engine.push();
1280
1281
  ctx.imageSmoothingEnabled = !pixelart;
@@ -1284,6 +1285,16 @@
1284
1285
  });
1285
1286
  };
1286
1287
 
1288
+ // src/image/flip.js
1289
+ var flip_default = (img, horizontal = true, vertically = false, engine = globalThis) => {
1290
+ return engine.paint(img.width, img.height, (ctx) => {
1291
+ engine.push();
1292
+ engine.scale(horizontal ? -1 : 1, vertically ? -1 : 1);
1293
+ engine.image(horizontal ? -img.width : 0, vertically ? -img.height : 0, img);
1294
+ engine.pop();
1295
+ });
1296
+ };
1297
+
1287
1298
  // src/_web.js
1288
1299
  globalThis.utils = Object.assign(globalThis.utils || {}, src_exports);
1289
1300
  })();
package/dist/all.min.js CHANGED
@@ -1,3 +1,3 @@
1
- (()=>{var lt=Object.defineProperty;var ut=(e,t)=>{for(var s in t)lt(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={};ut(W,{ANCHOR_BOT_LEFT:()=>Nt,ANCHOR_BOT_RIGHT:()=>Bt,ANCHOR_CENTER:()=>Xt,ANCHOR_TOP_LEFT:()=>J,ANCHOR_TOP_RIGHT:()=>Yt,Actor:()=>R,BACK_IN:()=>Gt,BACK_IN_OUT:()=>$t,BACK_OUT:()=>jt,BOUNCE_IN:()=>ot,BOUNCE_IN_OUT:()=>Kt,BOUNCE_OUT:()=>Y,Camera:()=>w,DOWN:()=>Dt,EASE_IN:()=>Ut,EASE_IN_OUT:()=>Vt,EASE_OUT:()=>Ft,ELASTIC_IN:()=>qt,ELASTIC_IN_OUT:()=>zt,ELASTIC_OUT:()=>Zt,Grid:()=>b,LEFT:()=>Rt,LINEAR:()=>rt,Noise:()=>B,ONE:()=>Ct,RIGHT:()=>Pt,TypedGrid:()=>S,UP:()=>Lt,Vector:()=>T,ZERO:()=>K,advance:()=>st,diff:()=>Q,fract:()=>tt,intersection:()=>I,mod:()=>it,range:()=>et,resolve:()=>F,scaleImage:()=>ct,tint:()=>at,tween:()=>Wt,vec:()=>n,vecAbs:()=>It,vecAdd:()=>L,vecAngle:()=>mt,vecAngleBetween:()=>wt,vecCeil:()=>bt,vecClamp:()=>Ot,vecCross:()=>Tt,vecDist:()=>gt,vecDist2:()=>yt,vecDiv:()=>A,vecDot:()=>z,vecEq:()=>C,vecFloor:()=>At,vecIsZero:()=>kt,vecLerp:()=>Et,vecLimit:()=>xt,vecMag:()=>q,vecMag2:()=>Z,vecMove:()=>St,vecMult:()=>E,vecNorm:()=>D,vecRand:()=>Mt,vecReflect:()=>_t,vecRotate:()=>pt,vecRound:()=>Ht,vecSet:()=>$,vecSetMag:()=>dt,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(`
2
- `)}},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,ft=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),pt=(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},_t=(e,t)=>{let s=D(n(t));return P(e,E(s,2*z(e,s)))},dt=(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},xt=(e,t=1)=>{let s=Z(e);return s>t*t&&(A(e,k(s)),E(e,t)),e},gt=(e,t)=>{let s=e.x-t.x,i=e.y-t.y;return k(s*s+i*i)},yt=(e,t)=>{let s=e.x-t.x,i=e.y-t.y;return s*s+i*i},mt=e=>Math.atan2(e.y,e.x),wt=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),z=(e,t)=>e.x*t.x+e.y*t.y,Tt=(e,t)=>e.x*t.y-e.y*t.x,Et=(e,t,s)=>(e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0,e),Mt=(e=1,t=e,s=globalThis.rand||Math.random)=>{let i=s()*ft,r=s()*(t-e)+e;return n(G(i)*r,j(i)*r)},It=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),bt=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),At=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),Ht=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),Ot=(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),St=(e,t,s=1)=>L(e,t.x*s,t.y*s),kt=e=>C(e,K),K=n(0,0),Ct=n(1,1),Lt=n(0,-1),Pt=n(1,0),Dt=n(0,1),Rt=n(-1,0);var Xt=n(.5,.5),J=n(0,0),Yt=n(1,0),Nt=n(0,1),Bt=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,Wt=(e,t,s,i=1,r=rt)=>new X(e,t,s,i,r),rt=e=>e,Ut=e=>e*e,Ft=e=>-e*(e-2),Vt=e=>e<.5?2*e*e:-2*e*e+4*e-1,Gt=e=>e*e*e-e*Math.sin(e*Math.PI),jt=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},$t=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},qt=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,zt=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,Kt=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,Jt=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+=Jt,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()});globalThis.utils=Object.assign(globalThis.utils||{},W);})();
1
+ (()=>{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(`
2
+ `)}},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);})();
3
3
  /*! @litecanvas/utils by Luiz Bills | MIT Licensed */
package/dist/image.js CHANGED
@@ -21,8 +21,8 @@
21
21
  });
22
22
  };
23
23
 
24
- // src/image/scale-image.js
25
- var scale_image_default = (img, factor, pixelart = true, engine = globalThis) => {
24
+ // src/image/scale.js
25
+ var scale_default = (img, factor, pixelart = true, engine = globalThis) => {
26
26
  return engine.paint(img.width * factor, img.height * factor, (ctx) => {
27
27
  engine.push();
28
28
  ctx.imageSmoothingEnabled = !pixelart;
@@ -32,9 +32,20 @@
32
32
  });
33
33
  };
34
34
 
35
+ // src/image/flip.js
36
+ var flip_default = (img, horizontal = true, vertically = false, engine = globalThis) => {
37
+ return engine.paint(img.width, img.height, (ctx) => {
38
+ engine.push();
39
+ engine.scale(horizontal ? -1 : 1, vertically ? -1 : 1);
40
+ engine.image(horizontal ? -img.width : 0, vertically ? -img.height : 0, img);
41
+ engine.pop();
42
+ });
43
+ };
44
+
35
45
  // src/image/_web.js
36
46
  globalThis.utils = Object.assign(globalThis.utils || {}, {
37
47
  tint: tint_default,
38
- scaleImage: scale_image_default
48
+ scaleImage: scale_default,
49
+ flipImage: flip_default
39
50
  });
40
51
  })();
package/dist/image.min.js CHANGED
@@ -1 +1 @@
1
- (()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let t in globalThis.utils)t!=="global"&&(globalThis[t]=globalThis.utils[t])};var n=(t,a,o=1,e=globalThis)=>e.paint(t.width,t.height,l=>{e.push(),e.alpha(o),e.rectfill(0,0,t.width,t.height,a),l.globalCompositeOperation="destination-atop",e.alpha(1),e.image(0,0,t),e.pop()});var i=(t,a,o=!0,e=globalThis)=>e.paint(t.width*a,t.height*a,l=>{e.push(),l.imageSmoothingEnabled=!o,e.scale(a),e.image(0,0,t),e.pop()});globalThis.utils=Object.assign(globalThis.utils||{},{tint:n,scaleImage:i});})();
1
+ (()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let t in globalThis.utils)t!=="global"&&(globalThis[t]=globalThis.utils[t])};var s=(t,a,o=1,e=globalThis)=>e.paint(t.width,t.height,l=>{e.push(),e.alpha(o),e.rectfill(0,0,t.width,t.height,a),l.globalCompositeOperation="destination-atop",e.alpha(1),e.image(0,0,t),e.pop()});var n=(t,a,o=!0,e=globalThis)=>e.paint(t.width*a,t.height*a,l=>{e.push(),l.imageSmoothingEnabled=!o,e.scale(a),e.image(0,0,t),e.pop()});var i=(t,a=!0,o=!1,e=globalThis)=>e.paint(t.width,t.height,l=>{e.push(),e.scale(a?-1:1,o?-1:1),e.image(a?-t.width:0,o?-t.height:0,t),e.pop()});globalThis.utils=Object.assign(globalThis.utils||{},{tint:s,scaleImage:n,flipImage:i});})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litecanvas/utils",
3
- "version": "0.25.0",
3
+ "version": "0.26.0",
4
4
  "description": "Utilities to help build litecanvas games",
5
5
  "author": "Luiz Bills <luizbills@pm.me>",
6
6
  "license": "MIT",
package/src/image/_web.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import "../_global.js"
2
2
  import tint from "./tint.js"
3
- import scaleImage from "./scale-image.js"
3
+ import scaleImage from "./scale.js"
4
+ import flipImage from "./flip.js"
4
5
 
5
6
  globalThis.utils = Object.assign(globalThis.utils || {}, {
6
7
  tint,
7
8
  scaleImage,
9
+ flipImage,
8
10
  })
@@ -0,0 +1,23 @@
1
+ import "litecanvas"
2
+
3
+ /**
4
+ * Flip a image with a color and optional opacity.
5
+ *
6
+ * @param {HTMLImageElement|HTMLCanvasElement|OffscreenCanvas} img
7
+ * @param {boolean} [horizontal=true]
8
+ * @param {boolean} [vertically=false]
9
+ * @param {LitecanvasInstance} [engine]
10
+ */
11
+ export default (
12
+ img,
13
+ horizontal = true,
14
+ vertically = false,
15
+ engine = globalThis
16
+ ) => {
17
+ return engine.paint(img.width, img.height, (ctx) => {
18
+ engine.push()
19
+ engine.scale(horizontal ? -1 : 1, vertically ? -1 : 1)
20
+ engine.image(horizontal ? -img.width : 0, vertically ? -img.height : 0, img)
21
+ engine.pop()
22
+ })
23
+ }
package/src/index.js CHANGED
@@ -8,4 +8,5 @@ export * from "./math/index.js"
8
8
  export * from "./tween/index.js"
9
9
  export * from "./noise/index.js"
10
10
  export { default as tint } from "./image/tint.js"
11
- export { default as scaleImage } from "./image/scale-image.js"
11
+ export { default as scaleImage } from "./image/scale.js"
12
+ export { default as flipImage } from "./image/flip.js"
File without changes