@litecanvas/utils 0.31.0 → 0.32.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.min.js +1 -1
- package/dist/all.js +91 -61
- package/dist/all.min.js +2 -2
- package/dist/collection.js +4 -6
- package/dist/collection.min.js +1 -1
- package/dist/collision.js +47 -6
- package/dist/collision.min.js +1 -1
- package/dist/image.min.js +1 -1
- package/dist/math.js +11 -10
- package/dist/math.min.js +1 -1
- package/dist/noise.min.js +1 -1
- package/dist/tween.min.js +1 -1
- package/package.json +1 -1
- package/src/collection/range.js +3 -7
- package/src/collection/shuffle.js +3 -3
- package/src/collision/README.md +17 -5
- package/src/collision/_web.js +2 -6
- package/src/collision/colcirc.js +23 -0
- package/src/collision/colrect.js +27 -0
- package/src/collision/index.js +4 -0
- package/src/debug/assert.js +7 -0
- package/src/index.js +1 -2
- package/src/math/index.js +1 -1
- package/src/math/median.js +17 -0
- package/src/math/roundd.js +0 -14
- /package/src/collision/{resolve.js → resolverect.js} +0 -0
package/dist/actor.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var c=Object.defineProperty;var
|
|
1
|
+
(()=>{var c=Object.defineProperty;var p=(s,t)=>{for(var e in t)c(s,e,{get:t[e],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(s=!0)=>{for(let t in globalThis.utils)t!=="global"&&(s||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var l={};p(l,{ANCHOR_BOT_LEFT:()=>d,ANCHOR_BOT_RIGHT:()=>y,ANCHOR_CENTER:()=>b,ANCHOR_TOP_LEFT:()=>m,ANCHOR_TOP_RIGHT:()=>f,Actor:()=>u});var x=2*Math.PI,i=class{x;y;constructor(t=0,e=t){this.x=t,this.y=e}toString(){return`Vector (${this.x}, ${this.y})`}},h=s=>s instanceof i,a=(s=0,t=s)=>(h(s)&&(t=s.y,s=s.x),new i(s,t));var b=a(.5,.5),m=a(0,0),f=a(1,0),d=a(0,1),y=a(1,1),u=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,e,r=m){this.sprite=t,this.pos=e||a(0),this._o=a(r),this._s=a(1,1)}set x(t){this.pos.x=t}get x(){return this.pos.x}set y(t){this.pos.y=t}get y(){return this.pos.y}set anchor(t){this._o.x=t.x,this._o.y=t.y}get anchor(){return this._o}get width(){return this.sprite.width*this._s.x}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}scaleTo(t,e=t){this._s.x=t,this._s.y=e}scaleBy(t,e=t){this._s.x*=t,this._s.y*=e}getBounds(t=!0){let e=this.sprite.width*(t?this._s.x:1),r=this.sprite.height*(t?this._s.y:1),n=this.pos.x-e*this.anchor.x,o=this.pos.y-r*this.anchor.y;return[n,o,e,r]}draw(t=globalThis,e=!0){this.hidden||this.opacity<=0||(e&&t.push(),this.transform(t),this.drawImage(t),e&&t.pop())}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(t.deg2rad(this.angle)),t.scale((this.flipX?-1:1)*this._s.x,(this.flipY?-1:1)*this._s.y)}drawImage(t,e=!0){let r=this.anchor,n=-this.sprite.width*(this.flipX?1-r.x:r.x),o=-this.sprite.height*(this.flipY?1-r.y:r.y);e&&t.alpha(this.opacity),t.image(n,o,this.sprite)}};globalThis.utils=Object.assign(globalThis.utils||{},l);})();
|
package/dist/all.js
CHANGED
|
@@ -51,6 +51,8 @@
|
|
|
51
51
|
ZERO: () => ZERO,
|
|
52
52
|
advance: () => advance_default,
|
|
53
53
|
choose: () => choose_default,
|
|
54
|
+
colcirc: () => colcirc_default,
|
|
55
|
+
colrect: () => colrect_default,
|
|
54
56
|
diff: () => diff_default,
|
|
55
57
|
dist: () => dist_default,
|
|
56
58
|
flipImage: () => flip_default,
|
|
@@ -63,10 +65,10 @@
|
|
|
63
65
|
makeCircle: () => make_circle_default,
|
|
64
66
|
makeRectangle: () => make_rectangle_default,
|
|
65
67
|
mean: () => mean_default,
|
|
68
|
+
median: () => median_default,
|
|
66
69
|
mod: () => mod_default,
|
|
67
70
|
range: () => range_default,
|
|
68
|
-
|
|
69
|
-
roundd: () => roundd_default,
|
|
71
|
+
resolverect: () => resolverect_default,
|
|
70
72
|
scaleImage: () => scale_default,
|
|
71
73
|
shuffle: () => shuffle_default,
|
|
72
74
|
sum: () => sum_default,
|
|
@@ -105,50 +107,6 @@
|
|
|
105
107
|
wave: () => wave_default
|
|
106
108
|
});
|
|
107
109
|
|
|
108
|
-
// src/collision/intersection.js
|
|
109
|
-
var intersection_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
110
|
-
const left = Math.max(x1, x2);
|
|
111
|
-
const width = Math.min(x1 + w1, x2 + w2) - left;
|
|
112
|
-
const top = Math.max(y1, y2);
|
|
113
|
-
const height = Math.min(y1 + h1, y2 + h2) - top;
|
|
114
|
-
return [left, top, width, height];
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
// src/collision/resolve.js
|
|
118
|
-
var resolve_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
119
|
-
const [left, top, width, height] = intersection_default(
|
|
120
|
-
x1,
|
|
121
|
-
y1,
|
|
122
|
-
w1,
|
|
123
|
-
h1,
|
|
124
|
-
x2,
|
|
125
|
-
y2,
|
|
126
|
-
w2,
|
|
127
|
-
h2
|
|
128
|
-
);
|
|
129
|
-
let direction = "";
|
|
130
|
-
let resolveX = x1;
|
|
131
|
-
let resolveY = y1;
|
|
132
|
-
if (width < height) {
|
|
133
|
-
if (x1 < x2) {
|
|
134
|
-
direction = "right";
|
|
135
|
-
resolveX = x2 - w1;
|
|
136
|
-
} else {
|
|
137
|
-
direction = "left";
|
|
138
|
-
resolveX = x2 + w2;
|
|
139
|
-
}
|
|
140
|
-
} else {
|
|
141
|
-
if (y1 < y2) {
|
|
142
|
-
direction = "bottom";
|
|
143
|
-
resolveY = y2 - h1;
|
|
144
|
-
} else {
|
|
145
|
-
direction = "top";
|
|
146
|
-
resolveY = y2 + h2;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
return { direction, x: resolveX, y: resolveY };
|
|
150
|
-
};
|
|
151
|
-
|
|
152
110
|
// src/camera/index.js
|
|
153
111
|
var Camera = class {
|
|
154
112
|
/** @type {LitecanvasInstance} */
|
|
@@ -345,6 +303,79 @@
|
|
|
345
303
|
}
|
|
346
304
|
};
|
|
347
305
|
|
|
306
|
+
// src/collision/intersection.js
|
|
307
|
+
var intersection_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
308
|
+
const left = Math.max(x1, x2);
|
|
309
|
+
const width = Math.min(x1 + w1, x2 + w2) - left;
|
|
310
|
+
const top = Math.max(y1, y2);
|
|
311
|
+
const height = Math.min(y1 + h1, y2 + h2) - top;
|
|
312
|
+
return [left, top, width, height];
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
// src/collision/resolverect.js
|
|
316
|
+
var resolverect_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
317
|
+
const [left, top, width, height] = intersection_default(
|
|
318
|
+
x1,
|
|
319
|
+
y1,
|
|
320
|
+
w1,
|
|
321
|
+
h1,
|
|
322
|
+
x2,
|
|
323
|
+
y2,
|
|
324
|
+
w2,
|
|
325
|
+
h2
|
|
326
|
+
);
|
|
327
|
+
let direction = "";
|
|
328
|
+
let resolveX = x1;
|
|
329
|
+
let resolveY = y1;
|
|
330
|
+
if (width < height) {
|
|
331
|
+
if (x1 < x2) {
|
|
332
|
+
direction = "right";
|
|
333
|
+
resolveX = x2 - w1;
|
|
334
|
+
} else {
|
|
335
|
+
direction = "left";
|
|
336
|
+
resolveX = x2 + w2;
|
|
337
|
+
}
|
|
338
|
+
} else {
|
|
339
|
+
if (y1 < y2) {
|
|
340
|
+
direction = "bottom";
|
|
341
|
+
resolveY = y2 - h1;
|
|
342
|
+
} else {
|
|
343
|
+
direction = "top";
|
|
344
|
+
resolveY = y2 + h2;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return { direction, x: resolveX, y: resolveY };
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
// src/debug/assert.js
|
|
351
|
+
var assert_default = (condition, message = "Assertion failed") => {
|
|
352
|
+
if (!condition) throw new Error(message);
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
// src/collision/colrect.js
|
|
356
|
+
var colrect_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
357
|
+
DEV: assert_default(isFinite(x1), "colrect: 1st param must be a number");
|
|
358
|
+
DEV: assert_default(isFinite(y1), "colrect: 2nd param must be a number");
|
|
359
|
+
DEV: assert_default(isFinite(w1), "colrect: 3rd param must be a number");
|
|
360
|
+
DEV: assert_default(isFinite(h1), "colrect: 4th param must be a number");
|
|
361
|
+
DEV: assert_default(isFinite(x2), "colrect: 5th param must be a number");
|
|
362
|
+
DEV: assert_default(isFinite(y2), "colrect: 6th param must be a number");
|
|
363
|
+
DEV: assert_default(isFinite(w2), "colrect: 7th param must be a number");
|
|
364
|
+
DEV: assert_default(isFinite(h2), "colrect: 8th param must be a number");
|
|
365
|
+
return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2;
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// src/collision/colcirc.js
|
|
369
|
+
var colcirc_default = (x1, y1, r1, x2, y2, r2) => {
|
|
370
|
+
DEV: assert_default(isFinite(x1), "colcirc: 1st param must be a number");
|
|
371
|
+
DEV: assert_default(isFinite(y1), "colcirc: 2nd param must be a number");
|
|
372
|
+
DEV: assert_default(isFinite(r1), "colcirc: 3rd param must be a number");
|
|
373
|
+
DEV: assert_default(isFinite(x2), "colcirc: 4th param must be a number");
|
|
374
|
+
DEV: assert_default(isFinite(y2), "colcirc: 5th param must be a number");
|
|
375
|
+
DEV: assert_default(isFinite(r2), "colcirc: 6th param must be a number");
|
|
376
|
+
return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= (r1 + r2) * (r1 + r2);
|
|
377
|
+
};
|
|
378
|
+
|
|
348
379
|
// src/grid/index.js
|
|
349
380
|
var Grid = class _Grid {
|
|
350
381
|
/** @type {number} The grid width */
|
|
@@ -923,15 +954,6 @@
|
|
|
923
954
|
// src/math/mod.js
|
|
924
955
|
var mod_default = (a, b) => (b + a % b) % b;
|
|
925
956
|
|
|
926
|
-
// src/math/roundd.js
|
|
927
|
-
var roundd_default = (n, precision = 0) => {
|
|
928
|
-
if (!precision) {
|
|
929
|
-
return Math.round(n);
|
|
930
|
-
}
|
|
931
|
-
const multiplier = Math.pow(10, precision);
|
|
932
|
-
return Math.round(n * multiplier) / multiplier;
|
|
933
|
-
};
|
|
934
|
-
|
|
935
957
|
// src/math/dist.js
|
|
936
958
|
var dist_default = (x1, y1, x2, y2) => {
|
|
937
959
|
return Math.hypot(x2 - x1, y2 - y1);
|
|
@@ -956,6 +978,16 @@
|
|
|
956
978
|
return sum_default(values) / values.length;
|
|
957
979
|
};
|
|
958
980
|
|
|
981
|
+
// src/math/median.js
|
|
982
|
+
var median_default = (...values) => {
|
|
983
|
+
const sorted = values.sort((a, b) => a - b);
|
|
984
|
+
const middle = Math.floor(sorted.length / 2);
|
|
985
|
+
if (sorted.length % 2 === 0) {
|
|
986
|
+
return (sorted[middle - 1] + sorted[middle]) / 2;
|
|
987
|
+
}
|
|
988
|
+
return sorted[middle];
|
|
989
|
+
};
|
|
990
|
+
|
|
959
991
|
// src/math/lerp-angle.js
|
|
960
992
|
var lerp_angle_default = (start, end, amount) => {
|
|
961
993
|
let dif = (end - start) % 360;
|
|
@@ -1381,15 +1413,13 @@
|
|
|
1381
1413
|
};
|
|
1382
1414
|
|
|
1383
1415
|
// src/collection/range.js
|
|
1384
|
-
var range_default = (size, from = 0, step = 1) => Array.
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
})
|
|
1388
|
-
);
|
|
1416
|
+
var range_default = (size, from = 0, step = 1) => [...new Array(size).keys()].map((i) => {
|
|
1417
|
+
return from + step * i;
|
|
1418
|
+
});
|
|
1389
1419
|
|
|
1390
1420
|
// src/collection/shuffle.js
|
|
1391
1421
|
var shuffle_default = (values, rng = globalThis.rand || Math.random) => {
|
|
1392
|
-
values =
|
|
1422
|
+
values = [...values];
|
|
1393
1423
|
for (let i = values.length - 1; i > 0; i--) {
|
|
1394
1424
|
let j = Math.floor(rng() * (i + 1));
|
|
1395
1425
|
let temp = values[i];
|
package/dist/all.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(()=>{var
|
|
2
|
-
`)}},
|
|
1
|
+
(()=>{var Mt=Object.defineProperty;var It=(e,t)=>{for(var s in t)Mt(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={};It(z,{ANCHOR_BOT_LEFT:()=>te,ANCHOR_BOT_RIGHT:()=>ee,ANCHOR_CENTER:()=>Qt,ANCHOR_TOP_LEFT:()=>J,ANCHOR_TOP_RIGHT:()=>Jt,Actor:()=>C,BACK_IN:()=>ne,BACK_IN_OUT:()=>le,BACK_OUT:()=>oe,BOUNCE_IN:()=>ct,BOUNCE_IN_OUT:()=>me,BOUNCE_OUT:()=>v,Camera:()=>y,DOWN:()=>Zt,EASE_IN:()=>re,EASE_IN_OUT:()=>ae,EASE_OUT:()=>ie,ELASTIC_IN:()=>he,ELASTIC_IN_OUT:()=>ce,ELASTIC_OUT:()=>ue,Grid:()=>M,LEFT:()=>Kt,LINEAR:()=>ut,Noise:()=>Y,ONE:()=>qt,RIGHT:()=>$t,TypedGrid:()=>P,UP:()=>Vt,Vector:()=>F,ZERO:()=>Q,advance:()=>rt,choose:()=>Ft,colcirc:()=>W,colrect:()=>B,diff:()=>tt,dist:()=>at,flipImage:()=>ft,fract:()=>st,head:()=>wt,intersection:()=>E,last:()=>Tt,lerpAngle:()=>ht,mag:()=>nt,makeCircle:()=>xt,makeRectangle:()=>_t,mean:()=>ot,median:()=>lt,mod:()=>it,range:()=>gt,resolverect:()=>U,scaleImage:()=>dt,shuffle:()=>yt,sum:()=>A,tail:()=>Et,tintImage:()=>bt,tween:()=>se,vec:()=>o,vecAbs:()=>zt,vecAdd:()=>L,vecAngle:()=>Ct,vecAngleBetween:()=>Rt,vecCeil:()=>Xt,vecClamp:()=>Wt,vecCross:()=>vt,vecDist:()=>Ot,vecDist2:()=>St,vecDiv:()=>I,vecDot:()=>K,vecEq:()=>D,vecFloor:()=>Ut,vecIsZero:()=>jt,vecLerp:()=>Nt,vecLimit:()=>Lt,vecMag:()=>$,vecMag2:()=>Z,vecMove:()=>Gt,vecMult:()=>w,vecNorm:()=>S,vecRand:()=>Yt,vecReflect:()=>Pt,vecRotate:()=>Ht,vecRound:()=>Bt,vecSet:()=>V,vecSetMag:()=>Dt,vecSub:()=>O,wave:()=>et});var y=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(t=null,s=0,r=0,i=null,a=null){this._engine=t||globalThis,this.ox=s,this.oy=r,this.resize(i||this._engine.WIDTH-s,a||this._engine.HEIGHT-r),this.x=this.width/2,this.y=this.height/2}resize(t,s){this.width=t,this.height=s,this._engine.emit("camera-resized",this)}start(t=!1){if(this._engine.push(),t){let i=path();i.rect(this.ox,this.oy,this.width,this.height),this._engine.clip(i)}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),a=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-a*s+this.x,r.y=a*t+i*s+this.y,r}getCameraPoint(t,s,r={}){let i=Math.cos(-this.rotation),a=Math.sin(-this.rotation);return t=t-this.x,s=s-this.y,t=i*t-a*s,s=a*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]}viewing(t,s,r,i){let a=this.width/2-this.x,n=this.height/2-this.y,l=this.width/this.scale,h=this.height/this.scale;return this._engine.colrect(t,s,r,i,a,n,l,h)}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 E=(e,t,s,r,i,a,n,l)=>{let h=Math.max(e,i),b=Math.min(e+s,i+n)-h,c=Math.max(t,a),x=Math.min(t+r,a+l)-c;return[h,c,b,x]};var U=(e,t,s,r,i,a,n,l)=>{let[h,b,c,x]=E(e,t,s,r,i,a,n,l),d="",g=e,u=t;return c<x?e<i?(d="right",g=i-s):(d="left",g=i+n):t<a?(d="bottom",u=a-r):(d="top",u=a+l),{direction:d,x:g,y:u}};var B=(e,t,s,r,i,a,n,l)=>e<i+n&&e+s>i&&t<a+l&&t+r>a;var W=(e,t,s,r,i,a)=>(r-e)*(r-e)+(i-t)*(i-t)<=(s+a)*(s+a);var M=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,a=s?-1:1;for(;r!==i;){let n=this.indexToPointX(r),l=this.indexToPointY(r),h=this._c[r];if(t(n,l,h,this)===!1)break;r+=a}}fill(t){this.forEach((s,r)=>{this.set(s,r,t)})}clampX(t){return G(t,0,this._w-1)}clampY(t){return G(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,a,n)=>{r[a]=r[a]||"",r[a]+=n+t}),r.join(`
|
|
2
|
+
`)}},P=class e extends M{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 G(e,t,s){return e<t?t:e>s?s:e}var At=Math.sqrt,j=Math.cos,q=Math.sin,kt=2*Math.PI,F=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},_=e=>e instanceof F,o=(e=0,t=e)=>(_(e)&&(t=e.y,e=e.x),new F(e,t)),D=(e,t,s=t)=>_(t)?D(e,t.x,t.y):e.x===t&&e.y===s,V=(e,t,s=t)=>(_(t)?V(e,t.x,t.y):(e.x=t,e.y=s),e),L=(e,t,s=t)=>_(t)?L(e,t.x,t.y):(e.x+=t,e.y+=s,e),O=(e,t,s=t)=>_(t)?O(e,t.x,t.y):(e.x-=t,e.y-=s,e),w=(e,t,s=t)=>_(t)?w(e,t.x,t.y):(e.x*=t,e.y*=s,e),I=(e,t,s=t)=>_(t)?I(e,t.x,t.y):(e.x/=t||1,e.y/=s||1,e),Ht=(e,t)=>{let s=j(t),r=q(t);return e.x=s*e.x-r*e.y,e.y=r*e.x+s*e.y,e},Pt=(e,t)=>{let s=S(o(t));return O(e,w(s,2*K(e,s)))},Dt=(e,t)=>(S(e),w(e,t),e),$=e=>Math.hypot(e.x,e.y),Z=e=>e.x*e.x+e.y*e.y,S=e=>{let t=$(e);return t>0&&I(e,t),e},Lt=(e,t=1)=>{let s=Z(e);return s>t*t&&(I(e,At(s)),w(e,t)),e},Ot=(e,t)=>Math.hypot(t.x-e.x,t.y-e.y),St=(e,t)=>{let s=e.x-t.x,r=e.y-t.y;return s*s+r*r},Ct=e=>Math.atan2(e.y,e.x),Rt=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),K=(e,t)=>e.x*t.x+e.y*t.y,vt=(e,t)=>e.x*t.y-e.y*t.x,Nt=(e,t,s)=>(e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0,e),Yt=(e=1,t=e,s=globalThis.rand||Math.random)=>{let r=s()*kt,i=s()*(t-e)+e;return o(j(r)*i,q(r)*i)},zt=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),Xt=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),Ut=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),Bt=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),Wt=(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),Gt=(e,t,s=1)=>L(e,t.x*s,t.y*s),jt=e=>D(e,Q),Q=o(0,0),qt=o(1,1),Vt=o(0,-1),$t=o(1,0),Zt=o(0,1),Kt=o(-1,0);var Qt=o(.5,.5),J=o(0,0),Jt=o(1,0),te=o(0,1),ee=o(1,1),C=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,r=J){this.sprite=t,this.pos=s||o(0),this._o=o(r),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.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,a=this.pos.y-r*this.anchor.y;return[i,a,s,r]}draw(t=globalThis,s=!0){this.hidden||this.opacity<=0||(s&&t.push(),this.transform(t),this.drawImage(t),s&&t.pop())}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(t.deg2rad(this.angle)),t.scale((this.flipX?-1:1)*this._s.x,(this.flipY?-1:1)*this._s.y)}drawImage(t,s=!0){let r=this.anchor,i=-this.sprite.width*(this.flipX?1-r.x:r.x),a=-this.sprite.height*(this.flipY?1-r.y:r.y);s&&t.alpha(this.opacity),t.image(i,a,this.sprite)}};var tt=(e,t)=>Math.abs(t-e)||0;var et=(e,t,s,r=Math.sin)=>e+(r(s)+1)/2*(t-e);var st=e=>e%1||0;var rt=advance=(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 it=(e,t)=>(t+e%t)%t;var at=(e,t,s,r)=>Math.hypot(s-e,r-t);var nt=(e,t)=>Math.hypot(e,t);var A=e=>{let t=0;for(let s=0;s<e.length;s++)t+=e[s];return t};var ot=e=>A(e)/e.length;var lt=(...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 ht=(e,t,s)=>{let r=(t-e)%360;return r>180?r-=360:r<-180&&(r+=360),e+r*s};var k=Math.PI/2,se=(e,t,s,r=1,i=ut)=>new R(e,t,s,r,i),ut=e=>e,re=e=>e*e,ie=e=>-e*(e-2),ae=e=>e<.5?2*e*e:-2*e*e+4*e-1,ne=e=>e*e*e-e*Math.sin(e*Math.PI),oe=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},le=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},he=e=>Math.sin(13*k*e)*Math.pow(2,10*(e-1)),ue=e=>Math.sin(-13*k*(e+1))*Math.pow(2,-10*e)+1,ce=e=>{if(e<.5){let r=Math.sin(13*k*(2*e)),i=Math.pow(2,10*(2*e-1));return .5*r*i}let t=Math.sin(-13*k*(2*e-1+1)),s=Math.pow(2,-10*(2*e-1));return .5*(t*s+2)},ct=e=>1-v(1-e),v=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,me=e=>e<.5?.5*ct(e*2):.5*v(e*2-1)+.5,R=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,a){this._o=t,this._p=s,this._x=r,this._d=i,this._e=a,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 a=this._t-this._w;this._o[this._p]=this._lc.lerp(s,r,this._e(a/this._d)),this._t+=i,a>=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 mt=4,H=1<<mt,pt=8,pe=1<<pt,f=4095,N=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),a=Math.floor(s),n=Math.floor(r),l=t-i,h=s-a,b=r-n,c,x,d=0,g=.5,u,m,T;for(let X=0;X<this._po;X++){let p=i+(a<<mt)+(n<<pt);c=N(l),x=N(h),u=this._p[p&f],u+=c*(this._p[p+1&f]-u),m=this._p[p+H&f],m+=c*(this._p[p+H+1&f]-m),u+=x*(m-u),p+=pe,m=this._p[p&f],m+=c*(this._p[p+1&f]-m),T=this._p[p+H&f],T+=c*(this._p[p+H+1&f]-T),m+=x*(T-m),u+=N(b)*(m-u),d+=u*g,g*=this._pf,i<<=1,l*=2,a<<=1,h*=2,n<<=1,b*=2,l>=1&&(i++,l--),h>=1&&(a++,h--),b>=1&&(n++,b--)}return d}noiseDetail(t,s){t>0&&(this._po=t),s>0&&(this._pf=s)}noiseSeed(t=null){t!=null&&this._e.seed(t);let s=this._e.rand||Math.random;for(let r=0;r<f+1;r++)this._p[r]=s()}};var ft=(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 dt=(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 bt=(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 a=e*2+s;return i.paint(a,a,()=>{i.circfill(a/2,a/2,e,t),s>0&&(i.linewidth(s),i.stroke(r))})};var _t=(e,t,s,{borderWidth:r=0,borderColor:i=0,engine:a=globalThis}={})=>{let n=e+r*2,l=t+r*2;return a.paint(n,l,()=>{a.rectfill(r>0?r:0,r>0?r:0,e,t,s),r>0&&(a.linewidth(r),a.stroke(i))})};var gt=(e,t=0,s=1)=>[...new Array(e).keys()].map(r=>t+s*r);var yt=(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 Ft=(e,t=globalThis.rand||Math.random)=>e[Math.floor(t()*e.length)];var wt=e=>e[0];var Tt=e=>e[e.length-1];var Et=e=>e.slice(1);globalThis.utils=Object.assign(globalThis.utils||{},z);})();
|
|
3
3
|
/*! @litecanvas/utils by Luiz Bills | MIT Licensed */
|
package/dist/collection.js
CHANGED
|
@@ -28,15 +28,13 @@
|
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
// src/collection/range.js
|
|
31
|
-
var range_default = (size, from = 0, step = 1) => Array.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
})
|
|
35
|
-
);
|
|
31
|
+
var range_default = (size, from = 0, step = 1) => [...new Array(size).keys()].map((i) => {
|
|
32
|
+
return from + step * i;
|
|
33
|
+
});
|
|
36
34
|
|
|
37
35
|
// src/collection/shuffle.js
|
|
38
36
|
var shuffle_default = (values, rng = globalThis.rand || Math.random) => {
|
|
39
|
-
values =
|
|
37
|
+
values = [...values];
|
|
40
38
|
for (let i = values.length - 1; i > 0; i--) {
|
|
41
39
|
let j = Math.floor(rng() * (i + 1));
|
|
42
40
|
let temp = values[i];
|
package/dist/collection.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var g=Object.defineProperty;var u=(t,l)=>{for(var o in l)g(t,o,{get:l[o],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(t=!0)=>{for(let l in globalThis.utils)l!=="global"&&(t||globalThis[l]===void 0)&&(globalThis[l]=globalThis.utils[l])};var
|
|
1
|
+
(()=>{var g=Object.defineProperty;var u=(t,l)=>{for(var o in l)g(t,o,{get:l[o],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(t=!0)=>{for(let l in globalThis.utils)l!=="global"&&(t||globalThis[l]===void 0)&&(globalThis[l]=globalThis.utils[l])};var r={};u(r,{choose:()=>f,head:()=>s,last:()=>h,range:()=>a,shuffle:()=>i,tail:()=>n});var a=(t,l=0,o=1)=>[...new Array(t).keys()].map(e=>l+o*e);var i=(t,l=globalThis.rand||Math.random)=>{t=[...t];for(let o=t.length-1;o>0;o--){let e=Math.floor(l()*(o+1)),d=t[o];t[o]=t[e],t[e]=d}return t};var f=(t,l=globalThis.rand||Math.random)=>t[Math.floor(l()*t.length)];var s=t=>t[0];var h=t=>t[t.length-1];var n=t=>t.slice(1);globalThis.utils=Object.assign(globalThis.utils||{},r);})();
|
package/dist/collision.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
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
|
+
|
|
2
8
|
// src/_global.js
|
|
3
9
|
globalThis.utils = globalThis.utils || {};
|
|
4
10
|
globalThis.utils.global = (overrides = true) => {
|
|
@@ -10,6 +16,15 @@
|
|
|
10
16
|
}
|
|
11
17
|
};
|
|
12
18
|
|
|
19
|
+
// src/collision/index.js
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
colcirc: () => colcirc_default,
|
|
23
|
+
colrect: () => colrect_default,
|
|
24
|
+
intersection: () => intersection_default,
|
|
25
|
+
resolverect: () => resolverect_default
|
|
26
|
+
});
|
|
27
|
+
|
|
13
28
|
// src/collision/intersection.js
|
|
14
29
|
var intersection_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
15
30
|
const left = Math.max(x1, x2);
|
|
@@ -19,8 +34,8 @@
|
|
|
19
34
|
return [left, top, width, height];
|
|
20
35
|
};
|
|
21
36
|
|
|
22
|
-
// src/collision/
|
|
23
|
-
var
|
|
37
|
+
// src/collision/resolverect.js
|
|
38
|
+
var resolverect_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
24
39
|
const [left, top, width, height] = intersection_default(
|
|
25
40
|
x1,
|
|
26
41
|
y1,
|
|
@@ -54,9 +69,35 @@
|
|
|
54
69
|
return { direction, x: resolveX, y: resolveY };
|
|
55
70
|
};
|
|
56
71
|
|
|
72
|
+
// src/debug/assert.js
|
|
73
|
+
var assert_default = (condition, message = "Assertion failed") => {
|
|
74
|
+
if (!condition) throw new Error(message);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// src/collision/colrect.js
|
|
78
|
+
var colrect_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
79
|
+
DEV: assert_default(isFinite(x1), "colrect: 1st param must be a number");
|
|
80
|
+
DEV: assert_default(isFinite(y1), "colrect: 2nd param must be a number");
|
|
81
|
+
DEV: assert_default(isFinite(w1), "colrect: 3rd param must be a number");
|
|
82
|
+
DEV: assert_default(isFinite(h1), "colrect: 4th param must be a number");
|
|
83
|
+
DEV: assert_default(isFinite(x2), "colrect: 5th param must be a number");
|
|
84
|
+
DEV: assert_default(isFinite(y2), "colrect: 6th param must be a number");
|
|
85
|
+
DEV: assert_default(isFinite(w2), "colrect: 7th param must be a number");
|
|
86
|
+
DEV: assert_default(isFinite(h2), "colrect: 8th param must be a number");
|
|
87
|
+
return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// src/collision/colcirc.js
|
|
91
|
+
var colcirc_default = (x1, y1, r1, x2, y2, r2) => {
|
|
92
|
+
DEV: assert_default(isFinite(x1), "colcirc: 1st param must be a number");
|
|
93
|
+
DEV: assert_default(isFinite(y1), "colcirc: 2nd param must be a number");
|
|
94
|
+
DEV: assert_default(isFinite(r1), "colcirc: 3rd param must be a number");
|
|
95
|
+
DEV: assert_default(isFinite(x2), "colcirc: 4th param must be a number");
|
|
96
|
+
DEV: assert_default(isFinite(y2), "colcirc: 5th param must be a number");
|
|
97
|
+
DEV: assert_default(isFinite(r2), "colcirc: 6th param must be a number");
|
|
98
|
+
return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= (r1 + r2) * (r1 + r2);
|
|
99
|
+
};
|
|
100
|
+
|
|
57
101
|
// src/collision/_web.js
|
|
58
|
-
globalThis.utils = Object.assign(globalThis.utils || {},
|
|
59
|
-
resolve: resolve_default,
|
|
60
|
-
intersection: intersection_default
|
|
61
|
-
});
|
|
102
|
+
globalThis.utils = Object.assign(globalThis.utils || {}, index_exports);
|
|
62
103
|
})();
|
package/dist/collision.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=(
|
|
1
|
+
(()=>{var x=Object.defineProperty;var w=(e,t)=>{for(var i in t)x(e,i,{get:t[i],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 h={};w(h,{colcirc:()=>T,colrect:()=>F,intersection:()=>c,resolverect:()=>g});var c=(e,t,i,a,r,s,o,l)=>{let u=Math.max(e,r),d=Math.min(e+i,r+o)-u,n=Math.max(t,s),b=Math.min(t+a,s+l)-n;return[u,n,d,b]};var g=(e,t,i,a,r,s,o,l)=>{let[u,d,n,b]=c(e,t,i,a,r,s,o,l),m="",p=e,f=t;return n<b?e<r?(m="right",p=r-i):(m="left",p=r+o):t<s?(m="bottom",f=s-a):(m="top",f=s+l),{direction:m,x:p,y:f}};var F=(e,t,i,a,r,s,o,l)=>e<r+o&&e+i>r&&t<s+l&&t+a>s;var T=(e,t,i,a,r,s)=>(a-e)*(a-e)+(r-t)*(r-t)<=(i+s)*(i+s);globalThis.utils=Object.assign(globalThis.utils||{},h);})();
|
package/dist/image.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var f=Object.defineProperty;var d=(
|
|
1
|
+
(()=>{var f=Object.defineProperty;var d=(t,a)=>{for(var s in a)f(t,s,{get:a[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(t=!0)=>{for(let a in globalThis.utils)a!=="global"&&(t||globalThis[a]===void 0)&&(globalThis[a]=globalThis.utils[a])};var n={};d(n,{flipImage:()=>o,makeCircle:()=>u,makeRectangle:()=>p,scaleImage:()=>l,tintImage:()=>m});var o=(t,a=!0,s=!1,e=globalThis)=>e.paint(t.width,t.height,r=>{e.push(),e.scale(a?-1:1,s?-1:1),e.image(a?-t.width:0,s?-t.height:0,t),e.pop()});var l=(t,a,s=!0,e=globalThis)=>e.paint(t.width*a,t.height*a,r=>{e.push(),r.imageSmoothingEnabled=!s,e.scale(a),e.image(0,0,t),e.pop()});var m=(t,a,s=1,e=globalThis)=>e.paint(t.width,t.height,r=>{e.push(),e.alpha(s),e.rectfill(0,0,t.width,t.height,a),r.globalCompositeOperation="destination-atop",e.alpha(1),e.image(0,0,t),e.pop()});var u=(t,a,{borderWidth:s=0,borderColor:e=0,engine:r=globalThis}={})=>{let i=t*2+s;return r.paint(i,i,()=>{r.circfill(i/2,i/2,t,a),s>0&&(r.linewidth(s),r.stroke(e))})};var p=(t,a,s,{borderWidth:e=0,borderColor:r=0,engine:i=globalThis}={})=>{let c=t+e*2,b=a+e*2;return i.paint(c,b,()=>{i.rectfill(e>0?e:0,e>0?e:0,t,a,s),e>0&&(i.linewidth(e),i.stroke(r))})};globalThis.utils=Object.assign(globalThis.utils||{},n);})();
|
package/dist/math.js
CHANGED
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
lerpAngle: () => lerp_angle_default,
|
|
27
27
|
mag: () => mag_default,
|
|
28
28
|
mean: () => mean_default,
|
|
29
|
+
median: () => median_default,
|
|
29
30
|
mod: () => mod_default,
|
|
30
|
-
roundd: () => roundd_default,
|
|
31
31
|
sum: () => sum_default,
|
|
32
32
|
wave: () => wave_default
|
|
33
33
|
});
|
|
@@ -57,15 +57,6 @@
|
|
|
57
57
|
// src/math/mod.js
|
|
58
58
|
var mod_default = (a, b) => (b + a % b) % b;
|
|
59
59
|
|
|
60
|
-
// src/math/roundd.js
|
|
61
|
-
var roundd_default = (n, precision = 0) => {
|
|
62
|
-
if (!precision) {
|
|
63
|
-
return Math.round(n);
|
|
64
|
-
}
|
|
65
|
-
const multiplier = Math.pow(10, precision);
|
|
66
|
-
return Math.round(n * multiplier) / multiplier;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
60
|
// src/math/dist.js
|
|
70
61
|
var dist_default = (x1, y1, x2, y2) => {
|
|
71
62
|
return Math.hypot(x2 - x1, y2 - y1);
|
|
@@ -90,6 +81,16 @@
|
|
|
90
81
|
return sum_default(values) / values.length;
|
|
91
82
|
};
|
|
92
83
|
|
|
84
|
+
// src/math/median.js
|
|
85
|
+
var median_default = (...values) => {
|
|
86
|
+
const sorted = values.sort((a, b) => a - b);
|
|
87
|
+
const middle = Math.floor(sorted.length / 2);
|
|
88
|
+
if (sorted.length % 2 === 0) {
|
|
89
|
+
return (sorted[middle - 1] + sorted[middle]) / 2;
|
|
90
|
+
}
|
|
91
|
+
return sorted[middle];
|
|
92
|
+
};
|
|
93
|
+
|
|
93
94
|
// src/math/lerp-angle.js
|
|
94
95
|
var lerp_angle_default = (start, end, amount) => {
|
|
95
96
|
let dif = (end - start) % 360;
|
package/dist/math.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var M=Object.defineProperty;var g=(e,t)=>{for(var r in t)M(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={};g(n,{advance:()=>a,diff:()=>c,dist:()=>p,fract:()=>u,lerpAngle:()=>h,mag:()=>f,mean:()=>y,median:()=>i,mod:()=>l,sum:()=>s,wave:()=>x});var c=(e,t)=>Math.abs(t-e)||0;var x=(e,t,r,o=Math.sin)=>e+(o(r)+1)/2*(t-e);var u=e=>e%1||0;var A=2*Math.PI;var a=advance=(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 l=(e,t)=>(t+e%t)%t;var p=(e,t,r,o)=>Math.hypot(r-e,o-t);var f=(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 y=e=>s(e)/e.length;var i=(...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 h=(e,t,r)=>{let o=(t-e)%360;return o>180?o-=360:o<-180&&(o+=360),e+o*r};globalThis.utils=Object.assign(globalThis.utils||{},n);})();
|
package/dist/noise.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var A=Object.defineProperty;var H=(o,e)=>{for(var t in e)A(o,t,{get:e[t],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(o=!0)=>{for(let e in globalThis.utils)e!=="global"&&(o||globalThis[e]===void 0)&&(globalThis[e]=globalThis.utils[e])};var y={};H(y,{Noise:()=>v});var T=4,h=1<<T,E=8,D=1<<E,i=4095,F=o=>.5*(1-Math.cos(o*Math.PI)),v=class{_p=[];_po=4;_pf=.5;_e=null;constructor(e){this._e=e||globalThis,this.noiseSeed()}noise(e,t=0,s=0){e<0&&(e=-e),t<0&&(t=-t),s<0&&(s=-s);let m=Math.floor(e),u=Math.floor(t),c=Math.floor(s),p=e-m,b=t-u,f=s-c,l,g,x=0,_=.5,n,a,d;for(let w=0;w<this._po;w++){let r=m+(u<<T)+(c<<E);l=F(p),g=F(b),n=this._p[r&i],n+=l*(this._p[r+1&i]-n),a=this._p[r+h&i],a+=l*(this._p[r+h+1&i]-a),n+=g*(a-n),r+=D,a=this._p[r&i],a+=l*(this._p[r+1&i]-a),d=this._p[r+h&i],d+=l*(this._p[r+h+1&i]-d),a+=g*(d-a),n+=F(f)*(a-n),x+=n*_,_*=this._pf,m<<=1,p*=2,u<<=1,b*=2,c<<=1,f*=2,p>=1&&(m++,p--),b>=1&&(u++,b--),f>=1&&(c++,f--)}return x}noiseDetail(e,t){e>0&&(this._po=e),t>0&&(this._pf=t)}noiseSeed(e=null){e!=null&&this._e.seed(e);let t=this._e.rand||Math.random;for(let s=0;s<i+1;s++)this._p[s]=t()}};globalThis.utils=Object.assign(globalThis.utils||{},y);})();
|
package/dist/tween.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var p=Object.defineProperty;var b=(t,e)=>{for(var s in e)p(t,s,{get:e[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(t=!0)=>{for(let e in globalThis.utils)e!=="global"&&(t||globalThis[e]===void 0)&&(globalThis[e]=globalThis.utils[e])};var l={};b(l,{BACK_IN:()=>_,BACK_IN_OUT:()=>x,BACK_OUT:()=>F,BOUNCE_IN:()=>c,BOUNCE_IN_OUT:()=>T,BOUNCE_OUT:()=>u,EASE_IN:()=>f,EASE_IN_OUT:()=>g,EASE_OUT:()=>d,ELASTIC_IN:()=>v,ELASTIC_IN_OUT:()=>w,ELASTIC_OUT:()=>y,LINEAR:()=>m,tween:()=>h});var n=Math.PI/2,h=(t,e,s,a=1,r=m)=>new o(t,e,s,a,r),m=t=>t,f=t=>t*t,d=t=>-t*(t-2),g=t=>t<.5?2*t*t:-2*t*t+4*t-1,_=t=>t*t*t-t*Math.sin(t*Math.PI),F=t=>{let e=1-t;return 1-(e*e*e-e*Math.sin(e*Math.PI))},x=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},v=t=>Math.sin(13*n*t)*Math.pow(2,10*(t-1)),y=t=>Math.sin(-13*n*(t+1))*Math.pow(2,-10*t)+1,w=t=>{if(t<.5){let a=Math.sin(13*n*(2*t)),r=Math.pow(2,10*(2*t-1));return .5*a*r}let e=Math.sin(-13*n*(2*t-1+1)),s=Math.pow(2,-10*(2*t-1));return .5*(e*s+2)},c=t=>1-u(1-t),u=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,T=t=>t<.5?.5*c(t*2):.5*u(t*2-1)+.5,o=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(e,s,a,r,i){this._o=e,this._p=s,this._x=a,this._d=r,this._e=i,this._w=0}start(e){if(this.running)return this;this._cu.stop(!1),this._ch=this._cu=this,this.running=!0;let s=this._o[this._p]||0,a=this._rel?s+this._x:this._x;return this._lc=this._lc||e||globalThis,this._u=this._lc.listen("update",r=>{if(this._t<=this._w){this._t+=r;return}let i=this._t-this._w;this._o[this._p]=this._lc.lerp(s,a,this._e(i/this._d)),this._t+=r,i>=this._d&&(this._o[this._p]=a,this.stop())}),this}stop(e=!0){if(!this._u)return this;if(this.running=!1,this._u(),this._t=0,e)for(let s of this._cb)s(this._o);return this}restart(e=null,s=!1){return this.stop(s).restart(e)}onEnd(e){return this._cb.push(e),this}chain(e){return this._ch.onEnd(()=>{this._cu=e.start(this._lc)}),this._ch=e,this}reset(){return this._cb.length=0,this.stop()}relative(e=!0){return this._rel=e,this}delay(e){return this._w=e,this}get current(){return this._cu}get progress(){return this.running&&this._t>this._w?(this._t-this._w)/this._d:0}};globalThis.utils=Object.assign(globalThis.utils||{},l);})();
|
package/package.json
CHANGED
package/src/collection/range.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creates an new array of shuffled values
|
|
3
3
|
*
|
|
4
|
-
* @param {
|
|
4
|
+
* @param {T[]} values
|
|
5
5
|
* @param {()=>number} [rng]
|
|
6
|
-
* @returns {
|
|
6
|
+
* @returns {T[]} the shuffled array
|
|
7
7
|
*/
|
|
8
8
|
export default (values, rng = globalThis.rand || Math.random) => {
|
|
9
|
-
values =
|
|
9
|
+
values = [...values] // clone the values
|
|
10
10
|
for (let i = values.length - 1; i > 0; i--) {
|
|
11
11
|
let j = Math.floor(rng() * (i + 1))
|
|
12
12
|
let temp = values[i]
|
package/src/collision/README.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
**CDN**: https://unpkg.com/@litecanvas/utils/dist/collision.js
|
|
4
4
|
|
|
5
|
+
## colrect
|
|
6
|
+
|
|
7
|
+
Check a collision between two rectangles.
|
|
8
|
+
|
|
9
|
+
Syntax: `colrect(x1, y1, w1, h1, x2, y2, w2, h2): boolean`
|
|
10
|
+
|
|
11
|
+
## colcirc
|
|
12
|
+
|
|
13
|
+
Check a collision between two circles.
|
|
14
|
+
|
|
15
|
+
Syntax: `colcirc(x1, y1, r1, x2, y2, r2): boolean`
|
|
16
|
+
|
|
5
17
|
## intersection
|
|
6
18
|
|
|
7
19
|
Returns the resulting rectangle of the intersection between two rectangles.
|
|
@@ -10,7 +22,7 @@ Syntax: `intersection(x1, y1, w1, h1, x2, y2, w2, h2): number[]`
|
|
|
10
22
|
|
|
11
23
|
```js
|
|
12
24
|
import litecanvas from "litecanvas"
|
|
13
|
-
import { intersection } from "@litecanvas/utils"
|
|
25
|
+
import { intersection, colrect } from "@litecanvas/utils"
|
|
14
26
|
|
|
15
27
|
litecanvas({
|
|
16
28
|
loop: { init, draw },
|
|
@@ -36,15 +48,15 @@ function draw() {
|
|
|
36
48
|
}
|
|
37
49
|
```
|
|
38
50
|
|
|
39
|
-
##
|
|
51
|
+
## resolverect
|
|
40
52
|
|
|
41
|
-
Syntax: `
|
|
53
|
+
Syntax: `resolverect(x1, y1, w1, h1, x2, y2, w2, h2): { direction: string, x: number, y: number }`
|
|
42
54
|
|
|
43
55
|
> Note: possible values for `direction` is `"top"`, `"botton"`, `"left"`, `"right"` or `""` (empty string, if no collision).
|
|
44
56
|
|
|
45
57
|
```js
|
|
46
58
|
import litecanvas from "litecanvas"
|
|
47
|
-
import {
|
|
59
|
+
import { resolverect, colrect } from "@litecanvas/utils"
|
|
48
60
|
|
|
49
61
|
litecanvas({
|
|
50
62
|
loop: { init, draw },
|
|
@@ -57,7 +69,7 @@ function init() {
|
|
|
57
69
|
|
|
58
70
|
// check the collision
|
|
59
71
|
if (colrect(...rect1, ...rect2)) {
|
|
60
|
-
const { direction, x, y } =
|
|
72
|
+
const { direction, x, y } = resolverect(...rect1, ...rect2)
|
|
61
73
|
|
|
62
74
|
// which side of rect1 is colliding with rect2
|
|
63
75
|
console.log(direction) // outputs "bottom"
|
package/src/collision/_web.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import "../_global.js"
|
|
2
|
-
import
|
|
3
|
-
import intersection from "./intersection.js"
|
|
2
|
+
import * as collisionUtils from "./index.js"
|
|
4
3
|
|
|
5
|
-
globalThis.utils = Object.assign(globalThis.utils || {},
|
|
6
|
-
resolve,
|
|
7
|
-
intersection,
|
|
8
|
-
})
|
|
4
|
+
globalThis.utils = Object.assign(globalThis.utils || {}, collisionUtils)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import assert from "../debug/assert"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check a collision between two circles
|
|
5
|
+
*
|
|
6
|
+
* @param {number} x1 first circle position X
|
|
7
|
+
* @param {number} y1 first circle position Y
|
|
8
|
+
* @param {number} r1 first circle position radius
|
|
9
|
+
* @param {number} x2 second circle position X
|
|
10
|
+
* @param {number} y2 second circle position Y
|
|
11
|
+
* @param {number} r2 second circle position radius
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
14
|
+
export default (x1, y1, r1, x2, y2, r2) => {
|
|
15
|
+
DEV: assert(isFinite(x1), "colcirc: 1st param must be a number")
|
|
16
|
+
DEV: assert(isFinite(y1), "colcirc: 2nd param must be a number")
|
|
17
|
+
DEV: assert(isFinite(r1), "colcirc: 3rd param must be a number")
|
|
18
|
+
DEV: assert(isFinite(x2), "colcirc: 4th param must be a number")
|
|
19
|
+
DEV: assert(isFinite(y2), "colcirc: 5th param must be a number")
|
|
20
|
+
DEV: assert(isFinite(r2), "colcirc: 6th param must be a number")
|
|
21
|
+
|
|
22
|
+
return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= (r1 + r2) * (r1 + r2)
|
|
23
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import assert from "../debug/assert"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check a collision between two rectangles
|
|
5
|
+
*
|
|
6
|
+
* @param {number} x1 first rectangle position X
|
|
7
|
+
* @param {number} y1 first rectangle position Y
|
|
8
|
+
* @param {number} w1 first rectangle width
|
|
9
|
+
* @param {number} h1 first rectangle height
|
|
10
|
+
* @param {number} x2 second rectangle position X
|
|
11
|
+
* @param {number} y2 second rectangle position Y
|
|
12
|
+
* @param {number} w2 second rectangle width
|
|
13
|
+
* @param {number} h2 second rectangle height
|
|
14
|
+
* @returns {boolean}
|
|
15
|
+
*/
|
|
16
|
+
export default (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
17
|
+
DEV: assert(isFinite(x1), "colrect: 1st param must be a number")
|
|
18
|
+
DEV: assert(isFinite(y1), "colrect: 2nd param must be a number")
|
|
19
|
+
DEV: assert(isFinite(w1), "colrect: 3rd param must be a number")
|
|
20
|
+
DEV: assert(isFinite(h1), "colrect: 4th param must be a number")
|
|
21
|
+
DEV: assert(isFinite(x2), "colrect: 5th param must be a number")
|
|
22
|
+
DEV: assert(isFinite(y2), "colrect: 6th param must be a number")
|
|
23
|
+
DEV: assert(isFinite(w2), "colrect: 7th param must be a number")
|
|
24
|
+
DEV: assert(isFinite(h2), "colrect: 8th param must be a number")
|
|
25
|
+
|
|
26
|
+
return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2
|
|
27
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
export { default as resolve } from "./collision/resolve.js"
|
|
2
|
-
export { default as intersection } from "./collision/intersection.js"
|
|
3
1
|
export { default as Camera } from "./camera/index.js"
|
|
2
|
+
export * from "./collision/index.js"
|
|
4
3
|
export * from "./grid/index.js"
|
|
5
4
|
export * from "./vector/index.js"
|
|
6
5
|
export * from "./actor/index.js"
|
package/src/math/index.js
CHANGED
|
@@ -3,9 +3,9 @@ export { default as wave } from "./wave.js"
|
|
|
3
3
|
export { default as fract } from "./fract.js"
|
|
4
4
|
export { default as advance } from "./advance.js"
|
|
5
5
|
export { default as mod } from "./mod.js"
|
|
6
|
-
export { default as roundd } from "./roundd.js"
|
|
7
6
|
export { default as dist } from "./dist.js"
|
|
8
7
|
export { default as mag } from "./mag.js"
|
|
9
8
|
export { default as mean } from "./mean.js"
|
|
9
|
+
export { default as median } from "./median.js"
|
|
10
10
|
export { default as sum } from "./sum.js"
|
|
11
11
|
export { default as lerpAngle } from "./lerp-angle.js"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compute the median of the values. The values are sorted and the middle value is returned.
|
|
3
|
+
* In case of an even number of values, the average of the two middle values is returned
|
|
4
|
+
*
|
|
5
|
+
* @param {number[]} values
|
|
6
|
+
* @returns {number}
|
|
7
|
+
*/
|
|
8
|
+
export default (...values) => {
|
|
9
|
+
const sorted = values.sort((a, b) => a - b)
|
|
10
|
+
const middle = Math.floor(sorted.length / 2)
|
|
11
|
+
|
|
12
|
+
if (sorted.length % 2 === 0) {
|
|
13
|
+
return (sorted[middle - 1] + sorted[middle]) / 2
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return sorted[middle]
|
|
17
|
+
}
|
package/src/math/roundd.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Calculates the integer closest to a number and optional precision.
|
|
3
|
-
*
|
|
4
|
-
* @param {number} n number to round.
|
|
5
|
-
* @param {number} [precision] number of decimal digits to round to, default is 0.
|
|
6
|
-
* @returns {number} rounded number.
|
|
7
|
-
*/
|
|
8
|
-
export default (n, precision = 0) => {
|
|
9
|
-
if (!precision) {
|
|
10
|
-
return Math.round(n)
|
|
11
|
-
}
|
|
12
|
-
const multiplier = Math.pow(10, precision)
|
|
13
|
-
return Math.round(n * multiplier) / multiplier
|
|
14
|
-
}
|
|
File without changes
|