@litecanvas/utils 0.15.0 → 0.17.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/actor.js CHANGED
@@ -74,6 +74,10 @@
74
74
  _o;
75
75
  /** @type {Vector} The actor scale */
76
76
  _s;
77
+ /** @type {boolean} */
78
+ flipX = false;
79
+ /** @type {boolean} */
80
+ flipY = false;
77
81
  /** @type {number} The actor angle (in radians) */
78
82
  angle = 0;
79
83
  /** @type {number} The actor opacity */
@@ -194,16 +198,20 @@
194
198
  transform(litecanvas) {
195
199
  litecanvas.translate(this.pos.x, this.pos.y);
196
200
  litecanvas.rotate(this.angle);
197
- litecanvas.scale(this._s.x, this._s.y);
201
+ litecanvas.scale(
202
+ (this.flipX ? -1 : 1) * this._s.x,
203
+ (this.flipY ? -1 : 1) * this._s.y
204
+ );
198
205
  }
199
206
  /**
200
207
  * @param {LitecanvasInstance} litecanvas
201
208
  */
202
209
  drawImage(litecanvas, alpha = true) {
203
- const anchorX = this.sprite.width * this.anchor.x;
204
- const anchorY = this.sprite.height * this.anchor.y;
210
+ const anchor = this.anchor;
211
+ const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x);
212
+ const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y);
205
213
  if (alpha) litecanvas.alpha(this.opacity);
206
- litecanvas.image(-anchorX, -anchorY, this.sprite);
214
+ litecanvas.image(x, y, this.sprite);
207
215
  }
208
216
  };
209
217
 
package/dist/actor.min.js CHANGED
@@ -1 +1 @@
1
- (()=>{var p=Object.defineProperty;var u=(o,t)=>{for(var e in t)p(o,e,{get:t[e],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let o in globalThis.utils)o!=="global"&&(globalThis[o]=globalThis.utils[o])};var c={};u(c,{ANCHOR_BOT_LEFT:()=>x,ANCHOR_BOT_RIGHT:()=>g,ANCHOR_CENTER:()=>d,ANCHOR_TOP_LEFT:()=>l,ANCHOR_TOP_RIGHT:()=>y,Actor:()=>a});var T=2*Math.PI,n=class{x;y;constructor(t=0,e=t){this.x=t,this.y=e}toString(){return`Vector (${this.x}, ${this.y})`}},h=o=>o instanceof n,s=(o=0,t=o)=>(h(o)&&(t=o.y,o=o.x),new n(o,t));globalThis.zzfxV=1;var d=s(.5,.5),l=s(0,0),y=s(1,0),x=s(0,1),g=s(1,1),a=class{sprite;pos;_o;_s;angle=0;opacity=1;hidden=!1;constructor(t,e,r=l){this.sprite=t,this.pos=e||s(0),this._o=s(r),this._s=s(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,e=t){this._s.x=t,this._s.y=e}scaleBy(t,e=t){this._s.x*=t,this._s.y*=e}getBounds(t=!0){let e=this.sprite.width*(t?this._s.x:1),r=this.sprite.height*(t?this._s.y:1),i=this.pos.x-e*this.anchor.x,f=this.pos.y-r*this.anchor.y;return[i,f,e,r]}draw(t=globalThis,e=!0){this.hidden||this.opacity<=0||(e&&t.push(),this.transform(t),this.drawImage(t),e&&t.pop())}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(this.angle),t.scale(this._s.x,this._s.y)}drawImage(t,e=!0){let r=this.sprite.width*this.anchor.x,i=this.sprite.height*this.anchor.y;e&&t.alpha(this.opacity),t.image(-r,-i,this.sprite)}};globalThis.utils=Object.assign(globalThis.utils||{},c);})();
1
+ (()=>{var p=Object.defineProperty;var u=(o,t)=>{for(var e in t)p(o,e,{get:t[e],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let o in globalThis.utils)o!=="global"&&(globalThis[o]=globalThis.utils[o])};var l={};u(l,{ANCHOR_BOT_LEFT:()=>x,ANCHOR_BOT_RIGHT:()=>g,ANCHOR_CENTER:()=>y,ANCHOR_TOP_LEFT:()=>f,ANCHOR_TOP_RIGHT:()=>d,Actor:()=>c});var T=2*Math.PI,n=class{x;y;constructor(t=0,e=t){this.x=t,this.y=e}toString(){return`Vector (${this.x}, ${this.y})`}},h=o=>o instanceof n,r=(o=0,t=o)=>(h(o)&&(t=o.y,o=o.x),new n(o,t));globalThis.zzfxV=1;var y=r(.5,.5),f=r(0,0),d=r(1,0),x=r(0,1),g=r(1,1),c=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,e,s=f){this.sprite=t,this.pos=e||r(0),this._o=r(s),this._s=r(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,e=t){this._s.x=t,this._s.y=e}scaleBy(t,e=t){this._s.x*=t,this._s.y*=e}getBounds(t=!0){let e=this.sprite.width*(t?this._s.x:1),s=this.sprite.height*(t?this._s.y:1),i=this.pos.x-e*this.anchor.x,a=this.pos.y-s*this.anchor.y;return[i,a,e,s]}draw(t=globalThis,e=!0){this.hidden||this.opacity<=0||(e&&t.push(),this.transform(t),this.drawImage(t),e&&t.pop())}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(this.angle),t.scale((this.flipX?-1:1)*this._s.x,(this.flipY?-1:1)*this._s.y)}drawImage(t,e=!0){let s=this.anchor,i=-this.sprite.width*(this.flipX?1-s.x:s.x),a=-this.sprite.height*(this.flipY?1-s.y:s.y);e&&t.alpha(this.opacity),t.image(i,a,this.sprite)}};globalThis.utils=Object.assign(globalThis.utils||{},l);})();
package/dist/all.js CHANGED
@@ -338,6 +338,23 @@
338
338
  this._h = Math.max(1, ~~height);
339
339
  this._c = values;
340
340
  }
341
+ [Symbol.iterator]() {
342
+ let i = 0;
343
+ return {
344
+ next: () => {
345
+ return {
346
+ value: [this.indexToPointX(i), this.indexToPointY(i), this._c[i++]],
347
+ done: i === this._c.length
348
+ };
349
+ }
350
+ };
351
+ }
352
+ /**
353
+ * @returns {Grid} the cloned grid
354
+ */
355
+ clone() {
356
+ return new _Grid(this._w, this._h, this._c);
357
+ }
341
358
  /**
342
359
  * Delete all cell values.
343
360
  */
