@litecanvas/utils 0.28.1 → 0.30.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 +1 -0
- package/dist/actor.min.js +1 -1
- package/dist/all.js +114 -18
- package/dist/all.min.js +2 -2
- package/dist/collection.js +65 -0
- package/dist/collection.min.js +1 -0
- package/dist/image.js +59 -16
- package/dist/image.min.js +1 -1
- package/dist/math.js +28 -4
- package/dist/math.min.js +1 -1
- package/dist/noise.min.js +1 -1
- package/dist/tween.min.js +1 -1
- package/dist/vector.js +3 -3
- package/dist/vector.min.js +1 -1
- package/package.json +2 -2
- package/src/collection/README.md +92 -0
- package/src/collection/_web.js +4 -0
- package/src/collection/head.js +7 -0
- package/src/collection/index.js +6 -0
- package/src/collection/last.js +7 -0
- package/src/collection/range.js +27 -0
- package/src/collection/sample.js +10 -0
- package/src/collection/shuffle.js +17 -0
- package/src/collection/tail.js +7 -0
- package/src/image/_web.js +2 -8
- package/src/image/index.js +5 -0
- package/src/image/make-circle.js +27 -0
- package/src/image/make-rectangle.js +37 -0
- package/src/index.js +2 -3
- package/src/math/README.md +37 -13
- package/src/math/index.js +3 -1
- package/src/math/lerp-angle.js +19 -0
- package/src/math/mean.js +11 -0
- package/src/math/sum.js +13 -0
- package/src/vector/index.js +3 -3
- package/src/math/range.js +0 -10
package/README.md
CHANGED
|
@@ -51,4 +51,5 @@ const pos = vec(0, 0)
|
|
|
51
51
|
- **Collision** utilities. [Usage & Docs](https://github.com/litecanvas/utils/tree/main/src/collision)
|
|
52
52
|
- **Tween** to create animations. [Usage & Docs](https://github.com/litecanvas/utils/tree/main/src/tween)
|
|
53
53
|
- **Image** to manipulate images. [Usage & Docs](https://github.com/litecanvas/utils/tree/main/src/image)
|
|
54
|
+
- **Collection** to manipulate lists (arrays). [Usage & Docs](https://github.com/litecanvas/utils/tree/main/src/collection)
|
|
54
55
|
- And some [math utilities](https://github.com/litecanvas/utils/tree/main/src/math)...
|
package/dist/actor.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var l=Object.defineProperty;var c=(r,t)=>{for(var e in t)l(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 m={};c(m,{ANCHOR_BOT_LEFT:()=>d,ANCHOR_BOT_RIGHT:()=>y,ANCHOR_CENTER:()=>f,ANCHOR_TOP_LEFT:()=>p,ANCHOR_TOP_RIGHT:()=>h,Actor:()=>u});var x=2*Math.PI,n=class{x;y;constructor(t=0,e=t){this.x=t,this.y=e}toString(){return`Vector (${this.x}, ${this.y})`}},b=r=>r instanceof n,s=(r=0,t=r)=>(b(r)&&(t=r.y,r=r.x),new n(r,t));var f=s(.5,.5),p=s(0,0),h=s(1,0),d=s(0,1),y=s(1,1),u=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,e,a=p){this.sprite=t,this.pos=e||s(0),this._o=s(a),this._s=s(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),a=this.sprite.height*(t?this._s.y:1),o=this.pos.x-e*this.anchor.x,i=this.pos.y-a*this.anchor.y;return[o,i,e,a]}draw(t=globalThis,e=!0){this.hidden||this.opacity<=0||(e&&t.push(),this.transform(t),this.drawImage(t),e&&t.pop())}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(t.deg2rad(this.angle)),t.scale((this.flipX?-1:1)*this._s.x,(this.flipY?-1:1)*this._s.y)}drawImage(t,e=!0){let a=this.anchor,o=-this.sprite.width*(this.flipX?1-a.x:a.x),i=-this.sprite.height*(this.flipY?1-a.y:a.y);e&&t.alpha(this.opacity),t.image(o,i,this.sprite)}};globalThis.utils=Object.assign(globalThis.utils||{},m);})();
|
package/dist/all.js
CHANGED
|
@@ -54,14 +54,24 @@
|
|
|
54
54
|
dist: () => dist_default,
|
|
55
55
|
flipImage: () => flip_default,
|
|
56
56
|
fract: () => fract_default,
|
|
57
|
+
head: () => head_default,
|
|
57
58
|
intersection: () => intersection_default,
|
|
59
|
+
last: () => last_default,
|
|
60
|
+
lerpAngle: () => lerp_angle_default,
|
|
58
61
|
mag: () => mag_default,
|
|
62
|
+
makeCircle: () => make_circle_default,
|
|
63
|
+
makeRectangle: () => make_rectangle_default,
|
|
64
|
+
mean: () => mean_default,
|
|
59
65
|
mod: () => mod_default,
|
|
60
66
|
range: () => range_default,
|
|
61
67
|
resolve: () => resolve_default,
|
|
62
68
|
roundd: () => roundd_default,
|
|
69
|
+
sample: () => sample_default,
|
|
63
70
|
scaleImage: () => scale_default,
|
|
64
|
-
|
|
71
|
+
shuffle: () => shuffle_default,
|
|
72
|
+
sum: () => sum_default,
|
|
73
|
+
tail: () => tail_default,
|
|
74
|
+
tintImage: () => tint_default,
|
|
65
75
|
tween: () => tween,
|
|
66
76
|
vec: () => vec,
|
|
67
77
|
vecAbs: () => vecAbs,
|
|
@@ -694,9 +704,9 @@
|
|
|
694
704
|
a.y += (b.y - a.y) * t || 0;
|
|
695
705
|
return a;
|
|
696
706
|
};
|
|
697
|
-
var vecRand = (minlength = 1, maxlength = minlength,
|
|
698
|
-
const angle =
|
|
699
|
-
const radius =
|
|
707
|
+
var vecRand = (minlength = 1, maxlength = minlength, rng = globalThis.rand || Math.random) => {
|
|
708
|
+
const angle = rng() * PI2;
|
|
709
|
+
const radius = rng() * (maxlength - minlength) + minlength;
|
|
700
710
|
return vec(cos(angle) * radius, sin(angle) * radius);
|
|
701
711
|
};
|
|
702
712
|
var vecAbs = (v) => {
|
|
@@ -900,9 +910,6 @@
|
|
|
900
910
|
// src/math/fract.js
|
|
901
911
|
var fract_default = (value) => value % 1 || 0;
|
|
902
912
|
|
|
903
|
-
// src/math/range.js
|
|
904
|
-
var range_default = (size) => Array.from(Array(size).keys());
|
|
905
|
-
|
|
906
913
|
// src/math/advance.js
|
|
907
914
|
var advance_default = advance = (position, velocity, acceleration, deltaTime = 1) => {
|
|
908
915
|
if (acceleration) {
|
|
@@ -935,6 +942,31 @@
|
|
|
935
942
|
return Math.hypot(x, y);
|
|
936
943
|
};
|
|
937
944
|
|
|
945
|
+
// src/math/sum.js
|
|
946
|
+
var sum_default = (values) => {
|
|
947
|
+
let result = 0;
|
|
948
|
+
for (let i = 0; i < values.length; i++) {
|
|
949
|
+
result += values[i];
|
|
950
|
+
}
|
|
951
|
+
return result;
|
|
952
|
+
};
|
|
953
|
+
|
|
954
|
+
// src/math/mean.js
|
|
955
|
+
var mean_default = (values) => {
|
|
956
|
+
return sum_default(values) / values.length;
|
|
957
|
+
};
|
|
958
|
+
|
|
959
|
+
// src/math/lerp-angle.js
|
|
960
|
+
var lerp_angle_default = (start, end, amount) => {
|
|
961
|
+
let dif = (end - start) % 360;
|
|
962
|
+
if (dif > 180) {
|
|
963
|
+
dif -= 360;
|
|
964
|
+
} else if (dif < -180) {
|
|
965
|
+
dif += 360;
|
|
966
|
+
}
|
|
967
|
+
return start + dif * amount;
|
|
968
|
+
};
|
|
969
|
+
|
|
938
970
|
// src/tween/index.js
|
|
939
971
|
var HALF_PI = Math.PI / 2;
|
|
940
972
|
var tween = (object, prop, toValue, duration = 1, easing = LINEAR) => {
|
|
@@ -1283,15 +1315,12 @@
|
|
|
1283
1315
|
}
|
|
1284
1316
|
};
|
|
1285
1317
|
|
|
1286
|
-
// src/image/
|
|
1287
|
-
var
|
|
1318
|
+
// src/image/flip.js
|
|
1319
|
+
var flip_default = (img, horizontal = true, vertically = false, engine = globalThis) => {
|
|
1288
1320
|
return engine.paint(img.width, img.height, (ctx) => {
|
|
1289
1321
|
engine.push();
|
|
1290
|
-
engine.
|
|
1291
|
-
engine.
|
|
1292
|
-
ctx.globalCompositeOperation = "destination-atop";
|
|
1293
|
-
engine.alpha(1);
|
|
1294
|
-
engine.image(0, 0, img);
|
|
1322
|
+
engine.scale(horizontal ? -1 : 1, vertically ? -1 : 1);
|
|
1323
|
+
engine.image(horizontal ? -img.width : 0, vertically ? -img.height : 0, img);
|
|
1295
1324
|
engine.pop();
|
|
1296
1325
|
});
|
|
1297
1326
|
};
|
|
@@ -1307,16 +1336,83 @@
|
|
|
1307
1336
|
});
|
|
1308
1337
|
};
|
|
1309
1338
|
|
|
1310
|
-
// src/image/
|
|
1311
|
-
var
|
|
1339
|
+
// src/image/tint.js
|
|
1340
|
+
var tint_default = (img, color, opacity = 1, engine = globalThis) => {
|
|
1312
1341
|
return engine.paint(img.width, img.height, (ctx) => {
|
|
1313
1342
|
engine.push();
|
|
1314
|
-
engine.
|
|
1315
|
-
engine.
|
|
1343
|
+
engine.alpha(opacity);
|
|
1344
|
+
engine.rectfill(0, 0, img.width, img.height, color);
|
|
1345
|
+
ctx.globalCompositeOperation = "destination-atop";
|
|
1346
|
+
engine.alpha(1);
|
|
1347
|
+
engine.image(0, 0, img);
|
|
1316
1348
|
engine.pop();
|
|
1317
1349
|
});
|
|
1318
1350
|
};
|
|
1319
1351
|
|
|
1352
|
+
// src/image/make-circle.js
|
|
1353
|
+
var make_circle_default = (radius, color, { borderWidth = 0, borderColor = 0, engine = globalThis } = {}) => {
|
|
1354
|
+
const imageSize = radius * 2 + borderWidth;
|
|
1355
|
+
return engine.paint(imageSize, imageSize, () => {
|
|
1356
|
+
engine.circfill(imageSize / 2, imageSize / 2, radius, color);
|
|
1357
|
+
if (borderWidth > 0) {
|
|
1358
|
+
engine.linewidth(borderWidth);
|
|
1359
|
+
engine.stroke(borderColor);
|
|
1360
|
+
}
|
|
1361
|
+
});
|
|
1362
|
+
};
|
|
1363
|
+
|
|
1364
|
+
// src/image/make-rectangle.js
|
|
1365
|
+
var make_rectangle_default = (width, height, color, { borderWidth = 0, borderColor = 0, engine = globalThis } = {}) => {
|
|
1366
|
+
const imageWidth = width + borderWidth * 2;
|
|
1367
|
+
const imageHeight = height + borderWidth * 2;
|
|
1368
|
+
return engine.paint(imageWidth, imageHeight, () => {
|
|
1369
|
+
engine.rectfill(
|
|
1370
|
+
borderWidth > 0 ? borderWidth : 0,
|
|
1371
|
+
borderWidth > 0 ? borderWidth : 0,
|
|
1372
|
+
width,
|
|
1373
|
+
height,
|
|
1374
|
+
color
|
|
1375
|
+
);
|
|
1376
|
+
if (borderWidth > 0) {
|
|
1377
|
+
engine.linewidth(borderWidth);
|
|
1378
|
+
engine.stroke(borderColor);
|
|
1379
|
+
}
|
|
1380
|
+
});
|
|
1381
|
+
};
|
|
1382
|
+
|
|
1383
|
+
// src/collection/range.js
|
|
1384
|
+
var range_default = (size, from = 0, step = 1) => Array.from(
|
|
1385
|
+
Array(size).keys().map((i) => {
|
|
1386
|
+
return from + step * i;
|
|
1387
|
+
})
|
|
1388
|
+
);
|
|
1389
|
+
|
|
1390
|
+
// src/collection/shuffle.js
|
|
1391
|
+
var shuffle_default = (values, rng = globalThis.rand || Math.random) => {
|
|
1392
|
+
values = Array.from(values);
|
|
1393
|
+
for (let i = values.length - 1; i > 0; i--) {
|
|
1394
|
+
let j = Math.floor(rng() * (i + 1));
|
|
1395
|
+
let temp = values[i];
|
|
1396
|
+
values[i] = values[j];
|
|
1397
|
+
values[j] = temp;
|
|
1398
|
+
}
|
|
1399
|
+
return values;
|
|
1400
|
+
};
|
|
1401
|
+
|
|
1402
|
+
// src/collection/sample.js
|
|
1403
|
+
var sample_default = (values, rng = globalThis.rand || Math.random) => {
|
|
1404
|
+
return values[Math.floor(rng() * values.length)];
|
|
1405
|
+
};
|
|
1406
|
+
|
|
1407
|
+
// src/collection/head.js
|
|
1408
|
+
var head_default = (values) => values[0];
|
|
1409
|
+
|
|
1410
|
+
// src/collection/last.js
|
|
1411
|
+
var last_default = (values) => values[values.length - 1];
|
|
1412
|
+
|
|
1413
|
+
// src/collection/tail.js
|
|
1414
|
+
var tail_default = (values) => values.slice(1);
|
|
1415
|
+
|
|
1320
1416
|
// src/_web.js
|
|
1321
1417
|
globalThis.utils = Object.assign(globalThis.utils || {}, index_exports);
|
|
1322
1418
|
})();
|
package/dist/all.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(()=>{var
|
|
2
|
-
`)}},
|
|
1
|
+
(()=>{var Mt=Object.defineProperty;var Et=(e,t)=>{for(var r in t)Mt(e,r,{get:t[r],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(e=!0)=>{for(let t in globalThis.utils)t!=="global"&&(e||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var U={};Et(U,{ANCHOR_BOT_LEFT:()=>Jt,ANCHOR_BOT_RIGHT:()=>vt,ANCHOR_CENTER:()=>Kt,ANCHOR_TOP_LEFT:()=>Q,ANCHOR_TOP_RIGHT:()=>Qt,Actor:()=>R,BACK_IN:()=>ae,BACK_IN_OUT:()=>ie,BACK_OUT:()=>ne,BOUNCE_IN:()=>ut,BOUNCE_IN_OUT:()=>le,BOUNCE_OUT:()=>Y,Camera:()=>y,DOWN:()=>$t,EASE_IN:()=>ee,EASE_IN_OUT:()=>se,EASE_OUT:()=>re,ELASTIC_IN:()=>oe,ELASTIC_IN_OUT:()=>ue,ELASTIC_OUT:()=>he,Grid:()=>I,LEFT:()=>Zt,LINEAR:()=>ht,Noise:()=>F,ONE:()=>jt,RIGHT:()=>Vt,TypedGrid:()=>P,UP:()=>qt,Vector:()=>T,ZERO:()=>K,advance:()=>et,diff:()=>J,dist:()=>at,flipImage:()=>ct,fract:()=>tt,head:()=>yt,intersection:()=>E,last:()=>Tt,lerpAngle:()=>ot,mag:()=>nt,makeCircle:()=>bt,makeRectangle:()=>dt,mean:()=>it,mod:()=>rt,range:()=>_t,resolve:()=>W,roundd:()=>st,sample:()=>gt,scaleImage:()=>mt,shuffle:()=>xt,sum:()=>k,tail:()=>wt,tintImage:()=>ft,tween:()=>te,vec:()=>i,vecAbs:()=>Xt,vecAdd:()=>D,vecAngle:()=>Lt,vecAngleBetween:()=>St,vecCeil:()=>Ft,vecClamp:()=>Wt,vecCross:()=>Rt,vecDist:()=>Ct,vecDist2:()=>Dt,vecDiv:()=>A,vecDot:()=>Z,vecEq:()=>C,vecFloor:()=>Ut,vecIsZero:()=>Gt,vecLerp:()=>Nt,vecLimit:()=>Pt,vecMag:()=>V,vecMag2:()=>$,vecMove:()=>zt,vecMult:()=>w,vecNorm:()=>S,vecRand:()=>Yt,vecReflect:()=>Ht,vecRotate:()=>kt,vecRound:()=>Bt,vecSet:()=>q,vecSetMag:()=>Ot,vecSub:()=>L,wave:()=>v});var E=(e,t,r,s,a,n,o,h)=>{let u=Math.max(e,a),d=Math.min(e+r,a+o)-u,p=Math.max(t,n),_=Math.min(t+s,n+h)-p;return[u,p,d,_]};var W=(e,t,r,s,a,n,o,h)=>{let[u,d,p,_]=E(e,t,r,s,a,n,o,h),b="",g=e,l=t;return p<_?e<a?(b="right",g=a-r):(b="left",g=a+o):t<n?(b="bottom",l=n-s):(b="top",l=n+h),{direction:b,x:g,y:l}};var y=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(t=null,r=0,s=0,a=null,n=null){this._engine=t||globalThis,this.ox=r,this.oy=s,this.resize(a||this._engine.WIDTH-r,n||this._engine.HEIGHT-s),this.x=this.width/2,this.y=this.height/2}resize(t,r){this.width=t,this.height=r,this._engine.emit("camera-resized",this)}start(t=!1){if(this._engine.push(),t){let a=path();a.rect(this.ox,this.oy,this.width,this.height),this._engine.clip(a)}let r=this.ox+this.width/2,s=this.oy+this.height/2;this._engine.translate(r,s),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,r){this.x=t,this.y=r}move(t,r){this.x+=t,this.y+=r}zoom(t){this.scale*=t}zoomTo(t){this.scale=t}rotate(t){this.rotation+=t}rotateTo(t){this.rotation=t}getWorldPoint(t,r,s={}){let a=Math.cos(-this.rotation),n=Math.sin(-this.rotation);return t=(t-this.width/2-this.ox)/this.scale,r=(r-this.height/2-this.oy)/this.scale,s.x=a*t-n*r+this.x,s.y=n*t+a*r+this.y,s}getCameraPoint(t,r,s={}){let a=Math.cos(-this.rotation),n=Math.sin(-this.rotation);return t=t-this.x,r=r-this.y,t=a*t-n*r,r=n*t+a*r,s.x=t*this.scale+this.width/2+this.ox,s.y=r*this.scale+this.height/2+this.oy,s}getBounds(){return[this.ox,this.oy,this.width,this.height]}viewing(t,r,s,a){let n=this.width/2-this.x,o=this.height/2-this.y,h=this.width/this.scale,u=this.height/this.scale;return this._engine.colrect(t,r,s,a,n,o,h,u)}shake(t=1,r=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",s=>{this._shake.x=this._engine.randi(-t,t),this._shake.y=this._engine.randi(-t,t),r-=s,r<=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=class e{_w;_h;_c;constructor(t,r,s=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~r),this._c=s}[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,r)=>this.set(t,r,void 0))}get width(){return this._w}get height(){return this._h}set(t,r,s){this._c[this.pointToIndex(t,r)]=s}get(t,r){return this._c[this.pointToIndex(t,r)]}has(t,r){return this.get(t,r)!=null}check(t,r){return t>=0&&t<this._w&&r>=0&&r<this._h}get length(){return this._w*this._h}pointToIndex(t,r){return this.clampX(~~t)+this.clampY(~~r)*this._w}indexToPointX(t){return t%this._w}indexToPointY(t){return Math.floor(t/this._w)}forEach(t,r=!1){let s=r?this.length-1:0,a=r?-1:this.length,n=r?-1:1;for(;s!==a;){let o=this.indexToPointX(s),h=this.indexToPointY(s),u=this._c[s];if(t(o,h,u,this)===!1)break;s+=n}}fill(t){this.forEach((r,s)=>{this.set(r,s,t)})}clampX(t){return z(t,0,this._w-1)}clampY(t){return z(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",r=!0){if(!r)return this._c.join(t);let s=[];return this.forEach((a,n,o)=>{s[n]=s[n]||"",s[n]+=o+t}),s.join(`
|
|
2
|
+
`)}},P=class e extends I{constructor(t,r,s=Uint8Array){super(t,r,null),this._c=new s(this._w*this._h)}has(t,r){return this.get(t,r)!==0}clone(){let t=new e(this._w,this._h,this._c.constructor);return this.forEach((r,s,a)=>{t.set(r,s,a)}),t}};function z(e,t,r){return e<t?t:e>r?r:e}var It=Math.sqrt,G=Math.cos,j=Math.sin,At=2*Math.PI,T=class{x;y;constructor(t=0,r=t){this.x=t,this.y=r}toString(){return`Vector (${this.x}, ${this.y})`}},x=e=>e instanceof T,i=(e=0,t=e)=>(x(e)&&(t=e.y,e=e.x),new T(e,t)),C=(e,t,r=t)=>x(t)?C(e,t.x,t.y):e.x===t&&e.y===r,q=(e,t,r=t)=>(x(t)?q(e,t.x,t.y):(e.x=t,e.y=r),e),D=(e,t,r=t)=>x(t)?D(e,t.x,t.y):(e.x+=t,e.y+=r,e),L=(e,t,r=t)=>x(t)?L(e,t.x,t.y):(e.x-=t,e.y-=r,e),w=(e,t,r=t)=>x(t)?w(e,t.x,t.y):(e.x*=t,e.y*=r,e),A=(e,t,r=t)=>x(t)?A(e,t.x,t.y):(e.x/=t||1,e.y/=r||1,e),kt=(e,t)=>{let r=G(t),s=j(t);return e.x=r*e.x-s*e.y,e.y=s*e.x+r*e.y,e},Ht=(e,t)=>{let r=S(i(t));return L(e,w(r,2*Z(e,r)))},Ot=(e,t)=>(S(e),w(e,t),e),V=e=>Math.hypot(e.x,e.y),$=e=>e.x*e.x+e.y*e.y,S=e=>{let t=V(e);return t>0&&A(e,t),e},Pt=(e,t=1)=>{let r=$(e);return r>t*t&&(A(e,It(r)),w(e,t)),e},Ct=(e,t)=>Math.hypot(t.x-e.x,t.y-e.y),Dt=(e,t)=>{let r=e.x-t.x,s=e.y-t.y;return r*r+s*s},Lt=e=>Math.atan2(e.y,e.x),St=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),Z=(e,t)=>e.x*t.x+e.y*t.y,Rt=(e,t)=>e.x*t.y-e.y*t.x,Nt=(e,t,r)=>(e.x+=(t.x-e.x)*r||0,e.y+=(t.y-e.y)*r||0,e),Yt=(e=1,t=e,r=globalThis.rand||Math.random)=>{let s=r()*At,a=r()*(t-e)+e;return i(G(s)*a,j(s)*a)},Xt=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),Ft=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),Ut=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),Bt=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),Wt=(e,t,r)=>(e.x<t.x&&(e.x=t.x),e.x>r.x&&(e.x=r.x),e.y<t.y&&(e.y=t.y),e.y>r.y&&(e.y=r.y),e),zt=(e,t,r=1)=>D(e,t.x*r,t.y*r),Gt=e=>C(e,K),K=i(0,0),jt=i(1,1),qt=i(0,-1),Vt=i(1,0),$t=i(0,1),Zt=i(-1,0);var Kt=i(.5,.5),Q=i(0,0),Qt=i(1,0),Jt=i(0,1),vt=i(1,1),R=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,r,s=Q){this.sprite=t,this.pos=r||i(0),this._o=i(s),this._s=i(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,r=t){this._s.x=t,this._s.y=r}scaleBy(t,r=t){this._s.x*=t,this._s.y*=r}getBounds(t=!0){let r=this.sprite.width*(t?this._s.x:1),s=this.sprite.height*(t?this._s.y:1),a=this.pos.x-r*this.anchor.x,n=this.pos.y-s*this.anchor.y;return[a,n,r,s]}draw(t=globalThis,r=!0){this.hidden||this.opacity<=0||(r&&t.push(),this.transform(t),this.drawImage(t),r&&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,r=!0){let s=this.anchor,a=-this.sprite.width*(this.flipX?1-s.x:s.x),n=-this.sprite.height*(this.flipY?1-s.y:s.y);r&&t.alpha(this.opacity),t.image(a,n,this.sprite)}};var J=(e,t)=>Math.abs(t-e)||0;var v=(e,t,r,s=Math.sin)=>e+(s(r)+1)/2*(t-e);var tt=e=>e%1||0;var et=advance=(e,t,r,s=1)=>{r&&(t.x+=r.x*s,t.y+=r.y*s),e.x+=t.x*s,e.y+=t.y*s};var rt=(e,t)=>(t+e%t)%t;var st=(e,t=0)=>{if(!t)return Math.round(e);let r=Math.pow(10,t);return Math.round(e*r)/r};var at=(e,t,r,s)=>Math.hypot(r-e,s-t);var nt=(e,t)=>Math.hypot(e,t);var k=e=>{let t=0;for(let r=0;r<e.length;r++)t+=e[r];return t};var it=e=>k(e)/e.length;var ot=(e,t,r)=>{let s=(t-e)%360;return s>180?s-=360:s<-180&&(s+=360),e+s*r};var H=Math.PI/2,te=(e,t,r,s=1,a=ht)=>new N(e,t,r,s,a),ht=e=>e,ee=e=>e*e,re=e=>-e*(e-2),se=e=>e<.5?2*e*e:-2*e*e+4*e-1,ae=e=>e*e*e-e*Math.sin(e*Math.PI),ne=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},ie=e=>{if(e<.5){let r=2*e;return .5*(r*r*r-r*Math.sin(r*Math.PI))}let t=1-(2*e-1);return .5*(1-(t*t*t-t*Math.sin(e*Math.PI)))+.5},oe=e=>Math.sin(13*H*e)*Math.pow(2,10*(e-1)),he=e=>Math.sin(-13*H*(e+1))*Math.pow(2,-10*e)+1,ue=e=>{if(e<.5){let s=Math.sin(13*H*(2*e)),a=Math.pow(2,10*(2*e-1));return .5*s*a}let t=Math.sin(-13*H*(2*e-1+1)),r=Math.pow(2,-10*(2*e-1));return .5*(t*r+2)},ut=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,le=e=>e<.5?.5*ut(e*2):.5*Y(e*2-1)+.5,N=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(t,r,s,a,n){this._o=t,this._p=r,this._x=s,this._d=a,this._e=n,this._w=0}start(t){if(this.running)return this;this._cu.stop(!1),this._ch=this._cu=this,this.running=!0;let r=this._o[this._p]||0,s=this._rel?r+this._x:this._x;return this._lc=this._lc||t||globalThis,this._u=this._lc.listen("update",a=>{if(this._t<=this._w){this._t+=a;return}let n=this._t-this._w;this._o[this._p]=this._lc.lerp(r,s,this._e(n/this._d)),this._t+=a,n>=this._d&&(this._o[this._p]=s,this.stop())}),this}stop(t=!0){if(!this._u)return this;if(this.running=!1,this._u(),this._t=0,t)for(let r of this._cb)r(this._o);return this}restart(t=null,r=!1){return this.stop(r).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 lt=4,O=1<<lt,pt=8,pe=1<<pt,f=4095,X=e=>.5*(1-Math.cos(e*Math.PI)),F=class{_p=[];_po=4;_pf=.5;_e=null;constructor(t){this._e=t||globalThis,this.noiseSeed()}noise(t,r=0,s=0){t<0&&(t=-t),r<0&&(r=-r),s<0&&(s=-s);let a=Math.floor(t),n=Math.floor(r),o=Math.floor(s),h=t-a,u=r-n,d=s-o,p,_,b=0,g=.5,l,c,M;for(let B=0;B<this._po;B++){let m=a+(n<<lt)+(o<<pt);p=X(h),_=X(u),l=this._p[m&f],l+=p*(this._p[m+1&f]-l),c=this._p[m+O&f],c+=p*(this._p[m+O+1&f]-c),l+=_*(c-l),m+=pe,c=this._p[m&f],c+=p*(this._p[m+1&f]-c),M=this._p[m+O&f],M+=p*(this._p[m+O+1&f]-M),c+=_*(M-c),l+=X(d)*(c-l),b+=l*g,g*=this._pf,a<<=1,h*=2,n<<=1,u*=2,o<<=1,d*=2,h>=1&&(a++,h--),u>=1&&(n++,u--),d>=1&&(o++,d--)}return b}noiseDetail(t,r){t>0&&(this._po=t),r>0&&(this._pf=r)}noiseSeed(t=null){t!=null&&this._e.seed(t);let r=this._e.rand||Math.random;for(let s=0;s<f+1;s++)this._p[s]=r()}};var ct=(e,t=!0,r=!1,s=globalThis)=>s.paint(e.width,e.height,a=>{s.push(),s.scale(t?-1:1,r?-1:1),s.image(t?-e.width:0,r?-e.height:0,e),s.pop()});var mt=(e,t,r=!0,s=globalThis)=>s.paint(e.width*t,e.height*t,a=>{s.push(),a.imageSmoothingEnabled=!r,s.scale(t),s.image(0,0,e),s.pop()});var ft=(e,t,r=1,s=globalThis)=>s.paint(e.width,e.height,a=>{s.push(),s.alpha(r),s.rectfill(0,0,e.width,e.height,t),a.globalCompositeOperation="destination-atop",s.alpha(1),s.image(0,0,e),s.pop()});var bt=(e,t,{borderWidth:r=0,borderColor:s=0,engine:a=globalThis}={})=>{let n=e*2+r;return a.paint(n,n,()=>{a.circfill(n/2,n/2,e,t),r>0&&(a.linewidth(r),a.stroke(s))})};var dt=(e,t,r,{borderWidth:s=0,borderColor:a=0,engine:n=globalThis}={})=>{let o=e+s*2,h=t+s*2;return n.paint(o,h,()=>{n.rectfill(s>0?s:0,s>0?s:0,e,t,r),s>0&&(n.linewidth(s),n.stroke(a))})};var _t=(e,t=0,r=1)=>Array.from(Array(e).keys().map(s=>t+r*s));var xt=(e,t=globalThis.rand||Math.random)=>{e=Array.from(e);for(let r=e.length-1;r>0;r--){let s=Math.floor(t()*(r+1)),a=e[r];e[r]=e[s],e[s]=a}return e};var gt=(e,t=globalThis.rand||Math.random)=>e[Math.floor(t()*e.length)];var yt=e=>e[0];var Tt=e=>e[e.length-1];var wt=e=>e.slice(1);globalThis.utils=Object.assign(globalThis.utils||{},U);})();
|
|
3
3
|
/*! @litecanvas/utils by Luiz Bills | MIT Licensed */
|
|
@@ -0,0 +1,65 @@
|
|
|
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/collection/index.js
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
head: () => head_default,
|
|
23
|
+
last: () => last_default,
|
|
24
|
+
range: () => range_default,
|
|
25
|
+
sample: () => sample_default,
|
|
26
|
+
shuffle: () => shuffle_default,
|
|
27
|
+
tail: () => tail_default
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// src/collection/range.js
|
|
31
|
+
var range_default = (size, from = 0, step = 1) => Array.from(
|
|
32
|
+
Array(size).keys().map((i) => {
|
|
33
|
+
return from + step * i;
|
|
34
|
+
})
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
// src/collection/shuffle.js
|
|
38
|
+
var shuffle_default = (values, rng = globalThis.rand || Math.random) => {
|
|
39
|
+
values = Array.from(values);
|
|
40
|
+
for (let i = values.length - 1; i > 0; i--) {
|
|
41
|
+
let j = Math.floor(rng() * (i + 1));
|
|
42
|
+
let temp = values[i];
|
|
43
|
+
values[i] = values[j];
|
|
44
|
+
values[j] = temp;
|
|
45
|
+
}
|
|
46
|
+
return values;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// src/collection/sample.js
|
|
50
|
+
var sample_default = (values, rng = globalThis.rand || Math.random) => {
|
|
51
|
+
return values[Math.floor(rng() * values.length)];
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// src/collection/head.js
|
|
55
|
+
var head_default = (values) => values[0];
|
|
56
|
+
|
|
57
|
+
// src/collection/last.js
|
|
58
|
+
var last_default = (values) => values[values.length - 1];
|
|
59
|
+
|
|
60
|
+
// src/collection/tail.js
|
|
61
|
+
var tail_default = (values) => values.slice(1);
|
|
62
|
+
|
|
63
|
+
// src/collection/_web.js
|
|
64
|
+
globalThis.utils = Object.assign(globalThis.utils || {}, index_exports);
|
|
65
|
+
})();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(()=>{var g=Object.defineProperty;var p=(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={};p(a,{head:()=>s,last:()=>h,range:()=>e,sample:()=>f,shuffle:()=>i,tail:()=>d});var e=(t,l=0,o=1)=>Array.from(Array(t).keys().map(r=>l+o*r));var i=(t,l=globalThis.rand||Math.random)=>{t=Array.from(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/image.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
(() => {
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __export = (target, all) => {
|
|
4
|
+
for (var name in all)
|
|
5
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
6
|
+
};
|
|
7
|
+
|
|
2
8
|
// src/_global.js
|
|
3
9
|
globalThis.utils = globalThis.utils || {};
|
|
4
10
|
globalThis.utils.global = (overrides = true) => {
|
|
@@ -10,15 +16,22 @@
|
|
|
10
16
|
}
|
|
11
17
|
};
|
|
12
18
|
|
|
13
|
-
// src/image/
|
|
14
|
-
var
|
|
19
|
+
// src/image/index.js
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
flipImage: () => flip_default,
|
|
23
|
+
makeCircle: () => make_circle_default,
|
|
24
|
+
makeRectangle: () => make_rectangle_default,
|
|
25
|
+
scaleImage: () => scale_default,
|
|
26
|
+
tintImage: () => tint_default
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// src/image/flip.js
|
|
30
|
+
var flip_default = (img, horizontal = true, vertically = false, engine = globalThis) => {
|
|
15
31
|
return engine.paint(img.width, img.height, (ctx) => {
|
|
16
32
|
engine.push();
|
|
17
|
-
engine.
|
|
18
|
-
engine.
|
|
19
|
-
ctx.globalCompositeOperation = "destination-atop";
|
|
20
|
-
engine.alpha(1);
|
|
21
|
-
engine.image(0, 0, img);
|
|
33
|
+
engine.scale(horizontal ? -1 : 1, vertically ? -1 : 1);
|
|
34
|
+
engine.image(horizontal ? -img.width : 0, vertically ? -img.height : 0, img);
|
|
22
35
|
engine.pop();
|
|
23
36
|
});
|
|
24
37
|
};
|
|
@@ -34,20 +47,50 @@
|
|
|
34
47
|
});
|
|
35
48
|
};
|
|
36
49
|
|
|
37
|
-
// src/image/
|
|
38
|
-
var
|
|
50
|
+
// src/image/tint.js
|
|
51
|
+
var tint_default = (img, color, opacity = 1, engine = globalThis) => {
|
|
39
52
|
return engine.paint(img.width, img.height, (ctx) => {
|
|
40
53
|
engine.push();
|
|
41
|
-
engine.
|
|
42
|
-
engine.
|
|
54
|
+
engine.alpha(opacity);
|
|
55
|
+
engine.rectfill(0, 0, img.width, img.height, color);
|
|
56
|
+
ctx.globalCompositeOperation = "destination-atop";
|
|
57
|
+
engine.alpha(1);
|
|
58
|
+
engine.image(0, 0, img);
|
|
43
59
|
engine.pop();
|
|
44
60
|
});
|
|
45
61
|
};
|
|
46
62
|
|
|
63
|
+
// src/image/make-circle.js
|
|
64
|
+
var make_circle_default = (radius, color, { borderWidth = 0, borderColor = 0, engine = globalThis } = {}) => {
|
|
65
|
+
const imageSize = radius * 2 + borderWidth;
|
|
66
|
+
return engine.paint(imageSize, imageSize, () => {
|
|
67
|
+
engine.circfill(imageSize / 2, imageSize / 2, radius, color);
|
|
68
|
+
if (borderWidth > 0) {
|
|
69
|
+
engine.linewidth(borderWidth);
|
|
70
|
+
engine.stroke(borderColor);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// src/image/make-rectangle.js
|
|
76
|
+
var make_rectangle_default = (width, height, color, { borderWidth = 0, borderColor = 0, engine = globalThis } = {}) => {
|
|
77
|
+
const imageWidth = width + borderWidth * 2;
|
|
78
|
+
const imageHeight = height + borderWidth * 2;
|
|
79
|
+
return engine.paint(imageWidth, imageHeight, () => {
|
|
80
|
+
engine.rectfill(
|
|
81
|
+
borderWidth > 0 ? borderWidth : 0,
|
|
82
|
+
borderWidth > 0 ? borderWidth : 0,
|
|
83
|
+
width,
|
|
84
|
+
height,
|
|
85
|
+
color
|
|
86
|
+
);
|
|
87
|
+
if (borderWidth > 0) {
|
|
88
|
+
engine.linewidth(borderWidth);
|
|
89
|
+
engine.stroke(borderColor);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
|
|
47
94
|
// src/image/_web.js
|
|
48
|
-
globalThis.utils = Object.assign(globalThis.utils || {},
|
|
49
|
-
tint: tint_default,
|
|
50
|
-
scaleImage: scale_default,
|
|
51
|
-
flipImage: flip_default
|
|
52
|
-
});
|
|
95
|
+
globalThis.utils = Object.assign(globalThis.utils || {}, index_exports);
|
|
53
96
|
})();
|
package/dist/image.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=(
|
|
1
|
+
(()=>{var f=Object.defineProperty;var d=(e,a)=>{for(var r in a)f(e,r,{get:a[r],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(e=!0)=>{for(let a in globalThis.utils)a!=="global"&&(e||globalThis[a]===void 0)&&(globalThis[a]=globalThis.utils[a])};var u={};d(u,{flipImage:()=>m,makeCircle:()=>o,makeRectangle:()=>p,scaleImage:()=>i,tintImage:()=>l});var m=(e,a=!0,r=!1,t=globalThis)=>t.paint(e.width,e.height,n=>{t.push(),t.scale(a?-1:1,r?-1:1),t.image(a?-e.width:0,r?-e.height:0,e),t.pop()});var i=(e,a,r=!0,t=globalThis)=>t.paint(e.width*a,e.height*a,n=>{t.push(),n.imageSmoothingEnabled=!r,t.scale(a),t.image(0,0,e),t.pop()});var l=(e,a,r=1,t=globalThis)=>t.paint(e.width,e.height,n=>{t.push(),t.alpha(r),t.rectfill(0,0,e.width,e.height,a),n.globalCompositeOperation="destination-atop",t.alpha(1),t.image(0,0,e),t.pop()});var o=(e,a,{borderWidth:r=0,borderColor:t=0,engine:n=globalThis}={})=>{let s=e*2+r;return n.paint(s,s,()=>{n.circfill(s/2,s/2,e,a),r>0&&(n.linewidth(r),n.stroke(t))})};var p=(e,a,r,{borderWidth:t=0,borderColor:n=0,engine:s=globalThis}={})=>{let c=e+t*2,b=a+t*2;return s.paint(c,b,()=>{s.rectfill(t>0?t:0,t>0?t:0,e,a,r),t>0&&(s.linewidth(t),s.stroke(n))})};globalThis.utils=Object.assign(globalThis.utils||{},u);})();
|
package/dist/math.js
CHANGED
|
@@ -23,10 +23,12 @@
|
|
|
23
23
|
diff: () => diff_default,
|
|
24
24
|
dist: () => dist_default,
|
|
25
25
|
fract: () => fract_default,
|
|
26
|
+
lerpAngle: () => lerp_angle_default,
|
|
26
27
|
mag: () => mag_default,
|
|
28
|
+
mean: () => mean_default,
|
|
27
29
|
mod: () => mod_default,
|
|
28
|
-
range: () => range_default,
|
|
29
30
|
roundd: () => roundd_default,
|
|
31
|
+
sum: () => sum_default,
|
|
30
32
|
wave: () => wave_default
|
|
31
33
|
});
|
|
32
34
|
|
|
@@ -39,9 +41,6 @@
|
|
|
39
41
|
// src/math/fract.js
|
|
40
42
|
var fract_default = (value) => value % 1 || 0;
|
|
41
43
|
|
|
42
|
-
// src/math/range.js
|
|
43
|
-
var range_default = (size) => Array.from(Array(size).keys());
|
|
44
|
-
|
|
45
44
|
// src/vector/index.js
|
|
46
45
|
var PI2 = 2 * Math.PI;
|
|
47
46
|
|
|
@@ -77,6 +76,31 @@
|
|
|
77
76
|
return Math.hypot(x, y);
|
|
78
77
|
};
|
|
79
78
|
|
|
79
|
+
// src/math/sum.js
|
|
80
|
+
var sum_default = (values) => {
|
|
81
|
+
let result = 0;
|
|
82
|
+
for (let i = 0; i < values.length; i++) {
|
|
83
|
+
result += values[i];
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// src/math/mean.js
|
|
89
|
+
var mean_default = (values) => {
|
|
90
|
+
return sum_default(values) / values.length;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// src/math/lerp-angle.js
|
|
94
|
+
var lerp_angle_default = (start, end, amount) => {
|
|
95
|
+
let dif = (end - start) % 360;
|
|
96
|
+
if (dif > 180) {
|
|
97
|
+
dif -= 360;
|
|
98
|
+
} else if (dif < -180) {
|
|
99
|
+
dif += 360;
|
|
100
|
+
}
|
|
101
|
+
return start + dif * amount;
|
|
102
|
+
};
|
|
103
|
+
|
|
80
104
|
// src/math/_web.js
|
|
81
105
|
globalThis.utils = Object.assign(globalThis.utils || {}, index_exports);
|
|
82
106
|
})();
|
package/dist/math.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var d=Object.defineProperty;var M=(e,t)=>{for(var r in t)d(e,r,{get:t[r],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(e=!0)=>{for(let t in globalThis.utils)t!=="global"&&(e||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var n={};M(n,{advance:()=>a,diff:()=>c,dist:()=>f,fract:()=>u,lerpAngle:()=>h,mag:()=>y,mean:()=>i,mod:()=>p,roundd:()=>l,sum:()=>s,wave:()=>x});var c=(e,t)=>Math.abs(t-e)||0;var x=(e,t,r,o=Math.sin)=>e+(o(r)+1)/2*(t-e);var u=e=>e%1||0;var R=2*Math.PI;var a=advance=(e,t,r,o=1)=>{r&&(t.x+=r.x*o,t.y+=r.y*o),e.x+=t.x*o,e.y+=t.y*o};var p=(e,t)=>(t+e%t)%t;var l=(e,t=0)=>{if(!t)return Math.round(e);let r=Math.pow(10,t);return Math.round(e*r)/r};var f=(e,t,r,o)=>Math.hypot(r-e,o-t);var y=(e,t)=>Math.hypot(e,t);var s=e=>{let t=0;for(let r=0;r<e.length;r++)t+=e[r];return t};var i=e=>s(e)/e.length;var h=(e,t,r)=>{let o=(t-e)%360;return o>180?o-=360:o<-180&&(o+=360),e+o*r};globalThis.utils=Object.assign(globalThis.utils||{},n);})();
|
package/dist/noise.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var D=Object.defineProperty;var H=(u,e)=>{for(var t in e)D(u,t,{get:e[t],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(u=!0)=>{for(let e in globalThis.utils)e!=="global"&&(u||globalThis[e]===void 0)&&(globalThis[e]=globalThis.utils[e])};var E={};H(E,{Noise:()=>T});var w=4,h=1<<w,I=8,L=1<<I,s=4095,_=u=>.5*(1-Math.cos(u*Math.PI)),T=class{_p=[];_po=4;_pf=.5;_e=null;constructor(e){this._e=e||globalThis,this.noiseSeed()}noise(e,t=0,r=0){e<0&&(e=-e),t<0&&(t=-t),r<0&&(r=-r);let o=Math.floor(e),l=Math.floor(t),p=Math.floor(r),c=e-o,b=t-l,f=r-p,m,g,v=0,y=.5,i,a,d;for(let A=0;A<this._po;A++){let n=o+(l<<w)+(p<<I);m=_(c),g=_(b),i=this._p[n&s],i+=m*(this._p[n+1&s]-i),a=this._p[n+h&s],a+=m*(this._p[n+h+1&s]-a),i+=g*(a-i),n+=L,a=this._p[n&s],a+=m*(this._p[n+1&s]-a),d=this._p[n+h&s],d+=m*(this._p[n+h+1&s]-d),a+=g*(d-a),i+=_(f)*(a-i),v+=i*y,y*=this._pf,o<<=1,c*=2,l<<=1,b*=2,p<<=1,f*=2,c>=1&&(o++,c--),b>=1&&(l++,b--),f>=1&&(p++,f--)}return v}noiseDetail(e,t){e>0&&(this._po=e),t>0&&(this._pf=t)}noiseSeed(e=null){e!=null&&this._e.seed(e);let t=this._e.rand||Math.random;for(let r=0;r<s+1;r++)this._p[r]=t()}};globalThis.utils=Object.assign(globalThis.utils||{},E);})();
|
package/dist/tween.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var c=Object.defineProperty;var
|
|
1
|
+
(()=>{var c=Object.defineProperty;var b=(t,e)=>{for(var a in e)c(t,a,{get:e[a],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(t=!0)=>{for(let e in globalThis.utils)e!=="global"&&(t||globalThis[e]===void 0)&&(globalThis[e]=globalThis.utils[e])};var m={};b(m,{BACK_IN:()=>g,BACK_IN_OUT:()=>E,BACK_OUT:()=>T,BOUNCE_IN:()=>p,BOUNCE_IN_OUT:()=>v,BOUNCE_OUT:()=>o,EASE_IN:()=>h,EASE_IN_OUT:()=>_,EASE_OUT:()=>d,ELASTIC_IN:()=>w,ELASTIC_IN_OUT:()=>I,ELASTIC_OUT:()=>A,LINEAR:()=>l,tween:()=>f});var i=Math.PI/2,f=(t,e,a,r=1,s=l)=>new u(t,e,a,r,s),l=t=>t,h=t=>t*t,d=t=>-t*(t-2),_=t=>t<.5?2*t*t:-2*t*t+4*t-1,g=t=>t*t*t-t*Math.sin(t*Math.PI),T=t=>{let e=1-t;return 1-(e*e*e-e*Math.sin(e*Math.PI))},E=t=>{if(t<.5){let a=2*t;return .5*(a*a*a-a*Math.sin(a*Math.PI))}let e=1-(2*t-1);return .5*(1-(e*e*e-e*Math.sin(t*Math.PI)))+.5},w=t=>Math.sin(13*i*t)*Math.pow(2,10*(t-1)),A=t=>Math.sin(-13*i*(t+1))*Math.pow(2,-10*t)+1,I=t=>{if(t<.5){let r=Math.sin(13*i*(2*t)),s=Math.pow(2,10*(2*t-1));return .5*r*s}let e=Math.sin(-13*i*(2*t-1+1)),a=Math.pow(2,-10*(2*t-1));return .5*(e*a+2)},p=t=>1-o(1-t),o=t=>t<4/11?121*t*t/16:t<8/11?363/40*t*t-99/10*t+17/5:t<9/10?4356/361*t*t-35442/1805*t+16061/1805:54/5*t*t-513/25*t+268/25,v=t=>t<.5?.5*p(t*2):.5*o(t*2-1)+.5,u=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(e,a,r,s,n){this._o=e,this._p=a,this._x=r,this._d=s,this._e=n,this._w=0}start(e){if(this.running)return this;this._cu.stop(!1),this._ch=this._cu=this,this.running=!0;let a=this._o[this._p]||0,r=this._rel?a+this._x:this._x;return this._lc=this._lc||e||globalThis,this._u=this._lc.listen("update",s=>{if(this._t<=this._w){this._t+=s;return}let n=this._t-this._w;this._o[this._p]=this._lc.lerp(a,r,this._e(n/this._d)),this._t+=s,n>=this._d&&(this._o[this._p]=r,this.stop())}),this}stop(e=!0){if(!this._u)return this;if(this.running=!1,this._u(),this._t=0,e)for(let a of this._cb)a(this._o);return this}restart(e=null,a=!1){return this.stop(a).restart(e)}onEnd(e){return this._cb.push(e),this}chain(e){return this._ch.onEnd(()=>{this._cu=e.start(this._lc)}),this._ch=e,this}reset(){return this._cb.length=0,this.stop()}relative(e=!0){return this._rel=e,this}delay(e){return this._w=e,this}get current(){return this._cu}get progress(){return this.running&&this._t>this._w?(this._t-this._w)/this._d:0}};globalThis.utils=Object.assign(globalThis.utils||{},m);})();
|
package/dist/vector.js
CHANGED
|
@@ -173,9 +173,9 @@
|
|
|
173
173
|
a.y += (b.y - a.y) * t || 0;
|
|
174
174
|
return a;
|
|
175
175
|
};
|
|
176
|
-
var vecRand = (minlength = 1, maxlength = minlength,
|
|
177
|
-
const angle =
|
|
178
|
-
const radius =
|
|
176
|
+
var vecRand = (minlength = 1, maxlength = minlength, rng = globalThis.rand || Math.random) => {
|
|
177
|
+
const angle = rng() * PI2;
|
|
178
|
+
const radius = rng() * (maxlength - minlength) + minlength;
|
|
179
179
|
return vec(cos(angle) * radius, sin(angle) * radius);
|
|
180
180
|
};
|
|
181
181
|
var vecAbs = (v) => {
|
package/dist/vector.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var q=Object.defineProperty;var A=(t,e)=>{for(var o in e)q(t,o,{get:e[o],enumerable:!0})};var
|
|
1
|
+
(()=>{var q=Object.defineProperty;var A=(t,e)=>{for(var o in e)q(t,o,{get:e[o],enumerable:!0})};var h={};A(h,{DOWN:()=>K,LEFT:()=>Q,ONE:()=>k,RIGHT:()=>J,UP:()=>z,Vector:()=>s,ZERO:()=>T,vec:()=>r,vecAbs:()=>$,vecAdd:()=>u,vecAngle:()=>P,vecAngleBetween:()=>w,vecCeil:()=>j,vecClamp:()=>H,vecCross:()=>F,vecDist:()=>L,vecDist2:()=>N,vecDiv:()=>x,vecDot:()=>D,vecEq:()=>p,vecFloor:()=>B,vecIsZero:()=>W,vecLerp:()=>U,vecLimit:()=>b,vecMag:()=>g,vecMag2:()=>R,vecMove:()=>V,vecMult:()=>y,vecNorm:()=>a,vecRand:()=>Z,vecReflect:()=>O,vecRotate:()=>I,vecRound:()=>G,vecSet:()=>d,vecSetMag:()=>S,vecSub:()=>i});var C=Math.sqrt,M=Math.cos,f=Math.sin,E=2*Math.PI,s=class{x;y;constructor(e=0,o=e){this.x=e,this.y=o}toString(){return`Vector (${this.x}, ${this.y})`}},n=t=>t instanceof s,r=(t=0,e=t)=>(n(t)&&(e=t.y,t=t.x),new s(t,e)),p=(t,e,o=e)=>n(e)?p(t,e.x,e.y):t.x===e&&t.y===o,d=(t,e,o=e)=>(n(e)?d(t,e.x,e.y):(t.x=e,t.y=o),t),u=(t,e,o=e)=>n(e)?u(t,e.x,e.y):(t.x+=e,t.y+=o,t),i=(t,e,o=e)=>n(e)?i(t,e.x,e.y):(t.x-=e,t.y-=o,t),y=(t,e,o=e)=>n(e)?y(t,e.x,e.y):(t.x*=e,t.y*=o,t),x=(t,e,o=e)=>n(e)?x(t,e.x,e.y):(t.x/=e||1,t.y/=o||1,t),I=(t,e)=>{let o=M(e),c=f(e);return t.x=o*t.x-c*t.y,t.y=c*t.x+o*t.y,t},O=(t,e)=>{let o=a(r(e));return i(t,y(o,2*D(t,o)))},S=(t,e)=>(a(t),y(t,e),t),g=t=>Math.hypot(t.x,t.y),R=t=>t.x*t.x+t.y*t.y,a=t=>{let e=g(t);return e>0&&x(t,e),t},b=(t,e=1)=>{let o=R(t);return o>e*e&&(x(t,C(o)),y(t,e)),t},L=(t,e)=>Math.hypot(e.x-t.x,e.y-t.y),N=(t,e)=>{let o=t.x-e.x,c=t.y-e.y;return o*o+c*c},P=t=>Math.atan2(t.y,t.x),w=(t,e)=>Math.atan2(e.y-t.y,e.x-t.x),D=(t,e)=>t.x*e.x+t.y*e.y,F=(t,e)=>t.x*e.y-t.y*e.x,U=(t,e,o)=>(t.x+=(e.x-t.x)*o||0,t.y+=(e.y-t.y)*o||0,t),Z=(t=1,e=t,o=globalThis.rand||Math.random)=>{let c=o()*E,l=o()*(e-t)+t;return r(M(c)*l,f(c)*l)},$=t=>(t.x=Math.abs(t.x),t.y=Math.abs(t.y),t),j=t=>(t.x=Math.ceil(t.x),t.y=Math.ceil(t.y),t),B=t=>(t.x=Math.floor(t.x),t.y=Math.floor(t.y),t),G=t=>(t.x=Math.round(t.x),t.y=Math.round(t.y),t),H=(t,e,o)=>(t.x<e.x&&(t.x=e.x),t.x>o.x&&(t.x=o.x),t.y<e.y&&(t.y=e.y),t.y>o.y&&(t.y=o.y),t),V=(t,e,o=1)=>u(t,e.x*o,e.y*o),W=t=>p(t,T),T=r(0,0),k=r(1,1),z=r(0,-1),J=r(1,0),K=r(0,1),Q=r(-1,0);globalThis.utils=Object.assign(globalThis.utils||{},h);})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@litecanvas/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.30.0",
|
|
4
4
|
"description": "Utilities to help build litecanvas games",
|
|
5
5
|
"author": "Luiz Bills <luizbills@pm.me>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,6 +39,6 @@
|
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"esbuild": "^0.25.0",
|
|
41
41
|
"gzip-size": "^7.0.0",
|
|
42
|
-
"prettier": "^3.5.
|
|
42
|
+
"prettier": "^3.5.3"
|
|
43
43
|
}
|
|
44
44
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Collection utilities
|
|
2
|
+
|
|
3
|
+
**CDN**: https://unpkg.com/@litecanvas/utils/dist/collection.js
|
|
4
|
+
|
|
5
|
+
## head
|
|
6
|
+
|
|
7
|
+
Returns the first element of an array.
|
|
8
|
+
|
|
9
|
+
Syntax: `head(any[]): any`
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
import { head } from "@litecanvas/utils"
|
|
13
|
+
|
|
14
|
+
head([1, 2, 3]) // => 1
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## last
|
|
18
|
+
|
|
19
|
+
Returns the last element of an array.
|
|
20
|
+
|
|
21
|
+
Syntax: `last(any[]): any`
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import { last } from "@litecanvas/utils"
|
|
25
|
+
|
|
26
|
+
last([1, 2, 3]) // => 3
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## tail
|
|
30
|
+
|
|
31
|
+
Returns all but the first element of an array.
|
|
32
|
+
|
|
33
|
+
Syntax: `tail(any[]): any[]`
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import { tail } from "@litecanvas/utils"
|
|
37
|
+
|
|
38
|
+
tail([1, 2, 3]) // => [2, 3]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## range
|
|
42
|
+
|
|
43
|
+
Returns a sequence of numbers.
|
|
44
|
+
|
|
45
|
+
Syntax: `range(size: number, first?: number, step?: number): number[]`
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import { range } from "@litecanvas/utils"
|
|
49
|
+
|
|
50
|
+
// prints 0 1 2 3 4
|
|
51
|
+
for (let i of range(5)) {
|
|
52
|
+
console.log(i)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// prints -5 -4 -3 -2 -1
|
|
56
|
+
for (let i of range(5, -5)) {
|
|
57
|
+
console.log(i)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// prints 10 20 30 40 50 60 70 80 90 100
|
|
61
|
+
for (let i of range(10, 10, 10)) {
|
|
62
|
+
console.log(i)
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## sample
|
|
67
|
+
|
|
68
|
+
Return a random value of an array.
|
|
69
|
+
|
|
70
|
+
Syntax: `sample(values: any[], rng = Math.random): any`
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
import { sample } from "@litecanvas/utils"
|
|
74
|
+
|
|
75
|
+
const values = [0, 1, 2]
|
|
76
|
+
|
|
77
|
+
sample(values) // returns a value of this array
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## shuffle
|
|
81
|
+
|
|
82
|
+
Creates an new array of shuffled values.
|
|
83
|
+
|
|
84
|
+
Syntax: `sample(values: any[], rng = Math.random): any`
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
import { shuffle } from "@litecanvas/utils"
|
|
88
|
+
|
|
89
|
+
const values = [0, 1, 2, 3]
|
|
90
|
+
|
|
91
|
+
shuffle(values) // => [2, 0, 3, 1] (just a example of shuffled)
|
|
92
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { default as range } from "./range.js"
|
|
2
|
+
export { default as shuffle } from "./shuffle.js"
|
|
3
|
+
export { default as sample } from "./sample.js"
|
|
4
|
+
export { default as head } from "./head.js"
|
|
5
|
+
export { default as last } from "./last.js"
|
|
6
|
+
export { default as tail } from "./tail.js"
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a sequence of numbers.
|
|
3
|
+
*
|
|
4
|
+
* @param {number} size the amount of numbers
|
|
5
|
+
* @param {number} [from] the first value (default = 0)
|
|
6
|
+
* @param {number} [step] the value that should be incremented (default = 1)
|
|
7
|
+
* @returns {number[]}
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // print 0 1 2 3 4
|
|
11
|
+
* for(let i of range(5)) {
|
|
12
|
+
* console.log(i)
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* // print 10 15 20 25 30
|
|
16
|
+
* for(let i of range(5, 10, 5)) {
|
|
17
|
+
* console.log(i)
|
|
18
|
+
* }
|
|
19
|
+
*/
|
|
20
|
+
export default (size, from = 0, step = 1) =>
|
|
21
|
+
Array.from(
|
|
22
|
+
Array(size)
|
|
23
|
+
.keys()
|
|
24
|
+
.map((i) => {
|
|
25
|
+
return from + step * i
|
|
26
|
+
})
|
|
27
|
+
)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an new array of shuffled values
|
|
3
|
+
*
|
|
4
|
+
* @param {number[]} values
|
|
5
|
+
* @param {()=>number} [rng]
|
|
6
|
+
* @returns {number[]} the same array but shuffled
|
|
7
|
+
*/
|
|
8
|
+
export default (values, rng = globalThis.rand || Math.random) => {
|
|
9
|
+
values = Array.from(values)
|
|
10
|
+
for (let i = values.length - 1; i > 0; i--) {
|
|
11
|
+
let j = Math.floor(rng() * (i + 1))
|
|
12
|
+
let temp = values[i]
|
|
13
|
+
values[i] = values[j]
|
|
14
|
+
values[j] = temp
|
|
15
|
+
}
|
|
16
|
+
return values
|
|
17
|
+
}
|
package/src/image/_web.js
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
import "../_global.js"
|
|
2
|
-
import
|
|
3
|
-
import scaleImage from "./scale.js"
|
|
4
|
-
import flipImage from "./flip.js"
|
|
2
|
+
import * as imageUtils from "./index.js"
|
|
5
3
|
|
|
6
|
-
globalThis.utils = Object.assign(globalThis.utils || {},
|
|
7
|
-
tint,
|
|
8
|
-
scaleImage,
|
|
9
|
-
flipImage,
|
|
10
|
-
})
|
|
4
|
+
globalThis.utils = Object.assign(globalThis.utils || {}, imageUtils)
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { default as flipImage } from "./flip.js"
|
|
2
|
+
export { default as scaleImage } from "./scale.js"
|
|
3
|
+
export { default as tintImage } from "./tint.js"
|
|
4
|
+
export { default as makeCircle } from "./make-circle.js"
|
|
5
|
+
export { default as makeRectangle } from "./make-rectangle.js"
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import "litecanvas"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates an image with the geometric shape of a circle with an optional border.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} radius
|
|
7
|
+
* @param {number} color
|
|
8
|
+
* @param {object} [options]
|
|
9
|
+
* @param {number} [options.borderWidth]
|
|
10
|
+
* @param {number} [options.borderColor]
|
|
11
|
+
* @param {LitecanvasInstance} [options.engine]
|
|
12
|
+
* @returns {OffscreenCanvas} the shape image
|
|
13
|
+
*/
|
|
14
|
+
export default (
|
|
15
|
+
radius,
|
|
16
|
+
color,
|
|
17
|
+
{ borderWidth = 0, borderColor = 0, engine = globalThis } = {}
|
|
18
|
+
) => {
|
|
19
|
+
const imageSize = radius * 2 + borderWidth
|
|
20
|
+
return engine.paint(imageSize, imageSize, () => {
|
|
21
|
+
engine.circfill(imageSize / 2, imageSize / 2, radius, color)
|
|
22
|
+
if (borderWidth > 0) {
|
|
23
|
+
engine.linewidth(borderWidth)
|
|
24
|
+
engine.stroke(borderColor)
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import "litecanvas"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates an image with the geometric shape of a rectangle with an optional border.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} width
|
|
7
|
+
* @param {number} height
|
|
8
|
+
* @param {number} color
|
|
9
|
+
* @param {object} [options]
|
|
10
|
+
* @param {number} [options.borderWidth]
|
|
11
|
+
* @param {number} [options.borderColor]
|
|
12
|
+
* @param {LitecanvasInstance} [options.engine]
|
|
13
|
+
* @returns {OffscreenCanvas} the shape image
|
|
14
|
+
*/
|
|
15
|
+
export default (
|
|
16
|
+
width,
|
|
17
|
+
height,
|
|
18
|
+
color,
|
|
19
|
+
{ borderWidth = 0, borderColor = 0, engine = globalThis } = {}
|
|
20
|
+
) => {
|
|
21
|
+
const imageWidth = width + borderWidth * 2
|
|
22
|
+
const imageHeight = height + borderWidth * 2
|
|
23
|
+
|
|
24
|
+
return engine.paint(imageWidth, imageHeight, () => {
|
|
25
|
+
engine.rectfill(
|
|
26
|
+
borderWidth > 0 ? borderWidth : 0,
|
|
27
|
+
borderWidth > 0 ? borderWidth : 0,
|
|
28
|
+
width,
|
|
29
|
+
height,
|
|
30
|
+
color
|
|
31
|
+
)
|
|
32
|
+
if (borderWidth > 0) {
|
|
33
|
+
engine.linewidth(borderWidth)
|
|
34
|
+
engine.stroke(borderColor)
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
}
|
package/src/index.js
CHANGED
|
@@ -7,6 +7,5 @@ export * from "./actor/index.js"
|
|
|
7
7
|
export * from "./math/index.js"
|
|
8
8
|
export * from "./tween/index.js"
|
|
9
9
|
export * from "./noise/index.js"
|
|
10
|
-
export
|
|
11
|
-
export
|
|
12
|
-
export { default as flipImage } from "./image/flip.js"
|
|
10
|
+
export * from "./image/index.js"
|
|
11
|
+
export * from "./collection/index.js"
|
package/src/math/README.md
CHANGED
|
@@ -99,28 +99,52 @@ function draw() {
|
|
|
99
99
|
|
|
100
100
|
[See in playground](https://litecanvas.js.org?c=eJxVjjELwjAQhff%2BihtTCKWCg0tdtKCLgxTEMV5SCYS0NGlUpP%2FdS1NQhxvue7yPN3ptXHE33U0YlmdGe4XCBuHoydrRotedBW21Zzm8M4C%2Bc1BBUMhKDrv61NTna048KPPlZSQCcSGrMrHpRzn2UnjFpE9aIYOwqBjpeXTxWOdAMYW6hRgUT9jC5bhvDqkzjyFYQUnf9KeXg3gsi9E4Ng9CPWDy8Ln54rDe0NGwDwbOS1Q%3D)
|
|
101
101
|
|
|
102
|
-
##
|
|
102
|
+
## mod
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
Math modulus (always returns a positive number).
|
|
105
105
|
|
|
106
|
-
Syntax: `
|
|
106
|
+
Syntax: `mod(dividend: number, divisor: number): number`
|
|
107
107
|
|
|
108
108
|
```js
|
|
109
|
-
import {
|
|
109
|
+
import { mod } from "@litecanvas/utils"
|
|
110
110
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
console.log(i)
|
|
114
|
-
}
|
|
111
|
+
mod(17, 5) // => 2
|
|
112
|
+
mod(-17, 5) // => 3
|
|
115
113
|
```
|
|
116
114
|
|
|
117
|
-
##
|
|
115
|
+
## mean
|
|
118
116
|
|
|
119
|
-
|
|
117
|
+
Computes the mean of the values in a array.
|
|
120
118
|
|
|
121
119
|
```js
|
|
122
|
-
import {
|
|
120
|
+
import { mean } from "@litecanvas/utils"
|
|
123
121
|
|
|
124
|
-
|
|
125
|
-
|
|
122
|
+
Syntax: `mean(values: number[]): number`
|
|
123
|
+
|
|
124
|
+
mean([10, 5, 3]) // => 6 or (10 + 5 + 3) / 3
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## sum
|
|
128
|
+
|
|
129
|
+
Computes the sum of the values in a array.
|
|
130
|
+
|
|
131
|
+
Syntax: `sum(values: number[]): number`
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
import { sum } from "@litecanvas/utils"
|
|
135
|
+
|
|
136
|
+
sum([10, 5, 3]) // => 18 or (10 + 5 + 3)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## lerpAngle
|
|
140
|
+
|
|
141
|
+
Calculates the linear interpolation of two angles (in degrees).
|
|
142
|
+
|
|
143
|
+
Syntax: `lerpAngle(start: number, end: number, amount: number): number`
|
|
144
|
+
|
|
145
|
+
```js
|
|
146
|
+
import { lerpAngle } from "@litecanvas/utils"
|
|
147
|
+
|
|
148
|
+
lerpAngle(0, 90, 0.5) // => 45
|
|
149
|
+
lerpAngle(0, 270, 0.5) // => -45
|
|
126
150
|
```
|
package/src/math/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export { default as diff } from "./diff.js"
|
|
2
2
|
export { default as wave } from "./wave.js"
|
|
3
3
|
export { default as fract } from "./fract.js"
|
|
4
|
-
export { default as range } from "./range.js"
|
|
5
4
|
export { default as advance } from "./advance.js"
|
|
6
5
|
export { default as mod } from "./mod.js"
|
|
7
6
|
export { default as roundd } from "./roundd.js"
|
|
8
7
|
export { default as dist } from "./dist.js"
|
|
9
8
|
export { default as mag } from "./mag.js"
|
|
9
|
+
export { default as mean } from "./mean.js"
|
|
10
|
+
export { default as sum } from "./sum.js"
|
|
11
|
+
export { default as lerpAngle } from "./lerp-angle.js"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the linear interpolation of two angles (in degrees).
|
|
3
|
+
*
|
|
4
|
+
* @param {number} start from angle
|
|
5
|
+
* @param {number} end to angle
|
|
6
|
+
* @param {number} amount usually a value between 0.0 and 1.0
|
|
7
|
+
* @returns {number} the interpolated value
|
|
8
|
+
*/
|
|
9
|
+
export default (start, end, amount) => {
|
|
10
|
+
let dif = (end - start) % 360
|
|
11
|
+
|
|
12
|
+
if (dif > 180) {
|
|
13
|
+
dif -= 360
|
|
14
|
+
} else if (dif < -180) {
|
|
15
|
+
dif += 360
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return start + dif * amount
|
|
19
|
+
}
|
package/src/math/mean.js
ADDED
package/src/math/sum.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Computes the sum of the values in a array.
|
|
3
|
+
*
|
|
4
|
+
* @param {number[]} values list of numbers
|
|
5
|
+
* @returns {number}
|
|
6
|
+
*/
|
|
7
|
+
export default (values) => {
|
|
8
|
+
let result = 0
|
|
9
|
+
for (let i = 0; i < values.length; i++) {
|
|
10
|
+
result += values[i]
|
|
11
|
+
}
|
|
12
|
+
return result
|
|
13
|
+
}
|
package/src/vector/index.js
CHANGED
|
@@ -345,10 +345,10 @@ export const vecLerp = (a, b, t) => {
|
|
|
345
345
|
export const vecRand = (
|
|
346
346
|
minlength = 1,
|
|
347
347
|
maxlength = minlength,
|
|
348
|
-
|
|
348
|
+
rng = globalThis.rand || Math.random
|
|
349
349
|
) => {
|
|
350
|
-
const angle =
|
|
351
|
-
const radius =
|
|
350
|
+
const angle = rng() * PI2
|
|
351
|
+
const radius = rng() * (maxlength - minlength) + minlength
|
|
352
352
|
return vec(cos(angle) * radius, sin(angle) * radius)
|
|
353
353
|
}
|
|
354
354
|
|
package/src/math/range.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns a sequence of numbers from `0` to `size - 1`.
|
|
3
|
-
*
|
|
4
|
-
* @param {number} size the amount of numbers
|
|
5
|
-
* @returns {number[]}
|
|
6
|
-
* @example
|
|
7
|
-
* // print 0 1 2 3 4
|
|
8
|
-
* for(let i of range(5)) console.log(i)
|
|
9
|
-
*/
|
|
10
|
-
export default (size) => Array.from(Array(size).keys())
|