@litecanvas/utils 0.45.0 → 0.47.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 +4 -0
- package/dist/all.js +51 -20
- package/dist/all.min.js +2 -2
- package/dist/debug.js +6 -12
- package/dist/debug.min.js +1 -1
- package/dist/math.js +46 -1
- package/dist/math.min.js +1 -1
- package/package.json +2 -2
- package/src/actor/README.md +23 -2
- package/src/collision/README.md +6 -0
- package/src/debug/assert.js +4 -1
- package/src/debug/dd.js +8 -3
- package/src/debug/index.js +0 -1
- package/src/math/index.js +2 -0
- package/src/math/move.js +27 -0
- package/src/math/wave.js +28 -0
- package/src/debug/log.js +0 -15
package/README.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
Small collection of tools for developing games with [Litecanvas](https://github.com/litecanvas/game-engine).
|
|
4
4
|
|
|
5
|
+
<!-- prettier-ignore -->
|
|
6
|
+
> [!TIP]
|
|
7
|
+
> **All modules of this package are automatically loaded on Litecanvas [playground](https://litecanvas.js.org/).**
|
|
8
|
+
|
|
5
9
|
## Install
|
|
6
10
|
|
|
7
11
|
### NPM package
|
package/dist/all.js
CHANGED
|
@@ -66,7 +66,6 @@
|
|
|
66
66
|
is: () => is_default,
|
|
67
67
|
last: () => last_default,
|
|
68
68
|
lerpAngle: () => lerp_angle_default,
|
|
69
|
-
log: () => log_default,
|
|
70
69
|
lpad: () => lpad_default,
|
|
71
70
|
mag: () => mag_default,
|
|
72
71
|
makeCircle: () => make_circle_default,
|
|
@@ -74,6 +73,7 @@
|
|
|
74
73
|
mean: () => mean_default,
|
|
75
74
|
median: () => median_default,
|
|
76
75
|
mod: () => mod_default,
|
|
76
|
+
move: () => move_default,
|
|
77
77
|
percent: () => percent_default,
|
|
78
78
|
range: () => range_default,
|
|
79
79
|
resolverect: () => resolverect_default,
|
|
@@ -115,7 +115,8 @@
|
|
|
115
115
|
vecSet: () => vecSet,
|
|
116
116
|
vecSetMag: () => vecSetMag,
|
|
117
117
|
vecSub: () => vecSub,
|
|
118
|
-
vecToArray: () => vecToArray
|
|
118
|
+
vecToArray: () => vecToArray,
|
|
119
|
+
wave: () => wave_default
|
|
119
120
|
});
|
|
120
121
|
|
|
121
122
|
// src/camera/index.js
|
|
@@ -1024,6 +1025,49 @@
|
|
|
1024
1025
|
// src/math/percent.js
|
|
1025
1026
|
var percent_default = (value, min = 0, max = 1) => max - min ? (value - min) / (max - min) : 0;
|
|
1026
1027
|
|
|
1028
|
+
// src/debug/is.js
|
|
1029
|
+
var is_default = (value, type) => {
|
|
1030
|
+
if (typeof type === "function") {
|
|
1031
|
+
return value instanceof type;
|
|
1032
|
+
}
|
|
1033
|
+
return typeof value === type;
|
|
1034
|
+
};
|
|
1035
|
+
|
|
1036
|
+
// src/math/wave.js
|
|
1037
|
+
var wave_default = (from, to, t, fn = Math.sin) => {
|
|
1038
|
+
DEV: assert_default(
|
|
1039
|
+
is_default(from, "number"),
|
|
1040
|
+
"[litecanvas] wave() 1st param must be a number"
|
|
1041
|
+
);
|
|
1042
|
+
DEV: assert_default(
|
|
1043
|
+
is_default(to, "number"),
|
|
1044
|
+
"[litecanvas] wave() 2nd param must be a number"
|
|
1045
|
+
);
|
|
1046
|
+
DEV: assert_default(is_default(t, "number"), "[litecanvas] wave() 3rd param must be a number");
|
|
1047
|
+
DEV: assert_default(
|
|
1048
|
+
is_default(fn, "function"),
|
|
1049
|
+
"[litecanvas] wave() 4rd param must be a function (n: number) => number"
|
|
1050
|
+
);
|
|
1051
|
+
return from + (fn(t) + 1) / 2 * (to - from);
|
|
1052
|
+
};
|
|
1053
|
+
|
|
1054
|
+
// src/math/move.js
|
|
1055
|
+
var move_default = (from, to, step) => {
|
|
1056
|
+
DEV: assert_default(
|
|
1057
|
+
is_default(from, "number"),
|
|
1058
|
+
"[litecanvas] move() 1st param must be a number"
|
|
1059
|
+
);
|
|
1060
|
+
DEV: assert_default(
|
|
1061
|
+
is_default(to, "number"),
|
|
1062
|
+
"[litecanvas] move() 2nd param must be a number"
|
|
1063
|
+
);
|
|
1064
|
+
DEV: assert_default(
|
|
1065
|
+
is_default(step, "number"),
|
|
1066
|
+
"[litecanvas] move() 3rd param must be a number"
|
|
1067
|
+
);
|
|
1068
|
+
return Math.abs(to - from) <= step ? to : from + Math.sign(to - from) * step;
|
|
1069
|
+
};
|
|
1070
|
+
|
|
1027
1071
|
// src/tween/index.js
|
|
1028
1072
|
var HALF_PI = Math.PI / 2;
|
|
1029
1073
|
var tween = (object, prop, toValue, duration = 1, easing = LINEAR) => {
|
|
@@ -1478,23 +1522,6 @@
|
|
|
1478
1522
|
// src/string/lpad.js
|
|
1479
1523
|
var lpad_default = (str, targetLength, padString = "0") => (str + "").padStart(targetLength, padString);
|
|
1480
1524
|
|
|
1481
|
-
// src/debug/is.js
|
|
1482
|
-
var is_default = (value, type) => {
|
|
1483
|
-
if (typeof type === "function") {
|
|
1484
|
-
return value instanceof type;
|
|
1485
|
-
}
|
|
1486
|
-
return typeof value === type;
|
|
1487
|
-
};
|
|
1488
|
-
|
|
1489
|
-
// src/debug/log.js
|
|
1490
|
-
var log_default = (data, context, engine = globalThis) => {
|
|
1491
|
-
return engine.text(
|
|
1492
|
-
16,
|
|
1493
|
-
16,
|
|
1494
|
-
(context ? `${context}: ` : "") + (is_default(data, "object") ? JSON.stringify(data, null, 4) : data)
|
|
1495
|
-
);
|
|
1496
|
-
};
|
|
1497
|
-
|
|
1498
1525
|
// src/debug/dd.js
|
|
1499
1526
|
var dd_default = (data, context, engine = globalThis) => {
|
|
1500
1527
|
engine.pal(["blue", "#fff"]);
|
|
@@ -1503,7 +1530,11 @@
|
|
|
1503
1530
|
engine.textfont("monospace");
|
|
1504
1531
|
engine.textsize(16);
|
|
1505
1532
|
engine.textalign("start", "top");
|
|
1506
|
-
|
|
1533
|
+
engine.text(
|
|
1534
|
+
16,
|
|
1535
|
+
16,
|
|
1536
|
+
`${context ?? "dd() output"}: ` + (is_default(data, "object") ? JSON.stringify(data, null, 4) : data)
|
|
1537
|
+
);
|
|
1507
1538
|
engine.quit();
|
|
1508
1539
|
};
|
|
1509
1540
|
|
package/dist/all.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(()=>{var
|
|
2
|
-
`)}},
|
|
1
|
+
(()=>{var St=Object.defineProperty;var kt=(e,t)=>{for(var s in t)St(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 H={};kt(H,{ANCHOR_BOT_LEFT:()=>he,ANCHOR_BOT_RIGHT:()=>ne,ANCHOR_CENTER:()=>oe,ANCHOR_TOP_LEFT:()=>st,ANCHOR_TOP_RIGHT:()=>ae,Actor:()=>B,BACK_IN:()=>fe,BACK_IN_OUT:()=>xe,BACK_OUT:()=>me,BOUNCE_IN:()=>_t,BOUNCE_IN_OUT:()=>be,BOUNCE_OUT:()=>Y,Camera:()=>b,DOWN:()=>re,EASE_IN:()=>ce,EASE_IN_OUT:()=>pe,EASE_OUT:()=>ue,ELASTIC_IN:()=>_e,ELASTIC_IN_OUT:()=>ye,ELASTIC_OUT:()=>de,Grid:()=>E,LEFT:()=>ie,LINEAR:()=>xt,Noise:()=>U,ONE:()=>te,RIGHT:()=>se,TypedGrid:()=>N,UP:()=>ee,Vector:()=>g,ZERO:()=>et,advance:()=>ot,assert:()=>D,choose:()=>Et,colcirc:()=>j,colrect:()=>V,colrectcirc:()=>w,dd:()=>Ft,diff:()=>rt,dist:()=>ht,flipImage:()=>dt,formatTime:()=>At,fract:()=>it,head:()=>Pt,intersection:()=>I,is:()=>R,last:()=>Tt,lerpAngle:()=>ut,lpad:()=>Lt,mag:()=>nt,makeCircle:()=>gt,makeRectangle:()=>Mt,mean:()=>lt,median:()=>ct,mod:()=>at,move:()=>mt,percent:()=>pt,range:()=>It,resolverect:()=>X,rpad:()=>Nt,scaleImage:()=>yt,shuffle:()=>wt,sum:()=>T,tail:()=>Rt,tintImage:()=>bt,tween:()=>le,vec:()=>n,vecAbs:()=>Vt,vecAdd:()=>L,vecAngle:()=>Ht,vecAngleBetween:()=>Wt,vecCeil:()=>jt,vecClamp:()=>Kt,vecCross:()=>Xt,vecDist:()=>Zt,vecDist2:()=>Ut,vecDiv:()=>S,vecDot:()=>tt,vecEq:()=>O,vecFloor:()=>qt,vecHeading:()=>v,vecIsZero:()=>Jt,vecLerp:()=>Dt,vecLimit:()=>Yt,vecMag:()=>J,vecMag2:()=>Q,vecMove:()=>Gt,vecMult:()=>P,vecNorm:()=>k,vecRand:()=>vt,vecReflect:()=>Ct,vecRem:()=>$t,vecRotate:()=>Bt,vecRound:()=>zt,vecSet:()=>$,vecSetMag:()=>G,vecSub:()=>F,vecToArray:()=>Qt,wave:()=>ft});var b=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;constructor(t=null,s=0,r=0,i=null,o=null){this._engine=t||globalThis,this.ox=s,this.oy=r,this.resize(i||this._engine.W-s,o||this._engine.H-r),this.x=this.width/2,this.y=this.height/2,this._shake={x:0,y:0,removeListener:null}}resize(t,s){this.width=t,this.height=s,this._engine.emit("camera-resized",this)}start(t=!1){this._engine.push(),t&&this._engine.clip(i=>{i.rect(this.ox,this.oy,this.width,this.height)});let s=this.ox+this.width/2,r=this.oy+this.height/2;this._engine.translate(s,r),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y)}end(){this._engine.pop()}lookAt(t,s){this.x=t,this.y=s}move(t,s){this.x+=t,this.y+=s}zoom(t){this.scale*=t}zoomTo(t){this.scale=t}rotate(t){this.rotation+=t}rotateTo(t){this.rotation=t}getWorldPoint(t,s,r={}){let i=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=(t-this.width/2-this.ox)/this.scale,s=(s-this.height/2-this.oy)/this.scale,r.x=i*t-o*s+this.x,r.y=o*t+i*s+this.y,r}getCameraPoint(t,s,r={}){let i=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=t-this.x,s=s-this.y,t=i*t-o*s,s=o*t+i*s,r.x=t*this.scale+this.width/2+this.ox,r.y=s*this.scale+this.height/2+this.oy,r}getBounds(){return[this.ox,this.oy,this.width,this.height]}shake(t=1,s=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",r=>{this._shake.x=this._engine.randi(-t,t),this._shake.y=this._engine.randi(-t,t),s-=r,s<=0&&this.unshake()}))}unshake(){this.shaking&&(this._shake.removeListener(),this._shake.removeListener=null,this._shake.x=this._shake.y=0)}get shaking(){return this._shake.removeListener!==null}};var I=(e,t,s,r,i,o,a,h)=>{let l=Math.max(e,i),x=Math.min(e+s,i+a)-l,u=Math.max(t,o),_=Math.min(t+r,o+h)-u;return[l,u,x,_]};var X=(e,t,s,r,i,o,a,h)=>{let[l,x,u,_]=I(e,t,s,r,i,o,a,h),m="",y=e,c=t;return u<_?e<i?(m="right",y=i-s):(m="left",y=i+a):t<o?(m="bottom",c=o-r):(m="top",c=o+h),{dir:m,x:y,y:c}};var D=(e,t="Assertion failed")=>{if(!e)throw new Error(t)};var V=(e,t,s,r,i,o,a,h)=>e<i+a&&e+s>i&&t<o+h&&t+r>o;var j=(e,t,s,r,i,o)=>(r-e)*(r-e)+(i-t)*(i-t)<=(s+o)*(s+o);var w=(e,t,s,r,i,o,a)=>{let h=i-Math.max(e,Math.min(i,e+s)),l=o-Math.max(t,Math.min(o,t+r));return h*h+l*l<=a*a};var E=class e{_w;_h;_c;constructor(t,s,r=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~s),this._c=r}[Symbol.iterator](){let t=0;return{next:()=>({value:[this.indexToPointX(t),this.indexToPointY(t),this._c[t++]],done:t>this._c.length})}}clone(){return new e(this._w,this._h,this._c)}clear(){this.forEach((t,s)=>this.set(t,s,void 0))}get width(){return this._w}get height(){return this._h}set(t,s,r){this._c[this.pointToIndex(t,s)]=r}get(t,s){return this._c[this.pointToIndex(t,s)]}has(t,s){return this.get(t,s)!=null}check(t,s){return t>=0&&t<this._w&&s>=0&&s<this._h}get length(){return this._w*this._h}pointToIndex(t,s){return this.clampX(~~t)+this.clampY(~~s)*this._w}indexToPointX(t){return t%this._w}indexToPointY(t){return Math.floor(t/this._w)}forEach(t,s=!1){let r=s?this.length-1:0,i=s?-1:this.length,o=s?-1:1;for(;r!==i;){let a=this.indexToPointX(r),h=this.indexToPointY(r),l=this._c[r];if(t(a,h,l,this)===!1)break;r+=o}}fill(t){this.forEach((s,r)=>{this.set(s,r,t)})}clampX(t){return q(t,0,this._w-1)}clampY(t){return q(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",s=!0){if(!s)return this._c.join(t);let r=[];return this.forEach((i,o,a)=>{r[o]=r[o]||"",r[o]+=a+t}),r.join(`
|
|
2
|
+
`)}},N=class e extends E{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 q(e,t,s){return e<t?t:e>s?s:e}var z=Math.cos,K=Math.sin,Ot=2*Math.PI,g=class{constructor(t=0,s=t){this.x=parseFloat(t)||0,this.y=parseFloat(s)||0}toString(){return`Vector (${this.x}, ${this.y})`}},d=e=>e instanceof g,n=(e=0,t=e)=>(d(e)&&(t=e.y,e=e.x),new g(e,t)),$=(e,t,s=t)=>(d(t)?$(e,t.x,t.y):(e.x=t,e.y=s),e),L=(e,t,s=t)=>d(t)?L(e,t.x,t.y):(e.x+=t,e.y+=s,e),F=(e,t,s=t)=>d(t)?F(e,t.x,t.y):(e.x-=t,e.y-=s,e),P=(e,t,s=t)=>d(t)?P(e,t.x,t.y):(e.x*=t,e.y*=s,e),S=(e,t,s=t)=>d(t)?S(e,t.x,t.y):(e.x/=t||1,e.y/=s||1,e),Bt=(e,t)=>{let s=z(t),r=K(t);return e.x=s*e.x-r*e.y,e.y=r*e.x+s*e.y,e},Ct=(e,t)=>{let s=k(n(t));return F(e,P(s,2*tt(e,s)))},G=(e,t)=>(k(e),P(e,t),e),J=e=>Math.hypot(e.x,e.y),Q=e=>e.x*e.x+e.y*e.y,k=e=>{let t=J(e);return t>0&&S(e,t),e},Yt=(e,t=1)=>(Q(e)>t*t&&G(e,t),e),Zt=(e,t)=>Math.hypot(t.x-e.x,t.y-e.y),Ut=(e,t)=>{let s=e.x-t.x,r=e.y-t.y;return s*s+r*r},v=e=>Math.atan2(e.y,e.x),Ht=e=>v(e),Wt=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),tt=(e,t)=>e.x*t.x+e.y*t.y,Xt=(e,t)=>e.x*t.y-e.y*t.x,Dt=(e,t,s)=>(e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0,e),Vt=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),jt=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),qt=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),zt=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),Kt=(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),$t=(e,t)=>(e.x%=t,e.y%=t,e),Gt=(e,t,s=1)=>L(e,t.x*s,t.y*s),O=(e,t,s=t)=>d(t)?O(e,t.x,t.y):e.x===t&&e.y===s,Jt=e=>O(e,et),Qt=e=>[e.x,e.y],vt=(e=1,t=e,s=globalThis.rand||Math.random)=>{let r=s()*Ot,i=s()*(t-e)+e;return n(z(r)*i,K(r)*i)},et=n(0,0),te=n(1,1),ee=n(0,-1),se=n(1,0),re=n(0,1),ie=n(-1,0);var oe=n(.5,.5),st=n(0,0),ae=n(1,0),he=n(0,1),ne=n(1,1),B=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,r=st){this.sprite=t||{width:0,height:0},this.pos=s||n(0),this._o=n(r),this._s=n(1,1)}set x(t){this.pos.x=t}get x(){return this.pos.x}set y(t){this.pos.y=t}get y(){return this.pos.y}set anchor(t){this._o.x=t.x,this._o.y=t.y}get anchor(){return this._o}get width(){return this.sprite.width*this._s.x}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}scaleTo(t,s=t){this._s.x=t,this._s.y=s}scaleBy(t,s=t){this._s.x*=t,this._s.y*=s}getBounds(t=!0){let s=this.sprite.width*(t?this._s.x:1),r=this.sprite.height*(t?this._s.y:1),i=this.pos.x-s*this.anchor.x,o=this.pos.y-r*this.anchor.y;return[i,o,s,r]}in(t,s,r=1){return w(...this.getBounds(),t,s,r)}col(t){return colrect(...this.getBounds(),...t.getBounds())}draw(t=globalThis,s=!0){s&&t.push(),this.transform(t),this.sprite.width&&this.sprite.height&&!this.hidden&&this.opacity>0&&this.drawImage(t),s&&t.pop()}transform(t=globalThis){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=globalThis,s=!0){let r=this.anchor,i=-this.sprite.width*(this.flipX?1-r.x:r.x),o=-this.sprite.height*(this.flipY?1-r.y:r.y);s&&t.alpha(this.opacity),t.image(i,o,this.sprite)}};var rt=(e,t)=>Math.abs(t-e)||0;var it=e=>e%1||0;var ot=(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 at=(e,t)=>(t+e%t)%t;var ht=(e,t,s,r)=>Math.hypot(s-e,r-t);var nt=(e,t)=>Math.hypot(e,t);var T=e=>{let t=0;for(let s=0;s<e.length;s++)t+=e[s];return t};var lt=e=>T(e)/e.length;var ct=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 ut=(e,t,s)=>{let r=(t-e)%360;return r>180?r-=360:r<-180&&(r+=360),e+r*s};var pt=(e,t=0,s=1)=>s-t?(e-t)/(s-t):0;var R=(e,t)=>typeof t=="function"?e instanceof t:typeof e===t;var ft=(e,t,s,r=Math.sin)=>e+(r(s)+1)/2*(t-e);var mt=(e,t,s)=>Math.abs(t-e)<=s?t:e+Math.sign(t-e)*s;var A=Math.PI/2,le=(e,t,s,r=1,i=xt)=>new C(e,t,s,r,i),xt=e=>e,ce=e=>e*e,ue=e=>-e*(e-2),pe=e=>e<.5?2*e*e:-2*e*e+4*e-1,fe=e=>e*e*e-e*Math.sin(e*Math.PI),me=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},xe=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},_e=e=>Math.sin(13*A*e)*Math.pow(2,10*(e-1)),de=e=>Math.sin(-13*A*(e+1))*Math.pow(2,-10*e)+1,ye=e=>{if(e<.5){let r=Math.sin(13*A*(2*e)),i=Math.pow(2,10*(2*e-1));return .5*r*i}let t=Math.sin(-13*A*(2*e-1+1)),s=Math.pow(2,-10*(2*e-1));return .5*(t*s+2)},_t=e=>1-Y(1-e),Y=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,be=e=>e<.5?.5*_t(e*2):.5*Y(e*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,s,r,i,o){this._o=t,this._p=s,this._x=r,this._d=i,this._e=o,this._w=0}start(t){if(this.running)return this;this._cu.stop(!1),this._ch=this._cu=this,this.running=!0;let s=this._o[this._p]||0,r=this._rel?s+this._x:this._x;return this._lc=this._lc||t||globalThis,this._u=this._lc.listen("update",i=>{if(this._t<=this._w){this._t+=i;return}let o=this._t-this._w;this._o[this._p]=this._lc.lerp(s,r,this._e(o/this._d)),this._t+=i,o>=this._d&&(this._o[this._p]=r,this.stop())}),this}stop(t=!0){if(!this._u)return this;if(this.running=!1,this._u(),this._t=0,t)for(let s of this._cb)s(this._o);return this}restart(t=null,s=!1){return this.stop(s).restart(t)}onEnd(t){return this._cb.push(t),this}chain(t){return this._ch.onEnd(()=>{this._cu=t.start(this._lc)}),this._ch=t,this}reset(){return this._cb.length=0,this.stop()}relative(t=!0){return this._rel=t,this}delay(t){return this._w=t,this}get current(){return this._cu}get progress(){return this.running&&this._t>this._w?(this._t-this._w)/this._d:0}};var Z=e=>.5*(1-Math.cos(e*Math.PI)),U=class{_p=[];_po=4;_pf=.5;_e=null;constructor(t){this._e=t||globalThis,this.noiseSeed()}noise(t,s=0,r=0){t<0&&(t=-t),s<0&&(s=-s),r<0&&(r=-r);let i=Math.floor(t),o=Math.floor(s),a=Math.floor(r),h=t-i,l=s-o,x=r-a,u,_,m=0,y=.5,c,p,M;for(let W=0;W<this._po;W++){let f=i+(o<<4)+(a<<8);u=Z(h),_=Z(l),c=this._p[f&4095],c+=u*(this._p[f+1&4095]-c),p=this._p[f+16&4095],p+=u*(this._p[f+16+1&4095]-p),c+=_*(p-c),f+=256,p=this._p[f&4095],p+=u*(this._p[f+1&4095]-p),M=this._p[f+16&4095],M+=u*(this._p[f+16+1&4095]-M),p+=_*(M-p),c+=Z(x)*(p-c),m+=c*y,y*=this._pf,i<<=1,h*=2,o<<=1,l*=2,a<<=1,x*=2,h>=1&&(i++,h--),l>=1&&(o++,l--),x>=1&&(a++,x--)}return m}noiseDetail(t,s){t>0&&(this._po=t),s>0&&(this._pf=s)}noiseSeed(t=null){t!=null&&this._e.rseed(t);let s=this._e.rand||Math.random;for(let r=0;r<4096;r++)this._p[r]=s()}};var dt=(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 yt=(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 gt=(e,t,{borderWidth:s=0,borderColor:r=0,engine:i=globalThis}={})=>{let o=e*2+s;return i.paint(o,o,()=>{i.circfill(o/2,o/2,e,t),s>0&&(i.linewidth(s),i.stroke(r))})};var Mt=(e,t,s,{borderWidth:r=0,borderColor:i=0,engine:o=globalThis}={})=>{let a=e+r*2,h=t+r*2;return o.paint(a,h,()=>{let l=r>0;l&&o.cls(i),o.rectfill(l?r:0,l?r:0,e,t,s)})};var It=(e,t=0,s=1)=>[...Array(e|0).keys()].map(r=>t+s*r);var wt=(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 Et=(e,t=globalThis.rand||Math.random)=>e[Math.floor(t()*e.length)];var Pt=e=>e[0];var Tt=e=>e[e.length-1];var Rt=e=>e.slice(1);var At=e=>~~(e/60)+":"+(e%60<10?"0":"")+~~(e%60);var Nt=(e,t,s="0")=>(e+"").padEnd(t,s);var Lt=(e,t,s="0")=>(e+"").padStart(t,s);var Ft=(e,t,s=globalThis)=>{s.pal(["blue","#fff"]),s.cls(0),s.ctx().resetTransform(),s.textfont("monospace"),s.textsize(16),s.textalign("start","top"),s.text(16,16,`${t??"dd() output"}: `+(R(e,"object")?JSON.stringify(e,null,4):e)),s.quit()};globalThis.utils=Object.assign(globalThis.utils||{},H);})();
|
|
3
3
|
/*! @litecanvas/utils by Luiz Bills | MIT Licensed */
|
package/dist/debug.js
CHANGED
|
@@ -21,8 +21,7 @@
|
|
|
21
21
|
__export(index_exports, {
|
|
22
22
|
assert: () => assert_default,
|
|
23
23
|
dd: () => dd_default,
|
|
24
|
-
is: () => is_default
|
|
25
|
-
log: () => log_default
|
|
24
|
+
is: () => is_default
|
|
26
25
|
});
|
|
27
26
|
|
|
28
27
|
// src/debug/assert.js
|
|
@@ -38,15 +37,6 @@
|
|
|
38
37
|
return typeof value === type;
|
|
39
38
|
};
|
|
40
39
|
|
|
41
|
-
// src/debug/log.js
|
|
42
|
-
var log_default = (data, context, engine = globalThis) => {
|
|
43
|
-
return engine.text(
|
|
44
|
-
16,
|
|
45
|
-
16,
|
|
46
|
-
(context ? `${context}: ` : "") + (is_default(data, "object") ? JSON.stringify(data, null, 4) : data)
|
|
47
|
-
);
|
|
48
|
-
};
|
|
49
|
-
|
|
50
40
|
// src/debug/dd.js
|
|
51
41
|
var dd_default = (data, context, engine = globalThis) => {
|
|
52
42
|
engine.pal(["blue", "#fff"]);
|
|
@@ -55,7 +45,11 @@
|
|
|
55
45
|
engine.textfont("monospace");
|
|
56
46
|
engine.textsize(16);
|
|
57
47
|
engine.textalign("start", "top");
|
|
58
|
-
|
|
48
|
+
engine.text(
|
|
49
|
+
16,
|
|
50
|
+
16,
|
|
51
|
+
`${context ?? "dd() output"}: ` + (is_default(data, "object") ? JSON.stringify(data, null, 4) : data)
|
|
52
|
+
);
|
|
59
53
|
engine.quit();
|
|
60
54
|
};
|
|
61
55
|
|
package/dist/debug.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var a=Object.defineProperty;var u=(l,t)=>{for(var o in t)a(l,o,{get:t[o],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(l=!0)=>{for(let t in globalThis.utils)t!=="global"&&(l||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var i={};u(i,{assert:()=>r,dd:()=>f,is:()=>s});var r=(l,t="Assertion failed")=>{if(!l)throw new Error(t)};var s=(l,t)=>typeof t=="function"?l instanceof t:typeof l===t;var f=(l,t,o=globalThis)=>{o.pal(["blue","#fff"]),o.cls(0),o.ctx().resetTransform(),o.textfont("monospace"),o.textsize(16),o.textalign("start","top"),o.text(16,16,`${t??"dd() output"}: `+(s(l,"object")?JSON.stringify(l,null,4):l)),o.quit()};globalThis.utils=Object.assign(globalThis.utils||{},i);})();
|
package/dist/math.js
CHANGED
|
@@ -28,8 +28,10 @@
|
|
|
28
28
|
mean: () => mean_default,
|
|
29
29
|
median: () => median_default,
|
|
30
30
|
mod: () => mod_default,
|
|
31
|
+
move: () => move_default,
|
|
31
32
|
percent: () => percent_default,
|
|
32
|
-
sum: () => sum_default
|
|
33
|
+
sum: () => sum_default,
|
|
34
|
+
wave: () => wave_default
|
|
33
35
|
});
|
|
34
36
|
|
|
35
37
|
// src/math/diff.js
|
|
@@ -110,6 +112,49 @@
|
|
|
110
112
|
// src/math/percent.js
|
|
111
113
|
var percent_default = (value, min = 0, max = 1) => max - min ? (value - min) / (max - min) : 0;
|
|
112
114
|
|
|
115
|
+
// src/debug/is.js
|
|
116
|
+
var is_default = (value, type) => {
|
|
117
|
+
if (typeof type === "function") {
|
|
118
|
+
return value instanceof type;
|
|
119
|
+
}
|
|
120
|
+
return typeof value === type;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// src/math/wave.js
|
|
124
|
+
var wave_default = (from, to, t, fn = Math.sin) => {
|
|
125
|
+
DEV: assert_default(
|
|
126
|
+
is_default(from, "number"),
|
|
127
|
+
"[litecanvas] wave() 1st param must be a number"
|
|
128
|
+
);
|
|
129
|
+
DEV: assert_default(
|
|
130
|
+
is_default(to, "number"),
|
|
131
|
+
"[litecanvas] wave() 2nd param must be a number"
|
|
132
|
+
);
|
|
133
|
+
DEV: assert_default(is_default(t, "number"), "[litecanvas] wave() 3rd param must be a number");
|
|
134
|
+
DEV: assert_default(
|
|
135
|
+
is_default(fn, "function"),
|
|
136
|
+
"[litecanvas] wave() 4rd param must be a function (n: number) => number"
|
|
137
|
+
);
|
|
138
|
+
return from + (fn(t) + 1) / 2 * (to - from);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// src/math/move.js
|
|
142
|
+
var move_default = (from, to, step) => {
|
|
143
|
+
DEV: assert_default(
|
|
144
|
+
is_default(from, "number"),
|
|
145
|
+
"[litecanvas] move() 1st param must be a number"
|
|
146
|
+
);
|
|
147
|
+
DEV: assert_default(
|
|
148
|
+
is_default(to, "number"),
|
|
149
|
+
"[litecanvas] move() 2nd param must be a number"
|
|
150
|
+
);
|
|
151
|
+
DEV: assert_default(
|
|
152
|
+
is_default(step, "number"),
|
|
153
|
+
"[litecanvas] move() 3rd param must be a number"
|
|
154
|
+
);
|
|
155
|
+
return Math.abs(to - from) <= step ? to : from + Math.sign(to - from) * step;
|
|
156
|
+
};
|
|
157
|
+
|
|
113
158
|
// src/math/_web.js
|
|
114
159
|
globalThis.utils = Object.assign(globalThis.utils || {}, index_exports);
|
|
115
160
|
})();
|
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 a={};g(a,{advance:()=>c,diff:()=>n,dist:()=>f,fract:()=>u,lerpAngle:()=>y,mag:()=>x,mean:()=>i,median:()=>l,mod:()=>p,move:()=>h,percent:()=>m,sum:()=>s,wave:()=>d});var n=(r,t)=>Math.abs(t-r)||0;var u=r=>r%1||0;var R=2*Math.PI;var c=(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 f=(r,t,e,o)=>Math.hypot(e-r,o-t);var x=(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 i=r=>s(r)/r.length;var l=r=>{let t=r.sort((o,b)=>o-b),e=Math.floor(t.length/2);return t.length%2===0?(t[e-1]+t[e])/2:t[e]};var y=(r,t,e)=>{let o=(t-r)%360;return o>180?o-=360:o<-180&&(o+=360),r+o*e};var m=(r,t=0,e=1)=>e-t?(r-t)/(e-t):0;var d=(r,t,e,o=Math.sin)=>r+(o(e)+1)/2*(t-r);var h=(r,t,e)=>Math.abs(t-r)<=e?t:r+Math.sign(t-r)*e;globalThis.utils=Object.assign(globalThis.utils||{},a);})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@litecanvas/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.47.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.27.
|
|
40
|
+
"esbuild": "^0.27.3",
|
|
41
41
|
"gzip-size": "^7.0.0",
|
|
42
42
|
"prettier": "^3.8.1"
|
|
43
43
|
}
|
package/src/actor/README.md
CHANGED
|
@@ -171,6 +171,27 @@ Set or get the actor sprite image. Useful to make animations.
|
|
|
171
171
|
player.sprite = anotherSprite
|
|
172
172
|
```
|
|
173
173
|
|
|
174
|
-
> The actor sprite must be a `Image`, `
|
|
174
|
+
> The actor sprite must be a `Image`, `ImageBitmap`, `HTMLCanvasElement` or `OffscreenCanvas`.
|
|
175
175
|
>
|
|
176
|
-
> Remember, you can create
|
|
176
|
+
> Remember, you can create images using the litecanvas' built-in `paint()` function or [loading a image](https://github.com/litecanvas/plugin-asset-loader?tab=readme-ov-file#loading-images).
|
|
177
|
+
|
|
178
|
+
## Actor#in(x, y, radius)
|
|
179
|
+
|
|
180
|
+
Check taps (clicks and touchs).
|
|
181
|
+
|
|
182
|
+
```js
|
|
183
|
+
function tapped(x, y) {
|
|
184
|
+
const pointArea = 8 // default = 1
|
|
185
|
+
if (player.in(x, y, pointArea)) {
|
|
186
|
+
console.log("tap!")
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Actor#col(other)
|
|
192
|
+
|
|
193
|
+
Check collision between actors
|
|
194
|
+
|
|
195
|
+
```js
|
|
196
|
+
const collided = player.col(anotherActor)
|
|
197
|
+
```
|
package/src/collision/README.md
CHANGED
|
@@ -14,6 +14,12 @@ Check a collision between two circles.
|
|
|
14
14
|
|
|
15
15
|
Syntax: `colcirc(x1, y1, r1, x2, y2, r2): boolean`
|
|
16
16
|
|
|
17
|
+
## colrectcirc
|
|
18
|
+
|
|
19
|
+
Check a collision between a rectangle and a circle.
|
|
20
|
+
|
|
21
|
+
Syntax: `colrectcirc(x1, y1, w1, h1, x2, y2, r2): boolean`
|
|
22
|
+
|
|
17
23
|
## intersection
|
|
18
24
|
|
|
19
25
|
Returns the resulting rectangle of the intersection between two rectangles.
|
package/src/debug/assert.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Throws an error if the condition is not true.
|
|
3
|
+
*
|
|
2
4
|
* @param {any} condition
|
|
3
|
-
* @param {string} message
|
|
5
|
+
* @param {string} [message="Assertion failed"]
|
|
6
|
+
* @throws {Error}
|
|
4
7
|
*/
|
|
5
8
|
export default (condition, message = "Assertion failed") => {
|
|
6
9
|
if (!condition) throw new Error(message)
|
package/src/debug/dd.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import is from "./is.js"
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Dump and die: print something and stops the engine.
|
|
5
5
|
*
|
|
6
6
|
* @param {any} data
|
|
7
7
|
* @param {string} context
|
|
@@ -14,6 +14,11 @@ export default (data, context, engine = globalThis) => {
|
|
|
14
14
|
engine.textfont("monospace")
|
|
15
15
|
engine.textsize(16)
|
|
16
16
|
engine.textalign("start", "top")
|
|
17
|
-
|
|
17
|
+
engine.text(
|
|
18
|
+
16,
|
|
19
|
+
16,
|
|
20
|
+
`${context ?? "dd() output"}: ` +
|
|
21
|
+
(is(data, "object") ? JSON.stringify(data, null, 4) : data)
|
|
22
|
+
)
|
|
18
23
|
engine.quit()
|
|
19
24
|
}
|
package/src/debug/index.js
CHANGED
package/src/math/index.js
CHANGED
|
@@ -9,3 +9,5 @@ export { default as median } from "./median.js"
|
|
|
9
9
|
export { default as sum } from "./sum.js"
|
|
10
10
|
export { default as lerpAngle } from "./lerp-angle.js"
|
|
11
11
|
export { default as percent } from "./percent.js"
|
|
12
|
+
export { default as wave } from "./wave.js"
|
|
13
|
+
export { default as move } from "./move.js"
|
package/src/math/move.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import assert from "../debug/assert.js"
|
|
2
|
+
import is from "../debug/is.js"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Moves a value toward a target using fixed increments, without overshooting.
|
|
6
|
+
*
|
|
7
|
+
* @param {number} from The current value.
|
|
8
|
+
* @param {number} to The target value.
|
|
9
|
+
* @param {number} step The size of the increment.
|
|
10
|
+
* @returns {number} The updated value.
|
|
11
|
+
*/
|
|
12
|
+
export default (from, to, step) => {
|
|
13
|
+
DEV: assert(
|
|
14
|
+
is(from, "number"),
|
|
15
|
+
"[litecanvas] move() 1st param must be a number"
|
|
16
|
+
)
|
|
17
|
+
DEV: assert(
|
|
18
|
+
is(to, "number"),
|
|
19
|
+
"[litecanvas] move() 2nd param must be a number"
|
|
20
|
+
)
|
|
21
|
+
DEV: assert(
|
|
22
|
+
is(step, "number"),
|
|
23
|
+
"[litecanvas] move() 3rd param must be a number"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
return Math.abs(to - from) <= step ? to : from + Math.sign(to - from) * step
|
|
27
|
+
}
|
package/src/math/wave.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import assert from "../debug/assert.js"
|
|
2
|
+
import is from "../debug/is.js"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Interpolate between 2 values using a periodic function.
|
|
6
|
+
*
|
|
7
|
+
* @param {number} from - the lower bound
|
|
8
|
+
* @param {number} to - the higher bound
|
|
9
|
+
* @param {number} t - value passed to the periodic function
|
|
10
|
+
* @param {(n: number) => number} [fn] - the periodic function (which default to `Math.sin`)
|
|
11
|
+
*/
|
|
12
|
+
export default (from, to, t, fn = Math.sin) => {
|
|
13
|
+
DEV: assert(
|
|
14
|
+
is(from, "number"),
|
|
15
|
+
"[litecanvas] wave() 1st param must be a number"
|
|
16
|
+
)
|
|
17
|
+
DEV: assert(
|
|
18
|
+
is(to, "number"),
|
|
19
|
+
"[litecanvas] wave() 2nd param must be a number"
|
|
20
|
+
)
|
|
21
|
+
DEV: assert(is(t, "number"), "[litecanvas] wave() 3rd param must be a number")
|
|
22
|
+
DEV: assert(
|
|
23
|
+
is(fn, "function"),
|
|
24
|
+
"[litecanvas] wave() 4rd param must be a function (n: number) => number"
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
return from + ((fn(t) + 1) / 2) * (to - from)
|
|
28
|
+
}
|
package/src/debug/log.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import is from "./is.js"
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @param {any} data
|
|
5
|
-
* @param {string} [context]
|
|
6
|
-
* @param {LitecanvasInstance} [engine]
|
|
7
|
-
*/
|
|
8
|
-
export default (data, context, engine = globalThis) => {
|
|
9
|
-
return engine.text(
|
|
10
|
-
16,
|
|
11
|
-
16,
|
|
12
|
-
(context ? `${context}: ` : "") +
|
|
13
|
-
(is(data, "object") ? JSON.stringify(data, null, 4) : data)
|
|
14
|
-
)
|
|
15
|
-
}
|