@litecanvas/utils 0.46.0 → 0.47.1
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 +36 -12
- package/dist/all.min.js +2 -2
- package/dist/math.js +36 -4
- package/dist/math.min.js +1 -1
- package/package.json +4 -1
- package/src/debug/assert.js +4 -1
- package/src/math/index.js +1 -0
- package/src/math/move.js +27 -0
- package/src/math/wave.js +11 -4
package/dist/all.js
CHANGED
|
@@ -73,6 +73,7 @@
|
|
|
73
73
|
mean: () => mean_default,
|
|
74
74
|
median: () => median_default,
|
|
75
75
|
mod: () => mod_default,
|
|
76
|
+
move: () => move_default,
|
|
76
77
|
percent: () => percent_default,
|
|
77
78
|
range: () => range_default,
|
|
78
79
|
resolverect: () => resolverect_default,
|
|
@@ -1024,18 +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
|
+
|
|
1027
1036
|
// src/math/wave.js
|
|
1028
1037
|
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
1038
|
DEV: assert_default(
|
|
1033
|
-
"
|
|
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"),
|
|
1034
1049
|
"[litecanvas] wave() 4rd param must be a function (n: number) => number"
|
|
1035
1050
|
);
|
|
1036
1051
|
return from + (fn(t) + 1) / 2 * (to - from);
|
|
1037
1052
|
};
|
|
1038
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
|
+
|
|
1039
1071
|
// src/tween/index.js
|
|
1040
1072
|
var HALF_PI = Math.PI / 2;
|
|
1041
1073
|
var tween = (object, prop, toValue, duration = 1, easing = LINEAR) => {
|
|
@@ -1490,14 +1522,6 @@
|
|
|
1490
1522
|
// src/string/lpad.js
|
|
1491
1523
|
var lpad_default = (str, targetLength, padString = "0") => (str + "").padStart(targetLength, padString);
|
|
1492
1524
|
|
|
1493
|
-
// src/debug/is.js
|
|
1494
|
-
var is_default = (value, type) => {
|
|
1495
|
-
if (typeof type === "function") {
|
|
1496
|
-
return value instanceof type;
|
|
1497
|
-
}
|
|
1498
|
-
return typeof value === type;
|
|
1499
|
-
};
|
|
1500
|
-
|
|
1501
1525
|
// src/debug/dd.js
|
|
1502
1526
|
var dd_default = (data, context, engine = globalThis) => {
|
|
1503
1527
|
engine.pal(["blue", "#fff"]);
|
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/math.js
CHANGED
|
@@ -28,6 +28,7 @@
|
|
|
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
33
|
sum: () => sum_default,
|
|
33
34
|
wave: () => wave_default
|
|
@@ -111,18 +112,49 @@
|
|
|
111
112
|
// src/math/percent.js
|
|
112
113
|
var percent_default = (value, min = 0, max = 1) => max - min ? (value - min) / (max - min) : 0;
|
|
113
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
|
+
|
|
114
123
|
// src/math/wave.js
|
|
115
124
|
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
125
|
DEV: assert_default(
|
|
120
|
-
"
|
|
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"),
|
|
121
136
|
"[litecanvas] wave() 4rd param must be a function (n: number) => number"
|
|
122
137
|
);
|
|
123
138
|
return from + (fn(t) + 1) / 2 * (to - from);
|
|
124
139
|
};
|
|
125
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
|
+
|
|
126
158
|
// src/math/_web.js
|
|
127
159
|
globalThis.utils = Object.assign(globalThis.utils || {}, index_exports);
|
|
128
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.1",
|
|
4
4
|
"description": "Utilities to help build litecanvas games",
|
|
5
5
|
"author": "Luiz Bills <luizbills@pm.me>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
},
|
|
15
15
|
"type": "module",
|
|
16
16
|
"main": "src/index.js",
|
|
17
|
+
"unpkg": "./dist/all.js",
|
|
18
|
+
"jsdelivr": "./dist/all.js",
|
|
19
|
+
"browser": "./dist/all.js",
|
|
17
20
|
"keywords": [
|
|
18
21
|
"litecanvas",
|
|
19
22
|
"utilities",
|
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/math/index.js
CHANGED
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
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import assert from "../debug/assert.js"
|
|
2
|
+
import is from "../debug/is.js"
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Interpolate between 2 values using a periodic function.
|
|
@@ -9,11 +10,17 @@ import assert from "../debug/assert.js"
|
|
|
9
10
|
* @param {(n: number) => number} [fn] - the periodic function (which default to `Math.sin`)
|
|
10
11
|
*/
|
|
11
12
|
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
13
|
DEV: assert(
|
|
16
|
-
"
|
|
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"),
|
|
17
24
|
"[litecanvas] wave() 4rd param must be a function (n: number) => number"
|
|
18
25
|
)
|
|
19
26
|
|