@@ -388,6 +405,16 @@
388
405
  has(x, y) {
389
406
  return this.get(x, y) != null;
390
407
  }
408
+ /**
409
+ * Checks if a which point (x, y) is within the grid.
410
+ *
411
+ * @param {number} x
412
+ * @param {number} y
413
+ * @returns {boolean}
414
+ */
415
+ check(x, y) {
416
+ return x >= 0 && x < this._w && y >= 0 && y < this._h;
417
+ }
391
418
  /**
392
419
  * Returns the total of cells.
393
420
  *
@@ -453,12 +480,6 @@
453
480
  this.set(x, y, value);
454
481
  });
455
482
  }
456
- /**
457
- * @returns {Grid} the cloned grid
458
- */
459
- clone() {
460
- return _Grid.fromArray(this._w, this._h, this._c);
461
- }
462
483
  /**
463
484
  * @param {number} y
464
485
  * @returns {number}
@@ -720,6 +741,10 @@
720
741
  _o;
721
742
  /** @type {Vector} The actor scale */
722
743
  _s;
744
+ /** @type {boolean} */
745
+ flipX = false;
746
+ /** @type {boolean} */
747
+ flipY = false;
723
748
  /** @type {number} The actor angle (in radians) */
724
749
  angle = 0;
725
750
  /** @type {number} The actor opacity */
@@ -840,16 +865,20 @@
840
865
  transform(litecanvas) {
841
866
  litecanvas.translate(this.pos.x, this.pos.y);
842
867
  litecanvas.rotate(this.angle);
843
- litecanvas.scale(this._s.x, this._s.y);
868
+ litecanvas.scale(
869
+ (this.flipX ? -1 : 1) * this._s.x,
870
+ (this.flipY ? -1 : 1) * this._s.y
871
+ );
844
872
  }
845
873
  /**
846
874
  * @param {LitecanvasInstance} litecanvas
847
875
  */
848
876
  drawImage(litecanvas, alpha = true) {
849
- const anchorX = this.sprite.width * this.anchor.x;
850
- const anchorY = this.sprite.height * this.anchor.y;
877
+ const anchor = this.anchor;
878
+ const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x);
879
+ const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y);
851
880
  if (alpha) litecanvas.alpha(this.opacity);
852
- litecanvas.image(-anchorX, -anchorY, this.sprite);
881
+ litecanvas.image(x, y, this.sprite);
853
882
  }
854
883
  };
855
884
 
