@litecanvas/utils 0.39.0 → 0.41.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/all.js +28 -4
- package/dist/all.min.js +2 -2
- package/dist/collection.js +1 -1
- package/dist/collection.min.js +1 -1
- package/dist/collision.js +1 -1
- package/dist/collision.min.js +1 -1
- package/dist/debug.js +52 -0
- package/dist/debug.min.js +1 -0
- package/dist/grid.js +1 -1
- package/dist/math.js +9 -1
- package/dist/math.min.js +1 -1
- package/package.json +2 -2
- package/src/collection/range.js +1 -1
- package/src/collision/resolverect.js +2 -2
- package/src/debug/_web.js +4 -0
- package/src/debug/index.js +3 -0
- package/src/debug/is.js +13 -0
- package/src/debug/log.js +15 -0
- package/src/grid/index.js +1 -1
- package/src/index.js +1 -0
- package/src/math/advance.js +2 -2
- package/src/math/mean.js +2 -0
- package/src/math/median.js +4 -1
- package/src/math/sum.js +5 -0
package/dist/all.js
CHANGED
|
@@ -50,6 +50,7 @@
|
|
|
50
50
|
Vector: () => Vector,
|
|
51
51
|
ZERO: () => ZERO,
|
|
52
52
|
advance: () => advance_default,
|
|
53
|
+
assert: () => assert_default,
|
|
53
54
|
choose: () => choose_default,
|
|
54
55
|
colcirc: () => colcirc_default,
|
|
55
56
|
colrect: () => colrect_default,
|
|
@@ -60,8 +61,10 @@
|
|
|
60
61
|
fract: () => fract_default,
|
|
61
62
|
head: () => head_default,
|
|
62
63
|
intersection: () => intersection_default,
|
|
64
|
+
is: () => is_default,
|
|
63
65
|
last: () => last_default,
|
|
64
66
|
lerpAngle: () => lerp_angle_default,
|
|
67
|
+
log: () => log_default,
|
|
65
68
|
mag: () => mag_default,
|
|
66
69
|
makeCircle: () => make_circle_default,
|
|
67
70
|
makeRectangle: () => make_rectangle_default,
|
|
@@ -320,7 +323,7 @@
|
|
|
320
323
|
resolveY = y2 + h2;
|
|
321
324
|
}
|
|
322
325
|
}
|
|
323
|
-
return { direction, x: resolveX, y: resolveY };
|
|
326
|
+
return { dir: direction, x: resolveX, y: resolveY };
|
|
324
327
|
};
|
|
325
328
|
|
|
326
329
|
// src/debug/assert.js
|
|
@@ -490,7 +493,7 @@
|
|
|
490
493
|
* @param {number} y
|
|
491
494
|
* @param {any} value
|
|
492
495
|
* @param {Grid} grid
|
|
493
|
-
* @returns {
|
|
496
|
+
* @returns {false|void} returns `false` to stop/break the loop
|
|
494
497
|
*
|
|
495
498
|
* @param {GridForEachCallback} callback
|
|
496
499
|
* @param {boolean} [reverse=false]
|
|
@@ -941,6 +944,7 @@
|
|
|
941
944
|
|
|
942
945
|
// src/math/sum.js
|
|
943
946
|
var sum_default = (values) => {
|
|
947
|
+
DEV: assert_default(Array.isArray(values), "1st param must be an array");
|
|
944
948
|
let result = 0;
|
|
945
949
|
for (let i = 0; i < values.length; i++) {
|
|
946
950
|
result += values[i];
|
|
@@ -950,11 +954,13 @@
|
|
|
950
954
|
|
|
951
955
|
// src/math/mean.js
|
|
952
956
|
var mean_default = (values) => {
|
|
957
|
+
DEV: assert_default(Array.isArray(values), "1st param must be an array");
|
|
953
958
|
return sum_default(values) / values.length;
|
|
954
959
|
};
|
|
955
960
|
|
|
956
961
|
// src/math/median.js
|
|
957
|
-
var median_default = (
|
|
962
|
+
var median_default = (values) => {
|
|
963
|
+
DEV: assert_default(Array.isArray(values), "1st param must be an array");
|
|
958
964
|
const sorted = values.sort((a, b) => a - b);
|
|
959
965
|
const middle = Math.floor(sorted.length / 2);
|
|
960
966
|
if (sorted.length % 2 === 0) {
|
|
@@ -1392,7 +1398,7 @@
|
|
|
1392
1398
|
};
|
|
1393
1399
|
|
|
1394
1400
|
// src/collection/range.js
|
|
1395
|
-
var range_default = (size, from = 0, step = 1) => [...
|
|
1401
|
+
var range_default = (size, from = 0, step = 1) => [...Array(size | 0).keys()].map((i) => {
|
|
1396
1402
|
return from + step * i;
|
|
1397
1403
|
});
|
|
1398
1404
|
|
|
@@ -1425,6 +1431,24 @@
|
|
|
1425
1431
|
// src/time/format-time.js
|
|
1426
1432
|
var format_time_default = (t) => ~~(t / 60) + ":" + (t % 60 < 10 ? "0" : "") + ~~(t % 60);
|
|
1427
1433
|
|
|
1434
|
+
// src/debug/is.js
|
|
1435
|
+
var is_default = (value, type) => {
|
|
1436
|
+
if (typeof type === "function") {
|
|
1437
|
+
return value instanceof type;
|
|
1438
|
+
}
|
|
1439
|
+
return typeof value === type;
|
|
1440
|
+
};
|
|
1441
|
+
|
|
1442
|
+
// src/debug/log.js
|
|
1443
|
+
var log_default = (data, color = 3, engine = globalThis) => {
|
|
1444
|
+
return engine.text(
|
|
1445
|
+
16,
|
|
1446
|
+
16,
|
|
1447
|
+
is_default(data, "object") ? JSON.stringify(data, null, 4) : data,
|
|
1448
|
+
color
|
|
1449
|
+
);
|
|
1450
|
+
};
|
|
1451
|
+
|
|
1428
1452
|
// src/_web.js
|
|
1429
1453
|
globalThis.utils = Object.assign(globalThis.utils || {}, index_exports);
|
|
1430
1454
|
})();
|
package/dist/all.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(()=>{var
|
|
2
|
-
`)}},
|
|
1
|
+
(()=>{var Tt=Object.defineProperty;var At=(s,t)=>{for(var e in t)Tt(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 U={};At(U,{ANCHOR_BOT_LEFT:()=>es,ANCHOR_BOT_RIGHT:()=>rs,ANCHOR_CENTER:()=>ts,ANCHOR_TOP_LEFT:()=>st,ANCHOR_TOP_RIGHT:()=>ss,Actor:()=>F,BACK_IN:()=>ns,BACK_IN_OUT:()=>cs,BACK_OUT:()=>ls,BOUNCE_IN:()=>ft,BOUNCE_IN_OUT:()=>_s,BOUNCE_OUT:()=>B,Camera:()=>g,DOWN:()=>Qt,EASE_IN:()=>os,EASE_IN_OUT:()=>as,EASE_OUT:()=>hs,ELASTIC_IN:()=>ps,ELASTIC_IN_OUT:()=>fs,ELASTIC_OUT:()=>us,Grid:()=>b,LEFT:()=>vt,LINEAR:()=>ut,Noise:()=>Z,ONE:()=>$t,RIGHT:()=>Jt,TypedGrid:()=>A,UP:()=>Gt,Vector:()=>I,ZERO:()=>tt,advance:()=>it,assert:()=>X,choose:()=>Et,colcirc:()=>V,colrect:()=>D,diff:()=>et,dist:()=>ht,flipImage:()=>_t,formatTime:()=>Pt,fract:()=>rt,head:()=>Mt,intersection:()=>M,is:()=>T,last:()=>bt,lerpAngle:()=>ct,log:()=>Rt,mag:()=>at,makeCircle:()=>dt,makeRectangle:()=>yt,mean:()=>nt,median:()=>lt,mod:()=>ot,percent:()=>pt,range:()=>gt,resolverect:()=>W,scaleImage:()=>xt,shuffle:()=>It,sum:()=>P,tail:()=>wt,tintImage:()=>mt,tween:()=>is,vec:()=>h,vecAbs:()=>Ut,vecAdd:()=>N,vecAngle:()=>Ct,vecAngleBetween:()=>Bt,vecCeil:()=>Ht,vecClamp:()=>Dt,vecCross:()=>Yt,vecDist:()=>Ot,vecDist2:()=>Ft,vecDiv:()=>S,vecDot:()=>v,vecEq:()=>O,vecFloor:()=>Wt,vecHeading:()=>Q,vecIsZero:()=>qt,vecLerp:()=>Zt,vecLimit:()=>kt,vecMag:()=>G,vecMag2:()=>J,vecMove:()=>jt,vecMult:()=>w,vecNorm:()=>k,vecRand:()=>Kt,vecReflect:()=>St,vecRem:()=>Vt,vecRotate:()=>Lt,vecRound:()=>Xt,vecSet:()=>K,vecSetMag:()=>$,vecSub:()=>L,vecToArray:()=>zt});var g=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;constructor(t=null,e=0,r=0,i=null,o=null){this._engine=t||globalThis,this.ox=e,this.oy=r,this.resize(i||this._engine.W-e,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,e){this.width=t,this.height=e,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 e=this.ox+this.width/2,r=this.oy+this.height/2;this._engine.translate(e,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,e){this.x=t,this.y=e}move(t,e){this.x+=t,this.y+=e}zoom(t){this.scale*=t}zoomTo(t){this.scale=t}rotate(t){this.rotation+=t}rotateTo(t){this.rotation=t}getWorldPoint(t,e,r={}){let i=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=(t-this.width/2-this.ox)/this.scale,e=(e-this.height/2-this.oy)/this.scale,r.x=i*t-o*e+this.x,r.y=o*t+i*e+this.y,r}getCameraPoint(t,e,r={}){let i=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=t-this.x,e=e-this.y,t=i*t-o*e,e=o*t+i*e,r.x=t*this.scale+this.width/2+this.ox,r.y=e*this.scale+this.height/2+this.oy,r}getBounds(){return[this.ox,this.oy,this.width,this.height]}shake(t=1,e=.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),e-=r,e<=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=(s,t,e,r,i,o,a,n)=>{let l=Math.max(s,i),x=Math.min(s+e,i+a)-l,p=Math.max(t,o),m=Math.min(t+r,o+n)-p;return[l,p,x,m]};var W=(s,t,e,r,i,o,a,n)=>{let[l,x,p,m]=M(s,t,e,r,i,o,a,n),_="",y=s,c=t;return p<m?s<i?(_="right",y=i-e):(_="left",y=i+a):t<o?(_="bottom",c=o-r):(_="top",c=o+n),{dir:_,x:y,y:c}};var X=(s,t="Assertion failed")=>{if(!s)throw new Error(t)};var D=(s,t,e,r,i,o,a,n)=>s<i+a&&s+e>i&&t<o+n&&t+r>o;var V=(s,t,e,r,i,o)=>(r-s)*(r-s)+(i-t)*(i-t)<=(e+o)*(e+o);var b=class s{_w;_h;_c;constructor(t,e,r=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~e),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 s(this._w,this._h,this._c)}clear(){this.forEach((t,e)=>this.set(t,e,void 0))}get width(){return this._w}get height(){return this._h}set(t,e,r){this._c[this.pointToIndex(t,e)]=r}get(t,e){return this._c[this.pointToIndex(t,e)]}has(t,e){return this.get(t,e)!=null}check(t,e){return t>=0&&t<this._w&&e>=0&&e<this._h}get length(){return this._w*this._h}pointToIndex(t,e){return this.clampX(~~t)+this.clampY(~~e)*this._w}indexToPointX(t){return t%this._w}indexToPointY(t){return Math.floor(t/this._w)}forEach(t,e=!1){let r=e?this.length-1:0,i=e?-1:this.length,o=e?-1:1;for(;r!==i;){let a=this.indexToPointX(r),n=this.indexToPointY(r),l=this._c[r];if(t(a,n,l,this)===!1)break;r+=o}}fill(t){this.forEach((e,r)=>{this.set(e,r,t)})}clampX(t){return j(t,0,this._w-1)}clampY(t){return j(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",e=!0){if(!e)return this._c.join(t);let r=[];return this.forEach((i,o,a)=>{r[o]=r[o]||"",r[o]+=a+t}),r.join(`
|
|
2
|
+
`)}},A=class s extends b{constructor(t,e,r=Uint8Array){super(t,e,null),this._c=new r(this._w*this._h)}has(t,e){return this.get(t,e)!==0}clone(){let t=new s(this._w,this._h,this._c.constructor);return this.forEach((e,r,i)=>{t.set(e,r,i)}),t}};function j(s,t,e){return s<t?t:s>e?e:s}var q=Math.cos,z=Math.sin,Nt=2*Math.PI,I=class{constructor(t=0,e=t){this.x=parseFloat(t)||0,this.y=parseFloat(e)||0}toString(){return`Vector (${this.x}, ${this.y})`}},d=s=>s instanceof I,h=(s=0,t=s)=>(d(s)&&(t=s.y,s=s.x),new I(s,t)),K=(s,t,e=t)=>(d(t)?K(s,t.x,t.y):(s.x=t,s.y=e),s),N=(s,t,e=t)=>d(t)?N(s,t.x,t.y):(s.x+=t,s.y+=e,s),L=(s,t,e=t)=>d(t)?L(s,t.x,t.y):(s.x-=t,s.y-=e,s),w=(s,t,e=t)=>d(t)?w(s,t.x,t.y):(s.x*=t,s.y*=e,s),S=(s,t,e=t)=>d(t)?S(s,t.x,t.y):(s.x/=t||1,s.y/=e||1,s),Lt=(s,t)=>{let e=q(t),r=z(t);return s.x=e*s.x-r*s.y,s.y=r*s.x+e*s.y,s},St=(s,t)=>{let e=k(h(t));return L(s,w(e,2*v(s,e)))},$=(s,t)=>(k(s),w(s,t),s),G=s=>Math.hypot(s.x,s.y),J=s=>s.x*s.x+s.y*s.y,k=s=>{let t=G(s);return t>0&&S(s,t),s},kt=(s,t=1)=>(J(s)>t*t&&$(s,t),s),Ot=(s,t)=>Math.hypot(t.x-s.x,t.y-s.y),Ft=(s,t)=>{let e=s.x-t.x,r=s.y-t.y;return e*e+r*r},Q=s=>Math.atan2(s.y,s.x),Ct=s=>Q(s),Bt=(s,t)=>Math.atan2(t.y-s.y,t.x-s.x),v=(s,t)=>s.x*t.x+s.y*t.y,Yt=(s,t)=>s.x*t.y-s.y*t.x,Zt=(s,t,e)=>(s.x+=(t.x-s.x)*e||0,s.y+=(t.y-s.y)*e||0,s),Ut=s=>(s.x=Math.abs(s.x),s.y=Math.abs(s.y),s),Ht=s=>(s.x=Math.ceil(s.x),s.y=Math.ceil(s.y),s),Wt=s=>(s.x=Math.floor(s.x),s.y=Math.floor(s.y),s),Xt=s=>(s.x=Math.round(s.x),s.y=Math.round(s.y),s),Dt=(s,t,e)=>(s.x<t.x&&(s.x=t.x),s.x>e.x&&(s.x=e.x),s.y<t.y&&(s.y=t.y),s.y>e.y&&(s.y=e.y),s),Vt=(s,t)=>(s.x%=t,s.y%=t,s),jt=(s,t,e=1)=>N(s,t.x*e,t.y*e),O=(s,t,e=t)=>d(t)?O(s,t.x,t.y):s.x===t&&s.y===e,qt=s=>O(s,tt),zt=s=>[s.x,s.y],Kt=(s=1,t=s,e=globalThis.rand||Math.random)=>{let r=e()*Nt,i=e()*(t-s)+s;return h(q(r)*i,z(r)*i)},tt=h(0,0),$t=h(1,1),Gt=h(0,-1),Jt=h(1,0),Qt=h(0,1),vt=h(-1,0);var ts=h(.5,.5),st=h(0,0),ss=h(1,0),es=h(0,1),rs=h(1,1),F=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,e,r=st){this.sprite=t||{width:0,height:0},this.pos=e||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,e=t){this._s.x=t,this._s.y=e}scaleBy(t,e=t){this._s.x*=t,this._s.y*=e}getBounds(t=!0){let e=this.sprite.width*(t?this._s.x:1),r=this.sprite.height*(t?this._s.y:1),i=this.pos.x-e*this.anchor.x,o=this.pos.y-r*this.anchor.y;return[i,o,e,r]}draw(t=globalThis,e=!0){e&&t.push(),this.transform(t),this.sprite.width&&this.sprite.height&&!this.hidden&&this.opacity>0&&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,i=-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(i,o,this.sprite)}};var et=(s,t)=>Math.abs(t-s)||0;var rt=s=>s%1||0;var it=(s,t,e,r=1)=>{e&&(t.x+=e.x*r,t.y+=e.y*r),s.x+=t.x*r,s.y+=t.y*r};var ot=(s,t)=>(t+s%t)%t;var ht=(s,t,e,r)=>Math.hypot(e-s,r-t);var at=(s,t)=>Math.hypot(s,t);var P=s=>{let t=0;for(let e=0;e<s.length;e++)t+=s[e];return t};var nt=s=>P(s)/s.length;var lt=s=>{let t=s.sort((r,i)=>r-i),e=Math.floor(t.length/2);return t.length%2===0?(t[e-1]+t[e])/2:t[e]};var ct=(s,t,e)=>{let r=(t-s)%360;return r>180?r-=360:r<-180&&(r+=360),s+r*e};var pt=(s,t=0,e=1)=>e-t?(s-t)/(e-t):0;var R=Math.PI/2,is=(s,t,e,r=1,i=ut)=>new C(s,t,e,r,i),ut=s=>s,os=s=>s*s,hs=s=>-s*(s-2),as=s=>s<.5?2*s*s:-2*s*s+4*s-1,ns=s=>s*s*s-s*Math.sin(s*Math.PI),ls=s=>{let t=1-s;return 1-(t*t*t-t*Math.sin(t*Math.PI))},cs=s=>{if(s<.5){let e=2*s;return .5*(e*e*e-e*Math.sin(e*Math.PI))}let t=1-(2*s-1);return .5*(1-(t*t*t-t*Math.sin(s*Math.PI)))+.5},ps=s=>Math.sin(13*R*s)*Math.pow(2,10*(s-1)),us=s=>Math.sin(-13*R*(s+1))*Math.pow(2,-10*s)+1,fs=s=>{if(s<.5){let r=Math.sin(13*R*(2*s)),i=Math.pow(2,10*(2*s-1));return .5*r*i}let t=Math.sin(-13*R*(2*s-1+1)),e=Math.pow(2,-10*(2*s-1));return .5*(t*e+2)},ft=s=>1-B(1-s),B=s=>s<4/11?121*s*s/16:s<8/11?363/40*s*s-99/10*s+17/5:s<9/10?4356/361*s*s-35442/1805*s+16061/1805:54/5*s*s-513/25*s+268/25,_s=s=>s<.5?.5*ft(s*2):.5*B(s*2-1)+.5,C=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(t,e,r,i,o){this._o=t,this._p=e,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 e=this._o[this._p]||0,r=this._rel?e+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(e,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 e of this._cb)e(this._o);return this}restart(t=null,e=!1){return this.stop(e).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 Y=s=>.5*(1-Math.cos(s*Math.PI)),Z=class{_p=[];_po=4;_pf=.5;_e=null;constructor(t){this._e=t||globalThis,this.noiseSeed()}noise(t,e=0,r=0){t<0&&(t=-t),e<0&&(e=-e),r<0&&(r=-r);let i=Math.floor(t),o=Math.floor(e),a=Math.floor(r),n=t-i,l=e-o,x=r-a,p,m,_=0,y=.5,c,u,E;for(let H=0;H<this._po;H++){let f=i+(o<<4)+(a<<8);p=Y(n),m=Y(l),c=this._p[f&4095],c+=p*(this._p[f+1&4095]-c),u=this._p[f+16&4095],u+=p*(this._p[f+16+1&4095]-u),c+=m*(u-c),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+=m*(E-u),c+=Y(x)*(u-c),_+=c*y,y*=this._pf,i<<=1,n*=2,o<<=1,l*=2,a<<=1,x*=2,n>=1&&(i++,n--),l>=1&&(o++,l--),x>=1&&(a++,x--)}return _}noiseDetail(t,e){t>0&&(this._po=t),e>0&&(this._pf=e)}noiseSeed(t=null){t!=null&&this._e.rseed(t);let e=this._e.rand||Math.random;for(let r=0;r<4096;r++)this._p[r]=e()}};var _t=(s,t=!0,e=!1,r=globalThis)=>r.paint(s.width,s.height,i=>{r.push(),r.scale(t?-1:1,e?-1:1),r.image(t?-s.width:0,e?-s.height:0,s),r.pop()});var xt=(s,t,e=!0,r=globalThis)=>r.paint(s.width*t,s.height*t,i=>{r.push(),i.imageSmoothingEnabled=!e,r.scale(t),r.image(0,0,s),r.pop()});var mt=(s,t,e=1,r=globalThis)=>r.paint(s.width,s.height,i=>{r.push(),r.alpha(e),r.rectfill(0,0,s.width,s.height,t),i.globalCompositeOperation="destination-atop",r.alpha(1),r.image(0,0,s),r.pop()});var dt=(s,t,{borderWidth:e=0,borderColor:r=0,engine:i=globalThis}={})=>{let o=s*2+e;return i.paint(o,o,()=>{i.circfill(o/2,o/2,s,t),e>0&&(i.linewidth(e),i.stroke(r))})};var yt=(s,t,e,{borderWidth:r=0,borderColor:i=0,engine:o=globalThis}={})=>{let a=s+r*2,n=t+r*2;return o.paint(a,n,()=>{let l=r>0;l&&o.cls(i),o.rectfill(l?r:0,l?r:0,s,t,e)})};var gt=(s,t=0,e=1)=>[...Array(s|0).keys()].map(r=>t+e*r);var It=(s,t=globalThis.rand||Math.random)=>{s=[...s];for(let e=s.length-1;e>0;e--){let r=Math.floor(t()*(e+1)),i=s[e];s[e]=s[r],s[r]=i}return s};var Et=(s,t=globalThis.rand||Math.random)=>s[Math.floor(t()*s.length)];var Mt=s=>s[0];var bt=s=>s[s.length-1];var wt=s=>s.slice(1);var Pt=s=>~~(s/60)+":"+(s%60<10?"0":"")+~~(s%60);var T=(s,t)=>typeof t=="function"?s instanceof t:typeof s===t;var Rt=(s,t=3,e=globalThis)=>e.text(16,16,T(s,"object")?JSON.stringify(s,null,4):s,t);globalThis.utils=Object.assign(globalThis.utils||{},U);})();
|
|
3
3
|
/*! @litecanvas/utils by Luiz Bills | MIT Licensed */
|
package/dist/collection.js
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
// src/collection/range.js
|
|
31
|
-
var range_default = (size, from = 0, step = 1) => [...
|
|
31
|
+
var range_default = (size, from = 0, step = 1) => [...Array(size | 0).keys()].map((i) => {
|
|
32
32
|
return from + step * i;
|
|
33
33
|
});
|
|
34
34
|
|
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 a={};u(a,{choose:()=>f,head:()=>s,last:()=>h,range:()=>e,shuffle:()=>i,tail:()=>d});var e=(t,l=0,o=1)=>[...Array(t|0).keys()].map(r=>l+o*r);var i=(t,l=globalThis.rand||Math.random)=>{t=[...t];for(let o=t.length-1;o>0;o--){let r=Math.floor(l()*(o+1)),n=t[o];t[o]=t[r],t[r]=n}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 d=t=>t.slice(1);globalThis.utils=Object.assign(globalThis.utils||{},a);})();
|
package/dist/collision.js
CHANGED
package/dist/collision.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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),{
|
|
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),{dir: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/debug.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __export = (target, all) => {
|
|
4
|
+
for (var name in all)
|
|
5
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// src/_global.js
|
|
9
|
+
globalThis.utils = globalThis.utils || {};
|
|
10
|
+
globalThis.utils.global = (overrides = true) => {
|
|
11
|
+
for (const key in globalThis.utils) {
|
|
12
|
+
if ("global" === key) continue;
|
|
13
|
+
if (overrides || globalThis[key] === void 0) {
|
|
14
|
+
globalThis[key] = globalThis.utils[key];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// src/debug/index.js
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
assert: () => assert_default,
|
|
23
|
+
is: () => is_default,
|
|
24
|
+
log: () => log_default
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// src/debug/assert.js
|
|
28
|
+
var assert_default = (condition, message = "Assertion failed") => {
|
|
29
|
+
if (!condition) throw new Error(message);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// src/debug/is.js
|
|
33
|
+
var is_default = (value, type) => {
|
|
34
|
+
if (typeof type === "function") {
|
|
35
|
+
return value instanceof type;
|
|
36
|
+
}
|
|
37
|
+
return typeof value === type;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/debug/log.js
|
|
41
|
+
var log_default = (data, color = 3, engine = globalThis) => {
|
|
42
|
+
return engine.text(
|
|
43
|
+
16,
|
|
44
|
+
16,
|
|
45
|
+
is_default(data, "object") ? JSON.stringify(data, null, 4) : data,
|
|
46
|
+
color
|
|
47
|
+
);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/debug/_web.js
|
|
51
|
+
globalThis.utils = Object.assign(globalThis.utils || {}, index_exports);
|
|
52
|
+
})();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(()=>{var f=Object.defineProperty;var a=(o,l)=>{for(var i in l)f(o,i,{get:l[i],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(o=!0)=>{for(let l in globalThis.utils)l!=="global"&&(o||globalThis[l]===void 0)&&(globalThis[l]=globalThis.utils[l])};var s={};a(s,{assert:()=>r,is:()=>t,log:()=>e});var r=(o,l="Assertion failed")=>{if(!o)throw new Error(l)};var t=(o,l)=>typeof l=="function"?o instanceof l:typeof o===l;var e=(o,l=3,i=globalThis)=>i.text(16,16,t(o,"object")?JSON.stringify(o,null,4):o,l);globalThis.utils=Object.assign(globalThis.utils||{},s);})();
|
package/dist/grid.js
CHANGED
|
@@ -159,7 +159,7 @@
|
|
|
159
159
|
* @param {number} y
|
|
160
160
|
* @param {any} value
|
|
161
161
|
* @param {Grid} grid
|
|
162
|
-
* @returns {
|
|
162
|
+
* @returns {false|void} returns `false` to stop/break the loop
|
|
163
163
|
*
|
|
164
164
|
* @param {GridForEachCallback} callback
|
|
165
165
|
* @param {boolean} [reverse=false]
|
package/dist/math.js
CHANGED
|
@@ -64,8 +64,14 @@
|
|
|
64
64
|
return Math.hypot(x, y);
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
+
// src/debug/assert.js
|
|
68
|
+
var assert_default = (condition, message = "Assertion failed") => {
|
|
69
|
+
if (!condition) throw new Error(message);
|
|
70
|
+
};
|
|
71
|
+
|
|
67
72
|
// src/math/sum.js
|
|
68
73
|
var sum_default = (values) => {
|
|
74
|
+
DEV: assert_default(Array.isArray(values), "1st param must be an array");
|
|
69
75
|
let result = 0;
|
|
70
76
|
for (let i = 0; i < values.length; i++) {
|
|
71
77
|
result += values[i];
|
|
@@ -75,11 +81,13 @@
|
|
|
75
81
|
|
|
76
82
|
// src/math/mean.js
|
|
77
83
|
var mean_default = (values) => {
|
|
84
|
+
DEV: assert_default(Array.isArray(values), "1st param must be an array");
|
|
78
85
|
return sum_default(values) / values.length;
|
|
79
86
|
};
|
|
80
87
|
|
|
81
88
|
// src/math/median.js
|
|
82
|
-
var median_default = (
|
|
89
|
+
var median_default = (values) => {
|
|
90
|
+
DEV: assert_default(Array.isArray(values), "1st param must be an array");
|
|
83
91
|
const sorted = values.sort((a, b) => a - b);
|
|
84
92
|
const middle = Math.floor(sorted.length / 2);
|
|
85
93
|
if (sorted.length % 2 === 0) {
|
package/dist/math.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var m=Object.defineProperty;var g=(r,t)=>{for(var e in t)m(r,e,{get:t[e],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(r=!0)=>{for(let t in globalThis.utils)t!=="global"&&(r||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var n={};g(n,{advance:()=>x,diff:()=>a,dist:()=>u,fract:()=>c,lerpAngle:()=>i,mag:()=>l,mean:()=>f,median:()=>y,mod:()=>p,percent:()=>h,sum:()=>s});var a=(r,t)=>Math.abs(t-r)||0;var c=r=>r%1||0;var T=2*Math.PI;var x=(r,t,e,o=1)=>{e&&(t.x+=e.x*o,t.y+=e.y*o),r.x+=t.x*o,r.y+=t.y*o};var p=(r,t)=>(t+r%t)%t;var u=(r,t,e,o)=>Math.hypot(e-r,o-t);var l=(r,t)=>Math.hypot(r,t);var s=r=>{let t=0;for(let e=0;e<r.length;e++)t+=r[e];return t};var f=r=>s(r)/r.length;var y=r=>{let t=r.sort((o,d)=>o-d),e=Math.floor(t.length/2);return t.length%2===0?(t[e-1]+t[e])/2:t[e]};var i=(r,t,e)=>{let o=(t-r)%360;return o>180?o-=360:o<-180&&(o+=360),r+o*e};var h=(r,t=0,e=1)=>e-t?(r-t)/(e-t):0;globalThis.utils=Object.assign(globalThis.utils||{},n);})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@litecanvas/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.41.0",
|
|
4
4
|
"description": "Utilities to help build litecanvas games",
|
|
5
5
|
"author": "Luiz Bills <luizbills@pm.me>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"litecanvas": "latest"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"esbuild": "^0.25.
|
|
40
|
+
"esbuild": "^0.25.10",
|
|
41
41
|
"gzip-size": "^7.0.0",
|
|
42
42
|
"prettier": "^3.6.2"
|
|
43
43
|
}
|
package/src/collection/range.js
CHANGED
|
@@ -12,7 +12,7 @@ import intersection from "./intersection.js"
|
|
|
12
12
|
* @param {number} y2
|
|
13
13
|
* @param {number} w2
|
|
14
14
|
* @param {number} h2
|
|
15
|
-
* @returns {{
|
|
15
|
+
* @returns {{dir: string, x: number, y: number}} An object containing the direction of the collision and the new position (X and Y) of the first rectangle.
|
|
16
16
|
*/
|
|
17
17
|
export default (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
18
18
|
// get the intersection area
|
|
@@ -49,5 +49,5 @@ export default (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
return { direction, x: resolveX, y: resolveY }
|
|
52
|
+
return { dir: direction, x: resolveX, y: resolveY }
|
|
53
53
|
}
|
package/src/debug/is.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check the type of a value.
|
|
3
|
+
*
|
|
4
|
+
* @param {unknown} value
|
|
5
|
+
* @param {string|function} type
|
|
6
|
+
* @returns {boolean}
|
|
7
|
+
*/
|
|
8
|
+
export default (value, type) => {
|
|
9
|
+
if (typeof type === "function") {
|
|
10
|
+
return value instanceof type
|
|
11
|
+
}
|
|
12
|
+
return typeof value === type
|
|
13
|
+
}
|
package/src/debug/log.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import is from "./is.js"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {any} data
|
|
5
|
+
* @param {number} [color]
|
|
6
|
+
* @param {LitecanvasInstance} [engine]
|
|
7
|
+
*/
|
|
8
|
+
export default (data, color = 3, engine = globalThis) => {
|
|
9
|
+
return engine.text(
|
|
10
|
+
16,
|
|
11
|
+
16,
|
|
12
|
+
is(data, "object") ? JSON.stringify(data, null, 4) : data,
|
|
13
|
+
color
|
|
14
|
+
)
|
|
15
|
+
}
|
package/src/grid/index.js
CHANGED
|
@@ -152,7 +152,7 @@ export class Grid {
|
|
|
152
152
|
* @param {number} y
|
|
153
153
|
* @param {any} value
|
|
154
154
|
* @param {Grid} grid
|
|
155
|
-
* @returns {
|
|
155
|
+
* @returns {false|void} returns `false` to stop/break the loop
|
|
156
156
|
*
|
|
157
157
|
* @param {GridForEachCallback} callback
|
|
158
158
|
* @param {boolean} [reverse=false]
|
package/src/index.js
CHANGED
package/src/math/advance.js
CHANGED
|
@@ -6,8 +6,8 @@ import { Vector } from "../vector/index.js"
|
|
|
6
6
|
*
|
|
7
7
|
* @param {Vector} position
|
|
8
8
|
* @param {Vector} velocity
|
|
9
|
-
* @param {Vector
|
|
10
|
-
* @param {number
|
|
9
|
+
* @param {Vector} [acceleration]
|
|
10
|
+
* @param {number} [deltaTime]
|
|
11
11
|
*/
|
|
12
12
|
export default (position, velocity, acceleration, deltaTime = 1) => {
|
|
13
13
|
if (acceleration) {
|
package/src/math/mean.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import assert from "../debug/assert.js"
|
|
1
2
|
import sum from "./sum.js"
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -7,5 +8,6 @@ import sum from "./sum.js"
|
|
|
7
8
|
* @returns {number}
|
|
8
9
|
*/
|
|
9
10
|
export default (values) => {
|
|
11
|
+
DEV: assert(Array.isArray(values), "1st param must be an array")
|
|
10
12
|
return sum(values) / values.length
|
|
11
13
|
}
|
package/src/math/median.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import assert from "../debug/assert"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Compute the median of the values. The values are sorted and the middle value is returned.
|
|
3
5
|
* In case of an even number of values, the average of the two middle values is returned
|
|
@@ -5,7 +7,8 @@
|
|
|
5
7
|
* @param {number[]} values
|
|
6
8
|
* @returns {number}
|
|
7
9
|
*/
|
|
8
|
-
export default (
|
|
10
|
+
export default (values) => {
|
|
11
|
+
DEV: assert(Array.isArray(values), "1st param must be an array")
|
|
9
12
|
const sorted = values.sort((a, b) => a - b)
|
|
10
13
|
const middle = Math.floor(sorted.length / 2)
|
|
11
14
|
|
package/src/math/sum.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
+
import assert from "../debug/assert"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Computes the sum of the values in a array.
|
|
3
5
|
*
|
|
6
|
+
* usage: `sum([2,3,5]) // returns 10`
|
|
7
|
+
*
|
|
4
8
|
* @param {number[]} values list of numbers
|
|
5
9
|
* @returns {number}
|
|
6
10
|
*/
|
|
7
11
|
export default (values) => {
|
|
12
|
+
DEV: assert(Array.isArray(values), "1st param must be an array")
|
|
8
13
|
let result = 0
|
|
9
14
|
for (let i = 0; i < values.length; i++) {
|
|
10
15
|
result += values[i]
|