@litecanvas/utils 0.5.1 → 0.8.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
@@ -5,6 +5,15 @@
5
5
  __defProp(target, name, { get: all[name], enumerable: true });
6
6
  };
7
7
 
8
+ // src/_global.js
9
+ globalThis.utils = globalThis.utils || {};
10
+ globalThis.utils.global = () => {
11
+ for (const key in globalThis.utils) {
12
+ if ("global" === key) continue;
13
+ globalThis[key] = globalThis.utils[key];
14
+ }
15
+ };
16
+
8
17
  // src/actor/index.js
9
18
  var actor_exports = {};
10
19
  __export(actor_exports, {
@@ -50,7 +59,7 @@
50
59
  /** @type {Image|HTMLCanvasElement|OffscreenCanvas} */
51
60
  sprite;
52
61
  /** @type {Vector} The actor position */
53
- _p;
62
+ pos;
54
63
  /** @type {Vector} The actor anchor (origin) */
55
64
  _o;
56
65
  /** @type {Vector} The actor scale */
@@ -67,7 +76,7 @@
67
76
  */
68
77
  constructor(sprite, position) {
69
78
  this.sprite = sprite;
70
- this._p = position || vec(0);
79
+ this.pos = position || vec(0);
71
80
  this._o = veccopy(ANCHOR_TOP_LEFT);
72
81
  this._s = vec(1, 1);
73
82
  }
@@ -75,25 +84,25 @@
75
84
  * @param {number}
76
85
  */
77
86
  set x(value) {
78
- this._p.x = value;
87
+ this.pos.x = value;
79
88
  }
80
89
  /**
81
90
  * @returns {number}
82
91
  */
83
92
  get x() {
84
- return this._p.x;
93
+ return this.pos.x;
85
94
  }
86
95
  /**
87
96
  * @param {number}
88
97
  */
89
98
  set y(value) {
90
- this._p.y = value;
99
+ this.pos.y = value;
91
100
  }
92
101
  /**
93
102
  * @returns {number}
94
103
  */
95
104
  get y() {
96
- return this._p.y;
105
+ return this.pos.y;
97
106
  }
98
107
  /**
99
108
  * @param {Vector}
@@ -144,7 +153,7 @@
144
153
  * @param {LitecanvasInstance} litecanvas
145
154
  */
146
155
  transform(litecanvas) {
147
- litecanvas.translate(this._p.x, this._p.y);
156
+ litecanvas.translate(this.pos.x, this.pos.y);
148
157
  litecanvas.rotate(this.angle);
149
158
  litecanvas.scale(this._s.x, this._s.y);
150
159
  litecanvas.alpha(this.opacity);
package/dist/actor.min.js CHANGED
@@ -1 +1 @@
1
- (()=>{var f=Object.defineProperty;var p=(e,t)=>{for(var r in t)f(e,r,{get:t[r],enumerable:!0})};var a={};p(a,{ANCHOR_BOT_LEFT:()=>d,ANCHOR_BOT_RIGHT:()=>x,ANCHOR_CENTER:()=>u,ANCHOR_TOP_LEFT:()=>c,ANCHOR_TOP_RIGHT:()=>h,Actor:()=>n});var s=class{x;y;constructor(t=0,r=t){this.x=t,this.y=r}toString(){return`Vector (${this.x}, ${this.y})`}},o=(e=0,t=e)=>new s(e,t);var i=e=>o(e.x,e.y);var u=o(.5,.5),c=o(0,0),h=o(1,0),d=o(0,1),x=o(1,1),n=class{sprite;_p;_o;_s;angle=0;opacity=1;hidden=!1;constructor(t,r){this.sprite=t,this._p=r||o(0),this._o=i(c),this._s=o(1,1)}set x(t){this._p.x=t}get x(){return this._p.x}set y(t){this._p.y=t}get y(){return this._p.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.y}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}draw(t=globalThis){if(this.hidden||this.opacity<=0)return;t.push(),this.transform(t);let r=this.sprite.width*this.anchor.x,l=this.sprite.height*this.anchor.y;t.image(-r,-l,this.sprite),t.pop()}transform(t){t.translate(this._p.x,this._p.y),t.rotate(this.angle),t.scale(this._s.x,this._s.y),t.alpha(this.opacity)}};globalThis.utils=Object.assign(globalThis.utils||{},a);})();
1
+ (()=>{var f=Object.defineProperty;var p=(e,t)=>{for(var s in t)f(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 i={};p(i,{ANCHOR_BOT_LEFT:()=>d,ANCHOR_BOT_RIGHT:()=>g,ANCHOR_CENTER:()=>u,ANCHOR_TOP_LEFT:()=>c,ANCHOR_TOP_RIGHT:()=>h,Actor:()=>n});var r=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},o=(e=0,t=e)=>new r(e,t);var a=e=>o(e.x,e.y);var u=o(.5,.5),c=o(0,0),h=o(1,0),d=o(0,1),g=o(1,1),n=class{sprite;pos;_o;_s;angle=0;opacity=1;hidden=!1;constructor(t,s){this.sprite=t,this.pos=s||o(0),this._o=a(c),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.y}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}draw(t=globalThis){if(this.hidden||this.opacity<=0)return;t.push(),this.transform(t);let s=this.sprite.width*this.anchor.x,l=this.sprite.height*this.anchor.y;t.image(-s,-l,this.sprite),t.pop()}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(this.angle),t.scale(this._s.x,this._s.y),t.alpha(this.opacity)}};globalThis.utils=Object.assign(globalThis.utils||{},i);})();
package/dist/all.js CHANGED
@@ -5,6 +5,15 @@
5
5
  __defProp(target, name, { get: all[name], enumerable: true });
6
6
  };
7
7
 
8
+ // src/_global.js
9
+ globalThis.utils = globalThis.utils || {};
10
+ globalThis.utils.global = () => {
11
+ for (const key in globalThis.utils) {
12
+ if ("global" === key) continue;
13
+ globalThis[key] = globalThis.utils[key];
14
+ }
15
+ };
16
+
8
17
  // src/index.js
9
18
  var src_exports = {};