package/dist/all.min.js CHANGED
@@ -1,3 +1,3 @@
1
- (()=>{var J=Object.defineProperty;var Q=(t,e)=>{for(var s in e)J(t,s,{get:e[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let t in globalThis.utils)t!=="global"&&(globalThis[t]=globalThis.utils[t])};var k={};Q(k,{ANCHOR_BOT_LEFT:()=>bt,ANCHOR_BOT_RIGHT:()=>At,ANCHOR_CENTER:()=>Mt,ANCHOR_TOP_LEFT:()=>G,ANCHOR_TOP_RIGHT:()=>It,Actor:()=>C,BACK_IN:()=>St,BACK_IN_OUT:()=>Pt,BACK_OUT:()=>kt,BOUNCE_IN:()=>Z,BOUNCE_IN_OUT:()=>Bt,BOUNCE_OUT:()=>S,Camera:()=>u,DOWN:()=>wt,EASE_IN:()=>Ot,EASE_IN_OUT:()=>Dt,EASE_OUT:()=>Ct,ELASTIC_IN:()=>Lt,ELASTIC_IN_OUT:()=>Xt,ELASTIC_OUT:()=>Rt,Grid:()=>g,LEFT:()=>Et,LINEAR:()=>K,ONE:()=>yt,RIGHT:()=>Tt,TypedGrid:()=>M,UP:()=>mt,Vector:()=>p,ZERO:()=>F,advance:()=>q,diff:()=>V,fract:()=>z,intersection:()=>d,range:()=>$,resolve:()=>L,tween:()=>Ht,vec:()=>n,vecAbs:()=>ft,vecAdd:()=>A,vecAngle:()=>nt,vecAngleBetween:()=>ht,vecCeil:()=>ut,vecClamp:()=>xt,vecCross:()=>at,vecDist:()=>rt,vecDist2:()=>ot,vecDiv:()=>y,vecDot:()=>N,vecEq:()=>b,vecFloor:()=>pt,vecIsZero:()=>gt,vecLerp:()=>ct,vecLimit:()=>it,vecMag:()=>U,vecMag2:()=>W,vecMove:()=>dt,vecMult:()=>_,vecNorm:()=>O,vecRand:()=>lt,vecReflect:()=>et,vecRotate:()=>tt,vecRound:()=>_t,vecSet:()=>Y,vecSetMag:()=>st,vecSub:()=>H,wave:()=>j});var d=(t,e,s,i,r,o,h,a)=>{let c=Math.max(t,r),P=Math.min(t+s,r+h)-c,x=Math.max(e,o),T=Math.min(e+i,o+a)-x;return[c,x,P,T]};var L=(t,e,s,i,r,o,h,a)=>{let[c,P,x,T]=d(t,e,s,i,r,o,h,a),f="",w=t,E=e;return x<T?t<r?(f="right",w=r-s):(f="left",w=r+h):e<o?(f="bottom",E=o-i):(f="top",E=o+a),{direction:f,x:w,y:E}};var u=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(e=null,s=0,i=0,r=null,o=null){this._engine=e||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(e,s){this.width=e,this.height=s,this._engine.emit("camera-resized",this)}start(e=!1){this._engine.push(),e&&this._engine.cliprect(this.ox,this.oy,this.width,this.height);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(e,s){this.x=e,this.y=s}move(e,s){this.x+=e,this.y+=s}zoom(e){this.scale*=e}zoomTo(e){this.scale=e}rotate(e){this.rotation+=e}rotateTo(e){this.rotation=e}getWorldPoint(e,s,i={}){let r=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return e=(e-this.width/2-this.ox)/this.scale,s=(s-this.height/2-this.oy)/this.scale,i.x=r*e-o*s+this.x,i.y=o*e+r*s+this.y,i}getCameraPoint(e,s,i={}){let r=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return e=e-this.x,s=s-this.y,e=r*e-o*s,s=o*e+r*s,i.x=e*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(e,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(e,s,i,r,o,h,a,c)}shake(e=1,s=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",i=>{this._shake.x=this._engine.randi(-e,e),this._shake.y=this._engine.randi(-e,e),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 g=class t{_w;_h;_c;constructor(e,s,i=[]){this._w=Math.max(1,~~e),this._h=Math.max(1,~~s),this._c=i}clear(){this.forEach((e,s)=>this.set(e,s,void 0))}get width(){return this._w}get height(){return this._h}set(e,s,i){this._c[this.pointToIndex(e,s)]=i}get(e,s){return this._c[this.pointToIndex(e,s)]}has(e,s){return this.get(e,s)!=null}get length(){return this._w*this._h}pointToIndex(e,s){return this.clampX(~~e)+this.clampY(~~s)*this._w}indexToPointX(e){return e%this._w}indexToPointY(e){return Math.floor(e/this._w)}forEach(e,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(e(h,a,c,this)===!1)break;i+=o}}fill(e){this.forEach((s,i)=>{this.set(s,i,e)})}clone(){return t.fromArray(this._w,this._h,this._c)}clampX(e){return R(e,0,this._w-1)}clampY(e){return R(e,0,this._h-1)}toArray(){return this._c.slice()}toString(e=" ",s=!0){if(!s)return this._c.join(e);let i=[];return this.forEach((r,o,h)=>{i[o]=i[o]||"",i[o]+=h+e}),i.join(`
2
- `)}},M=class t extends g{constructor(e,s,i=Uint8Array){super(e,s,null),this._c=new i(this._w*this._h)}has(e,s){return this.get(e,s)!==0}clone(){let e=new t(this._w,this._h,this._c.constructor);return this.forEach((s,i,r)=>{e.set(s,i,r)}),e}};function R(t,e,s){return t<e?e:t>s?s:t}var I=Math.sqrt,X=Math.cos,B=Math.sin,v=2*Math.PI,p=class{x;y;constructor(e=0,s=e){this.x=e,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},l=t=>t instanceof p,n=(t=0,e=t)=>(l(t)&&(e=t.y,t=t.x),new p(t,e)),b=(t,e,s=e)=>l(e)?b(t,e.x,e.y):t.x===e&&t.y===s,Y=(t,e,s=e)=>(l(e)?Y(t,e.x,e.y):(t.x=e,t.y=s),t),A=(t,e,s=e)=>l(e)?A(t,e.x,e.y):(t.x+=e,t.y+=s,t),H=(t,e,s=e)=>l(e)?H(t,e.x,e.y):(t.x-=e,t.y-=s,t),_=(t,e,s=e)=>l(e)?_(t,e.x,e.y):(t.x*=e,t.y*=s,t),y=(t,e,s=e)=>l(e)?y(t,e.x,e.y):(t.x/=e||1,t.y/=s||1,t),tt=(t,e)=>{let s=X(e),i=B(e);return t.x=s*t.x-i*t.y,t.y=i*t.x+s*t.y,t},et=(t,e)=>{let s=O(n(e));return H(t,_(s,2*N(t,s)))},st=(t,e)=>(O(t),_(t,e),t),U=t=>I(t.x*t.x+t.y*t.y),W=t=>t.x*t.x+t.y*t.y,O=t=>{let e=U(t);return e>0&&y(t,e),t},it=(t,e=1)=>{let s=W(t);return s>e*e&&(y(t,I(s)),_(t,e)),t},rt=(t,e)=>{let s=t.x-e.x,i=t.y-e.y;return I(s*s+i*i)},ot=(t,e)=>{let s=t.x-e.x,i=t.y-e.y;return s*s+i*i},nt=t=>Math.atan2(t.y,t.x),ht=(t,e)=>Math.atan2(e.y-t.y,e.x-t.x),N=(t,e)=>t.x*e.x+t.y*e.y,at=(t,e)=>t.x*e.y-t.y*e.x,ct=(t,e,s)=>(t.x+=(e.x-t.x)*s||0,t.y+=(e.y-t.y)*s||0,t),lt=(t=1,e=t,s=globalThis.rand||Math.random)=>{let i=s()*v,r=s()*(e-t)+t;return n(X(i)*r,B(i)*r)},ft=t=>(t.x=Math.abs(t.x),t.y=Math.abs(t.y),t),ut=t=>(t.x=Math.ceil(t.x),t.y=Math.ceil(t.y),t),pt=t=>(t.x=Math.floor(t.x),t.y=Math.floor(t.y),t),_t=t=>(t.x=Math.round(t.x),t.y=Math.round(t.y),t),xt=(t,e,s)=>(t.x<e.x&&(t.x=e.x),t.x>s.x&&(t.x=s.x),t.y<e.y&&(t.y=e.y),t.y>s.y&&(t.y=s.y),t),dt=(t,e,s=1)=>A(t,e.x*s,e.y*s),gt=t=>b(t,F),F=n(0,0),yt=n(1,1),mt=n(0,-1),Tt=n(1,0),wt=n(0,1),Et=n(-1,0);globalThis.zzfxV=1;var Mt=n(.5,.5),G=n(0,0),It=n(1,0),bt=n(0,1),At=n(1,1),C=class{sprite;pos;_o;_s;angle=0;opacity=1;hidden=!1;constructor(e,s,i=G){this.sprite=e,this.pos=s||n(0),this._o=n(i),this._s=n(1,1)}set x(e){this.pos.x=e}get x(){return this.pos.x}set y(e){this.pos.y=e}get y(){return this.pos.y}set anchor(e){this._o.x=e.x,this._o.y=e.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(e,s=e){this._s.x=e,this._s.y=s}scaleBy(e,s=e){this._s.x*=e,this._s.y*=s}getBounds(e=!0){let s=this.sprite.width*(e?this._s.x:1),i=this.sprite.height*(e?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(e=globalThis,s=!0){this.hidden||this.opacity<=0||(s&&e.push(),this.transform(e),this.drawImage(e),s&&e.pop())}transform(e){e.translate(this.pos.x,this.pos.y),e.rotate(this.angle),e.scale(this._s.x,this._s.y)}drawImage(e,s=!0){let i=this.sprite.width*this.anchor.x,r=this.sprite.height*this.anchor.y;s&&e.alpha(this.opacity),e.image(-i,-r,this.sprite)}};var V=(t,e)=>Math.abs(e-t)||0;var j=(t,e,s,i=Math.sin)=>t+(i(s)+1)/2*(e-t);var z=t=>t%1||0;var $=t=>Array.from(Array(t).keys());var q=advance=(t,e,s,i=1)=>{s&&(e.x+=s.x*i,e.y+=s.y*i),t.x+=e.x*i,t.y+=e.y*i};var m=Math.PI/2,Ht=(t,e,s,i=1,r=K)=>new D(t,e,s,i,r),K=t=>t,Ot=t=>t*t,Ct=t=>-t*(t-2),Dt=t=>t<.5?2*t*t:-2*t*t+4*t-1,St=t=>t*t*t-t*Math.sin(t*Math.PI),kt=t=>{let e=1-t;return 1-(e*e*e-e*Math.sin(e*Math.PI))},Pt=t=>{if(t<.5){let s=2*t;return .5*(s*s*s-s*Math.sin(s*Math.PI))}let e=1-(2*t-1);return .5*(1-(e*e*e-e*Math.sin(t*Math.PI)))+.5},Lt=t=>Math.sin(13*m*t)*Math.pow(2,10*(t-1)),Rt=t=>Math.sin(-13*m*(t+1))*Math.pow(2,-10*t)+1,Xt=t=>{if(t<.5){let i=Math.sin(13*m*(2*t)),r=Math.pow(2,10*(2*t-1));return .5*i*r}let e=Math.sin(-13*m*(2*t-1+1)),s=Math.pow(2,-10*(2*t-1));return .5*(e*s+2)},Z=t=>1-S(1-t),S=t=>t<4/11?121*t*t/16:t<8/11?363/40*t*t-99/10*t+17/5:t<9/10?4356/361*t*t-35442/1805*t+16061/1805:54/5*t*t-513/25*t+268/25,Bt=t=>t<.5?.5*Z(t*2):.5*S(t*2-1)+.5,D=class{running=!1;_o;_p;_x;_d;_e;_cb=[];_t=0;_u=0;_lc;constructor(e,s,i,r,o){this._o=e,this._p=s,this._x=i,this._d=r,this._e=o}start(e=globalThis){this.running||this.stop();let s=this._o[this._p]||0;return this._lc=e,this._u=e.listen("update",i=>{this._o[this._p]=e.lerp(s,this._x,this._e(this._t/this._d)),this._t+=i,this._t>=this._d&&(this._o[this._p]=this._x,this.stop())}),this.running=!0,this}onEnd(e){this._cb.push(e)}stop(e=!0){if(!(!this.running||!this._u)&&(this.running=!1,this._u(),e))for(let s of this._cb)s(this._o)}reset(){this._cb.length=0,this.stop()}get progress(){return this.running?this._t/this._d:0}};globalThis.utils=Object.assign(globalThis.utils||{},k);})();
1
+ (()=>{var J=Object.defineProperty;var Q=(e,t)=>{for(var s in t)J(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 D={};Q(D,{ANCHOR_BOT_LEFT:()=>bt,ANCHOR_BOT_RIGHT:()=>At,ANCHOR_CENTER:()=>Mt,ANCHOR_TOP_LEFT:()=>G,ANCHOR_TOP_RIGHT:()=>It,Actor:()=>C,BACK_IN:()=>kt,BACK_IN_OUT:()=>Pt,BACK_OUT:()=>Dt,BOUNCE_IN:()=>Z,BOUNCE_IN_OUT:()=>Yt,BOUNCE_OUT:()=>k,Camera:()=>u,DOWN:()=>wt,EASE_IN:()=>Ot,EASE_IN_OUT:()=>St,EASE_OUT:()=>Ct,ELASTIC_IN:()=>Lt,ELASTIC_IN_OUT:()=>Rt,ELASTIC_OUT:()=>Xt,Grid:()=>g,LEFT:()=>Et,LINEAR:()=>K,ONE:()=>yt,RIGHT:()=>Tt,TypedGrid:()=>M,UP:()=>mt,Vector:()=>p,ZERO:()=>F,advance:()=>q,diff:()=>V,fract:()=>z,intersection:()=>d,range:()=>$,resolve:()=>L,tween:()=>Ht,vec:()=>n,vecAbs:()=>ft,vecAdd:()=>A,vecAngle:()=>nt,vecAngleBetween:()=>ht,vecCeil:()=>ut,vecClamp:()=>xt,vecCross:()=>at,vecDist:()=>rt,vecDist2:()=>ot,vecDiv:()=>y,vecDot:()=>N,vecEq:()=>b,vecFloor:()=>pt,vecIsZero:()=>gt,vecLerp:()=>ct,vecLimit:()=>it,vecMag:()=>U,vecMag2:()=>W,vecMove:()=>dt,vecMult:()=>_,vecNorm:()=>O,vecRand:()=>lt,vecReflect:()=>et,vecRotate:()=>tt,vecRound:()=>_t,vecSet:()=>B,vecSetMag:()=>st,vecSub:()=>H,wave:()=>j});var d=(e,t,s,i,r,o,h,a)=>{let c=Math.max(e,r),P=Math.min(e+s,r+h)-c,x=Math.max(t,o),T=Math.min(t+i,o+a)-x;return[c,x,P,T]};var L=(e,t,s,i,r,o,h,a)=>{let[c,P,x,T]=d(e,t,s,i,r,o,h,a),f="",w=e,E=t;return x<T?e<r?(f="right",w=r-s):(f="left",w=r+h):t<o?(f="bottom",E=o-i):(f="top",E=o+a),{direction:f,x:w,y:E}};var u=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){this._engine.push(),t&&this._engine.cliprect(this.ox,this.oy,this.width,this.height);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 g=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 X(t,0,this._w-1)}clampY(t){return X(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
+ `)}},M=class e extends g{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 X(e,t,s){return e<t?t:e>s?s:e}var I=Math.sqrt,R=Math.cos,Y=Math.sin,v=2*Math.PI,p=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},l=e=>e instanceof p,n=(e=0,t=e)=>(l(e)&&(t=e.y,e=e.x),new p(e,t)),b=(e,t,s=t)=>l(t)?b(e,t.x,t.y):e.x===t&&e.y===s,B=(e,t,s=t)=>(l(t)?B(e,t.x,t.y):(e.x=t,e.y=s),e),A=(e,t,s=t)=>l(t)?A(e,t.x,t.y):(e.x+=t,e.y+=s,e),H=(e,t,s=t)=>l(t)?H(e,t.x,t.y):(e.x-=t,e.y-=s,e),_=(e,t,s=t)=>l(t)?_(e,t.x,t.y):(e.x*=t,e.y*=s,e),y=(e,t,s=t)=>l(t)?y(e,t.x,t.y):(e.x/=t||1,e.y/=s||1,e),tt=(e,t)=>{let s=R(t),i=Y(t);return e.x=s*e.x-i*e.y,e.y=i*e.x+s*e.y,e},et=(e,t)=>{let s=O(n(t));return H(e,_(s,2*N(e,s)))},st=(e,t)=>(O(e),_(e,t),e),U=e=>I(e.x*e.x+e.y*e.y),W=e=>e.x*e.x+e.y*e.y,O=e=>{let t=U(e);return t>0&&y(e,t),e},it=(e,t=1)=>{let s=W(e);return s>t*t&&(y(e,I(s)),_(e,t)),e},rt=(e,t)=>{let s=e.x-t.x,i=e.y-t.y;return I(s*s+i*i)},ot=(e,t)=>{let s=e.x-t.x,i=e.y-t.y;return s*s+i*i},nt=e=>Math.atan2(e.y,e.x),ht=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),N=(e,t)=>e.x*t.x+e.y*t.y,at=(e,t)=>e.x*t.y-e.y*t.x,ct=(e,t,s)=>(e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0,e),lt=(e=1,t=e,s=globalThis.rand||Math.random)=>{let i=s()*v,r=s()*(t-e)+e;return n(R(i)*r,Y(i)*r)},ft=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),ut=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),pt=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),_t=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),xt=(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),dt=(e,t,s=1)=>A(e,t.x*s,t.y*s),gt=e=>b(e,F),F=n(0,0),yt=n(1,1),mt=n(0,-1),Tt=n(1,0),wt=n(0,1),Et=n(-1,0);globalThis.zzfxV=1;var Mt=n(.5,.5),G=n(0,0),It=n(1,0),bt=n(0,1),At=n(1,1),C=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,i=G){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(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 V=(e,t)=>Math.abs(t-e)||0;var j=(e,t,s,i=Math.sin)=>e+(i(s)+1)/2*(t-e);var z=e=>e%1||0;var $=e=>Array.from(Array(e).keys());var q=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 m=Math.PI/2,Ht=(e,t,s,i=1,r=K)=>new S(e,t,s,i,r),K=e=>e,Ot=e=>e*e,Ct=e=>-e*(e-2),St=e=>e<.5?2*e*e:-2*e*e+4*e-1,kt=e=>e*e*e-e*Math.sin(e*Math.PI),Dt=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},Pt=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},Lt=e=>Math.sin(13*m*e)*Math.pow(2,10*(e-1)),Xt=e=>Math.sin(-13*m*(e+1))*Math.pow(2,-10*e)+1,Rt=e=>{if(e<.5){let i=Math.sin(13*m*(2*e)),r=Math.pow(2,10*(2*e-1));return .5*i*r}let t=Math.sin(-13*m*(2*e-1+1)),s=Math.pow(2,-10*(2*e-1));return .5*(t*s+2)},Z=e=>1-k(1-e),k=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,Yt=e=>e<.5?.5*Z(e*2):.5*k(e*2-1)+.5,S=class{running=!1;_o;_p;_x;_d;_e;_cb=[];_t=0;_u=0;_lc;constructor(t,s,i,r,o){this._o=t,this._p=s,this._x=i,this._d=r,this._e=o}start(t=globalThis){this.running||this.stop();let s=this._o[this._p]||0;return this._lc=t,this._u=t.listen("update",i=>{this._o[this._p]=t.lerp(s,this._x,this._e(this._t/this._d)),this._t+=i,this._t>=this._d&&(this._o[this._p]=this._x,this.stop())}),this.running=!0,this}onEnd(t){this._cb.push(t)}stop(t=!0){if(!(!this.running||!this._u)&&(this.running=!1,this._u(),t))for(let s of this._cb)s(this._o)}reset(){this._cb.length=0,this.stop()}get progress(){return this.running?this._t/this._d:0}};globalThis.utils=Object.assign(globalThis.utils||{},D);})();
3
3
  /*! @litecanvas/utils by Luiz Bills | MIT Licensed */
