@litecanvas/utils 0.14.0 → 0.16.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/README.md +2 -1
- package/dist/actor.js +43 -15
- package/dist/actor.min.js +1 -1
- package/dist/all.js +219 -17
- package/dist/all.min.js +2 -2
- package/dist/math.js +14 -0
- package/dist/math.min.js +1 -1
- package/dist/tween.js +190 -0
- package/dist/tween.min.js +1 -0
- package/dist/vector.js +2 -2
- package/dist/vector.min.js +1 -1
- package/package.json +1 -1
- package/src/actor/README.md +31 -1
- package/src/actor/index.js +48 -15
- package/src/index.js +1 -0
- package/src/math/README.md +35 -0
- package/src/math/advance.js +20 -0
- package/src/math/index.js +1 -0
- package/src/tween/README.md +181 -0
- package/src/tween/_web.js +4 -0
- package/src/tween/index.js +170 -0
- package/src/vector/index.js +3 -2
package/README.md
CHANGED
|
@@ -49,4 +49,5 @@ const pos = vec(0, 0)
|
|
|
49
49
|
- **Actor**: class to represent game entities. [Usage & Docs](https://github.com/litecanvas/utils/tree/main/src/actor)
|
|
50
50
|
- **Grid**: class to handle retangular grid areas. [Usage & Docs](https://github.com/litecanvas/utils/tree/main/src/grid)
|
|
51
51
|
- **Collision** utilities. [Usage & Docs](https://github.com/litecanvas/utils/tree/main/src/collision)
|
|
52
|
-
-
|
|
52
|
+
- **Tween** to create animations. [Usage & Docs](https://github.com/litecanvas/utils/tree/main/src/tween)
|
|
53
|
+
- And some [math utilities](https://github.com/litecanvas/utils/tree/main/src/math)...
|
package/dist/actor.js
CHANGED
|
@@ -60,11 +60,11 @@
|
|
|
60
60
|
globalThis.zzfxV = 1;
|
|
61
61
|
|
|
62
62
|
// src/actor/index.js
|
|
63
|
-
var ANCHOR_CENTER = vec(0.5, 0.5);
|
|
64
|
-
var ANCHOR_TOP_LEFT = vec(0, 0);
|
|
65
|
-
var ANCHOR_TOP_RIGHT = vec(1, 0);
|
|
66
|
-
var ANCHOR_BOT_LEFT = vec(0, 1);
|
|
67
|
-
var ANCHOR_BOT_RIGHT = vec(1, 1);
|
|
63
|
+
var ANCHOR_CENTER = /* @__PURE__ */ vec(0.5, 0.5);
|
|
64
|
+
var ANCHOR_TOP_LEFT = /* @__PURE__ */ vec(0, 0);
|
|
65
|
+
var ANCHOR_TOP_RIGHT = /* @__PURE__ */ vec(1, 0);
|
|
66
|
+
var ANCHOR_BOT_LEFT = /* @__PURE__ */ vec(0, 1);
|
|
67
|
+
var ANCHOR_BOT_RIGHT = /* @__PURE__ */ vec(1, 1);
|
|
68
68
|
var Actor = class {
|
|
69
69
|
/** @type {Image|HTMLCanvasElement|OffscreenCanvas} */
|
|
70
70
|
sprite;
|
|
@@ -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 */
|
|
@@ -146,6 +150,26 @@
|
|
|
146
150
|
get scale() {
|
|
147
151
|
return this._s;
|
|
148
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Sets the actor scale
|
|
155
|
+
*
|
|
156
|
+
* @param {number} x
|
|
157
|
+
* @param {number} [y]
|
|
158
|
+
*/
|
|
159
|
+
scaleTo(x, y = x) {
|
|
160
|
+
this._s.x = x;
|
|
161
|
+
this._s.y = y;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Multiplies the actor scale
|
|
165
|
+
*
|
|
166
|
+
* @param {number} x
|
|
167
|
+
* @param {number} [y]
|
|
168
|
+
*/
|
|
169
|
+
scaleBy(x, y = x) {
|
|
170
|
+
this._s.x *= x;
|
|
171
|
+
this._s.y *= y;
|
|
172
|
+
}
|
|
149
173
|
/**
|
|
150
174
|
* @returns {number[]}
|
|
151
175
|
*/
|
|
@@ -157,16 +181,16 @@
|
|
|
157
181
|
return [x, y, w, h];
|
|
158
182
|
}
|
|
159
183
|
/**
|
|
160
|
-
*
|
|
184
|
+
* Draw the actor
|
|
161
185
|
*
|
|
162
186
|
* @param {LitecanvasInstance} [litecanvas]
|
|
163
187
|
*/
|
|
164
|
-
draw(litecanvas = globalThis) {
|
|
188
|
+
draw(litecanvas = globalThis, saveContext = true) {
|
|
165
189
|
if (this.hidden || this.opacity <= 0) return;
|
|
166
|
-
litecanvas.push();
|
|
190
|
+
if (saveContext) litecanvas.push();
|
|
167
191
|
this.transform(litecanvas);
|
|
168
192
|
this.drawImage(litecanvas);
|
|
169
|
-
litecanvas.pop();
|
|
193
|
+
if (saveContext) litecanvas.pop();
|
|
170
194
|
}
|
|
171
195
|
/**
|
|
172
196
|
* @param {LitecanvasInstance} litecanvas
|
|
@@ -174,16 +198,20 @@
|
|
|
174
198
|
transform(litecanvas) {
|
|
175
199
|
litecanvas.translate(this.pos.x, this.pos.y);
|
|
176
200
|
litecanvas.rotate(this.angle);
|
|
177
|
-
litecanvas.scale(
|
|
201
|
+
litecanvas.scale(
|
|
202
|
+
(this.flipX ? -1 : 1) * this._s.x,
|
|
203
|
+
(this.flipY ? -1 : 1) * this._s.y
|
|
204
|
+
);
|
|
178
205
|
}
|
|
179
206
|
/**
|
|
180
207
|
* @param {LitecanvasInstance} litecanvas
|
|
181
208
|
*/
|
|
182
|
-
drawImage(litecanvas) {
|
|
183
|
-
const
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
litecanvas.
|
|
209
|
+
drawImage(litecanvas, alpha = true) {
|
|
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);
|
|
213
|
+
if (alpha) litecanvas.alpha(this.opacity);
|
|
214
|
+
litecanvas.image(x, y, this.sprite);
|
|
187
215
|
}
|
|
188
216
|
};
|
|
189
217
|
|
package/dist/actor.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var p=Object.defineProperty;var u=(
|
|
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
|
@@ -23,21 +23,36 @@
|
|
|
23
23
|
ANCHOR_TOP_LEFT: () => ANCHOR_TOP_LEFT,
|
|
24
24
|
ANCHOR_TOP_RIGHT: () => ANCHOR_TOP_RIGHT,
|
|
25
25
|
Actor: () => Actor,
|
|
26
|
+
BACK_IN: () => BACK_IN,
|
|
27
|
+
BACK_IN_OUT: () => BACK_IN_OUT,
|
|
28
|
+
BACK_OUT: () => BACK_OUT,
|
|
29
|
+
BOUNCE_IN: () => BOUNCE_IN,
|
|
30
|
+
BOUNCE_IN_OUT: () => BOUNCE_IN_OUT,
|
|
31
|
+
BOUNCE_OUT: () => BOUNCE_OUT,
|
|
26
32
|
Camera: () => Camera,
|
|
27
33
|
DOWN: () => DOWN,
|
|
34
|
+
EASE_IN: () => EASE_IN,
|
|
35
|
+
EASE_IN_OUT: () => EASE_IN_OUT,
|
|
36
|
+
EASE_OUT: () => EASE_OUT,
|
|
37
|
+
ELASTIC_IN: () => ELASTIC_IN,
|
|
38
|
+
ELASTIC_IN_OUT: () => ELASTIC_IN_OUT,
|
|
39
|
+
ELASTIC_OUT: () => ELASTIC_OUT,
|
|
28
40
|
Grid: () => Grid,
|
|
29
41
|
LEFT: () => LEFT,
|
|
42
|
+
LINEAR: () => LINEAR,
|
|
30
43
|
ONE: () => ONE,
|
|
31
44
|
RIGHT: () => RIGHT,
|
|
32
45
|
TypedGrid: () => TypedGrid,
|
|
33
46
|
UP: () => UP,
|
|
34
47
|
Vector: () => Vector,
|
|
35
48
|
ZERO: () => ZERO,
|
|
49
|
+
advance: () => advance_default,
|
|
36
50
|
diff: () => diff_default,
|
|
37
51
|
fract: () => fract_default,
|
|
38
52
|
intersection: () => intersection_default,
|
|
39
53
|
range: () => range_default,
|
|
40
54
|
resolve: () => resolve_default,
|
|
55
|
+
tween: () => tween,
|
|
41
56
|
vec: () => vec,
|
|
42
57
|
vecAbs: () => vecAbs,
|
|
43
58
|
vecAdd: () => vecAdd,
|
|
@@ -591,8 +606,8 @@
|
|
|
591
606
|
if (isVector(x)) {
|
|
592
607
|
return vecDiv(v, x.x, x.y);
|
|
593
608
|
}
|
|
594
|
-
v.x /= x;
|
|
595
|
-
v.y /= y;
|
|
609
|
+
v.x /= x || 1;
|
|
610
|
+
v.y /= y || 1;
|
|
596
611
|
return v;
|
|
597
612
|
};
|
|
598
613
|
var vecRotate = (v, radians) => {
|
|
@@ -691,11 +706,11 @@
|
|
|
691
706
|
globalThis.zzfxV = 1;
|
|
692
707
|
|
|
693
708
|
// src/actor/index.js
|
|
694
|
-
var ANCHOR_CENTER = vec(0.5, 0.5);
|
|
695
|
-
var ANCHOR_TOP_LEFT = vec(0, 0);
|
|
696
|
-
var ANCHOR_TOP_RIGHT = vec(1, 0);
|
|
697
|
-
var ANCHOR_BOT_LEFT = vec(0, 1);
|
|
698
|
-
var ANCHOR_BOT_RIGHT = vec(1, 1);
|
|
709
|
+
var ANCHOR_CENTER = /* @__PURE__ */ vec(0.5, 0.5);
|
|
710
|
+
var ANCHOR_TOP_LEFT = /* @__PURE__ */ vec(0, 0);
|
|
711
|
+
var ANCHOR_TOP_RIGHT = /* @__PURE__ */ vec(1, 0);
|
|
712
|
+
var ANCHOR_BOT_LEFT = /* @__PURE__ */ vec(0, 1);
|
|
713
|
+
var ANCHOR_BOT_RIGHT = /* @__PURE__ */ vec(1, 1);
|
|
699
714
|
var Actor = class {
|
|
700
715
|
/** @type {Image|HTMLCanvasElement|OffscreenCanvas} */
|
|
701
716
|
sprite;
|
|
@@ -705,6 +720,10 @@
|
|
|
705
720
|
_o;
|
|
706
721
|
/** @type {Vector} The actor scale */
|
|
707
722
|
_s;
|
|
723
|
+
/** @type {boolean} */
|
|
724
|
+
flipX = false;
|
|
725
|
+
/** @type {boolean} */
|
|
726
|
+
flipY = false;
|
|
708
727
|
/** @type {number} The actor angle (in radians) */
|
|
709
728
|
angle = 0;
|
|
710
729
|
/** @type {number} The actor opacity */
|
|
@@ -777,6 +796,26 @@
|
|
|
777
796
|
get scale() {
|
|
778
797
|
return this._s;
|
|
779
798
|
}
|
|
799
|
+
/**
|
|
800
|
+
* Sets the actor scale
|
|
801
|
+
*
|
|
802
|
+
* @param {number} x
|
|
803
|
+
* @param {number} [y]
|
|
804
|
+
*/
|
|
805
|
+
scaleTo(x, y = x) {
|
|
806
|
+
this._s.x = x;
|
|
807
|
+
this._s.y = y;
|
|
808
|
+
}
|
|
809
|
+
/**
|
|
810
|
+
* Multiplies the actor scale
|
|
811
|
+
*
|
|
812
|
+
* @param {number} x
|
|
813
|
+
* @param {number} [y]
|
|
814
|
+
*/
|
|
815
|
+
scaleBy(x, y = x) {
|
|
816
|
+
this._s.x *= x;
|
|
817
|
+
this._s.y *= y;
|
|
818
|
+
}
|
|
780
819
|
/**
|
|
781
820
|
* @returns {number[]}
|
|
782
821
|
*/
|
|
@@ -788,16 +827,16 @@
|
|
|
788
827
|
return [x, y, w, h];
|
|
789
828
|
}
|
|
790
829
|
/**
|
|
791
|
-
*
|
|
830
|
+
* Draw the actor
|
|
792
831
|
*
|
|
793
832
|
* @param {LitecanvasInstance} [litecanvas]
|
|
794
833
|
*/
|
|
795
|
-
draw(litecanvas = globalThis) {
|
|
834
|
+
draw(litecanvas = globalThis, saveContext = true) {
|
|
796
835
|
if (this.hidden || this.opacity <= 0) return;
|
|
797
|
-
litecanvas.push();
|
|
836
|
+
if (saveContext) litecanvas.push();
|
|
798
837
|
this.transform(litecanvas);
|
|
799
838
|
this.drawImage(litecanvas);
|
|
800
|
-
litecanvas.pop();
|
|
839
|
+
if (saveContext) litecanvas.pop();
|
|
801
840
|
}
|
|
802
841
|
/**
|
|
803
842
|
* @param {LitecanvasInstance} litecanvas
|
|
@@ -805,16 +844,20 @@
|
|
|
805
844
|
transform(litecanvas) {
|
|
806
845
|
litecanvas.translate(this.pos.x, this.pos.y);
|
|
807
846
|
litecanvas.rotate(this.angle);
|
|
808
|
-
litecanvas.scale(
|
|
847
|
+
litecanvas.scale(
|
|
848
|
+
(this.flipX ? -1 : 1) * this._s.x,
|
|
849
|
+
(this.flipY ? -1 : 1) * this._s.y
|
|
850
|
+
);
|
|
809
851
|
}
|
|
810
852
|
/**
|
|
811
853
|
* @param {LitecanvasInstance} litecanvas
|
|
812
854
|
*/
|
|
813
|
-
drawImage(litecanvas) {
|
|
814
|
-
const
|
|
815
|
-
const
|
|
816
|
-
|
|
817
|
-
litecanvas.
|
|
855
|
+
drawImage(litecanvas, alpha = true) {
|
|
856
|
+
const anchor = this.anchor;
|
|
857
|
+
const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x);
|
|
858
|
+
const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y);
|
|
859
|
+
if (alpha) litecanvas.alpha(this.opacity);
|
|
860
|
+
litecanvas.image(x, y, this.sprite);
|
|
818
861
|
}
|
|
819
862
|
};
|
|
820
863
|
|
|
@@ -830,6 +873,165 @@
|
|
|
830
873
|
// src/math/range.js
|
|
831
874
|
var range_default = (size) => Array.from(Array(size).keys());
|
|
832
875
|
|
|
876
|
+
// src/math/advance.js
|
|
877
|
+
var advance_default = advance = (position, velocity, acceleration, deltaTime = 1) => {
|
|
878
|
+
if (acceleration) {
|
|
879
|
+
velocity.x += acceleration.x * deltaTime;
|
|
880
|
+
velocity.y += acceleration.y * deltaTime;
|
|
881
|
+
}
|
|
882
|
+
position.x += velocity.x * deltaTime;
|
|
883
|
+
position.y += velocity.y * deltaTime;
|
|
884
|
+
};
|
|
885
|
+
|
|
886
|
+
// src/tween/index.js
|
|
887
|
+
var HALF_PI = Math.PI / 2;
|
|
888
|
+
var tween = (object, prop, toValue, duration = 1, easing = LINEAR) => {
|
|
889
|
+
return new TweenController(object, prop, toValue, duration, easing);
|
|
890
|
+
};
|
|
891
|
+
var LINEAR = (n) => n;
|
|
892
|
+
var EASE_IN = (n) => n * n;
|
|
893
|
+
var EASE_OUT = (n) => -n * (n - 2);
|
|
894
|
+
var EASE_IN_OUT = (n) => {
|
|
895
|
+
if (n < 0.5) {
|
|
896
|
+
return 2 * n * n;
|
|
897
|
+
}
|
|
898
|
+
return -2 * n * n + 4 * n - 1;
|
|
899
|
+
};
|
|
900
|
+
var BACK_IN = (n) => n * n * n - n * Math.sin(n * Math.PI);
|
|
901
|
+
var BACK_OUT = (n) => {
|
|
902
|
+
let a = 1 - n;
|
|
903
|
+
return 1 - (a * a * a - a * Math.sin(a * Math.PI));
|
|
904
|
+
};
|
|
905
|
+
var BACK_IN_OUT = (n) => {
|
|
906
|
+
if (n < 0.5) {
|
|
907
|
+
let a2 = 2 * n;
|
|
908
|
+
return 0.5 * (a2 * a2 * a2 - a2 * Math.sin(a2 * Math.PI));
|
|
909
|
+
}
|
|
910
|
+
let a = 1 - (2 * n - 1);
|
|
911
|
+
return 0.5 * (1 - (a * a * a - a * Math.sin(n * Math.PI))) + 0.5;
|
|
912
|
+
};
|
|
913
|
+
var ELASTIC_IN = (n) => {
|
|
914
|
+
return Math.sin(13 * HALF_PI * n) * Math.pow(2, 10 * (n - 1));
|
|
915
|
+
};
|
|
916
|
+
var ELASTIC_OUT = (n) => {
|
|
917
|
+
return Math.sin(-13 * HALF_PI * (n + 1)) * Math.pow(2, -10 * n) + 1;
|
|
918
|
+
};
|
|
919
|
+
var ELASTIC_IN_OUT = (n) => {
|
|
920
|
+
if (n < 0.5) {
|
|
921
|
+
let a2 = Math.sin(13 * HALF_PI * (2 * n));
|
|
922
|
+
let b2 = Math.pow(2, 10 * (2 * n - 1));
|
|
923
|
+
return 0.5 * a2 * b2;
|
|
924
|
+
}
|
|
925
|
+
let a = Math.sin(-13 * HALF_PI * (2 * n - 1 + 1));
|
|
926
|
+
let b = Math.pow(2, -10 * (2 * n - 1));
|
|
927
|
+
return 0.5 * (a * b + 2);
|
|
928
|
+
};
|
|
929
|
+
var BOUNCE_IN = (n) => 1 - BOUNCE_OUT(1 - n);
|
|
930
|
+
var BOUNCE_OUT = (n) => {
|
|
931
|
+
if (n < 4 / 11) {
|
|
932
|
+
return 121 * n * n / 16;
|
|
933
|
+
} else if (n < 8 / 11) {
|
|
934
|
+
return 363 / 40 * n * n - 99 / 10 * n + 17 / 5;
|
|
935
|
+
} else if (n < 9 / 10) {
|
|
936
|
+
return 4356 / 361 * n * n - 35442 / 1805 * n + 16061 / 1805;
|
|
937
|
+
}
|
|
938
|
+
return 54 / 5 * n * n - 513 / 25 * n + 268 / 25;
|
|
939
|
+
};
|
|
940
|
+
var BOUNCE_IN_OUT = (n) => {
|
|
941
|
+
if (n < 0.5) {
|
|
942
|
+
return 0.5 * BOUNCE_IN(n * 2);
|
|
943
|
+
}
|
|
944
|
+
return 0.5 * BOUNCE_OUT(n * 2 - 1) + 0.5;
|
|
945
|
+
};
|
|
946
|
+
var TweenController = class {
|
|
947
|
+
/** @type {boolean} */
|
|
948
|
+
running = false;
|
|
949
|
+
/** @type {*} */
|
|
950
|
+
_o;
|
|
951
|
+
/** @type {string} */
|
|
952
|
+
_p;
|
|
953
|
+
/** @type {number} */
|
|
954
|
+
_x;
|
|
955
|
+
/** @type {number} */
|
|
956
|
+
_d;
|
|
957
|
+
/** @type {(x: number) => number} */
|
|
958
|
+
_e;
|
|
959
|
+
/** @type {Function[]} */
|
|
960
|
+
_cb = [];
|
|
961
|
+
/** @type {number} */
|
|
962
|
+
_t = 0;
|
|
963
|
+
/** @type {Function} */
|
|
964
|
+
_u = 0;
|
|
965
|
+
/** @type {LitecanvasInstance} */
|
|
966
|
+
_lc;
|
|
967
|
+
/**
|
|
968
|
+
* @param {*} object
|
|
969
|
+
* @param {string} prop
|
|
970
|
+
* @param {number} toValue
|
|
971
|
+
* @param {number} duration
|
|
972
|
+
* @param {(x: number) => number} easing
|
|
973
|
+
*/
|
|
974
|
+
constructor(object, prop, toValue, duration, easing) {
|
|
975
|
+
this._o = object;
|
|
976
|
+
this._p = prop;
|
|
977
|
+
this._x = toValue;
|
|
978
|
+
this._d = duration;
|
|
979
|
+
this._e = easing;
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
*
|
|
983
|
+
* @param {LitecanvasInstance} engine
|
|
984
|
+
*/
|
|
985
|
+
start(engine = globalThis) {
|
|
986
|
+
if (!this.running) {
|
|
987
|
+
this.stop();
|
|
988
|
+
}
|
|
989
|
+
const fromValue = this._o[this._p] || 0;
|
|
990
|
+
this._lc = engine;
|
|
991
|
+
this._u = engine.listen("update", (dt) => {
|
|
992
|
+
this._o[this._p] = engine.lerp(
|
|
993
|
+
fromValue,
|
|
994
|
+
this._x,
|
|
995
|
+
this._e(this._t / this._d)
|
|
996
|
+
);
|
|
997
|
+
this._t += dt;
|
|
998
|
+
if (this._t >= this._d) {
|
|
999
|
+
this._o[this._p] = this._x;
|
|
1000
|
+
this.stop();
|
|
1001
|
+
}
|
|
1002
|
+
});
|
|
1003
|
+
this.running = true;
|
|
1004
|
+
return this;
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
1007
|
+
* @param {Function} callback
|
|
1008
|
+
*/
|
|
1009
|
+
onEnd(callback) {
|
|
1010
|
+
this._cb.push(callback);
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* @param {boolean} completed if `false` don't call the `onEnd()` registered callbacks.
|
|
1014
|
+
* @returns
|
|
1015
|
+
*/
|
|
1016
|
+
stop(completed = true) {
|
|
1017
|
+
if (!this.running || !this._u) return;
|
|
1018
|
+
this.running = false;
|
|
1019
|
+
this._u();
|
|
1020
|
+
if (completed) {
|
|
1021
|
+
for (const callback of this._cb) {
|
|
1022
|
+
callback(this._o);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
reset() {
|
|
1027
|
+
this._cb.length = 0;
|
|
1028
|
+
this.stop();
|
|
1029
|
+
}
|
|
1030
|
+
get progress() {
|
|
1031
|
+
return this.running ? this._t / this._d : 0;
|
|
1032
|
+
}
|
|
1033
|
+
};
|
|
1034
|
+
|
|
833
1035
|
// src/_web.js
|
|
834
1036
|
globalThis.utils = Object.assign(globalThis.utils || {}, src_exports);
|
|
835
1037
|
})();
|
package/dist/all.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(()=>{var
|
|
2
|
-
`)}},
|
|
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:()=>Yt,BOUNCE_OUT:()=>S,Camera:()=>u,DOWN:()=>wt,EASE_IN:()=>Ot,EASE_IN_OUT:()=>Dt,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=(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 X(e,0,this._w-1)}clampY(e){return X(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 X(t,e,s){return t<e?e:t>s?s:t}var I=Math.sqrt,R=Math.cos,Y=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,B=(t,e,s=e)=>(l(e)?B(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=R(e),i=Y(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(R(i)*r,Y(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;flipX=!1;flipY=!1;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.flipX?-1:1)*this._s.x,(this.flipY?-1:1)*this._s.y)}drawImage(e,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&&e.alpha(this.opacity),e.image(r,o,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)),Xt=t=>Math.sin(-13*m*(t+1))*Math.pow(2,-10*t)+1,Rt=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,Yt=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);})();
|
|
3
3
|
/*! @litecanvas/utils by Luiz Bills | MIT Licensed */
|
package/dist/math.js
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
// src/math/index.js
|
|
18
18
|
var math_exports = {};
|
|
19
19
|
__export(math_exports, {
|
|
20
|
+
advance: () => advance_default,
|
|
20
21
|
diff: () => diff_default,
|
|
21
22
|
fract: () => fract_default,
|
|
22
23
|
range: () => range_default,
|
|
@@ -35,6 +36,19 @@
|
|
|
35
36
|
// src/math/range.js
|
|
36
37
|
var range_default = (size) => Array.from(Array(size).keys());
|
|
37
38
|
|
|
39
|
+
// src/vector/index.js
|
|
40
|
+
var PI2 = 2 * Math.PI;
|
|
41
|
+
|
|
42
|
+
// src/math/advance.js
|
|
43
|
+
var advance_default = advance = (position, velocity, acceleration, deltaTime = 1) => {
|
|
44
|
+
if (acceleration) {
|
|
45
|
+
velocity.x += acceleration.x * deltaTime;
|
|
46
|
+
velocity.y += acceleration.y * deltaTime;
|
|
47
|
+
}
|
|
48
|
+
position.x += velocity.x * deltaTime;
|
|
49
|
+
position.y += velocity.y * deltaTime;
|
|
50
|
+
};
|
|
51
|
+
|
|
38
52
|
// src/math/_web.js
|
|
39
53
|
globalThis.utils = Object.assign(globalThis.utils || {}, math_exports);
|
|
40
54
|
})();
|
package/dist/math.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var a=Object.defineProperty;var p=(t,o)=>{for(var r in o)a(t,r,{get:o[r],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let t in globalThis.utils)t!=="global"&&(globalThis[t]=globalThis.utils[t])};var s={};p(s,{advance:()=>u,diff:()=>n,fract:()=>x,range:()=>y,wave:()=>c});var n=(t,o)=>Math.abs(o-t)||0;var c=(t,o,r,e=Math.sin)=>t+(e(r)+1)/2*(o-t);var x=t=>t%1||0;var y=t=>Array.from(Array(t).keys());var d=2*Math.PI;var u=advance=(t,o,r,e=1)=>{r&&(o.x+=r.x*e,o.y+=r.y*e),t.x+=o.x*e,t.y+=o.y*e};globalThis.utils=Object.assign(globalThis.utils||{},s);})();
|