@litecanvas/utils 0.37.0 → 0.39.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 +20 -23
- package/dist/actor.min.js +1 -1
- package/dist/all.js +44 -39
- package/dist/all.min.js +2 -2
- package/dist/math.min.js +1 -1
- package/dist/vector.js +26 -22
- package/dist/vector.min.js +1 -1
- package/package.json +1 -1
- package/src/actor/index.js +23 -17
- package/src/vector/README.md +12 -4
- package/src/vector/index.js +75 -49
package/dist/actor.js
CHANGED
|
@@ -30,17 +30,13 @@
|
|
|
30
30
|
// src/vector/index.js
|
|
31
31
|
var PI2 = 2 * Math.PI;
|
|
32
32
|
var Vector = class {
|
|
33
|
-
/** @type {number} */
|
|
34
|
-
x;
|
|
35
|
-
/** @type {number} */
|
|
36
|
-
y;
|
|
37
33
|
/**
|
|
38
34
|
* @param {number} [x=0]
|
|
39
35
|
* @param {number} [y]
|
|
40
36
|
*/
|
|
41
37
|
constructor(x = 0, y = x) {
|
|
42
|
-
this.x = x;
|
|
43
|
-
this.y = y;
|
|
38
|
+
this.x = parseFloat(x) || 0;
|
|
39
|
+
this.y = parseFloat(y) || 0;
|
|
44
40
|
}
|
|
45
41
|
/**
|
|
46
42
|
* @returns {string}
|
|
@@ -89,7 +85,7 @@
|
|
|
89
85
|
* @param {Vector} anchor
|
|
90
86
|
*/
|
|
91
87
|
constructor(sprite, position, anchor = ANCHOR_TOP_LEFT) {
|
|
92
|
-
this.sprite = sprite;
|
|
88
|
+
this.sprite = sprite || { width: 0, height: 0 };
|
|
93
89
|
this.pos = position || vec(0);
|
|
94
90
|
this._o = vec(anchor);
|
|
95
91
|
this._s = vec(1, 1);
|
|
@@ -182,35 +178,36 @@
|
|
|
182
178
|
/**
|
|
183
179
|
* Draw the actor
|
|
184
180
|
*
|
|
185
|
-
* @param {LitecanvasInstance} [
|
|
181
|
+
* @param {LitecanvasInstance} [engine]
|
|
186
182
|
*/
|
|
187
|
-
draw(
|
|
188
|
-
if (
|
|
189
|
-
|
|
190
|
-
this.
|
|
191
|
-
|
|
192
|
-
|
|
183
|
+
draw(engine = globalThis, saveContext = true) {
|
|
184
|
+
if (saveContext) engine.push();
|
|
185
|
+
this.transform(engine);
|
|
186
|
+
if (this.sprite.width && this.sprite.height && !this.hidden && this.opacity > 0) {
|
|
187
|
+
this.drawImage(engine);
|
|
188
|
+
}
|
|
189
|
+
if (saveContext) engine.pop();
|
|
193
190
|
}
|
|
194
191
|
/**
|
|
195
|
-
* @param {LitecanvasInstance}
|
|
192
|
+
* @param {LitecanvasInstance} engine
|
|
196
193
|
*/
|
|
197
|
-
transform(
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
194
|
+
transform(engine) {
|
|
195
|
+
engine.translate(this.pos.x, this.pos.y);
|
|
196
|
+
engine.rotate(engine.deg2rad(this.angle));
|
|
197
|
+
engine.scale(
|
|
201
198
|
(this.flipX ? -1 : 1) * this._s.x,
|
|
202
199
|
(this.flipY ? -1 : 1) * this._s.y
|
|
203
200
|
);
|
|
204
201
|
}
|
|
205
202
|
/**
|
|
206
|
-
* @param {LitecanvasInstance}
|
|
203
|
+
* @param {LitecanvasInstance} engine
|
|
207
204
|
*/
|
|
208
|
-
drawImage(
|
|
205
|
+
drawImage(engine, alpha = true) {
|
|
209
206
|
const anchor = this.anchor;
|
|
210
207
|
const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x);
|
|
211
208
|
const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y);
|
|
212
|
-
if (alpha)
|
|
213
|
-
|
|
209
|
+
if (alpha) engine.alpha(this.opacity);
|
|
210
|
+
engine.image(x, y, this.sprite);
|
|
214
211
|
}
|
|
215
212
|
};
|
|
216
213
|
|
package/dist/actor.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var x=Object.defineProperty;var
|
|
1
|
+
(()=>{var x=Object.defineProperty;var a=(o,t)=>{for(var s in t)x(o,s,{get:t[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(o=!0)=>{for(let t in globalThis.utils)t!=="global"&&(o||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var p={};a(p,{ANCHOR_BOT_LEFT:()=>g,ANCHOR_BOT_RIGHT:()=>d,ANCHOR_CENTER:()=>u,ANCHOR_TOP_LEFT:()=>y,ANCHOR_TOP_RIGHT:()=>f,Actor:()=>h});var T=2*Math.PI,i=class{constructor(t=0,s=t){this.x=parseFloat(t)||0,this.y=parseFloat(s)||0}toString(){return`Vector (${this.x}, ${this.y})`}},l=o=>o instanceof i,e=(o=0,t=o)=>(l(o)&&(t=o.y,o=o.x),new i(o,t));var u=e(.5,.5),y=e(0,0),f=e(1,0),g=e(0,1),d=e(1,1),h=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,r=y){this.sprite=t||{width:0,height:0},this.pos=s||e(0),this._o=e(r),this._s=e(1,1)}set x(t){this.pos.x=t}get x(){return this.pos.x}set y(t){this.pos.y=t}get y(){return this.pos.y}set anchor(t){this._o.x=t.x,this._o.y=t.y}get anchor(){return this._o}get width(){return this.sprite.width*this._s.x}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}scaleTo(t,s=t){this._s.x=t,this._s.y=s}scaleBy(t,s=t){this._s.x*=t,this._s.y*=s}getBounds(t=!0){let s=this.sprite.width*(t?this._s.x:1),r=this.sprite.height*(t?this._s.y:1),c=this.pos.x-s*this.anchor.x,n=this.pos.y-r*this.anchor.y;return[c,n,s,r]}draw(t=globalThis,s=!0){s&&t.push(),this.transform(t),this.sprite.width&&this.sprite.height&&!this.hidden&&this.opacity>0&&this.drawImage(t),s&&t.pop()}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(t.deg2rad(this.angle)),t.scale((this.flipX?-1:1)*this._s.x,(this.flipY?-1:1)*this._s.y)}drawImage(t,s=!0){let r=this.anchor,c=-this.sprite.width*(this.flipX?1-r.x:r.x),n=-this.sprite.height*(this.flipY?1-r.y:r.y);s&&t.alpha(this.opacity),t.image(c,n,this.sprite)}};globalThis.utils=Object.assign(globalThis.utils||{},p);})();
|
package/dist/all.js
CHANGED
|
@@ -91,6 +91,7 @@
|
|
|
91
91
|
vecDot: () => vecDot,
|
|
92
92
|
vecEq: () => vecEq,
|
|
93
93
|
vecFloor: () => vecFloor,
|
|
94
|
+
vecHeading: () => vecHeading,
|
|
94
95
|
vecIsZero: () => vecIsZero,
|
|
95
96
|
vecLerp: () => vecLerp,
|
|
96
97
|
vecLimit: () => vecLimit,
|
|
@@ -101,11 +102,13 @@
|
|
|
101
102
|
vecNorm: () => vecNorm,
|
|
102
103
|
vecRand: () => vecRand,
|
|
103
104
|
vecReflect: () => vecReflect,
|
|
105
|
+
vecRem: () => vecRem,
|
|
104
106
|
vecRotate: () => vecRotate,
|
|
105
107
|
vecRound: () => vecRound,
|
|
106
108
|
vecSet: () => vecSet,
|
|
107
109
|
vecSetMag: () => vecSetMag,
|
|
108
|
-
vecSub: () => vecSub
|
|
110
|
+
vecSub: () => vecSub,
|
|
111
|
+
vecToArray: () => vecToArray
|
|
109
112
|
});
|
|
110
113
|
|
|
111
114
|
// src/camera/index.js
|
|
@@ -580,22 +583,17 @@
|
|
|
580
583
|
}
|
|
581
584
|
|
|
582
585
|
// src/vector/index.js
|
|
583
|
-
var sqrt = Math.sqrt;
|
|
584
586
|
var cos = Math.cos;
|
|
585
587
|
var sin = Math.sin;
|
|
586
588
|
var PI2 = 2 * Math.PI;
|
|
587
589
|
var Vector = class {
|
|
588
|
-
/** @type {number} */
|
|
589
|
-
x;
|
|
590
|
-
/** @type {number} */
|
|
591
|
-
y;
|
|
592
590
|
/**
|
|
593
591
|
* @param {number} [x=0]
|
|
594
592
|
* @param {number} [y]
|
|
595
593
|
*/
|
|
596
594
|
constructor(x = 0, y = x) {
|
|
597
|
-
this.x = x;
|
|
598
|
-
this.y = y;
|
|
595
|
+
this.x = parseFloat(x) || 0;
|
|
596
|
+
this.y = parseFloat(y) || 0;
|
|
599
597
|
}
|
|
600
598
|
/**
|
|
601
599
|
* @returns {string}
|
|
@@ -612,12 +610,6 @@
|
|
|
612
610
|
}
|
|
613
611
|
return new Vector(x, y);
|
|
614
612
|
};
|
|
615
|
-
var vecEq = (v, x, y = x) => {
|
|
616
|
-
if (isVector(x)) {
|
|
617
|
-
return vecEq(v, x.x, x.y);
|
|
618
|
-
}
|
|
619
|
-
return v.x === x && v.y === y;
|
|
620
|
-
};
|
|
621
613
|
var vecSet = (v, x, y = x) => {
|
|
622
614
|
if (isVector(x)) {
|
|
623
615
|
vecSet(v, x.x, x.y);
|
|
@@ -686,8 +678,7 @@
|
|
|
686
678
|
var vecLimit = (v, max = 1) => {
|
|
687
679
|
const sq = vecMag2(v);
|
|
688
680
|
if (sq > max * max) {
|
|
689
|
-
|
|
690
|
-
vecMult(v, max);
|
|
681
|
+
vecSetMag(v, max);
|
|
691
682
|
}
|
|
692
683
|
return v;
|
|
693
684
|
};
|
|
@@ -699,7 +690,8 @@
|
|
|
699
690
|
const dy = a.y - b.y;
|
|
700
691
|
return dx * dx + dy * dy;
|
|
701
692
|
};
|
|
702
|
-
var
|
|
693
|
+
var vecHeading = (v) => Math.atan2(v.y, v.x);
|
|
694
|
+
var vecAngle = (v) => vecHeading(v);
|
|
703
695
|
var vecAngleBetween = (v1, v2) => Math.atan2(v2.y - v1.y, v2.x - v1.x);
|
|
704
696
|
var vecDot = (a, b) => a.x * b.x + a.y * b.y;
|
|
705
697
|
var vecCross = (a, b) => a.x * b.y - a.y * b.x;
|
|
@@ -708,11 +700,6 @@
|
|
|
708
700
|
a.y += (b.y - a.y) * t || 0;
|
|
709
701
|
return a;
|
|
710
702
|
};
|
|
711
|
-
var vecRand = (minlength = 1, maxlength = minlength, rng = globalThis.rand || Math.random) => {
|
|
712
|
-
const angle = rng() * PI2;
|
|
713
|
-
const radius = rng() * (maxlength - minlength) + minlength;
|
|
714
|
-
return vec(cos(angle) * radius, sin(angle) * radius);
|
|
715
|
-
};
|
|
716
703
|
var vecAbs = (v) => {
|
|
717
704
|
v.x = Math.abs(v.x);
|
|
718
705
|
v.y = Math.abs(v.y);
|
|
@@ -740,8 +727,25 @@
|
|
|
740
727
|
if (v.y > max.y) v.y = max.y;
|
|
741
728
|
return v;
|
|
742
729
|
};
|
|
730
|
+
var vecRem = (v, value) => {
|
|
731
|
+
v.x %= value;
|
|
732
|
+
v.y %= value;
|
|
733
|
+
return v;
|
|
734
|
+
};
|
|
743
735
|
var vecMove = (from, to, delta = 1) => vecAdd(from, to.x * delta, to.y * delta);
|
|
736
|
+
var vecEq = (v, x, y = x) => {
|
|
737
|
+
if (isVector(x)) {
|
|
738
|
+
return vecEq(v, x.x, x.y);
|
|
739
|
+
}
|
|
740
|
+
return v.x === x && v.y === y;
|
|
741
|
+
};
|
|
744
742
|
var vecIsZero = (v) => vecEq(v, ZERO);
|
|
743
|
+
var vecToArray = (v) => [v.x, v.y];
|
|
744
|
+
var vecRand = (minlength = 1, maxlength = minlength, rng = globalThis.rand || Math.random) => {
|
|
745
|
+
const angle = rng() * PI2;
|
|
746
|
+
const radius = rng() * (maxlength - minlength) + minlength;
|
|
747
|
+
return vec(cos(angle) * radius, sin(angle) * radius);
|
|
748
|
+
};
|
|
745
749
|
var ZERO = /* @__PURE__ */ vec(0, 0);
|
|
746
750
|
var ONE = /* @__PURE__ */ vec(1, 1);
|
|
747
751
|
var UP = /* @__PURE__ */ vec(0, -1);
|
|
@@ -780,7 +784,7 @@
|
|
|
780
784
|
* @param {Vector} anchor
|
|
781
785
|
*/
|
|
782
786
|
constructor(sprite, position, anchor = ANCHOR_TOP_LEFT) {
|
|
783
|
-
this.sprite = sprite;
|
|
787
|
+
this.sprite = sprite || { width: 0, height: 0 };
|
|
784
788
|
this.pos = position || vec(0);
|
|
785
789
|
this._o = vec(anchor);
|
|
786
790
|
this._s = vec(1, 1);
|
|
@@ -873,35 +877,36 @@
|
|
|
873
877
|
/**
|
|
874
878
|
* Draw the actor
|
|
875
879
|
*
|
|
876
|
-
* @param {LitecanvasInstance} [
|
|
880
|
+
* @param {LitecanvasInstance} [engine]
|
|
877
881
|
*/
|
|
878
|
-
draw(
|
|
879
|
-
if (
|
|
880
|
-
|
|
881
|
-
this.
|
|
882
|
-
|
|
883
|
-
|
|
882
|
+
draw(engine = globalThis, saveContext = true) {
|
|
883
|
+
if (saveContext) engine.push();
|
|
884
|
+
this.transform(engine);
|
|
885
|
+
if (this.sprite.width && this.sprite.height && !this.hidden && this.opacity > 0) {
|
|
886
|
+
this.drawImage(engine);
|
|
887
|
+
}
|
|
888
|
+
if (saveContext) engine.pop();
|
|
884
889
|
}
|
|
885
890
|
/**
|
|
886
|
-
* @param {LitecanvasInstance}
|
|
891
|
+
* @param {LitecanvasInstance} engine
|
|
887
892
|
*/
|
|
888
|
-
transform(
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
893
|
+
transform(engine) {
|
|
894
|
+
engine.translate(this.pos.x, this.pos.y);
|
|
895
|
+
engine.rotate(engine.deg2rad(this.angle));
|
|
896
|
+
engine.scale(
|
|
892
897
|
(this.flipX ? -1 : 1) * this._s.x,
|
|
893
898
|
(this.flipY ? -1 : 1) * this._s.y
|
|
894
899
|
);
|
|
895
900
|
}
|
|
896
901
|
/**
|
|
897
|
-
* @param {LitecanvasInstance}
|
|
902
|
+
* @param {LitecanvasInstance} engine
|
|
898
903
|
*/
|
|
899
|
-
drawImage(
|
|
904
|
+
drawImage(engine, alpha = true) {
|
|
900
905
|
const anchor = this.anchor;
|
|
901
906
|
const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x);
|
|
902
907
|
const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y);
|
|
903
|
-
if (alpha)
|
|
904
|
-
|
|
908
|
+
if (alpha) engine.alpha(this.opacity);
|
|
909
|
+
engine.image(x, y, this.sprite);
|
|
905
910
|
}
|
|
906
911
|
};
|
|
907
912
|
|
package/dist/all.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(()=>{var
|
|
2
|
-
`)}},
|
|
1
|
+
(()=>{var bt=Object.defineProperty;var Pt=(e,t)=>{for(var s in t)bt(e,s,{get:t[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(e=!0)=>{for(let t in globalThis.utils)t!=="global"&&(e||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var Z={};Pt(Z,{ANCHOR_BOT_LEFT:()=>vt,ANCHOR_BOT_RIGHT:()=>te,ANCHOR_CENTER:()=>Jt,ANCHOR_TOP_LEFT:()=>v,ANCHOR_TOP_RIGHT:()=>Qt,Actor:()=>O,BACK_IN:()=>oe,BACK_IN_OUT:()=>ae,BACK_OUT:()=>he,BOUNCE_IN:()=>pt,BOUNCE_IN_OUT:()=>pe,BOUNCE_OUT:()=>C,Camera:()=>y,DOWN:()=>$t,EASE_IN:()=>se,EASE_IN_OUT:()=>ie,EASE_OUT:()=>re,ELASTIC_IN:()=>ne,ELASTIC_IN_OUT:()=>le,ELASTIC_OUT:()=>ce,Grid:()=>w,LEFT:()=>Gt,LINEAR:()=>lt,Noise:()=>Y,ONE:()=>qt,RIGHT:()=>Kt,TypedGrid:()=>T,UP:()=>zt,Vector:()=>I,ZERO:()=>Q,advance:()=>st,choose:()=>yt,colcirc:()=>X,colrect:()=>W,diff:()=>tt,dist:()=>it,flipImage:()=>ut,formatTime:()=>wt,fract:()=>et,head:()=>It,intersection:()=>M,last:()=>Et,lerpAngle:()=>nt,mag:()=>ot,makeCircle:()=>xt,makeRectangle:()=>dt,mean:()=>ht,median:()=>at,mod:()=>rt,percent:()=>ct,range:()=>mt,resolverect:()=>H,scaleImage:()=>ft,shuffle:()=>gt,sum:()=>P,tail:()=>Mt,tintImage:()=>_t,tween:()=>ee,vec:()=>h,vecAbs:()=>Bt,vecAdd:()=>N,vecAngle:()=>St,vecAngleBetween:()=>Ot,vecCeil:()=>Yt,vecClamp:()=>Ht,vecCross:()=>Ft,vecDist:()=>Lt,vecDist2:()=>kt,vecDiv:()=>L,vecDot:()=>J,vecEq:()=>S,vecFloor:()=>Zt,vecHeading:()=>G,vecIsZero:()=>Dt,vecLerp:()=>Ct,vecLimit:()=>At,vecMag:()=>K,vecMag2:()=>$,vecMove:()=>Xt,vecMult:()=>b,vecNorm:()=>k,vecRand:()=>jt,vecReflect:()=>Nt,vecRem:()=>Wt,vecRotate:()=>Tt,vecRound:()=>Ut,vecSet:()=>q,vecSetMag:()=>z,vecSub:()=>A,vecToArray:()=>Vt});var y=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;constructor(t=null,s=0,r=0,i=null,o=null){this._engine=t||globalThis,this.ox=s,this.oy=r,this.resize(i||this._engine.W-s,o||this._engine.H-r),this.x=this.width/2,this.y=this.height/2,this._shake={x:0,y:0,removeListener:null}}resize(t,s){this.width=t,this.height=s,this._engine.emit("camera-resized",this)}start(t=!1){this._engine.push(),t&&this._engine.clip(i=>{i.rect(this.ox,this.oy,this.width,this.height)});let s=this.ox+this.width/2,r=this.oy+this.height/2;this._engine.translate(s,r),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y)}end(){this._engine.pop()}lookAt(t,s){this.x=t,this.y=s}move(t,s){this.x+=t,this.y+=s}zoom(t){this.scale*=t}zoomTo(t){this.scale=t}rotate(t){this.rotation+=t}rotateTo(t){this.rotation=t}getWorldPoint(t,s,r={}){let i=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,r.x=i*t-o*s+this.x,r.y=o*t+i*s+this.y,r}getCameraPoint(t,s,r={}){let i=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=t-this.x,s=s-this.y,t=i*t-o*s,s=o*t+i*s,r.x=t*this.scale+this.width/2+this.ox,r.y=s*this.scale+this.height/2+this.oy,r}getBounds(){return[this.ox,this.oy,this.width,this.height]}shake(t=1,s=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",r=>{this._shake.x=this._engine.randi(-t,t),this._shake.y=this._engine.randi(-t,t),s-=r,s<=0&&this.unshake()}))}unshake(){this.shaking&&(this._shake.removeListener(),this._shake.removeListener=null,this._shake.x=this._shake.y=0)}get shaking(){return this._shake.removeListener!==null}};var M=(e,t,s,r,i,o,a,n)=>{let c=Math.max(e,i),x=Math.min(e+s,i+a)-c,p=Math.max(t,o),d=Math.min(t+r,o+n)-p;return[c,p,x,d]};var H=(e,t,s,r,i,o,a,n)=>{let[c,x,p,d]=M(e,t,s,r,i,o,a,n),_="",g=e,l=t;return p<d?e<i?(_="right",g=i-s):(_="left",g=i+a):t<o?(_="bottom",l=o-r):(_="top",l=o+n),{direction:_,x:g,y:l}};var W=(e,t,s,r,i,o,a,n)=>e<i+a&&e+s>i&&t<o+n&&t+r>o;var X=(e,t,s,r,i,o)=>(r-e)*(r-e)+(i-t)*(i-t)<=(s+o)*(s+o);var w=class e{_w;_h;_c;constructor(t,s,r=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~s),this._c=r}[Symbol.iterator](){let t=0;return{next:()=>({value:[this.indexToPointX(t),this.indexToPointY(t),this._c[t++]],done:t>this._c.length})}}clone(){return new e(this._w,this._h,this._c)}clear(){this.forEach((t,s)=>this.set(t,s,void 0))}get width(){return this._w}get height(){return this._h}set(t,s,r){this._c[this.pointToIndex(t,s)]=r}get(t,s){return this._c[this.pointToIndex(t,s)]}has(t,s){return this.get(t,s)!=null}check(t,s){return t>=0&&t<this._w&&s>=0&&s<this._h}get length(){return this._w*this._h}pointToIndex(t,s){return this.clampX(~~t)+this.clampY(~~s)*this._w}indexToPointX(t){return t%this._w}indexToPointY(t){return Math.floor(t/this._w)}forEach(t,s=!1){let r=s?this.length-1:0,i=s?-1:this.length,o=s?-1:1;for(;r!==i;){let a=this.indexToPointX(r),n=this.indexToPointY(r),c=this._c[r];if(t(a,n,c,this)===!1)break;r+=o}}fill(t){this.forEach((s,r)=>{this.set(s,r,t)})}clampX(t){return D(t,0,this._w-1)}clampY(t){return D(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",s=!0){if(!s)return this._c.join(t);let r=[];return this.forEach((i,o,a)=>{r[o]=r[o]||"",r[o]+=a+t}),r.join(`
|
|
2
|
+
`)}},T=class e extends w{constructor(t,s,r=Uint8Array){super(t,s,null),this._c=new r(this._w*this._h)}has(t,s){return this.get(t,s)!==0}clone(){let t=new e(this._w,this._h,this._c.constructor);return this.forEach((s,r,i)=>{t.set(s,r,i)}),t}};function D(e,t,s){return e<t?t:e>s?s:e}var V=Math.cos,j=Math.sin,Rt=2*Math.PI,I=class{constructor(t=0,s=t){this.x=parseFloat(t)||0,this.y=parseFloat(s)||0}toString(){return`Vector (${this.x}, ${this.y})`}},m=e=>e instanceof I,h=(e=0,t=e)=>(m(e)&&(t=e.y,e=e.x),new I(e,t)),q=(e,t,s=t)=>(m(t)?q(e,t.x,t.y):(e.x=t,e.y=s),e),N=(e,t,s=t)=>m(t)?N(e,t.x,t.y):(e.x+=t,e.y+=s,e),A=(e,t,s=t)=>m(t)?A(e,t.x,t.y):(e.x-=t,e.y-=s,e),b=(e,t,s=t)=>m(t)?b(e,t.x,t.y):(e.x*=t,e.y*=s,e),L=(e,t,s=t)=>m(t)?L(e,t.x,t.y):(e.x/=t||1,e.y/=s||1,e),Tt=(e,t)=>{let s=V(t),r=j(t);return e.x=s*e.x-r*e.y,e.y=r*e.x+s*e.y,e},Nt=(e,t)=>{let s=k(h(t));return A(e,b(s,2*J(e,s)))},z=(e,t)=>(k(e),b(e,t),e),K=e=>Math.hypot(e.x,e.y),$=e=>e.x*e.x+e.y*e.y,k=e=>{let t=K(e);return t>0&&L(e,t),e},At=(e,t=1)=>($(e)>t*t&&z(e,t),e),Lt=(e,t)=>Math.hypot(t.x-e.x,t.y-e.y),kt=(e,t)=>{let s=e.x-t.x,r=e.y-t.y;return s*s+r*r},G=e=>Math.atan2(e.y,e.x),St=e=>G(e),Ot=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),J=(e,t)=>e.x*t.x+e.y*t.y,Ft=(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),Bt=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),Yt=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),Zt=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),Ut=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),Ht=(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),Wt=(e,t)=>(e.x%=t,e.y%=t,e),Xt=(e,t,s=1)=>N(e,t.x*s,t.y*s),S=(e,t,s=t)=>m(t)?S(e,t.x,t.y):e.x===t&&e.y===s,Dt=e=>S(e,Q),Vt=e=>[e.x,e.y],jt=(e=1,t=e,s=globalThis.rand||Math.random)=>{let r=s()*Rt,i=s()*(t-e)+e;return h(V(r)*i,j(r)*i)},Q=h(0,0),qt=h(1,1),zt=h(0,-1),Kt=h(1,0),$t=h(0,1),Gt=h(-1,0);var Jt=h(.5,.5),v=h(0,0),Qt=h(1,0),vt=h(0,1),te=h(1,1),O=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,r=v){this.sprite=t||{width:0,height:0},this.pos=s||h(0),this._o=h(r),this._s=h(1,1)}set x(t){this.pos.x=t}get x(){return this.pos.x}set y(t){this.pos.y=t}get y(){return this.pos.y}set anchor(t){this._o.x=t.x,this._o.y=t.y}get anchor(){return this._o}get width(){return this.sprite.width*this._s.x}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}scaleTo(t,s=t){this._s.x=t,this._s.y=s}scaleBy(t,s=t){this._s.x*=t,this._s.y*=s}getBounds(t=!0){let s=this.sprite.width*(t?this._s.x:1),r=this.sprite.height*(t?this._s.y:1),i=this.pos.x-s*this.anchor.x,o=this.pos.y-r*this.anchor.y;return[i,o,s,r]}draw(t=globalThis,s=!0){s&&t.push(),this.transform(t),this.sprite.width&&this.sprite.height&&!this.hidden&&this.opacity>0&&this.drawImage(t),s&&t.pop()}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(t.deg2rad(this.angle)),t.scale((this.flipX?-1:1)*this._s.x,(this.flipY?-1:1)*this._s.y)}drawImage(t,s=!0){let r=this.anchor,i=-this.sprite.width*(this.flipX?1-r.x:r.x),o=-this.sprite.height*(this.flipY?1-r.y:r.y);s&&t.alpha(this.opacity),t.image(i,o,this.sprite)}};var tt=(e,t)=>Math.abs(t-e)||0;var et=e=>e%1||0;var st=(e,t,s,r=1)=>{s&&(t.x+=s.x*r,t.y+=s.y*r),e.x+=t.x*r,e.y+=t.y*r};var rt=(e,t)=>(t+e%t)%t;var it=(e,t,s,r)=>Math.hypot(s-e,r-t);var ot=(e,t)=>Math.hypot(e,t);var P=e=>{let t=0;for(let s=0;s<e.length;s++)t+=e[s];return t};var ht=e=>P(e)/e.length;var at=(...e)=>{let t=e.sort((r,i)=>r-i),s=Math.floor(t.length/2);return t.length%2===0?(t[s-1]+t[s])/2:t[s]};var nt=(e,t,s)=>{let r=(t-e)%360;return r>180?r-=360:r<-180&&(r+=360),e+r*s};var ct=(e,t=0,s=1)=>s-t?(e-t)/(s-t):0;var R=Math.PI/2,ee=(e,t,s,r=1,i=lt)=>new F(e,t,s,r,i),lt=e=>e,se=e=>e*e,re=e=>-e*(e-2),ie=e=>e<.5?2*e*e:-2*e*e+4*e-1,oe=e=>e*e*e-e*Math.sin(e*Math.PI),he=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},ae=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},ne=e=>Math.sin(13*R*e)*Math.pow(2,10*(e-1)),ce=e=>Math.sin(-13*R*(e+1))*Math.pow(2,-10*e)+1,le=e=>{if(e<.5){let r=Math.sin(13*R*(2*e)),i=Math.pow(2,10*(2*e-1));return .5*r*i}let t=Math.sin(-13*R*(2*e-1+1)),s=Math.pow(2,-10*(2*e-1));return .5*(t*s+2)},pt=e=>1-C(1-e),C=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,pe=e=>e<.5?.5*pt(e*2):.5*C(e*2-1)+.5,F=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(t,s,r,i,o){this._o=t,this._p=s,this._x=r,this._d=i,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,r=this._rel?s+this._x:this._x;return this._lc=this._lc||t||globalThis,this._u=this._lc.listen("update",i=>{if(this._t<=this._w){this._t+=i;return}let o=this._t-this._w;this._o[this._p]=this._lc.lerp(s,r,this._e(o/this._d)),this._t+=i,o>=this._d&&(this._o[this._p]=r,this.stop())}),this}stop(t=!0){if(!this._u)return this;if(this.running=!1,this._u(),this._t=0,t)for(let s of this._cb)s(this._o);return this}restart(t=null,s=!1){return this.stop(s).restart(t)}onEnd(t){return this._cb.push(t),this}chain(t){return this._ch.onEnd(()=>{this._cu=t.start(this._lc)}),this._ch=t,this}reset(){return this._cb.length=0,this.stop()}relative(t=!0){return this._rel=t,this}delay(t){return this._w=t,this}get current(){return this._cu}get progress(){return this.running&&this._t>this._w?(this._t-this._w)/this._d:0}};var B=e=>.5*(1-Math.cos(e*Math.PI)),Y=class{_p=[];_po=4;_pf=.5;_e=null;constructor(t){this._e=t||globalThis,this.noiseSeed()}noise(t,s=0,r=0){t<0&&(t=-t),s<0&&(s=-s),r<0&&(r=-r);let i=Math.floor(t),o=Math.floor(s),a=Math.floor(r),n=t-i,c=s-o,x=r-a,p,d,_=0,g=.5,l,u,E;for(let U=0;U<this._po;U++){let f=i+(o<<4)+(a<<8);p=B(n),d=B(c),l=this._p[f&4095],l+=p*(this._p[f+1&4095]-l),u=this._p[f+16&4095],u+=p*(this._p[f+16+1&4095]-u),l+=d*(u-l),f+=256,u=this._p[f&4095],u+=p*(this._p[f+1&4095]-u),E=this._p[f+16&4095],E+=p*(this._p[f+16+1&4095]-E),u+=d*(E-u),l+=B(x)*(u-l),_+=l*g,g*=this._pf,i<<=1,n*=2,o<<=1,c*=2,a<<=1,x*=2,n>=1&&(i++,n--),c>=1&&(o++,c--),x>=1&&(a++,x--)}return _}noiseDetail(t,s){t>0&&(this._po=t),s>0&&(this._pf=s)}noiseSeed(t=null){t!=null&&this._e.rseed(t);let s=this._e.rand||Math.random;for(let r=0;r<4096;r++)this._p[r]=s()}};var ut=(e,t=!0,s=!1,r=globalThis)=>r.paint(e.width,e.height,i=>{r.push(),r.scale(t?-1:1,s?-1:1),r.image(t?-e.width:0,s?-e.height:0,e),r.pop()});var ft=(e,t,s=!0,r=globalThis)=>r.paint(e.width*t,e.height*t,i=>{r.push(),i.imageSmoothingEnabled=!s,r.scale(t),r.image(0,0,e),r.pop()});var _t=(e,t,s=1,r=globalThis)=>r.paint(e.width,e.height,i=>{r.push(),r.alpha(s),r.rectfill(0,0,e.width,e.height,t),i.globalCompositeOperation="destination-atop",r.alpha(1),r.image(0,0,e),r.pop()});var xt=(e,t,{borderWidth:s=0,borderColor:r=0,engine:i=globalThis}={})=>{let o=e*2+s;return i.paint(o,o,()=>{i.circfill(o/2,o/2,e,t),s>0&&(i.linewidth(s),i.stroke(r))})};var dt=(e,t,s,{borderWidth:r=0,borderColor:i=0,engine:o=globalThis}={})=>{let a=e+r*2,n=t+r*2;return o.paint(a,n,()=>{let c=r>0;c&&o.cls(i),o.rectfill(c?r:0,c?r:0,e,t,s)})};var mt=(e,t=0,s=1)=>[...new Array(e).keys()].map(r=>t+s*r);var gt=(e,t=globalThis.rand||Math.random)=>{e=[...e];for(let s=e.length-1;s>0;s--){let r=Math.floor(t()*(s+1)),i=e[s];e[s]=e[r],e[r]=i}return e};var yt=(e,t=globalThis.rand||Math.random)=>e[Math.floor(t()*e.length)];var It=e=>e[0];var Et=e=>e[e.length-1];var Mt=e=>e.slice(1);var wt=e=>~~(e/60)+":"+(e%60<10?"0":"")+~~(e%60);globalThis.utils=Object.assign(globalThis.utils||{},Z);})();
|
|
3
3
|
/*! @litecanvas/utils by Luiz Bills | MIT Licensed */
|
package/dist/math.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var g=Object.defineProperty;var M=(e,t)=>{for(var r in t)g(e,r,{get:t[r],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(e=!0)=>{for(let t in globalThis.utils)t!=="global"&&(e||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var n={};M(n,{advance:()=>p,diff:()=>c,dist:()=>a,fract:()=>x,lerpAngle:()=>i,mag:()=>l,mean:()=>f,median:()=>y,mod:()=>u,percent:()=>h,sum:()=>s});var c=(e,t)=>Math.abs(t-e)||0;var x=e=>e%1||0;var R=2*Math.PI;var p=(e,t,r,o=1)=>{r&&(t.x+=r.x*o,t.y+=r.y*o),e.x+=t.x*o,e.y+=t.y*o};var u=(e,t)=>(t+e%t)%t;var a=(e,t,r,o)=>Math.hypot(r-e,o-t);var l=(e,t)=>Math.hypot(e,t);var s=e=>{let t=0;for(let r=0;r<e.length;r++)t+=e[r];return t};var f=e=>s(e)/e.length;var y=(...e)=>{let t=e.sort((o,d)=>o-d),r=Math.floor(t.length/2);return t.length%2===0?(t[r-1]+t[r])/2:t[r]};var i=(e,t,r)=>{let o=(t-e)%360;return o>180?o-=360:o<-180&&(o+=360),e+o*r};var h=(e,t=0,r=1)=>r-t?(e-t)/(r-t):0;globalThis.utils=Object.assign(globalThis.utils||{},n);})();
|
package/dist/vector.js
CHANGED
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
vecDot: () => vecDot,
|
|
30
30
|
vecEq: () => vecEq,
|
|
31
31
|
vecFloor: () => vecFloor,
|
|
32
|
+
vecHeading: () => vecHeading,
|
|
32
33
|
vecIsZero: () => vecIsZero,
|
|
33
34
|
vecLerp: () => vecLerp,
|
|
34
35
|
vecLimit: () => vecLimit,
|
|
@@ -39,28 +40,25 @@
|
|
|
39
40
|
vecNorm: () => vecNorm,
|
|
40
41
|
vecRand: () => vecRand,
|
|
41
42
|
vecReflect: () => vecReflect,
|
|
43
|
+
vecRem: () => vecRem,
|
|
42
44
|
vecRotate: () => vecRotate,
|
|
43
45
|
vecRound: () => vecRound,
|
|
44
46
|
vecSet: () => vecSet,
|
|
45
47
|
vecSetMag: () => vecSetMag,
|
|
46
|
-
vecSub: () => vecSub
|
|
48
|
+
vecSub: () => vecSub,
|
|
49
|
+
vecToArray: () => vecToArray
|
|
47
50
|
});
|
|
48
|
-
var sqrt = Math.sqrt;
|
|
49
51
|
var cos = Math.cos;
|
|
50
52
|
var sin = Math.sin;
|
|
51
53
|
var PI2 = 2 * Math.PI;
|
|
52
54
|
var Vector = class {
|
|
53
|
-
/** @type {number} */
|
|
54
|
-
x;
|
|
55
|
-
/** @type {number} */
|
|
56
|
-
y;
|
|
57
55
|
/**
|
|
58
56
|
* @param {number} [x=0]
|
|
59
57
|
* @param {number} [y]
|
|
60
58
|
*/
|
|
61
59
|
constructor(x = 0, y = x) {
|
|
62
|
-
this.x = x;
|
|
63
|
-
this.y = y;
|
|
60
|
+
this.x = parseFloat(x) || 0;
|
|
61
|
+
this.y = parseFloat(y) || 0;
|
|
64
62
|
}
|
|
65
63
|
/**
|
|
66
64
|
* @returns {string}
|
|
@@ -77,12 +75,6 @@
|
|
|
77
75
|
}
|
|
78
76
|
return new Vector(x, y);
|
|
79
77
|
};
|
|
80
|
-
var vecEq = (v, x, y = x) => {
|
|
81
|
-
if (isVector(x)) {
|
|
82
|
-
return vecEq(v, x.x, x.y);
|
|
83
|
-
}
|
|
84
|
-
return v.x === x && v.y === y;
|
|
85
|
-
};
|
|
86
78
|
var vecSet = (v, x, y = x) => {
|
|
87
79
|
if (isVector(x)) {
|
|
88
80
|
vecSet(v, x.x, x.y);
|
|
@@ -151,8 +143,7 @@
|
|
|
151
143
|
var vecLimit = (v, max = 1) => {
|
|
152
144
|
const sq = vecMag2(v);
|
|
153
145
|
if (sq > max * max) {
|
|
154
|
-
|
|
155
|
-
vecMult(v, max);
|
|
146
|
+
vecSetMag(v, max);
|
|
156
147
|
}
|
|
157
148
|
return v;
|
|
158
149
|
};
|
|
@@ -164,7 +155,8 @@
|
|
|
164
155
|
const dy = a.y - b.y;
|
|
165
156
|
return dx * dx + dy * dy;
|
|
166
157
|
};
|
|
167
|
-
var
|
|
158
|
+
var vecHeading = (v) => Math.atan2(v.y, v.x);
|
|
159
|
+
var vecAngle = (v) => vecHeading(v);
|
|
168
160
|
var vecAngleBetween = (v1, v2) => Math.atan2(v2.y - v1.y, v2.x - v1.x);
|
|
169
161
|
var vecDot = (a, b) => a.x * b.x + a.y * b.y;
|
|
170
162
|
var vecCross = (a, b) => a.x * b.y - a.y * b.x;
|
|
@@ -173,11 +165,6 @@
|
|
|
173
165
|
a.y += (b.y - a.y) * t || 0;
|
|
174
166
|
return a;
|
|
175
167
|
};
|
|
176
|
-
var vecRand = (minlength = 1, maxlength = minlength, rng = globalThis.rand || Math.random) => {
|
|
177
|
-
const angle = rng() * PI2;
|
|
178
|
-
const radius = rng() * (maxlength - minlength) + minlength;
|
|
179
|
-
return vec(cos(angle) * radius, sin(angle) * radius);
|
|
180
|
-
};
|
|
181
168
|
var vecAbs = (v) => {
|
|
182
169
|
v.x = Math.abs(v.x);
|
|
183
170
|
v.y = Math.abs(v.y);
|
|
@@ -205,8 +192,25 @@
|
|
|
205
192
|
if (v.y > max.y) v.y = max.y;
|
|
206
193
|
return v;
|
|
207
194
|
};
|
|
195
|
+
var vecRem = (v, value) => {
|
|
196
|
+
v.x %= value;
|
|
197
|
+
v.y %= value;
|
|
198
|
+
return v;
|
|
199
|
+
};
|
|
208
200
|
var vecMove = (from, to, delta = 1) => vecAdd(from, to.x * delta, to.y * delta);
|
|
201
|
+
var vecEq = (v, x, y = x) => {
|
|
202
|
+
if (isVector(x)) {
|
|
203
|
+
return vecEq(v, x.x, x.y);
|
|
204
|
+
}
|
|
205
|
+
return v.x === x && v.y === y;
|
|
206
|
+
};
|
|
209
207
|
var vecIsZero = (v) => vecEq(v, ZERO);
|
|
208
|
+
var vecToArray = (v) => [v.x, v.y];
|
|
209
|
+
var vecRand = (minlength = 1, maxlength = minlength, rng = globalThis.rand || Math.random) => {
|
|
210
|
+
const angle = rng() * PI2;
|
|
211
|
+
const radius = rng() * (maxlength - minlength) + minlength;
|
|
212
|
+
return vec(cos(angle) * radius, sin(angle) * radius);
|
|
213
|
+
};
|
|
210
214
|
var ZERO = /* @__PURE__ */ vec(0, 0);
|
|
211
215
|
var ONE = /* @__PURE__ */ vec(1, 1);
|
|
212
216
|
var UP = /* @__PURE__ */ vec(0, -1);
|
package/dist/vector.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var E=Object.defineProperty;var F=(t,e)=>{for(var o in e)E(t,o,{get:e[o],enumerable:!0})};var h={};F(h,{DOWN:()=>X,LEFT:()=>Y,ONE:()=>J,RIGHT:()=>Q,UP:()=>K,Vector:()=>s,ZERO:()=>C,vec:()=>r,vecAbs:()=>U,vecAdd:()=>y,vecAngle:()=>N,vecAngleBetween:()=>P,vecCeil:()=>Z,vecClamp:()=>B,vecCross:()=>w,vecDist:()=>q,vecDist2:()=>L,vecDiv:()=>u,vecDot:()=>D,vecEq:()=>a,vecFloor:()=>$,vecHeading:()=>A,vecIsZero:()=>W,vecLerp:()=>H,vecLimit:()=>b,vecMag:()=>R,vecMag2:()=>T,vecMove:()=>V,vecMult:()=>x,vecNorm:()=>i,vecRand:()=>z,vecReflect:()=>S,vecRem:()=>G,vecRotate:()=>O,vecRound:()=>j,vecSet:()=>d,vecSetMag:()=>g,vecSub:()=>p,vecToArray:()=>k});var M=Math.cos,f=Math.sin,I=2*Math.PI,s=class{constructor(e=0,o=e){this.x=parseFloat(e)||0,this.y=parseFloat(o)||0}toString(){return`Vector (${this.x}, ${this.y})`}},n=t=>t instanceof s,r=(t=0,e=t)=>(n(t)&&(e=t.y,t=t.x),new s(t,e)),d=(t,e,o=e)=>(n(e)?d(t,e.x,e.y):(t.x=e,t.y=o),t),y=(t,e,o=e)=>n(e)?y(t,e.x,e.y):(t.x+=e,t.y+=o,t),p=(t,e,o=e)=>n(e)?p(t,e.x,e.y):(t.x-=e,t.y-=o,t),x=(t,e,o=e)=>n(e)?x(t,e.x,e.y):(t.x*=e,t.y*=o,t),u=(t,e,o=e)=>n(e)?u(t,e.x,e.y):(t.x/=e||1,t.y/=o||1,t),O=(t,e)=>{let o=M(e),c=f(e);return t.x=o*t.x-c*t.y,t.y=c*t.x+o*t.y,t},S=(t,e)=>{let o=i(r(e));return p(t,x(o,2*D(t,o)))},g=(t,e)=>(i(t),x(t,e),t),R=t=>Math.hypot(t.x,t.y),T=t=>t.x*t.x+t.y*t.y,i=t=>{let e=R(t);return e>0&&u(t,e),t},b=(t,e=1)=>(T(t)>e*e&&g(t,e),t),q=(t,e)=>Math.hypot(e.x-t.x,e.y-t.y),L=(t,e)=>{let o=t.x-e.x,c=t.y-e.y;return o*o+c*c},A=t=>Math.atan2(t.y,t.x),N=t=>A(t),P=(t,e)=>Math.atan2(e.y-t.y,e.x-t.x),D=(t,e)=>t.x*e.x+t.y*e.y,w=(t,e)=>t.x*e.y-t.y*e.x,H=(t,e,o)=>(t.x+=(e.x-t.x)*o||0,t.y+=(e.y-t.y)*o||0,t),U=t=>(t.x=Math.abs(t.x),t.y=Math.abs(t.y),t),Z=t=>(t.x=Math.ceil(t.x),t.y=Math.ceil(t.y),t),$=t=>(t.x=Math.floor(t.x),t.y=Math.floor(t.y),t),j=t=>(t.x=Math.round(t.x),t.y=Math.round(t.y),t),B=(t,e,o)=>(t.x<e.x&&(t.x=e.x),t.x>o.x&&(t.x=o.x),t.y<e.y&&(t.y=e.y),t.y>o.y&&(t.y=o.y),t),G=(t,e)=>(t.x%=e,t.y%=e,t),V=(t,e,o=1)=>y(t,e.x*o,e.y*o),a=(t,e,o=e)=>n(e)?a(t,e.x,e.y):t.x===e&&t.y===o,W=t=>a(t,C),k=t=>[t.x,t.y],z=(t=1,e=t,o=globalThis.rand||Math.random)=>{let c=o()*I,l=o()*(e-t)+t;return r(M(c)*l,f(c)*l)},C=r(0,0),J=r(1,1),K=r(0,-1),Q=r(1,0),X=r(0,1),Y=r(-1,0);globalThis.utils=Object.assign(globalThis.utils||{},h);})();
|
package/package.json
CHANGED
package/src/actor/index.js
CHANGED
|
@@ -40,7 +40,7 @@ export class Actor {
|
|
|
40
40
|
* @param {Vector} anchor
|
|
41
41
|
*/
|
|
42
42
|
constructor(sprite, position, anchor = ANCHOR_TOP_LEFT) {
|
|
43
|
-
this.sprite = sprite
|
|
43
|
+
this.sprite = sprite || { width: 0, height: 0 }
|
|
44
44
|
this.pos = position || vec(0)
|
|
45
45
|
this._o = vec(anchor) // clone the anchor vector
|
|
46
46
|
this._s = vec(1, 1)
|
|
@@ -146,40 +146,46 @@ export class Actor {
|
|
|
146
146
|
/**
|
|
147
147
|
* Draw the actor
|
|
148
148
|
*
|
|
149
|
-
* @param {LitecanvasInstance} [
|
|
149
|
+
* @param {LitecanvasInstance} [engine]
|
|
150
150
|
*/
|
|
151
|
-
draw(
|
|
152
|
-
if (
|
|
151
|
+
draw(engine = globalThis, saveContext = true) {
|
|
152
|
+
if (saveContext) engine.push()
|
|
153
153
|
|
|
154
|
-
|
|
154
|
+
this.transform(engine)
|
|
155
155
|
|
|
156
|
-
|
|
157
|
-
|
|
156
|
+
if (
|
|
157
|
+
this.sprite.width &&
|
|
158
|
+
this.sprite.height &&
|
|
159
|
+
!this.hidden &&
|
|
160
|
+
this.opacity > 0
|
|
161
|
+
) {
|
|
162
|
+
this.drawImage(engine)
|
|
163
|
+
}
|
|
158
164
|
|
|
159
|
-
if (saveContext)
|
|
165
|
+
if (saveContext) engine.pop()
|
|
160
166
|
}
|
|
161
167
|
|
|
162
168
|
/**
|
|
163
|
-
* @param {LitecanvasInstance}
|
|
169
|
+
* @param {LitecanvasInstance} engine
|
|
164
170
|
*/
|
|
165
|
-
transform(
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
171
|
+
transform(engine) {
|
|
172
|
+
engine.translate(this.pos.x, this.pos.y)
|
|
173
|
+
engine.rotate(engine.deg2rad(this.angle))
|
|
174
|
+
engine.scale(
|
|
169
175
|
(this.flipX ? -1 : 1) * this._s.x,
|
|
170
176
|
(this.flipY ? -1 : 1) * this._s.y
|
|
171
177
|
)
|
|
172
178
|
}
|
|
173
179
|
|
|
174
180
|
/**
|
|
175
|
-
* @param {LitecanvasInstance}
|
|
181
|
+
* @param {LitecanvasInstance} engine
|
|
176
182
|
*/
|
|
177
|
-
drawImage(
|
|
183
|
+
drawImage(engine, alpha = true) {
|
|
178
184
|
const anchor = this.anchor
|
|
179
185
|
const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x)
|
|
180
186
|
const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y)
|
|
181
187
|
|
|
182
|
-
if (alpha)
|
|
183
|
-
|
|
188
|
+
if (alpha) engine.alpha(this.opacity)
|
|
189
|
+
engine.image(x, y, this.sprite)
|
|
184
190
|
}
|
|
185
191
|
}
|
package/src/vector/README.md
CHANGED
|
@@ -129,17 +129,19 @@ Returns the distance between two points represented by vectors squared.
|
|
|
129
129
|
|
|
130
130
|
Syntax: `vecDist2(a: Vector, b: Vector): number`
|
|
131
131
|
|
|
132
|
-
##
|
|
132
|
+
## vecHeading
|
|
133
133
|
|
|
134
134
|
Calculates the angle a vector makes with the positive x-axis.
|
|
135
135
|
|
|
136
|
-
Syntax: `
|
|
136
|
+
Syntax: `vecHeading(v: Vector): number`
|
|
137
137
|
|
|
138
138
|
```js
|
|
139
|
-
const a =
|
|
140
|
-
const b =
|
|
139
|
+
const a = vecHeading(vec(1, 0)) // outputs 0 (zero)
|
|
140
|
+
const b = vecHeading(vec(0, 1)) // outputs ~1.571 (same as Math.PI/2 or 90°)
|
|
141
141
|
```
|
|
142
142
|
|
|
143
|
+
> Note: `vecAngle` is alias of `vecHeading`.
|
|
144
|
+
|
|
143
145
|
## vecDot
|
|
144
146
|
|
|
145
147
|
Calculates the dot product of two vectors.
|
|
@@ -163,6 +165,12 @@ values equal to the new vector's.
|
|
|
163
165
|
|
|
164
166
|
Syntax: `vecLerp(a: Vector, b: Vector, atm: number): Lerp`
|
|
165
167
|
|
|
168
|
+
## vecRem
|
|
169
|
+
|
|
170
|
+
Performs modulo (remainder) division with a vector's components.
|
|
171
|
+
|
|
172
|
+
Syntax: `vecRem(v: Vector, divisor: number): Vector`
|
|
173
|
+
|
|
166
174
|
## vecRand
|
|
167
175
|
|
|
168
176
|
Creates a vector with random direction and (optional) length.
|
package/src/vector/index.js
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
|
-
const
|
|
2
|
-
cos = Math.cos,
|
|
1
|
+
const cos = Math.cos,
|
|
3
2
|
sin = Math.sin,
|
|
4
3
|
PI2 = 2 * Math.PI
|
|
5
4
|
|
|
6
5
|
export class Vector {
|
|
7
|
-
/** @type {number} */
|
|
8
|
-
x
|
|
9
|
-
/** @type {number} */
|
|
10
|
-
y
|
|
11
|
-
|
|
12
6
|
/**
|
|
13
7
|
* @param {number} [x=0]
|
|
14
8
|
* @param {number} [y]
|
|
15
9
|
*/
|
|
16
10
|
constructor(x = 0, y = x) {
|
|
17
|
-
this.x = x
|
|
18
|
-
this.y = y
|
|
11
|
+
this.x = parseFloat(x) || 0
|
|
12
|
+
this.y = parseFloat(y) || 0
|
|
19
13
|
}
|
|
20
14
|
|
|
21
15
|
/**
|
|
@@ -47,21 +41,6 @@ export const vec = (x = 0, y = x) => {
|
|
|
47
41
|
return new Vector(x, y)
|
|
48
42
|
}
|
|
49
43
|
|
|
50
|
-
/**
|
|
51
|
-
* Checks whether two vectors are equal.
|
|
52
|
-
*
|
|
53
|
-
* @param {Vector} v
|
|
54
|
-
* @param {number|Vector} x
|
|
55
|
-
* @param {number} [y]
|
|
56
|
-
* @returns {boolean}
|
|
57
|
-
*/
|
|
58
|
-
export const vecEq = (v, x, y = x) => {
|
|
59
|
-
if (isVector(x)) {
|
|
60
|
-
return vecEq(v, x.x, x.y)
|
|
61
|
-
}
|
|
62
|
-
return v.x === x && v.y === y
|
|
63
|
-
}
|
|
64
|
-
|
|
65
44
|
/**
|
|
66
45
|
* Assigns new values to a vector.
|
|
67
46
|
*
|
|
@@ -187,7 +166,7 @@ export const vecReflect = (v, normal) => {
|
|
|
187
166
|
}
|
|
188
167
|
|
|
189
168
|
/**
|
|
190
|
-
*
|
|
169
|
+
* Sets a vector's magnitude to value.
|
|
191
170
|
*
|
|
192
171
|
* @param {Vector} v
|
|
193
172
|
* @param {Vector} normal
|
|
@@ -239,8 +218,7 @@ export const vecNorm = (v) => {
|
|
|
239
218
|
export const vecLimit = (v, max = 1) => {
|
|
240
219
|
const sq = vecMag2(v)
|
|
241
220
|
if (sq > max * max) {
|
|
242
|
-
|
|
243
|
-
vecMult(v, max)
|
|
221
|
+
vecSetMag(v, max)
|
|
244
222
|
}
|
|
245
223
|
return v
|
|
246
224
|
}
|
|
@@ -275,7 +253,15 @@ export const vecDist2 = (a, b) => {
|
|
|
275
253
|
* @param {Vector} v
|
|
276
254
|
* @returns {number}
|
|
277
255
|
*/
|
|
278
|
-
export const
|
|
256
|
+
export const vecHeading = (v) => Math.atan2(v.y, v.x)
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* `vecHeading` alias.
|
|
260
|
+
*
|
|
261
|
+
* @param {Vector} v
|
|
262
|
+
* @returns {number}
|
|
263
|
+
*/
|
|
264
|
+
export const vecAngle = (v) => vecHeading(v)
|
|
279
265
|
|
|
280
266
|
/**
|
|
281
267
|
* Calculates the angle between two vectors.
|
|
@@ -331,27 +317,6 @@ export const vecLerp = (a, b, t) => {
|
|
|
331
317
|
return a
|
|
332
318
|
}
|
|
333
319
|
|
|
334
|
-
/**
|
|
335
|
-
* Sample a vector with random direction and (optional) length.
|
|
336
|
-
*
|
|
337
|
-
* If the `litecanvas#rand()` not is globally explosed, uses `Math.random()`.
|
|
338
|
-
* You can set `vecconfig.random` to set your own "random" function.
|
|
339
|
-
*
|
|
340
|
-
* @param {number} [minlength]
|
|
341
|
-
* @param {number} [maxlength]
|
|
342
|
-
* @param {() => number} [randomFn]
|
|
343
|
-
* @returns {Vector}
|
|
344
|
-
*/
|
|
345
|
-
export const vecRand = (
|
|
346
|
-
minlength = 1,
|
|
347
|
-
maxlength = minlength,
|
|
348
|
-
rng = globalThis.rand || Math.random
|
|
349
|
-
) => {
|
|
350
|
-
const angle = rng() * PI2
|
|
351
|
-
const radius = rng() * (maxlength - minlength) + minlength
|
|
352
|
-
return vec(cos(angle) * radius, sin(angle) * radius)
|
|
353
|
-
}
|
|
354
|
-
|
|
355
320
|
/**
|
|
356
321
|
* @param {Vector} v
|
|
357
322
|
* @returns {Vector}
|
|
@@ -414,6 +379,19 @@ export const vecClamp = (v, min, max) => {
|
|
|
414
379
|
return v
|
|
415
380
|
}
|
|
416
381
|
|
|
382
|
+
/**
|
|
383
|
+
* Performs modulo (remainder) division with a vector's components.
|
|
384
|
+
*
|
|
385
|
+
* @param {Vector} v
|
|
386
|
+
* @param {number} value
|
|
387
|
+
* @returns {Vector}
|
|
388
|
+
*/
|
|
389
|
+
export const vecRem = (v, value) => {
|
|
390
|
+
v.x %= value
|
|
391
|
+
v.y %= value
|
|
392
|
+
return v
|
|
393
|
+
}
|
|
394
|
+
|
|
417
395
|
/**
|
|
418
396
|
* @param {Vector} from
|
|
419
397
|
* @param {Vector} to
|
|
@@ -423,8 +401,56 @@ export const vecClamp = (v, min, max) => {
|
|
|
423
401
|
export const vecMove = (from, to, delta = 1) =>
|
|
424
402
|
vecAdd(from, to.x * delta, to.y * delta)
|
|
425
403
|
|
|
404
|
+
/**
|
|
405
|
+
* Checks whether two vectors are equal.
|
|
406
|
+
*
|
|
407
|
+
* @param {Vector} v
|
|
408
|
+
* @param {number|Vector} x
|
|
409
|
+
* @param {number} [y]
|
|
410
|
+
* @returns {boolean}
|
|
411
|
+
*/
|
|
412
|
+
export const vecEq = (v, x, y = x) => {
|
|
413
|
+
if (isVector(x)) {
|
|
414
|
+
return vecEq(v, x.x, x.y)
|
|
415
|
+
}
|
|
416
|
+
return v.x === x && v.y === y
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Checks if the vector's components are zero.
|
|
421
|
+
*
|
|
422
|
+
* @param {Vector} v
|
|
423
|
+
* @returns {boolean}
|
|
424
|
+
*/
|
|
426
425
|
export const vecIsZero = (v) => vecEq(v, ZERO)
|
|
427
426
|
|
|
427
|
+
/**
|
|
428
|
+
* Returns the vector's components as an array of numbers.
|
|
429
|
+
*
|
|
430
|
+
* @returns {number[]}
|
|
431
|
+
*/
|
|
432
|
+
export const vecToArray = (v) => [v.x, v.y]
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Sample a vector with random direction and (optional) length.
|
|
436
|
+
*
|
|
437
|
+
* If the `litecanvas#rand()` not is globally explosed, uses `Math.random()`.
|
|
438
|
+
*
|
|
439
|
+
* @param {number} [minlength]
|
|
440
|
+
* @param {number} [maxlength]
|
|
441
|
+
* @param {() => number} [randomFn]
|
|
442
|
+
* @returns {Vector}
|
|
443
|
+
*/
|
|
444
|
+
export const vecRand = (
|
|
445
|
+
minlength = 1,
|
|
446
|
+
maxlength = minlength,
|
|
447
|
+
rng = globalThis.rand || Math.random
|
|
448
|
+
) => {
|
|
449
|
+
const angle = rng() * PI2
|
|
450
|
+
const radius = rng() * (maxlength - minlength) + minlength
|
|
451
|
+
return vec(cos(angle) * radius, sin(angle) * radius)
|
|
452
|
+
}
|
|
453
|
+
|
|
428
454
|
// constants
|
|
429
455
|
export const ZERO = /** @__PURE__ */ vec(0, 0)
|
|
430
456
|
export const ONE = /** @__PURE__ */ vec(1, 1)
|