package/dist/grid.js CHANGED
@@ -36,6 +36,23 @@
36
36
  this._h = Math.max(1, ~~height);
37
37
  this._c = values;
38
38
  }
39
+ [Symbol.iterator]() {
40
+ let i = 0;
41
+ return {
42
+ next: () => {
43
+ return {
44
+ value: [this.indexToPointX(i), this.indexToPointY(i), this._c[i++]],
45
+ done: i === this._c.length
46
+ };
47
+ }
48
+ };
49
+ }
50
+ /**
51
+ * @returns {Grid} the cloned grid
52
+ */
53
+ clone() {
54
+ return new _Grid(this._w, this._h, this._c);
55
+ }
39
56
  /**
40
57
  * Delete all cell values.
41
58
  */
@@ -86,6 +103,16 @@
86
103
  has(x, y) {
87
104
  return this.get(x, y) != null;
88
105
  }
106
+ /**
107
+ * Checks if a which point (x, y) is within the grid.
108
+ *
109
+ * @param {number} x
110
+ * @param {number} y
111
+ * @returns {boolean}
112
+ */
113
+ check(x, y) {
114
+ return x >= 0 && x < this._w && y >= 0 && y < this._h;
115
+ }
89
116
  /**
90
117
  * Returns the total of cells.
91
118
  *
@@ -151,12 +178,6 @@
151
178
  this.set(x, y, value);
152
179
  });
153
180
  }
154
- /**
155
- * @returns {Grid} the cloned grid
156
- */
157
- clone() {
158
- return _Grid.fromArray(this._w, this._h, this._c);
159
- }
160
181
  /**
161
182
  * @param {number} y
162
183
  * @returns {number}
package/dist/grid.min.js CHANGED
@@ -1,2 +1,2 @@
1
- (()=>{var g=Object.defineProperty;var f=(h,t)=>{for(var i in t)g(h,i,{get:t[i],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let h in globalThis.utils)h!=="global"&&(globalThis[h]=globalThis.utils[h])};var c={};f(c,{Grid:()=>o,TypedGrid:()=>l});var o=class h{_w;_h;_c;constructor(t,i,s=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~i),this._c=s}clear(){this.forEach((t,i)=>this.set(t,i,void 0))}get width(){return this._w}get height(){return this._h}set(t,i,s){this._c[this.pointToIndex(t,i)]=s}get(t,i){return this._c[this.pointToIndex(t,i)]}has(t,i){return this.get(t,i)!=null}get length(){return this._w*this._h}pointToIndex(t,i){return this.clampX(~~t)+this.clampY(~~i)*this._w}indexToPointX(t){return t%this._w}indexToPointY(t){return Math.floor(t/this._w)}forEach(t,i=!1){let s=i?this.length-1:0,r=i?-1:this.length,n=i?-1:1;for(;s!==r;){let e=this.indexToPointX(s),a=this.indexToPointY(s),_=this._c[s];if(t(e,a,_,this)===!1)break;s+=n}}fill(t){this.forEach((i,s)=>{this.set(i,s,t)})}clone(){return h.fromArray(this._w,this._h,this._c)}clampX(t){return u(t,0,this._w-1)}clampY(t){return u(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",i=!0){if(!i)return this._c.join(t);let s=[];return this.forEach((r,n,e)=>{s[n]=s[n]||"",s[n]+=e+t}),s.join(`
2
- `)}},l=class h extends o{constructor(t,i,s=Uint8Array){super(t,i,null),this._c=new s(this._w*this._h)}has(t,i){return this.get(t,i)!==0}clone(){let t=new h(this._w,this._h,this._c.constructor);return this.forEach((i,s,r)=>{t.set(i,s,r)}),t}};function u(h,t,i){return h<t?t:h>i?i:h}globalThis.utils=Object.assign(globalThis.utils||{},c);})();
1
+ (()=>{var g=Object.defineProperty;var f=(h,t)=>{for(var i in t)g(h,i,{get:t[i],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let h in globalThis.utils)h!=="global"&&(globalThis[h]=globalThis.utils[h])};var c={};f(c,{Grid:()=>r,TypedGrid:()=>l});var r=class h{_w;_h;_c;constructor(t,i,s=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~i),this._c=s}[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 h(this._w,this._h,this._c)}clear(){this.forEach((t,i)=>this.set(t,i,void 0))}get width(){return this._w}get height(){return this._h}set(t,i,s){this._c[this.pointToIndex(t,i)]=s}get(t,i){return this._c[this.pointToIndex(t,i)]}has(t,i){return this.get(t,i)!=null}check(t,i){return t>=0&&t<this._w&&i>=0&&i<this._h}get length(){return this._w*this._h}pointToIndex(t,i){return this.clampX(~~t)+this.clampY(~~i)*this._w}indexToPointX(t){return t%this._w}indexToPointY(t){return Math.floor(t/this._w)}forEach(t,i=!1){let s=i?this.length-1:0,e=i?-1:this.length,n=i?-1:1;for(;s!==e;){let o=this.indexToPointX(s),a=this.indexToPointY(s),_=this._c[s];if(t(o,a,_,this)===!1)break;s+=n}}fill(t){this.forEach((i,s)=>{this.set(i,s,t)})}clampX(t){return u(t,0,this._w-1)}clampY(t){return u(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",i=!0){if(!i)return this._c.join(t);let s=[];return this.forEach((e,n,o)=>{s[n]=s[n]||"",s[n]+=o+t}),s.join(`
2
+ `)}},l=class h extends r{constructor(t,i,s=Uint8Array){super(t,i,null),this._c=new s(this._w*this._h)}has(t,i){return this.get(t,i)!==0}clone(){let t=new h(this._w,this._h,this._c.constructor);return this.forEach((i,s,e)=>{t.set(i,s,e)}),t}};function u(h,t,i){return h<t?t:h>i?i:h}globalThis.utils=Object.assign(globalThis.utils||{},c);})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litecanvas/utils",
3
- "version": "0.15.0",
3
+ "version": "0.17.0",
4
4
  "description": "Utilities to help build litecanvas games",
5
5
  "author": "Luiz Bills <luizbills@pm.me>",
6
6
  "license": "MIT",
@@ -56,7 +56,7 @@ player.y = 200
56
56
 
57
57
  ## Actor#scale
58
58
 
59
- Set or get the actor scale/size
59
+ Set or get the actor scale vector
60
60
 
61
61
  ```js
62
62
  // twice bigger
@@ -64,6 +64,36 @@ player.scale.x = 2
64
64
  player.scale.y = 2
65
65
  ```
66
66
 
67
+ ## Actor#scaleBy(x, y = x)
68
+
69
+ ```js
70
+ // multiplies the scale (y is optional)
71
+ player.scaleBy(3)
72
+
73
+ // same as
74
+ player.scale.x *= 3
75
+ player.scale.y *= 3
76
+ ```
77
+
78
+ ## Actor#scaleTo(x, y = x)
79
+
80
+ ```js
81
+ // sets the scale (y is optional)
82
+ player.scaleTo(3)
83
+
84
+ // same as
85
+ player.scale.x = 3
86
+ player.scale.y = 3
87
+ ```
88
+
89
+ ## Actor#flipX
90
+
91
+ If `true` the actor sprite is flipped horizontally. Default is `false`.
92
+
93
+ ## Actor#flipY
94
+
95
+ If `true` the actor sprite is flipped vertically. Default is `false`.
96
+
67
97
  ## Actor#anchor
68
98
 
69
99
  Set or get the actor anchor (origin). By default, the anchor is a vector `(0, 0)` (meaning anchor Top Left).
@@ -20,6 +20,12 @@ export class Actor {
20
20
  /** @type {Vector} The actor scale */
