@litecanvas/utils 0.45.0 → 0.46.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 +19 -12
- package/dist/all.min.js +2 -2
- package/dist/debug.js +6 -12
- package/dist/debug.min.js +1 -1
- package/dist/math.js +14 -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/dd.js +8 -3
- package/src/debug/index.js +0 -1
- package/src/math/index.js +1 -0
- package/src/math/wave.js +21 -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,
|
|
@@ -115,7 +114,8 @@
|
|
|
115
114
|
vecSet: () => vecSet,
|
|
116
115
|
vecSetMag: () => vecSetMag,
|
|
117
116
|
vecSub: () => vecSub,
|
|
118
|
-
vecToArray: () => vecToArray
|
|
117
|
+
vecToArray: () => vecToArray,
|
|
118
|
+
wave: () => wave_default
|
|
119
119
|
});
|
|
120
120
|
|
|
121
121
|
// src/camera/index.js
|
|
@@ -1024,6 +1024,18 @@
|
|
|
1024
1024
|
// src/math/percent.js
|
|
1025
1025
|
var percent_default = (value, min = 0, max = 1) => max - min ? (value - min) / (max - min) : 0;
|
|
1026
1026
|
|
|
1027
|
+
// src/math/wave.js
|
|
1028
|
+
var wave_default = (from, to, t, fn = Math.sin) => {
|
|
1029
|
+
DEV: assert_default(isNumber(from), "[litecanvas] wave() 1st param must be a number");
|
|
1030
|
+
DEV: assert_default(isNumber(to), "[litecanvas] wave() 2nd param must be a number");
|
|
1031
|
+
DEV: assert_default(isNumber(t), "[litecanvas] wave() 3rd param must be a number");
|
|
1032
|
+
DEV: assert_default(
|
|
1033
|
+
"function" === typeof fn,
|
|
1034
|
+
"[litecanvas] wave() 4rd param must be a function (n: number) => number"
|
|
1035
|
+
);
|
|
1036
|
+
return from + (fn(t) + 1) / 2 * (to - from);
|
|
1037
|
+
};
|
|
1038
|
+
|
|
1027
1039
|
// src/tween/index.js
|
|
1028
1040
|
var HALF_PI = Math.PI / 2;
|
|
1029
1041
|
var tween = (object, prop, toValue, duration = 1, easing = LINEAR) => {
|
|
@@ -1486,15 +1498,6 @@
|
|
|
1486
1498
|
return typeof value === type;
|
|
1487
1499
|
};
|
|
1488
1500
|
|
|
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
1501
|
// src/debug/dd.js
|
|
1499
1502
|
var dd_default = (data, context, engine = globalThis) => {
|
|
1500
1503
|
engine.pal(["blue", "#fff"]);
|
|
@@ -1503,7 +1506,11 @@
|
|
|
1503
1506
|
engine.textfont("monospace");
|
|
1504
1507
|
engine.textsize(16);
|
|
1505
1508
|
engine.textalign("start", "top");
|
|
1506
|
-
|
|
1509
|
+
engine.text(
|
|
1510
|
+
16,
|
|
1511
|
+
16,
|
|
1512
|
+
`${context ?? "dd() output"}: ` + (is_default(data, "object") ? JSON.stringify(data, null, 4) : data)
|
|
1513
|
+
);
|
|
1507
1514
|
engine.quit();
|
|
1508
1515
|
};
|
|
1509
1516
|
|
package/dist/all.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(()=>{var Ft=Object.defineProperty;var St=(e,t)=>{for(var s in t)Ft(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
|
|
2
|
-
`)}},
|
|
1
|
+
(()=>{var Ft=Object.defineProperty;var St=(e,t)=>{for(var s in t)Ft(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={};St(H,{ANCHOR_BOT_LEFT:()=>ae,ANCHOR_BOT_RIGHT:()=>he,ANCHOR_CENTER:()=>ie,ANCHOR_TOP_LEFT:()=>st,ANCHOR_TOP_RIGHT:()=>oe,Actor:()=>B,BACK_IN:()=>pe,BACK_IN_OUT:()=>xe,BACK_OUT:()=>fe,BOUNCE_IN:()=>_t,BOUNCE_IN_OUT:()=>ye,BOUNCE_OUT:()=>Y,Camera:()=>g,DOWN:()=>se,EASE_IN:()=>le,EASE_IN_OUT:()=>ue,EASE_OUT:()=>ce,ELASTIC_IN:()=>_e,ELASTIC_IN_OUT:()=>de,ELASTIC_OUT:()=>me,Grid:()=>E,LEFT:()=>re,LINEAR:()=>xt,Noise:()=>U,ONE:()=>vt,RIGHT:()=>ee,TypedGrid:()=>A,UP:()=>te,Vector:()=>b,ZERO:()=>et,advance:()=>ot,assert:()=>D,choose:()=>wt,colcirc:()=>j,colrect:()=>V,colrectcirc:()=>w,dd:()=>Lt,diff:()=>rt,dist:()=>ht,flipImage:()=>mt,formatTime:()=>Rt,fract:()=>it,head:()=>Et,intersection:()=>I,is:()=>N,last:()=>Pt,lerpAngle:()=>ut,lpad:()=>At,mag:()=>nt,makeCircle:()=>gt,makeRectangle:()=>bt,mean:()=>lt,median:()=>ct,mod:()=>at,percent:()=>pt,range:()=>Mt,resolverect:()=>X,rpad:()=>Nt,scaleImage:()=>dt,shuffle:()=>It,sum:()=>T,tail:()=>Tt,tintImage:()=>yt,tween:()=>ne,vec:()=>n,vecAbs:()=>Dt,vecAdd:()=>L,vecAngle:()=>Ut,vecAngleBetween:()=>Ht,vecCeil:()=>Vt,vecClamp:()=>zt,vecCross:()=>Wt,vecDist:()=>Yt,vecDist2:()=>Zt,vecDiv:()=>S,vecDot:()=>tt,vecEq:()=>O,vecFloor:()=>jt,vecHeading:()=>v,vecIsZero:()=>Gt,vecLerp:()=>Xt,vecLimit:()=>Ct,vecMag:()=>J,vecMag2:()=>Q,vecMove:()=>$t,vecMult:()=>P,vecNorm:()=>k,vecRand:()=>Qt,vecReflect:()=>Bt,vecRem:()=>Kt,vecRotate:()=>Ot,vecRound:()=>qt,vecSet:()=>$,vecSetMag:()=>G,vecSub:()=>F,vecToArray:()=>Jt,wave:()=>ft});var g=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),_=Math.min(e+s,i+a)-l,u=Math.max(t,o),m=Math.min(t+r,o+h)-u;return[l,u,_,m]};var X=(e,t,s,r,i,o,a,h)=>{let[l,_,u,m]=I(e,t,s,r,i,o,a,h),x="",y=e,c=t;return u<m?e<i?(x="right",y=i-s):(x="left",y=i+a):t<o?(x="bottom",c=o-r):(x="top",c=o+h),{dir:x,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
|
+
`)}},A=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,kt=2*Math.PI,b=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 b,n=(e=0,t=e)=>(d(e)&&(t=e.y,e=e.x),new b(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),Ot=(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},Bt=(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},Ct=(e,t=1)=>(Q(e)>t*t&&G(e,t),e),Yt=(e,t)=>Math.hypot(t.x-e.x,t.y-e.y),Zt=(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),Ut=e=>v(e),Ht=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),tt=(e,t)=>e.x*t.x+e.y*t.y,Wt=(e,t)=>e.x*t.y-e.y*t.x,Xt=(e,t,s)=>(e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0,e),Dt=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),Vt=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),jt=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),qt=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),zt=(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),Kt=(e,t)=>(e.x%=t,e.y%=t,e),$t=(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,Gt=e=>O(e,et),Jt=e=>[e.x,e.y],Qt=(e=1,t=e,s=globalThis.rand||Math.random)=>{let r=s()*kt,i=s()*(t-e)+e;return n(z(r)*i,K(r)*i)},et=n(0,0),vt=n(1,1),te=n(0,-1),ee=n(1,0),se=n(0,1),re=n(-1,0);var ie=n(.5,.5),st=n(0,0),oe=n(1,0),ae=n(0,1),he=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 ft=(e,t,s,r=Math.sin)=>e+(r(s)+1)/2*(t-e);var R=Math.PI/2,ne=(e,t,s,r=1,i=xt)=>new C(e,t,s,r,i),xt=e=>e,le=e=>e*e,ce=e=>-e*(e-2),ue=e=>e<.5?2*e*e:-2*e*e+4*e-1,pe=e=>e*e*e-e*Math.sin(e*Math.PI),fe=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*R*e)*Math.pow(2,10*(e-1)),me=e=>Math.sin(-13*R*(e+1))*Math.pow(2,-10*e)+1,de=e=>{if(e<.5){let r=Math.sin(13*R*(2*e)),i=Math.pow(2,10*(2*e-1));return .5*r*i}let t=Math.sin(-13*R*(2*e-1+1)),s=Math.pow(2,-10*(2*e-1));return .5*(t*s+2)},_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,ye=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,_=r-a,u,m,x=0,y=.5,c,p,M;for(let W=0;W<this._po;W++){let f=i+(o<<4)+(a<<8);u=Z(h),m=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+=m*(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*(M-p),c+=Z(_)*(p-c),x+=c*y,y*=this._pf,i<<=1,h*=2,o<<=1,l*=2,a<<=1,_*=2,h>=1&&(i++,h--),l>=1&&(o++,l--),_>=1&&(a++,_--)}return x}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 mt=(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 yt=(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 bt=(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 Mt=(e,t=0,s=1)=>[...Array(e|0).keys()].map(r=>t+s*r);var It=(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 wt=(e,t=globalThis.rand||Math.random)=>e[Math.floor(t()*e.length)];var Et=e=>e[0];var Pt=e=>e[e.length-1];var Tt=e=>e.slice(1);var Rt=e=>~~(e/60)+":"+(e%60<10?"0":"")+~~(e%60);var Nt=(e,t,s="0")=>(e+"").padEnd(t,s);var At=(e,t,s="0")=>(e+"").padStart(t,s);var N=(e,t)=>typeof t=="function"?e instanceof t:typeof e===t;var Lt=(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"}: `+(N(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
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
median: () => median_default,
|
|
30
30
|
mod: () => mod_default,
|
|
31
31
|
percent: () => percent_default,
|
|
32
|
-
sum: () => sum_default
|
|
32
|
+
sum: () => sum_default,
|
|
33
|
+
wave: () => wave_default
|
|
33
34
|
});
|
|
34
35
|
|
|
35
36
|
// src/math/diff.js
|
|
@@ -110,6 +111,18 @@
|
|
|
110
111
|
// src/math/percent.js
|
|
111
112
|
var percent_default = (value, min = 0, max = 1) => max - min ? (value - min) / (max - min) : 0;
|
|
112
113
|
|
|
114
|
+
// src/math/wave.js
|
|
115
|
+
var wave_default = (from, to, t, fn = Math.sin) => {
|
|
116
|
+
DEV: assert_default(isNumber(from), "[litecanvas] wave() 1st param must be a number");
|
|
117
|
+
DEV: assert_default(isNumber(to), "[litecanvas] wave() 2nd param must be a number");
|
|
118
|
+
DEV: assert_default(isNumber(t), "[litecanvas] wave() 3rd param must be a number");
|
|
119
|
+
DEV: assert_default(
|
|
120
|
+
"function" === typeof fn,
|
|
121
|
+
"[litecanvas] wave() 4rd param must be a function (n: number) => number"
|
|
122
|
+
);
|
|
123
|
+
return from + (fn(t) + 1) / 2 * (to - from);
|
|
124
|
+
};
|
|
125
|
+
|
|
113
126
|
// src/math/_web.js
|
|
114
127
|
globalThis.utils = Object.assign(globalThis.utils || {}, index_exports);
|
|
115
128
|
})();
|
package/dist/math.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var b=Object.defineProperty;var M=(r,t)=>{for(var e in t)b(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={};M(a,{advance:()=>u,diff:()=>n,dist:()=>x,fract:()=>c,lerpAngle:()=>y,mag:()=>l,mean:()=>f,median:()=>i,mod:()=>p,percent:()=>d,sum:()=>s,wave:()=>m});var n=(r,t)=>Math.abs(t-r)||0;var c=r=>r%1||0;var w=2*Math.PI;var u=(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 x=(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 i=r=>{let t=r.sort((o,h)=>o-h),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 d=(r,t=0,e=1)=>e-t?(r-t)/(e-t):0;var m=(r,t,e,o=Math.sin)=>r+(o(e)+1)/2*(t-r);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.46.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/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
package/src/math/wave.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import assert from "../debug/assert.js"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Interpolate between 2 values using a periodic function.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} from - the lower bound
|
|
7
|
+
* @param {number} to - the higher bound
|
|
8
|
+
* @param {number} t - value passed to the periodic function
|
|
9
|
+
* @param {(n: number) => number} [fn] - the periodic function (which default to `Math.sin`)
|
|
10
|
+
*/
|
|
11
|
+
export default (from, to, t, fn = Math.sin) => {
|
|
12
|
+
DEV: assert(isNumber(from), "[litecanvas] wave() 1st param must be a number")
|
|
13
|
+
DEV: assert(isNumber(to), "[litecanvas] wave() 2nd param must be a number")
|
|
14
|
+
DEV: assert(isNumber(t), "[litecanvas] wave() 3rd param must be a number")
|
|
15
|
+
DEV: assert(
|
|
16
|
+
"function" === typeof fn,
|
|
17
|
+
"[litecanvas] wave() 4rd param must be a function (n: number) => number"
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
return from + ((fn(t) + 1) / 2) * (to - from)
|
|
21
|
+
}
|
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
|
-
}
|