@litecanvas/utils 0.11.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/all.js +13 -9
- package/dist/all.min.js +2 -2
- package/dist/math.js +19 -5
- package/dist/math.min.js +1 -1
- package/package.json +1 -1
- package/src/index.js +1 -3
- package/src/math/_web.js +2 -8
- package/src/math/diff.js +3 -0
- package/src/math/fract.js +2 -0
- package/src/math/index.js +4 -0
- package/src/math/range.js +10 -0
package/dist/all.js
CHANGED
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
fract: () => fract_default,
|
|
38
38
|
intersection: () => intersection_default,
|
|
39
39
|
isvector: () => isvector,
|
|
40
|
+
range: () => range_default,
|
|
40
41
|
resolve: () => resolve_default,
|
|
41
42
|
vec: () => vec,
|
|
42
43
|
vecadd: () => vecadd,
|
|
@@ -62,15 +63,6 @@
|
|
|
62
63
|
wave: () => wave_default
|
|
63
64
|
});
|
|
64
65
|
|
|
65
|
-
// src/math/diff.js
|
|
66
|
-
var diff_default = (a, b) => Math.abs(b - a) || 0;
|
|
67
|
-
|
|
68
|
-
// src/math/fract.js
|
|
69
|
-
var fract_default = (value) => value % 1 || 0;
|
|
70
|
-
|
|
71
|
-
// src/math/wave.js
|
|
72
|
-
var wave_default = (lower, higher, t, fn = Math.sin) => lower + (fn(t) + 1) / 2 * (higher - lower);
|
|
73
|
-
|
|
74
66
|
// src/collision/intersection.js
|
|
75
67
|
var intersection_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
76
68
|
const left = Math.max(x1, x2);
|
|
@@ -768,6 +760,18 @@
|
|
|
768
760
|
}
|
|
769
761
|
};
|
|
770
762
|
|
|
763
|
+
// src/math/diff.js
|
|
764
|
+
var diff_default = (a, b) => Math.abs(b - a) || 0;
|
|
765
|
+
|
|
766
|
+
// src/math/wave.js
|
|
767
|
+
var wave_default = (lower, higher, t, fn = Math.sin) => lower + (fn(t) + 1) / 2 * (higher - lower);
|
|
768
|
+
|
|
769
|
+
// src/math/fract.js
|
|
770
|
+
var fract_default = (value) => value % 1 || 0;
|
|
771
|
+
|
|
772
|
+
// src/math/range.js
|
|
773
|
+
var range_default = (n) => Array.from(Array(n).keys());
|
|
774
|
+
|
|
771
775
|
// src/_web.js
|
|
772
776
|
globalThis.utils = Object.assign(globalThis.utils || {}, src_exports);
|
|
773
777
|
})();
|
package/dist/all.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(()=>{var
|
|
2
|
-
`)}},w=class e extends x{constructor(t,s,i=Uint8Array){super(t,s,null),this._c=new i(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,i,r)=>{t.set(s,i,r)}),t}};function
|
|
1
|
+
(()=>{var B=Object.defineProperty;var F=(e,t)=>{for(var s in t)B(e,s,{get:t[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let e in globalThis.utils)e!=="global"&&(globalThis[e]=globalThis.utils[e])};var M={};F(M,{ANCHOR_BOT_LEFT:()=>at,ANCHOR_BOT_RIGHT:()=>ht,ANCHOR_CENTER:()=>rt,ANCHOR_TOP_LEFT:()=>Y,ANCHOR_TOP_RIGHT:()=>nt,Actor:()=>b,Camera:()=>p,DOWN:()=>it,Grid:()=>x,LEFT:()=>ot,ONE:()=>tt,RIGHT:()=>st,TypedGrid:()=>w,UP:()=>et,Vector:()=>u,ZERO:()=>Q,diff:()=>R,fract:()=>L,intersection:()=>g,isvector:()=>l,range:()=>v,resolve:()=>P,vec:()=>n,vecadd:()=>k,vecconfig:()=>H,veccopy:()=>E,veccross:()=>Z,vecdir:()=>V,vecdist:()=>q,vecdist2:()=>N,vecdiv:()=>_,vecdot:()=>U,veceq:()=>O,veclerp:()=>J,veclimit:()=>$,vecmag:()=>z,vecmag2:()=>S,vecmult:()=>I,vecnorm:()=>j,vecrand:()=>K,vecrot:()=>G,vecset:()=>C,vecsub:()=>X,wave:()=>W});var g=(e,t,s,i,r,o,a,h)=>{let c=Math.max(e,r),A=Math.min(e+s,r+a)-c,d=Math.max(t,o),m=Math.min(t+i,o+h)-d;return[c,d,A,m]};var P=(e,t,s,i,r,o,a,h)=>{let[c,A,d,m]=g(e,t,s,i,r,o,a,h),f="",y=e,T=t;return d<m?e<r?(f="right",y=r-s):(f="left",y=r+a):t<o?(f="bottom",T=o-i):(f="top",T=o+h),{direction:f,x:y,y:T}};var p=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,s=0,i=0,r=null,o=null){this._engine=t||globalThis,this.ox=s,this.oy=i,this.resize(r||this._engine.WIDTH-s,o||this._engine.HEIGHT-i),this.x=this.width/2,this.y=this.height/2}resize(t,s){this.width=t,this.height=s,this._engine.emit("camera-resized",this)}start(t=!1){this._engine.push(),t&&this._engine.cliprect(this.ox,this.oy,this.width,this.height);let s=this.ox+this.width/2,i=this.oy+this.height/2;this._engine.translate(s,i),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,i={}){let r=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,i.x=r*t-o*s+this.x,i.y=o*t+r*s+this.y,i}getCameraPoint(t,s,i={}){let r=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=t-this.x,s=s-this.y,t=r*t-o*s,s=o*t+r*s,i.x=t*this.scale+this.width/2+this.ox,i.y=s*this.scale+this.height/2+this.oy,i}getBounds(){return[this.ox,this.oy,this.width,this.height]}viewing(t,s,i,r){let o=this.width/2-this.x,a=this.height/2-this.y,h=this.width/this.scale,c=this.height/this.scale;return this._engine.colrect(t,s,i,r,o,a,h,c)}shake(t=1,s=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",i=>{this._shake.x=this._engine.randi(-t,t),this._shake.y=this._engine.randi(-t,t),s-=i,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 x=class e{_w;_h;_c;constructor(t,s,i=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~s),this._c=i}clear(){this.forEach((t,s)=>this.set(t,s,void 0))}get width(){return this._w}get height(){return this._h}set(t,s,i){this._c[this.pointToIndex(t,s)]=i}get(t,s){return this._c[this.pointToIndex(t,s)]}has(t,s){return this.get(t,s)!=null}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 i=s?this.length-1:0,r=s?-1:this.length,o=s?-1:1;for(;i!==r;){let a=this.indexToPointX(i),h=this.indexToPointY(i),c=this._c[i];if(t(a,h,c,this)===!1)break;i+=o}}fill(t){this.forEach((s,i)=>{this.set(s,i,t)})}clone(){return e.fromArray(this._w,this._h,this._c)}clampX(t){return D(t,0,this._w-1)}clampY(t){return D(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",s=!0){if(!s)return this._c.join(t);let i=[];return this.forEach((r,o,a)=>{i[o]=i[o]||"",i[o]+=a+t}),i.join(`
|
|
2
|
+
`)}},w=class e extends x{constructor(t,s,i=Uint8Array){super(t,s,null),this._c=new i(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,i,r)=>{t.set(s,i,r)}),t}};function D(e,t,s){return e<t?t:e>s?s:e}var u=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},n=(e=0,t=e)=>new u(e,t),O=(e,t,s=t)=>l(t)?O(e,t.x,t.y):e.x===t&&e.y===s,E=e=>n(e.x,e.y),C=(e,t,s=t)=>{l(t)?C(e,t.x,t.y):(e.x=t,e.y=s)},k=(e,t,s=t)=>{l(t)?k(e,t.x,t.y):(e.x+=t,e.y+=s)},X=(e,t,s=t)=>{l(t)?X(e,t.x,t.y):(e.x-=t,e.y-=s)},I=(e,t,s=t)=>{l(t)?I(e,t.x,t.y):(e.x*=t,e.y*=s)},_=(e,t,s=t)=>{l(t)?_(e,t.x,t.y):(e.x/=t,e.y/=s)},G=(e,t)=>{let s=Math.cos(t),i=Math.sin(t);e.x=s*e.x-i*e.y,e.y=i*e.x+s*e.y},z=e=>Math.sqrt(e.x*e.x+e.y*e.y),S=e=>e.x*e.x+e.y*e.y,j=e=>{let t=z(e);t>0&&_(e,t)},$=(e,t)=>{let s=S(e);s>t*t&&(_(e,Math.sqrt(s)),I(e,t))},q=(e,t)=>{let s=e.x-t.x,i=e.y-t.y;return Math.sqrt(s*s+i*i)},N=(e,t)=>{let s=e.x-t.x,i=e.y-t.y;return s*s+i*i},V=e=>Math.atan2(e.y,e.x),U=(e,t)=>e.x*t.x+e.y*t.y,Z=(e,t)=>e.x*t.y-e.y*t.x,J=(e,t,s)=>{e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0},K=(e=1,t=e)=>{let s=H.random()*2*Math.PI,i=H.random()*(t-e)+e;return n(Math.cos(s)*i,Math.sin(s)*i)},l=e=>e instanceof u,H={random:()=>globalThis.rand?rand():Math.random()},Q=n(0,0),tt=n(1,1),et=n(0,-1),st=n(1,0),it=n(0,1),ot=n(-1,0);var rt=n(.5,.5),Y=n(0,0),nt=n(1,0),at=n(0,1),ht=n(1,1),b=class{sprite;pos;_o;_s;angle=0;opacity=1;hidden=!1;constructor(t,s,i=Y){this.sprite=t,this.pos=s||n(0),this._o=E(i),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}getBounds(t=!0){let s=this.sprite.width*(t?this._s.x:1),i=this.sprite.height*(t?this._s.y:1),r=this.pos.x-s*this.anchor.x,o=this.pos.y-i*this.anchor.y;return[r,o,s,i]}get scale(){return this._s}draw(t=globalThis){this.hidden||this.opacity<=0||(t.push(),this.transform(t),this.drawImage(t),t.pop())}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(this.angle),t.scale(this._s.x,this._s.y)}drawImage(t){let s=this.sprite.width*this.anchor.x,i=this.sprite.height*this.anchor.y;t.alpha(this.opacity),t.image(-s,-i,this.sprite)}};var R=(e,t)=>Math.abs(t-e)||0;var W=(e,t,s,i=Math.sin)=>e+(i(s)+1)/2*(t-e);var L=e=>e%1||0;var v=e=>Array.from(Array(e).keys());globalThis.utils=Object.assign(globalThis.utils||{},M);})();
|
package/dist/math.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 = () => {
|
|
@@ -8,6 +14,15 @@
|
|
|
8
14
|
}
|
|
9
15
|
};
|
|
10
16
|
|
|
17
|
+
// src/math/index.js
|
|
18
|
+
var math_exports = {};
|
|
19
|
+
__export(math_exports, {
|
|
20
|
+
diff: () => diff_default,
|
|
21
|
+
fract: () => fract_default,
|
|
22
|
+
range: () => range_default,
|
|
23
|
+
wave: () => wave_default
|
|
24
|
+
});
|
|
25
|
+
|
|
11
26
|
// src/math/diff.js
|
|
12
27
|
var diff_default = (a, b) => Math.abs(b - a) || 0;
|
|
13
28
|
|
|
@@ -17,10 +32,9 @@
|
|
|
17
32
|
// src/math/fract.js
|
|
18
33
|
var fract_default = (value) => value % 1 || 0;
|
|
19
34
|
|
|
35
|
+
// src/math/range.js
|
|
36
|
+
var range_default = (n) => Array.from(Array(n).keys());
|
|
37
|
+
|
|
20
38
|
// src/math/_web.js
|
|
21
|
-
globalThis.utils = Object.assign(globalThis.utils || {},
|
|
22
|
-
diff: diff_default,
|
|
23
|
-
wave: wave_default,
|
|
24
|
-
fract: fract_default
|
|
25
|
-
});
|
|
39
|
+
globalThis.utils = Object.assign(globalThis.utils || {}, math_exports);
|
|
26
40
|
})();
|
package/dist/math.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let l in globalThis.utils)l!=="global"&&(globalThis[l]=globalThis.utils[l])};var
|
|
1
|
+
(()=>{var u=Object.defineProperty;var b=(l,a)=>{for(var t in a)u(l,t,{get:a[t],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let l in globalThis.utils)l!=="global"&&(globalThis[l]=globalThis.utils[l])};var o={};b(o,{diff:()=>s,fract:()=>r,range:()=>e,wave:()=>i});var s=(l,a)=>Math.abs(a-l)||0;var i=(l,a,t,f=Math.sin)=>l+(f(t)+1)/2*(a-l);var r=l=>l%1||0;var e=l=>Array.from(Array(l).keys());globalThis.utils=Object.assign(globalThis.utils||{},o);})();
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
export { default as diff } from "./math/diff.js"
|
|
2
|
-
export { default as fract } from "./math/fract.js"
|
|
3
|
-
export { default as wave } from "./math/wave.js"
|
|
4
1
|
export { default as resolve } from "./collision/resolve.js"
|
|
5
2
|
export { default as intersection } from "./collision/intersection.js"
|
|
6
3
|
export { default as Camera } from "./camera/index.js"
|
|
7
4
|
export * from "./grid/index.js"
|
|
8
5
|
export * from "./vector/index.js"
|
|
9
6
|
export * from "./actor/index.js"
|
|
7
|
+
export * from "./math/index.js"
|
package/src/math/_web.js
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
import "../_global.js"
|
|
2
|
-
import
|
|
3
|
-
import wave from "./wave.js"
|
|
4
|
-
import fract from "./fract.js"
|
|
2
|
+
import * as mathUtils from "./index.js"
|
|
5
3
|
|
|
6
|
-
globalThis.utils = Object.assign(globalThis.utils || {},
|
|
7
|
-
diff,
|
|
8
|
-
wave,
|
|
9
|
-
fract,
|
|
10
|
-
})
|
|
4
|
+
globalThis.utils = Object.assign(globalThis.utils || {}, mathUtils)
|
package/src/math/diff.js
CHANGED
package/src/math/fract.js
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a sequence of numbers from 0 to n-1.
|
|
3
|
+
*
|
|
4
|
+
* @param {number} n the amount of numbers
|
|
5
|
+
* @returns {number[]}
|
|
6
|
+
* @example
|
|
7
|
+
* // print 0 1 2 3 4 5
|
|
8
|
+
* for(let i of range(5)) console.log(i)
|
|
9
|
+
*/
|
|
10
|
+
export default (n) => Array.from(Array(n).keys())
|