21
21
  _s
22
22
 
23
+ /** @type {boolean} */
24
+ flipX = false
25
+
26
+ /** @type {boolean} */
27
+ flipY = false
28
+
23
29
  /** @type {number} The actor angle (in radians) */
24
30
  angle = 0
25
31
 
@@ -160,18 +166,21 @@ export class Actor {
160
166
  transform(litecanvas) {
161
167
  litecanvas.translate(this.pos.x, this.pos.y)
162
168
  litecanvas.rotate(this.angle)
163
- litecanvas.scale(this._s.x, this._s.y)
169
+ litecanvas.scale(
170
+ (this.flipX ? -1 : 1) * this._s.x,
171
+ (this.flipY ? -1 : 1) * this._s.y
172
+ )
164
173
  }
165
174
 
166
175
  /**
167
176
  * @param {LitecanvasInstance} litecanvas
168
177
  */
169
178
  drawImage(litecanvas, alpha = true) {
170
- const anchorX = this.sprite.width * this.anchor.x
171
- const anchorY = this.sprite.height * this.anchor.y
179
+ const anchor = this.anchor
180
+ const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x)
181
+ const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y)
172
182
 
173
183
  if (alpha) litecanvas.alpha(this.opacity)
174
-
175
- litecanvas.image(-anchorX, -anchorY, this.sprite)
184
+ litecanvas.image(x, y, this.sprite)
176
185
  }