10
19
  __export(src_exports, {
@@ -20,6 +29,7 @@
20
29
  LEFT: () => LEFT,
21
30
  ONE: () => ONE,
22
31
  RIGHT: () => RIGHT,
32
+ TypedGrid: () => TypedGrid,
23
33
  UP: () => UP,
24
34
  Vector: () => Vector,
25
35
  ZERO: () => ZERO,
@@ -213,49 +223,32 @@
213
223
  _h;
214
224
  /** @type {any[]} The grid cells */
215
225
  _c;
216
- /**
217
- * @static
218
- * @param {number} width
219
- * @param {number} height
220
- * @param {any[]} values
221
- */
222
- static fromArray(width, height, values) {
223
- const grid = new _Grid(width, height);
224
- for (let i = 0; i < values.length; i++) {
225
- grid._c[i] = values[i];
226
- }
227
- return grid;
228
- }
229
226
  /**
230
227
  * @param {number} width The grid width
231
228
  * @param {number} height The grid height
232
229
  */
233
- constructor(width, height) {
234
- this.width = width;
235
- this.height = height;
236
- this.clear();
230
+ constructor(width, height, values = []) {
231
+ this._w = Math.max(1, ~~width);
232
+ this._h = Math.max(1, ~~height);
233
+ this._c = values;
237
234
  }
238
235
  /**
239
236
  * Delete all cell values.
240
237
  */
241
238
  clear() {
242
- this._c = Array(this._w * this._h);
239
+ this.forEach((x, y) => this.set(x, y, void 0));
243
240
  }
244
241
  /**
245
- * @param {number} value
242
+ * @returns {number}
243
+ * @readonly
246
244
  */
247
- set width(value) {
248
- this._w = Math.max(1, ~~value);
249
- }
250
245
  get width() {
251
246
  return this._w;
252
247
  }
253
248
  /**
254
- * @param {number} value
249
+ * @returns {number}
250
+ * @readonly
255
251
  */
256
- set height(value) {
257
- this._h = Math.max(1, ~~value);
258
- }
259
252
  get height() {
260
253
  return this._h;
261
254
  }
@@ -380,7 +373,7 @@
380
373
  * @returns {any[]}
381
374
  */
382
375
  toArray() {
383
- return this._c.slice(0);
376
+ return this._c.slice();
384
377
  }
385
378
  /**
386
379
  * @param {string} separator
@@ -397,6 +390,34 @@
397
390
  return rows.join("\n");
398
391
  }
399
392
  };
393
+ var TypedGrid = class _TypedGrid extends Grid {
394
+ /**
395
+ * @param {number} width The grid width
396
+ * @param {number} height The grid height
397
+ */
398
+ constructor(width, height, TypedArray = Uint8Array) {
399
+ super(width, height, null);
400
+ this._c = new TypedArray(this._w * this._h);
401
+ }
402
+ /**
403
+ * @param {number} x
404
+ * @param {number} y
405
+ * @returns {boolean}
406
+ */
407
+ has(x, y) {
408
+ return this.get(x, y) !== 0;
409
+ }
410
+ /**
411
+ * @returns {TypedGrid}
412
+ */
413
+ clone() {
414
+ const copy = new _TypedGrid(this._w, this._h, this._c.constructor);
415
+ this.forEach((x, y, value) => {
416
+ copy.set(x, y, value);
417
+ });
418
+ return copy;
419
+ }
420
+ };
400
421
  function _clamp(value, min, max) {
401
422
  if (value < min) return min;
402
423
  if (value > max) return max;
@@ -537,7 +558,7 @@
537
558
  /** @type {Image|HTMLCanvasElement|OffscreenCanvas} */
538
559
  sprite;
539
560
  /** @type {Vector} The actor position */
540
- _p;
561
+ pos;
541
562
  /** @type {Vector} The actor anchor (origin) */
542
563
  _o;
543
564
  /** @type {Vector} The actor scale */
@@ -554,7 +575,7 @@
554
575
  */
555
576
  constructor(sprite, position) {
556
577
  this.sprite = sprite;
557
- this._p = position || vec(0);
578
+ this.pos = position || vec(0);
558
579
  this._o = veccopy(ANCHOR_TOP_LEFT);
559
580
  this._s = vec(1, 1);
560
581
  }
@@ -562,25 +583,25 @@
562
583
  * @param {number}
563
584
  */
564
585
  set x(value) {
565
- this._p.x = value;
586
+ this.pos.x = value;
566
587
  }
567
588
  /**
568
589
  * @returns {number}
569
590
  */
570
591
  get x() {
571
- return this._p.x;
592
+ return this.pos.x;
572
593
  }
573
594
  /**
574
595
  * @param {number}
575
596
  */
576
597
  set y(value) {
577
- this._p.y = value;
598
+ this.pos.y = value;
578
599
  }
579
600
  /**
580
601
  * @returns {number}
581
602
  */
582
603
  get y() {
583
- return this._p.y;
604
+ return this.pos.y;
584
605
  }
585
606
  /**
586
607
  * @param {Vector}
@@ -631,7 +652,7 @@
631
652
  * @param {LitecanvasInstance} litecanvas
632
653
  */
633
654
  transform(litecanvas) {
634
- litecanvas.translate(this._p.x, this._p.y);
655
+ litecanvas.translate(this.pos.x, this.pos.y);
635
656
  litecanvas.rotate(this.angle);
636
657
  litecanvas.scale(this._s.x, this._s.y);
637
658
  litecanvas.alpha(this.opacity);
@@ -639,5 +660,5 @@
639
660
  };
640
661
 
641
662
  // src/_web.js
642
- globalThis.utils = src_exports;
663
+ globalThis.utils = Object.assign(globalThis.utils || {}, src_exports);
643
664
  })();
package/dist/all.min.js CHANGED
@@ -1,2 +1,2 @@
1
- (()=>{var L=Object.defineProperty;var v=(e,t)=>{for(var s in t)L(e,s,{get:t[s],enumerable:!0})};var A={};v(A,{ANCHOR_BOT_LEFT:()=>it,ANCHOR_BOT_RIGHT:()=>nt,ANCHOR_CENTER:()=>ot,ANCHOR_TOP_LEFT:()=>W,ANCHOR_TOP_RIGHT:()=>rt,Actor:()=>I,Camera:()=>p,DOWN:()=>et,Grid:()=>u,LEFT:()=>st,ONE:()=>K,RIGHT:()=>tt,UP:()=>Q,Vector:()=>d,ZERO:()=>J,diff:()=>M,fract:()=>P,intersection:()=>g,isvector:()=>c,resolve:()=>O,vec:()=>i,vecadd:()=>S,vecconfig:()=>w,veccopy:()=>H,veccross:()=>V,vecdir:()=>$,vecdist:()=>B,vecdist2:()=>j,vecdiv:()=>_,vecdot:()=>q,veceq:()=>k,veclerp:()=>U,veclimit:()=>F,vecmag:()=>R,vecmag2:()=>Y,vecmult:()=>E,vecnorm:()=>N,vecrand:()=>Z,vecrot:()=>G,vecset:()=>z,vecsub:()=>X,wave:()=>b});var M=(e,t)=>Math.abs(t-e)||0;var P=e=>e%1||0;var b=(e,t,s,o=Math.sin)=>e+(o(s)+1)/2*(t-e);var g=(e,t,s,o,n,r,a,h)=>{let l=Math.max(e,n),D=Math.min(e+s,n+a)-l,x=Math.max(t,r),m=Math.min(t+o,r+h)-x;return[l,x,D,m]};var O=(e,t,s,o,n,r,a,h)=>{let[l,D,x,m]=g(e,t,s,o,n,r,a,h),f="",y=e,T=t;return x<m?e<n?(f="right",y=n-s):(f="left",y=n+a):t<r?(f="bottom",T=r-o):(f="top",T=r+h),{direction:f,x:y,y:T}};var p=class{_engine=null;x=0;y=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(t=null){t=t||globalThis,this._engine=t,this.size(t.WIDTH||0,t.HEIGHT||0),this.x=this.width/2,this.y=this.height/2}size(t,s){this.width=t,this.height=s}start(t=!1){let s=this.width/2,o=this.height/2;this._engine.push(),this._engine.translate(s,o),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y),t&&this._engine.cliprect(this.x,this.y,this.width,this.height)}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}shake(t=.3,s=1){this.shaking()||(this._shake.removeListener=this._engine.listen("update",o=>{this._shake.x=this._engine.randi(-s,s),this._shake.y=this._engine.randi(-s,s),t-=o,t<=0&&this.unshake()}))}unshake(){this.shaking()&&(this._shake.removeListener(),this._shake.removeListener=null,this._shake.x=this._shake.y=0)}shaking(){return this._shake.removeListener!==null}};var u=class e{_w;_h;_c;static fromArray(t,s,o){let n=new e(t,s);for(let r=0;r<o.length;r++)n._c[r]=o[r];return n}constructor(t,s){this.width=t,this.height=s,this.clear()}clear(){this._c=Array(this._w*this._h)}set width(t){this._w=Math.max(1,~~t)}get width(){return this._w}set height(t){this._h=Math.max(1,~~t)}get height(){return this._h}set(t,s,o){this._c[this.pointToIndex(t,s)]=o}get(t,s){return this._c[this.pointToIndex(t,s)]}has(t,s){return this.get(t,s)!=null}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 o=s?this.length-1:0,n=s?-1:this.length,r=s?-1:1;for(;o!==n;){let a=this.indexToPointX(o),h=this.indexToPointY(o),l=this._c[o];if(t(a,h,l,this)===!1)break;o+=r}}fill(t){this.forEach((s,o)=>{this.set(s,o,t)})}clone(){return e.fromArray(this._w,this._h,this._c)}clampX(t){return C(t,0,this._w-1)}clampY(t){return C(t,0,this._h-1)}toArray(){return this._c.slice(0)}toString(t=" ",s=!0){if(!s)return this._c.join(t);let o=[];return this.forEach((n,r,a)=>{o[r]=o[r]||"",o[r]+=a+t}),o.join(`
2
- `)}};function C(e,t,s){return e<t?t:e>s?s:e}var d=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},i=(e=0,t=e)=>new d(e,t),k=(e,t,s=t)=>c(t)?k(e,t.x,t.y):e.x===t&&e.y===s,H=e=>i(e.x,e.y),z=(e,t,s=t)=>{c(t)?z(e,t.x,t.y):(e.x=t,e.y=s)},S=(e,t,s=t)=>{c(t)?S(e,t.x,t.y):(e.x+=t,e.y+=s)},X=(e,t,s=t)=>{c(t)?X(e,t.x,t.y):(e.x-=t,e.y-=s)},E=(e,t,s=t)=>{c(t)?E(e,t.x,t.y):(e.x*=t,e.y*=s)},_=(e,t,s=t)=>{c(t)?_(e,t.x,t.y):(e.x/=t,e.y/=s)},G=(e,t)=>{let s=Math.cos(t),o=Math.sin(t);e.x=s*e.x-o*e.y,e.y=o*e.x+s*e.y},R=e=>Math.sqrt(e.x*e.x+e.y*e.y),Y=e=>e.x*e.x+e.y*e.y,N=e=>{let t=R(e);t>0&&_(e,t)},F=(e,t)=>{let s=Y(e);s>t*t&&(_(e,Math.sqrt(s)),E(e,t))},B=(e,t)=>{let s=e.x-t.x,o=e.y-t.y;return Math.sqrt(s*s+o*o)},j=(e,t)=>{let s=e.x-t.x,o=e.y-t.y;return s*s+o*o},$=e=>Math.atan2(e.y,e.x),q=(e,t)=>e.x*t.x+e.y*t.y,V=(e,t)=>e.x*t.y-e.y*t.x,U=(e,t,s)=>{e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0},Z=(e=1,t=e)=>{let s=w.random()*2*Math.PI,o=w.random()*(t-e)+e;return i(Math.cos(s)*o,Math.sin(s)*o)},c=e=>e instanceof d,w={random:()=>globalThis.rand?rand():Math.random()},J=i(0,0),K=i(1,1),Q=i(0,-1),tt=i(1,0),et=i(0,1),st=i(-1,0);var ot=i(.5,.5),W=i(0,0),rt=i(1,0),it=i(0,1),nt=i(1,1),I=class{sprite;_p;_o;_s;angle=0;opacity=1;hidden=!1;constructor(t,s){this.sprite=t,this._p=s||i(0),this._o=H(W),this._s=i(1,1)}set x(t){this._p.x=t}get x(){return this._p.x}set y(t){this._p.y=t}get y(){return this._p.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.y}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}draw(t=globalThis){if(this.hidden||this.opacity<=0)return;t.push(),this.transform(t);let s=this.sprite.width*this.anchor.x,o=this.sprite.height*this.anchor.y;t.image(-s,-o,this.sprite),t.pop()}transform(t){t.translate(this._p.x,this._p.y),t.rotate(this.angle),t.scale(this._s.x,this._s.y),t.alpha(this.opacity)}};globalThis.utils=A;})();
1
+ (()=>{var v=Object.defineProperty;var F=(e,t)=>{for(var s in t)v(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 A={};F(A,{ANCHOR_BOT_LEFT:()=>nt,ANCHOR_BOT_RIGHT:()=>at,ANCHOR_CENTER:()=>it,ANCHOR_TOP_LEFT:()=>L,ANCHOR_TOP_RIGHT:()=>rt,Actor:()=>I,Camera:()=>p,DOWN:()=>st,Grid:()=>g,LEFT:()=>ot,ONE:()=>Q,RIGHT:()=>et,TypedGrid:()=>w,UP:()=>tt,Vector:()=>u,ZERO:()=>K,diff:()=>M,fract:()=>P,intersection:()=>x,isvector:()=>c,resolve:()=>C,vec:()=>i,vecadd:()=>X,vecconfig:()=>H,veccopy:()=>E,veccross:()=>U,vecdir:()=>N,vecdist:()=>$,vecdist2:()=>q,vecdiv:()=>_,vecdot:()=>V,veceq:()=>z,veclerp:()=>Z,veclimit:()=>j,vecmag:()=>Y,vecmag2:()=>W,vecmult:()=>b,vecnorm:()=>B,vecrand:()=>J,vecrot:()=>G,vecset:()=>S,vecsub:()=>R,wave:()=>O});var M=(e,t)=>Math.abs(t-e)||0;var P=e=>e%1||0;var O=(e,t,s,o=Math.sin)=>e+(o(s)+1)/2*(t-e);var x=(e,t,s,o,n,r,a,l)=>{let h=Math.max(e,n),D=Math.min(e+s,n+a)-h,d=Math.max(t,r),m=Math.min(t+o,r+l)-d;return[h,d,D,m]};var C=(e,t,s,o,n,r,a,l)=>{let[h,D,d,m]=x(e,t,s,o,n,r,a,l),f="",y=e,T=t;return d<m?e<n?(f="right",y=n-s):(f="left",y=n+a):t<r?(f="bottom",T=r-o):(f="top",T=r+l),{direction:f,x:y,y:T}};var p=class{_engine=null;x=0;y=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(t=null){t=t||globalThis,this._engine=t,this.size(t.WIDTH||0,t.HEIGHT||0),this.x=this.width/2,this.y=this.height/2}size(t,s){this.width=t,this.height=s}start(t=!1){let s=this.width/2,o=this.height/2;this._engine.push(),this._engine.translate(s,o),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y),t&&this._engine.cliprect(this.x,this.y,this.width,this.height)}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}shake(t=.3,s=1){this.shaking()||(this._shake.removeListener=this._engine.listen("update",o=>{this._shake.x=this._engine.randi(-s,s),this._shake.y=this._engine.randi(-s,s),t-=o,t<=0&&this.unshake()}))}unshake(){this.shaking()&&(this._shake.removeListener(),this._shake.removeListener=null,this._shake.x=this._shake.y=0)}shaking(){return this._shake.removeListener!==null}};var g=class e{_w;_h;_c;constructor(t,s,o=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~s),this._c=o}clear(){this.forEach((t,s)=>this.set(t,s,void 0))}get width(){return this._w}get height(){return this._h}set(t,s,o){this._c[this.pointToIndex(t,s)]=o}get(t,s){return this._c[this.pointToIndex(t,s)]}has(t,s){return this.get(t,s)!=null}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 o=s?this.length-1:0,n=s?-1:this.length,r=s?-1:1;for(;o!==n;){let a=this.indexToPointX(o),l=this.indexToPointY(o),h=this._c[o];if(t(a,l,h,this)===!1)break;o+=r}}fill(t){this.forEach((s,o)=>{this.set(s,o,t)})}clone(){return e.fromArray(this._w,this._h,this._c)}clampX(t){return k(t,0,this._w-1)}clampY(t){return k(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",s=!0){if(!s)return this._c.join(t);let o=[];return this.forEach((n,r,a)=>{o[r]=o[r]||"",o[r]+=a+t}),o.join(`
2
+ `)}},w=class e extends g{constructor(t,s,o=Uint8Array){super(t,s,null),this._c=new o(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,o,n)=>{t.set(s,o,n)}),t}};function k(e,t,s){return e<t?t:e>s?s:e}var u=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},i=(e=0,t=e)=>new u(e,t),z=(e,t,s=t)=>c(t)?z(e,t.x,t.y):e.x===t&&e.y===s,E=e=>i(e.x,e.y),S=(e,t,s=t)=>{c(t)?S(e,t.x,t.y):(e.x=t,e.y=s)},X=(e,t,s=t)=>{c(t)?X(e,t.x,t.y):(e.x+=t,e.y+=s)},R=(e,t,s=t)=>{c(t)?R(e,t.x,t.y):(e.x-=t,e.y-=s)},b=(e,t,s=t)=>{c(t)?b(e,t.x,t.y):(e.x*=t,e.y*=s)},_=(e,t,s=t)=>{c(t)?_(e,t.x,t.y):(e.x/=t,e.y/=s)},G=(e,t)=>{let s=Math.cos(t),o=Math.sin(t);e.x=s*e.x-o*e.y,e.y=o*e.x+s*e.y},Y=e=>Math.sqrt(e.x*e.x+e.y*e.y),W=e=>e.x*e.x+e.y*e.y,B=e=>{let t=Y(e);t>0&&_(e,t)},j=(e,t)=>{let s=W(e);s>t*t&&(_(e,Math.sqrt(s)),b(e,t))},$=(e,t)=>{let s=e.x-t.x,o=e.y-t.y;return Math.sqrt(s*s+o*o)},q=(e,t)=>{let s=e.x-t.x,o=e.y-t.y;return s*s+o*o},N=e=>Math.atan2(e.y,e.x),V=(e,t)=>e.x*t.x+e.y*t.y,U=(e,t)=>e.x*t.y-e.y*t.x,Z=(e,t,s)=>{e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0},J=(e=1,t=e)=>{let s=H.random()*2*Math.PI,o=H.random()*(t-e)+e;return i(Math.cos(s)*o,Math.sin(s)*o)},c=e=>e instanceof u,H={random:()=>globalThis.rand?rand():Math.random()},K=i(0,0),Q=i(1,1),tt=i(0,-1),et=i(1,0),st=i(0,1),ot=i(-1,0);var it=i(.5,.5),L=i(0,0),rt=i(1,0),nt=i(0,1),at=i(1,1),I=class{sprite;pos;_o;_s;angle=0;opacity=1;hidden=!1;constructor(t,s){this.sprite=t,this.pos=s||i(0),this._o=E(L),this._s=i(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.y}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}draw(t=globalThis){if(this.hidden||this.opacity<=0)return;t.push(),this.transform(t);let s=this.sprite.width*this.anchor.x,o=this.sprite.height*this.anchor.y;t.image(-s,-o,this.sprite),t.pop()}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(this.angle),t.scale(this._s.x,this._s.y),t.alpha(this.opacity)}};globalThis.utils=Object.assign(globalThis.utils||{},A);})();
package/dist/camera.js CHANGED
@@ -1,4 +1,13 @@
1
1
  (() => {
2
+ // src/_global.js
3
+ globalThis.utils = globalThis.utils || {};
4
+ globalThis.utils.global = () => {
5
+ for (const key in globalThis.utils) {
6
+ if ("global" === key) continue;
7
+ globalThis[key] = globalThis.utils[key];
8
+ }
9
+ };
10
+
2
11
  // src/camera/index.js
3
12
  var Camera = class {
4
13
  /** @type {LitecanvasInstance} */
@@ -1 +1 @@
1
- (()=>{var i=class{_engine=null;x=0;y=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(t=null){t=t||globalThis,this._engine=t,this.size(t.WIDTH||0,t.HEIGHT||0),this.x=this.width/2,this.y=this.height/2}size(t,s){this.width=t,this.height=s}start(t=!1){let s=this.width/2,h=this.height/2;this._engine.push(),this._engine.translate(s,h),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y),t&&this._engine.cliprect(this.x,this.y,this.width,this.height)}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}shake(t=.3,s=1){this.shaking()||(this._shake.removeListener=this._engine.listen("update",h=>{this._shake.x=this._engine.randi(-s,s),this._shake.y=this._engine.randi(-s,s),t-=h,t<=0&&this.unshake()}))}unshake(){this.shaking()&&(this._shake.removeListener(),this._shake.removeListener=null,this._shake.x=this._shake.y=0)}shaking(){return this._shake.removeListener!==null}};globalThis.utils=Object.assign(globalThis.utils||{},{Camera:i});})();
1
+ (()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let h in globalThis.utils)h!=="global"&&(globalThis[h]=globalThis.utils[h])};var t=class{_engine=null;x=0;y=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(i=null){i=i||globalThis,this._engine=i,this.size(i.WIDTH||0,i.HEIGHT||0),this.x=this.width/2,this.y=this.height/2}size(i,s){this.width=i,this.height=s}start(i=!1){let s=this.width/2,e=this.height/2;this._engine.push(),this._engine.translate(s,e),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y),i&&this._engine.cliprect(this.x,this.y,this.width,this.height)}end(){this._engine.pop()}lookAt(i,s){this.x=i,this.y=s}move(i,s){this.x+=i,this.y+=s}zoom(i){this.scale*=i}zoomTo(i){this.scale=i}rotate(i){this.rotation+=i}rotateTo(i){this.rotation=i}shake(i=.3,s=1){this.shaking()||(this._shake.removeListener=this._engine.listen("update",e=>{this._shake.x=this._engine.randi(-s,s),this._shake.y=this._engine.randi(-s,s),i-=e,i<=0&&this.unshake()}))}unshake(){this.shaking()&&(this._shake.removeListener(),this._shake.removeListener=null,this._shake.x=this._shake.y=0)}shaking(){return this._shake.removeListener!==null}};globalThis.utils=Object.assign(globalThis.utils||{},{Camera:t});})();
package/dist/collision.js CHANGED
@@ -1,4 +1,13 @@
1
1
  (() => {
2
+ // src/_global.js
3
+ globalThis.utils = globalThis.utils || {};
4
+ globalThis.utils.global = () => {
5
+ for (const key in globalThis.utils) {
6
+ if ("global" === key) continue;
7
+ globalThis[key] = globalThis.utils[key];
8
+ }
9
+ };
10
+
2
11
  // src/collision/intersection.js
3
12
  var intersection_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
4
13
  const left = Math.max(x1, x2);
@@ -1 +1 @@
1
- (()=>{var h=(o,i,r,s,t,e,n,a)=>{let m=Math.max(o,t),u=Math.min(o+r,t+n)-m,f=Math.max(i,e),c=Math.min(i+s,e+a)-f;return[m,f,u,c]};var d=(o,i,r,s,t,e,n,a)=>{let[m,u,f,c]=h(o,i,r,s,t,e,n,a),l="",p=o,g=i;return f<c?o<t?(l="right",p=t-r):(l="left",p=t+n):i<e?(l="bottom",g=e-s):(l="top",g=e+a),{direction:l,x:p,y:g}};globalThis.utils=Object.assign(globalThis.utils||{},{resolve:d,intersection:h});})();
1
+ (()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let t in globalThis.utils)t!=="global"&&(globalThis[t]=globalThis.utils[t])};var g=(t,o,s,r,l,i,a,n)=>{let f=Math.max(t,l),c=Math.min(t+s,l+a)-f,h=Math.max(o,i),b=Math.min(o+r,i+n)-h;return[f,h,c,b]};var p=(t,o,s,r,l,i,a,n)=>{let[f,c,h,b]=g(t,o,s,r,l,i,a,n),e="",m=t,u=o;return h<b?t<l?(e="right",m=l-s):(e="left",m=l+a):o<i?(e="bottom",u=i-r):(e="top",u=i+n),{direction:e,x:m,y:u}};globalThis.utils=Object.assign(globalThis.utils||{},{resolve:p,intersection:g});})();
package/dist/grid.js CHANGED
@@ -1,5 +1,25 @@
1
1
  (() => {
2
+ var __defProp = Object.defineProperty;
3
+ var __export = (target, all) => {
4
+ for (var name in all)
5
+ __defProp(target, name, { get: all[name], enumerable: true });
6
+ };
7
+
8
+ // src/_global.js
9
+ globalThis.utils = globalThis.utils || {};
10
+ globalThis.utils.global = () => {
11
+ for (const key in globalThis.utils) {
12
+ if ("global" === key) continue;
13
+ globalThis[key] = globalThis.utils[key];
14
+ }
15
+ };
16
+
2
17
  // src/grid/index.js
18
+ var grid_exports = {};
19
+ __export(grid_exports, {
20
+ Grid: () => Grid,
21
+ TypedGrid: () => TypedGrid
22
+ });
3
23
  var Grid = class _Grid {
4
24
  /** @type {number} The grid width */
5
25
  _w;
@@ -7,49 +27,32 @@
7
27
  _h;
8
28
  /** @type {any[]} The grid cells */
9
29
  _c;
10
- /**
11
- * @static
12
- * @param {number} width
13
- * @param {number} height
14
- * @param {any[]} values
15
- */
16
- static fromArray(width, height, values) {
17
- const grid = new _Grid(width, height);
18
- for (let i = 0; i < values.length; i++) {
19
- grid._c[i] = values[i];
20
- }
21
- return grid;
22
- }
23
30
  /**
24
31
  * @param {number} width The grid width
25
32
  * @param {number} height The grid height
26
33
  */
27
- constructor(width, height) {
28
- this.width = width;
29
- this.height = height;
30
- this.clear();
34
+ constructor(width, height, values = []) {
35
+ this._w = Math.max(1, ~~width);
36
+ this._h = Math.max(1, ~~height);
37
+ this._c = values;
31
38
  }
32
39
  /**
33
40
  * Delete all cell values.
34
41
  */
35
42
  clear() {
36
- this._c = Array(this._w * this._h);
43
+ this.forEach((x, y) => this.set(x, y, void 0));
37
44
  }
38
45
  /**
39
- * @param {number} value
46
+ * @returns {number}
47
+ * @readonly
40
48
  */
41
- set width(value) {
42
- this._w = Math.max(1, ~~value);
43
- }
44
49
  get width() {
45
50
  return this._w;
46
51
  }
47
52
  /**
48
- * @param {number} value
53
+ * @returns {number}
54
+ * @readonly
49
55
  */
50
- set height(value) {
51
- this._h = Math.max(1, ~~value);
52
- }
53
56
  get height() {
54
57
  return this._h;
55
58
  }
@@ -174,7 +177,7 @@
174
177
  * @returns {any[]}
175
178
  */
176
179
  toArray() {
177
- return this._c.slice(0);
180
+ return this._c.slice();
178
181
  }
179
182
  /**
180
183
  * @param {string} separator
@@ -191,6 +194,34 @@
191
194
  return rows.join("\n");
192
195
  }
193
196
  };
197
+ var TypedGrid = class _TypedGrid extends Grid {
198
+ /**
199
+ * @param {number} width The grid width
200
+ * @param {number} height The grid height
201
+ */
202
+ constructor(width, height, TypedArray = Uint8Array) {
203
+ super(width, height, null);
204
+ this._c = new TypedArray(this._w * this._h);
205
+ }
206
+ /**
207
+ * @param {number} x
208
+ * @param {number} y
209
+ * @returns {boolean}
210
+ */
211
+ has(x, y) {
212
+ return this.get(x, y) !== 0;
213
+ }
214
+ /**
215
+ * @returns {TypedGrid}
216
+ */
217
+ clone() {
218
+ const copy = new _TypedGrid(this._w, this._h, this._c.constructor);
219
+ this.forEach((x, y, value) => {
220
+ copy.set(x, y, value);
221
+ });
222
+ return copy;
223
+ }
224
+ };
194
225
  function _clamp(value, min, max) {
195
226
  if (value < min) return min;
196
227
  if (value > max) return max;
@@ -198,5 +229,5 @@
198
229
  }
199
230
 
200
231
  // src/grid/_web.js
201
- globalThis.utils = Object.assign(globalThis.utils || {}, { Grid });
232
+ globalThis.utils = Object.assign(globalThis.utils || {}, grid_exports);
202
233
  })();
package/dist/grid.min.js CHANGED
@@ -1,2 +1,2 @@
1
- (()=>{var e=class s{_w;_h;_c;static fromArray(t,i,h){let n=new s(t,i);for(let r=0;r<h.length;r++)n._c[r]=h[r];return n}constructor(t,i){this.width=t,this.height=i,this.clear()}clear(){this._c=Array(this._w*this._h)}set width(t){this._w=Math.max(1,~~t)}get width(){return this._w}set height(t){this._h=Math.max(1,~~t)}get height(){return this._h}set(t,i,h){this._c[this.pointToIndex(t,i)]=h}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 h=i?this.length-1:0,n=i?-1:this.length,r=i?-1:1;for(;h!==n;){let o=this.indexToPointX(h),c=this.indexToPointY(h),_=this._c[h];if(t(o,c,_,this)===!1)break;h+=r}}fill(t){this.forEach((i,h)=>{this.set(i,h,t)})}clone(){return s.fromArray(this._w,this._h,this._c)}clampX(t){return l(t,0,this._w-1)}clampY(t){return l(t,0,this._h-1)}toArray(){return this._c.slice(0)}toString(t=" ",i=!0){if(!i)return this._c.join(t);let h=[];return this.forEach((n,r,o)=>{h[r]=h[r]||"",h[r]+=o+t}),h.join(`
2
- `)}};function l(s,t,i){return s<t?t:s>i?i:s}globalThis.utils=Object.assign(globalThis.utils||{},{Grid:e});})();
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);})();
package/dist/math.js CHANGED
@@ -1,4 +1,13 @@
1
1
  (() => {
2
+ // src/_global.js
3
+ globalThis.utils = globalThis.utils || {};
4
+ globalThis.utils.global = () => {
5
+ for (const key in globalThis.utils) {
6
+ if ("global" === key) continue;
7
+ globalThis[key] = globalThis.utils[key];
8
+ }
9
+ };
10
+
2
11
  // src/math/diff.js
3
12
  var diff_default = (a, b) => Math.abs(b - a) || 0;
4
13
 
package/dist/math.min.js CHANGED
@@ -1 +1 @@
1
- (()=>{var i=(t,a)=>Math.abs(a-t)||0;var o=(t,a,r,l=Math.sin)=>t+(l(r)+1)/2*(a-t);var f=t=>t%1||0;globalThis.utils=Object.assign(globalThis.utils||{},{diff:i,wave:o,fract:f});})();
1
+ (()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let l in globalThis.utils)l!=="global"&&(globalThis[l]=globalThis.utils[l])};var t=(l,i)=>Math.abs(i-l)||0;var o=(l,i,a,b=Math.sin)=>l+(b(a)+1)/2*(i-l);var s=l=>l%1||0;globalThis.utils=Object.assign(globalThis.utils||{},{diff:t,wave:o,fract:s});})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litecanvas/utils",
3
- "version": "0.5.1",
3
+ "version": "0.8.0",
4
4
  "description": "Utilities to help build litecanvas games",
5
5
  "author": "Luiz Bills <luizbills@pm.me>",
6
6
  "license": "MIT",
@@ -17,9 +17,10 @@
17
17
  "keywords": [
18
18
  "litecanvas",
19
19
  "utilities",
20
+ "helpers",
20
21
  "camera",
21
22
  "vector",
22
- "gamedev"
23
+ "grid"
23
24
  ],
24
25
  "scripts": {
25
26
  "dev": "esbuild src/_web.js --bundle --watch --outfile=dist/all.js --servedir=.",
package/src/_global.js ADDED
@@ -0,0 +1,8 @@
1
+ globalThis.utils = globalThis.utils || {}
2
+
3
+ globalThis.utils.global = () => {
4
+ for (const key in globalThis.utils) {
5
+ if ("global" === key) continue
6
+ globalThis[key] = globalThis.utils[key]
7
+ }
8
+ }
package/src/_web.js CHANGED
@@ -1,3 +1,4 @@
1
+ import "./_global.js"
1
2
  import * as utils from "./index.js"
2
3
 
3
- globalThis.utils = utils
4
+ globalThis.utils = Object.assign(globalThis.utils || {}, utils)
@@ -36,9 +36,18 @@ function draw() {
36
36
  }
37
37
  ```
38
38
 
39
+ ## Actor#pos
40
+
41
+ Set or get the actor position vector
42
+
43
+ ```js
44
+ player.pos.x = 100
45
+ player.pos.x = 200
46
+ ```
47
+
39
48
  ## Actor#x / Actor#y
40
49
 
41
- Set or get the actor position
50
+ Set or get the actor position X or Y
42
51
 
43
52
  ```js
44
53
  player.x = 100
@@ -122,7 +131,7 @@ Get (not set) the actor current height.
122
131
  ```js
123
132
  console.log(player.height) // => 30
124
133
 
125
- player.scale.x = 2
134
+ player.scale.y = 2
126
135
 
127
136
  console.log(player.height) // => 60
128
137
  ```
package/src/actor/_web.js CHANGED
@@ -1,3 +1,4 @@
1
+ import "../_global.js"
1
2
  import * as actorUtils from "./index.js"
2
3
 
3
4
  globalThis.utils = Object.assign(globalThis.utils || {}, actorUtils)
@@ -12,7 +12,7 @@ export class Actor {
12
12
  sprite
13
13
 
14
14
  /** @type {Vector} The actor position */
15
- _p
15
+ pos
16
16
 
17
17
  /** @type {Vector} The actor anchor (origin) */
18
18
  _o
@@ -35,7 +35,7 @@ export class Actor {
35
35
  */
36
36
  constructor(sprite, position) {
37
37
  this.sprite = sprite
38
- this._p = position || vec(0)
38
+ this.pos = position || vec(0)
39
39
  this._o = veccopy(ANCHOR_TOP_LEFT)
40
40
  this._s = vec(1, 1)
41
41
  }
@@ -44,28 +44,28 @@ export class Actor {
44
44
  * @param {number}
45
45
  */
46
46
  set x(value) {
47
- this._p.x = value
47
+ this.pos.x = value
48
48
  }
49
49
 
50
50
  /**
51
51
  * @returns {number}
52
52
  */
53
53
  get x() {
54
- return this._p.x
54
+ return this.pos.x
55
55
  }
56
56
 
57
57
  /**
58
58
  * @param {number}
59
59
  */
60
60
  set y(value) {
61
- this._p.y = value
61
+ this.pos.y = value
62
62
  }
63
63
 
64
64
  /**
65
65
  * @returns {number}
66
66
  */
67
67
  get y() {
68
- return this._p.y
68
+ return this.pos.y
69
69
  }
70
70
 
71
71
  /**
@@ -127,7 +127,7 @@ export class Actor {
127
127
  * @param {LitecanvasInstance} litecanvas
128
128
  */
129
129
  transform(litecanvas) {
130
- litecanvas.translate(this._p.x, this._p.y)
130
+ litecanvas.translate(this.pos.x, this.pos.y)
131
131
  litecanvas.rotate(this.angle)
132
132
  litecanvas.scale(this._s.x, this._s.y)
133
133
  litecanvas.alpha(this.opacity)
@@ -1,3 +1,4 @@
1
+ import "../_global.js"
1
2
  import Camera from "./index.js"
2
3
 
3
4
  globalThis.utils = Object.assign(globalThis.utils || {}, { Camera })
@@ -1,3 +1,4 @@
1
+ import "../_global.js"
1
2
  import resolve from "./resolve.js"
2
3
  import intersection from "./intersection.js"
3
4
 
@@ -1,4 +1,4 @@
1
- # Grid
1
+ # Grid utils
2
2
 
3
3
  **CDN**: https://unpkg.com/@litecanvas/utils/dist/grid.js
4
4
 
@@ -45,3 +45,17 @@ The result:
45
45
  # . . . . . . . . . . . #
46
46
  # # # # # # # # # # # # #
47
47
  ```
48
+
49
+ ## Typed Grid
50
+
51
+ 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
+
53
+ ```
54
+ import { TypedGrid } from "@litecanvas/utils"
55
+
56
+ let u8grid = new TypedGrid(5, 5) // by default, uses Uint8Array
57
+
58
+ let i16grid = new TypedGrid(5, 5, Int16Array) // or specify your typed array
59
+ ```
60
+
61
+ > Note: `TypedGrid` inherits all methods from `Grid`.
package/src/grid/_web.js CHANGED
@@ -1,3 +1,4 @@
1
- import Grid from "./index.js"
1
+ import "../_global.js"
2
+ import * as gridUtils from "./index.js"
2
3
 
3
- globalThis.utils = Object.assign(globalThis.utils || {}, { Grid })
4
+ globalThis.utils = Object.assign(globalThis.utils || {}, gridUtils)
package/src/grid/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export default class Grid {
1
+ export class Grid {
2
2
  /** @type {number} The grid width */
3
3
  _w
4
4
 
@@ -8,55 +8,35 @@ export default class Grid {
8
8
  /** @type {any[]} The grid cells */
9
9
  _c
10
10
 
11
- /**
12
- * @static
13
- * @param {number} width
14
- * @param {number} height
15
- * @param {any[]} values
16
- */
17
- static fromArray(width, height, values) {
18
- const grid = new Grid(width, height)
19
- for (let i = 0; i < values.length; i++) {
20
- grid._c[i] = values[i]
21
- }
22
- return grid
23
- }
24
-
25
11
  /**
26
12
  * @param {number} width The grid width
27
13
  * @param {number} height The grid height
28
14
  */
29
- constructor(width, height) {
30
- this.width = width
31
- this.height = height
32
- this.clear()
15
+ constructor(width, height, values = []) {
16
+ this._w = Math.max(1, ~~width)
17
+ this._h = Math.max(1, ~~height)
18
+ this._c = values
33
19
  }
34
20
 
35
21
  /**
36
22
  * Delete all cell values.
37
23
  */
38
24
  clear() {
39
- this._c = Array(this._w * this._h)
25
+ this.forEach((x, y) => this.set(x, y, undefined))
40
26
  }
41
27
 
42
28
  /**
43
- * @param {number} value
29
+ * @returns {number}
30
+ * @readonly
44
31
  */
45
- set width(value) {
46
- this._w = Math.max(1, ~~value)
47
- }
48
-
49
32
  get width() {
50
33
  return this._w
51
34
  }
52
35
 
53
36
  /**
54
- * @param {number} value
37
+ * @returns {number}
38
+ * @readonly
55
39
  */
56
- set height(value) {
57
- this._h = Math.max(1, ~~value)
58
- }
59
-
60
40
  get height() {
61
41
  return this._h
62
42
  }
@@ -201,7 +181,7 @@ export default class Grid {
201
181
  * @returns {any[]}
202
182
  */
203
183
  toArray() {
204
- return this._c.slice(0)
184
+ return this._c.slice()
205
185
  }
206
186
 
207
187
  /**
@@ -222,6 +202,37 @@ export default class Grid {
222
202
  }
223
203
  }
224
204
 
205
+ export class TypedGrid extends Grid {
206
+ /**
207
+ * @param {number} width The grid width
208
+ * @param {number} height The grid height
209
+ */
210
+ constructor(width, height, TypedArray = Uint8Array) {
211
+ super(width, height, null)
212
+ this._c = new TypedArray(this._w * this._h)
213
+ }
214
+
215
+ /**
216
+ * @param {number} x
217
+ * @param {number} y
218
+ * @returns {boolean}
219
+ */
220
+ has(x, y) {
221
+ return this.get(x, y) !== 0
222
+ }
223
+
224
+ /**
225
+ * @returns {TypedGrid}
226
+ */
227
+ clone() {
228
+ const copy = new TypedGrid(this._w, this._h, this._c.constructor)
229
+ this.forEach((x, y, value) => {
230
+ copy.set(x, y, value)
231
+ })
232
+ return copy
233
+ }
234
+ }
235
+
225
236
  /**
226
237
  * Constrains a number between `min` and `max`.
227
238
  *
@@ -235,5 +246,3 @@ function _clamp(value, min, max) {
235
246
  if (value > max) return max
236
247
  return value
237
248
  }
238
-
239
- function _fill(x, y) {}
package/src/index.js CHANGED
@@ -4,6 +4,6 @@ export { default as wave } from "./math/wave.js"
4
4
  export { default as resolve } from "./collision/resolve.js"
5
5
  export { default as intersection } from "./collision/intersection.js"
6
6
  export { default as Camera } from "./camera/index.js"
7
- export { default as Grid } from "./grid/index.js"
7
+ export * from "./grid/index.js"
8
8
  export * from "./vector/index.js"
9
9
  export * from "./actor/index.js"
package/src/math/_web.js CHANGED
@@ -1,3 +1,4 @@
1
+ import "../_global.js"
1
2
  import diff from "./diff.js"
2
3
  import wave from "./wave.js"
3
4
  import fract from "./fract.js"