177
186
  }
@@ -9,8 +9,8 @@ Lets build an arena with [ASCII graphics](https://en.wikipedia.org/wiki/ASCII_ar
9
9
  ```js
10
10
  import { Grid } from "@litecanvas/utils"
11
11
 
12
- // make a grid 13x13
13
- let grid = new Grid(13, 13)
12
+ // make a grid 7x7
13
+ let grid = new Grid(7, 7)
14
14
 
15
15
  // fill the entire grid with "."
16
16
  grid.fill(".")
@@ -18,8 +18,8 @@ grid.fill(".")
18
18
  // put a '@' in the middle
19
19
  grid.set(grid.width / 2, grid.height / 2, "@")
20
20
 
21
- // put '#' around the grid
22
- grid.forEach((x, y, cellValue) => {
21
+ // loop over the grid and put "#" around
22
+ for (let [x, y, cellValue] of grid) {
23
23
  if (x === 0 || y === 0 || x === grid.width - 1 || y === grid.height - 1) {
24
24
  grid.set(x, y, "#")
25
25
  }
@@ -31,31 +31,41 @@ document.body.innerHTML = "<pre>" + grid.toString() + "</pre>"
31
31
  The result:
32
32
 
33
33
  ```
34
- # # # # # # # # # # # # #
35
- # . . . . . . . . . . . #
36
- # . . . . . . . . . . . #
37
- # . . . . . . . . . . . #
38
- # . . . . . . . . . . . #
39
- # . . . . . . . . . . . #
40
- # . . . . . @ . . . . . #
41
- # . . . . . . . . . . . #
42
- # . . . . . . . . . . . #
43
- # . . . . . . . . . . . #
44
- # . . . . . . . . . . . #
45
- # . . . . . . . . . . . #
46
- # # # # # # # # # # # # #
34
+ # # # # # # #
35
+ # . . . . . #
36
+ # . . . . . #
37
+ # . . @ . . #
38
+ # . . . . . #
39
+ # . . . . . #
40
+ # # # # # # #
41
+ ```
42
+
43
+ ### .forEach()
44
+
45
+ Instead of a `for-of` loop, you can use the `.forEach()` method:
46
+
47
+ ```js
48
+ grid.forEach((x, y, cellValue, grid) => {
49
+ if (x === 0 || y === 0 || x === grid.width - 1 || y === grid.height - 1) {
50
+ grid.set(x, y, "#")
51
+ }
52
+ // optional: you can return `false` to break/stop that loop
53
+ // return false
54
+ })
47
55
  ```
48
56
 
49
57
  ## Typed Grid
50
58
 
51
59
  You can create a grid structure thats uses a [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Typed_arrays) rather than a "normal" array.
52
60
 
53
- ```
61
+ ```js
54
62
  import { TypedGrid } from "@litecanvas/utils"
55
63
 
56
- let u8grid = new TypedGrid(5, 5) // by default, uses Uint8Array
64
+ // by default, uses Uint8Array
65
+ let u8grid = new TypedGrid(5, 5)
57
66
 
58
- let i16grid = new TypedGrid(5, 5, Int16Array) // or specify your typed array
67
+ // or specify your typed array
68
+ let i16grid = new TypedGrid(5, 5, Int16Array)
59
69
  ```
60
70
 
61
71
  > Note: `TypedGrid` inherits all methods from `Grid`.
package/src/grid/index.js CHANGED
@@ -18,6 +18,25 @@ export class Grid {
18
18
  this._c = values
19
19
  }
20
20
 
21
+ [Symbol.iterator]() {
22
+ let i = 0
23
+ return {
24
+ next: () => {
25
+ return {
26
+ value: [this.indexToPointX(i), this.indexToPointY(i), this._c[i++]],
27
+ done: i === this._c.length,
28
+ }
29
+ },
30
+ }
31
+ }
32
+
33
+ /**
34
+ * @returns {Grid} the cloned grid
35
+ */
36
+ clone() {
37
+ return new Grid(this._w, this._h, this._c)
38
+ }
39
+
21
40
  /**
22
41
  * Delete all cell values.
23
42
  */
@@ -74,6 +93,17 @@ export class Grid {
74
93
  return this.get(x, y) != null
75
94
  }
76
95
 
96
+ /**
97
+ * Checks if a which point (x, y) is within the grid.
98
+ *
99
+ * @param {number} x
100
+ * @param {number} y
101
+ * @returns {boolean}
102
+ */
103
+ check(x, y) {
104
+ return x >= 0 && x < this._w && y >= 0 && y < this._h
105
+ }
106
+
77
107
  /**
78
108
  * Returns the total of cells.
79
109
  *
@@ -152,13 +182,6 @@ export class Grid {
152
182
  })
153
183
  }
154
184
 
155
- /**
156
- * @returns {Grid} the cloned grid
157
- */
158
- clone() {
159
- return Grid.fromArray(this._w, this._h, this._c)
160
- }
161
-
162
185
  /**
163
186
  * @param {number} y
164
187
  * @returns {number}
@@ -62,6 +62,41 @@ function draw() {
62
62
  }
63
63
  ```
64
64
 
65
+ [See in playground](https://litecanvas.js.org?c=eJwlyrEKwjAUheE9T3HGBKME0UVwEM0mIuqgY4ytBC6JNGlLKXl3bVwOP4evTY7i4k3haYgLRi5V1vjORD4ygEL4bDDi1ZgeWbIsGKtbb5MLvpxcYHKWIldiiuBjwoAtetNVfL5USqKMPu7OV30oyDW2dkR8r083fblL%2FOOBGQaJ9U%2BvBMtfqxAsyg%3D%3D)
66
+
67
+ ## advance
68
+
69
+ Returns a sequence of numbers from `0` to `size - 1`.
70
+
71
+ Syntax: `range(size: number): number[]`
72
+
73
+ ```js
74
+ import litecanvas from "litecanvas"
75
+ import { vec, advance } from "@litecanvas/utils"
76
+
77
+ litecanvas()
78
+
79
+ function init() {
80
+ pos = vec(0, CENTERY)
81
+ vel = vec(0, 0)
82
+ acc = vec(100, 0)
83
+ }
84
+
85
+ function update(dt) {
86
+ advance(pos, vel, acc, dt)
87
+ if (pos.x > WIDTH) {
88
+ pos.x = 0
89
+ }
90
+ }
91
+
92
+ function draw() {
93
+ cls(0)
94
+ circ(pos.x, pos.y, 48, 4)
95
+ }
96
+ ```
97
+
98
+ [See in playground](https://litecanvas.js.org?c=eJxVjjELwjAQhff%2BihtTCKWCg0tdtKCLgxTEMV5SCYS0NGlUpP%2FdS1NQhxvue7yPN3ptXHE33U0YlmdGe4XCBuHoydrRotedBW21Zzm8M4C%2Bc1BBUMhKDrv61NTna048KPPlZSQCcSGrMrHpRzn2UnjFpE9aIYOwqBjpeXTxWOdAMYW6hRgUT9jC5bhvDqkzjyFYQUnf9KeXg3gsi9E4Ng9CPWDy8Ln54rDe0NGwDwbOS1Q%3D)
99
+
65
100
  ## range
66
101
 
67
102
  Returns a sequence of numbers from `0` to `size - 1`.
@@ -134,15 +134,15 @@ Returns the percentage of the animation's progress, a number between `0.0` and `
134
134
  We provide few easing functions:
135
135
 
136
136
  - `LINEAR` (the default)
137
- - `BOUNCE_IN`
138
- - `BOUNCE_OUT`
139
- - `BOUNCE_IN_OUT`
140
137
  - `EASE_IN`
141
138
  - `EASE_OUT`
142
139
  - `EASE_IN_OUT`
143
140
  - `ELASTIC_IN`
144
141
  - `ELASTIC_OUT`
145
142
  - `ELASTIC_IN_OUT`
143
+ - `BOUNCE_IN`
144
+ - `BOUNCE_OUT`
145
+ - `BOUNCE_IN_OUT`
146
146
  - `BACK_IN`
147
147
  - `BACK_OUT`
148
148
  - `BACK_IN_OUT`
@@ -172,4 +172,10 @@ function draw() {
172
172
  }
173
173
  ```
174
174
 
175
- [See all Easing Functions in action](https://litecanvas.js.org?c=eJylVN9v2jAQfs9fceuTs2YpbN0LEpMYYwOtAmml2sM0TSY5wJvroNhpQVP%2F951jOxCg28Mkgu63v7vv7MoIqdOVLBZcsjiKskJpA8i1UCsNffgdAdxMpqPBl4Sk0eB29GMybcTZ3fzA3Kg3g9v5ZBgCvXbiDJb3s7vpMJT1yrGrMQyGn0OkFdtmpz61mpjye6Q%2BZoufmJn0F%2B40893FrbiPlcqMKNQ%2B9oHLCg%2BiJYbYYVWWqAyFdpqJkSKFwYyrB66ZHdujyM26B286HQuRV6bQGZfYgyWXGgknjXsZThVKGBbX4y7q4%2F3sKVGJe25jeqAqKZPauO1Bx0k7Lz1F9KcNL23mA2as2%2BnEZEKVe8PXyYf5GF4BORIYjyafxnOnxTY1r0ru%2B%2B%2FC1RVka65WCGYtNE10D7Ta5Nwgy40DK5bAHOC0ARpDiaYqVdQ0k26pbA0u3e6NTQI5zSOiYnVHzuu6u9heOIHaSLdODEi9o8XetxZB3ykiTutzGcbRmZPTQo1Uzmjy%2FXd%2B3mfA2bnbER%2BW2DUd7cj4HPzdAfzdf8I%2FZCEv%2BSPbM%2FDibxRkUrN6FaRQWC8lu647IZZtHeIY%2FeZwWharEVybKMqMedaS0GxCC53A2zj4HTG%2BweA7Ku5gYe5n41OXQsqwO1QhzDWB1ydFOGzKYlWi1rDgJX3aFlNgS5WUVZeiLPq5LX95ynSokMD1OYzKvhPFEjJ%2Ftx0TECZuOcatsYd06du%2FLUeUtWkyfLPB3BPVCry89NQdvSj9%2FtFKpBLVyqxjv52nD5C9%2Bs%2Fdw%2FMbTStVbFj8r22Pnv4AeWDaqw%3D%3D)
175
+ [See all Easing Functions in action](https://litecanvas.js.org?c=eJylVEtvGjEQvvMrJpy8zXZD2vSCRCVKaYMaEakh6qGqKrM24Nax0a43sIry3zt%2BLSyQ9lCJRfP2fPONXRkhy2wp9ZxKknQ6uValAU5LoZYlDOCpA3AzmY6HX1OUxsO78c%2FJtBFv72d75ka9Gd7NJqMYGLQjZ7R8uL2fjmLZoBy6GsNw9CVGWrFt9upzC8SUPnDEcTv%2FxXOT%2FeZ1SQK6pBX3qVK5EVrtYh%2BprPhetOQxdlQVBVcGQ3vNxFCRwvCcqkdaEju2jWBm1Ye3vZ5tkVZGlzmVvA8LKkuOfeK4F%2FFUoYQhiRs3qwoaOrlEFX%2FaNRTYwFJKPLiIPqhKytQZt33oeakO0nMH%2F0pDC5v5yHNy2eslaOKKBcO3ycfZNbwGdKRwPZ58vp55LbGpTOfVA8LM5prVGV2vMXG0EpIRd0zjzgtODZ%2FxrZlqxkl3RtdgNOQrqpYczIrD2E0N4ojPuraNBIna4a%2FWDIsQZvwMxAKIR501aBMouKkK1Wkmkm0RiEOYbXfGJgGdZsO58v16rx9Rd9v1AkLKtl6MYw%2BO1lJ8b%2FH%2Bw3afuXMJTzonTs60GitGkNDB%2B0DaieYseZan%2FRJ1g6hG40vt13vt1%2F%2FZ%2Fj4LrKAbsmPg7G8U5LIkbp%2BkUNztOrlySC4uXB3HvF8%2FihtnNWzXJooiJ4G1NIJN8Z6k8C6Jfk9MABh9B8V9W5yF2YTUhZAy7g5WiHNN4c1REQrrQi8LXpYwpwV%2BpS2mwJYqMMuVwiz8%2Bavy6pjpWCGFq1M9Kvv86AXk4cnwTECcuOUYL4495BK%2F3ZN1QFmbJmMvIwtEtQLPzwN1Bw%2FVYHCwEpnkamlWSdjO43fNvh8v3cPTG40rpdck%2Bde2I5I%2FARL3JQ%3D%3D)
176
+
177
+ ## Useful links
178
+
179
+ - https://en.wikipedia.org/wiki/Linear_interpolation
180
+ - https://gamedev.net/tutorials/programming/general-and-gameplay-programming/a-brief-introduction-to-lerp-r4954/
181
+ - https://easings.net/