@devrals/math 0.1.2 → 0.2.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/math.js +61 -9
- package/dist/math.umd.cjs +1 -1
- package/dist/src/functions.d.ts +12 -0
- package/dist/src/types.d.ts +11 -0
- package/dist/src/vectors.d.ts +93 -13
- package/package.json +3 -3
package/dist/math.js
CHANGED
|
@@ -6,9 +6,12 @@ var e = class e {
|
|
|
6
6
|
static initial(t) {
|
|
7
7
|
return new e(t, t);
|
|
8
8
|
}
|
|
9
|
-
static
|
|
9
|
+
static zero() {
|
|
10
10
|
return new e(0, 0);
|
|
11
11
|
}
|
|
12
|
+
static one() {
|
|
13
|
+
return new e(1, 1);
|
|
14
|
+
}
|
|
12
15
|
add(e) {
|
|
13
16
|
return typeof e == "number" ? this.x += e : this.x += e.x, typeof e == "number" ? this.y += e : this.y += e.y, this;
|
|
14
17
|
}
|
|
@@ -21,6 +24,12 @@ var e = class e {
|
|
|
21
24
|
div(e) {
|
|
22
25
|
return typeof e == "number" ? this.x /= e : this.x /= e.x, typeof e == "number" ? this.y /= e : this.y /= e.y, this;
|
|
23
26
|
}
|
|
27
|
+
dot(e) {
|
|
28
|
+
return this.x * e.x + this.y * e.y;
|
|
29
|
+
}
|
|
30
|
+
cross(e) {
|
|
31
|
+
return this.x * e.y - this.y * e.x;
|
|
32
|
+
}
|
|
24
33
|
map(e) {
|
|
25
34
|
return this.x = e(this.x), this.y = e(this.y), this;
|
|
26
35
|
}
|
|
@@ -31,18 +40,38 @@ var e = class e {
|
|
|
31
40
|
0
|
|
32
41
|
];
|
|
33
42
|
}
|
|
34
|
-
|
|
43
|
+
length() {
|
|
44
|
+
return Math.hypot(this.x, this.y);
|
|
45
|
+
}
|
|
46
|
+
lengthSquared() {
|
|
47
|
+
return this.x * this.x + this.y * this.y;
|
|
48
|
+
}
|
|
49
|
+
normalize() {
|
|
50
|
+
let e = this.length();
|
|
51
|
+
return e !== 0 && this.div(e), this;
|
|
52
|
+
}
|
|
53
|
+
distance(e) {
|
|
54
|
+
let t = e.x - this.x, n = e.y - this.y;
|
|
55
|
+
return Math.hypot(t, n);
|
|
56
|
+
}
|
|
57
|
+
clone() {
|
|
58
|
+
return new e(this.x, this.y);
|
|
59
|
+
}
|
|
60
|
+
}, t = class e {
|
|
35
61
|
constructor(e, t, n) {
|
|
36
|
-
|
|
62
|
+
this.x = e, this.y = t, this.z = n;
|
|
37
63
|
}
|
|
38
|
-
static initial(
|
|
39
|
-
return new t
|
|
64
|
+
static initial(t) {
|
|
65
|
+
return new e(t, t, t);
|
|
40
66
|
}
|
|
41
|
-
static
|
|
42
|
-
return
|
|
67
|
+
static zero() {
|
|
68
|
+
return e.initial(0);
|
|
43
69
|
}
|
|
44
|
-
static
|
|
45
|
-
return
|
|
70
|
+
static one() {
|
|
71
|
+
return e.initial(1);
|
|
72
|
+
}
|
|
73
|
+
static fromHex(t) {
|
|
74
|
+
return new e(parseInt(t.slice(0, 2), 16), parseInt(t.slice(2, 4), 16), parseInt(t.slice(4, 6), 16));
|
|
46
75
|
}
|
|
47
76
|
add(e) {
|
|
48
77
|
return typeof e == "number" ? this.x += e : this.x += e.x, typeof e == "number" ? this.y += e : this.y += e.y, typeof e == "number" ? this.z += e : this.z += e.z, this;
|
|
@@ -56,6 +85,12 @@ var e = class e {
|
|
|
56
85
|
div(e) {
|
|
57
86
|
return typeof e == "number" ? this.x /= e : this.x /= e.x, typeof e == "number" ? this.y /= e : this.y /= e.y, typeof e == "number" ? this.z /= e : this.z /= e.z, this;
|
|
58
87
|
}
|
|
88
|
+
dot(e) {
|
|
89
|
+
return this.x * e.x + this.y * e.y + this.z * e.z;
|
|
90
|
+
}
|
|
91
|
+
cross(t) {
|
|
92
|
+
return new e(this.y * t.z - this.z * t.y, this.z * t.x - this.x * t.z, this.x * t.y - this.y * t.x);
|
|
93
|
+
}
|
|
59
94
|
map(e) {
|
|
60
95
|
return this.x = e(this.x), this.y = e(this.y), this.z = e(this.z), this;
|
|
61
96
|
}
|
|
@@ -66,6 +101,23 @@ var e = class e {
|
|
|
66
101
|
this.z
|
|
67
102
|
];
|
|
68
103
|
}
|
|
104
|
+
length() {
|
|
105
|
+
return Math.hypot(this.x, this.y, this.z);
|
|
106
|
+
}
|
|
107
|
+
lengthSquared() {
|
|
108
|
+
return this.x * this.x + this.y * this.y + this.z * this.z;
|
|
109
|
+
}
|
|
110
|
+
normalize() {
|
|
111
|
+
let e = this.length();
|
|
112
|
+
return e !== 0 && this.div(e), this;
|
|
113
|
+
}
|
|
114
|
+
distance(e) {
|
|
115
|
+
let t = e.x - this.x, n = e.y - this.y, r = e.z - this.z;
|
|
116
|
+
return Math.hypot(t, n, r);
|
|
117
|
+
}
|
|
118
|
+
clone() {
|
|
119
|
+
return new e(this.x, this.y, this.z);
|
|
120
|
+
}
|
|
69
121
|
}, n = (e) => e[Math.floor(Math.random() * e.length)], r = (e, t) => (e % t + t) % t, i = (e, t) => e + Math.random() * (t - e), a = (e, t, n) => Math.max(Math.min(e, n), t), o = (e, t, n) => e * (1 - n) + t * n, s = (e, t, n) => e < t ? Math.min(e + n, t) : Math.max(e - n, t), c = (n, r) => "z" in n && "z" in r ? new t(n.x + r.x, n.y + n.y, n.z + n.z) : new e(n.x + r.x, n.y + n.y), l = (n, r) => "z" in n && "z" in r ? new t(n.x - r.x, n.y - n.y, n.z - n.z) : new e(n.x - r.x, n.y - n.y), u = (n, r) => "z" in n && "z" in r ? new t(n.x * r.x, n.y * n.y, n.z * n.z) : new e(n.x * r.x, n.y * n.y), d = (n, r) => "z" in n && "z" in r ? new t(n.x / r.x, n.y / n.y, n.z / n.z) : new e(n.x / r.x, n.y / n.y);
|
|
70
122
|
//#endregion
|
|
71
123
|
export { e as Vec2, t as Vec3, c as add, s as approach, n as chooseRand, a as clamp, d as div, o as lerp, r as mod, u as mul, i as randRange, l as sub };
|
package/dist/math.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.index={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=class e{constructor(e,t){this.x=e,this.y=t}static initial(t){return new e(t,t)}static
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.index={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=class e{constructor(e,t){this.x=e,this.y=t}static initial(t){return new e(t,t)}static zero(){return new e(0,0)}static one(){return new e(1,1)}add(e){return typeof e==`number`?this.x+=e:this.x+=e.x,typeof e==`number`?this.y+=e:this.y+=e.y,this}sub(e){return typeof e==`number`?this.x-=e:this.x-=e.x,typeof e==`number`?this.y-=e:this.y-=e.y,this}mul(e){return typeof e==`number`?this.x*=e:this.x*=e.x,typeof e==`number`?this.y*=e:this.y*=e.y,this}div(e){return typeof e==`number`?this.x/=e:this.x/=e.x,typeof e==`number`?this.y/=e:this.y/=e.y,this}dot(e){return this.x*e.x+this.y*e.y}cross(e){return this.x*e.y-this.y*e.x}map(e){return this.x=e(this.x),this.y=e(this.y),this}toArr(){return[this.x,this.y,0]}length(){return Math.hypot(this.x,this.y)}lengthSquared(){return this.x*this.x+this.y*this.y}normalize(){let e=this.length();return e!==0&&this.div(e),this}distance(e){let t=e.x-this.x,n=e.y-this.y;return Math.hypot(t,n)}clone(){return new e(this.x,this.y)}},n=class e{constructor(e,t,n){this.x=e,this.y=t,this.z=n}static initial(t){return new e(t,t,t)}static zero(){return e.initial(0)}static one(){return e.initial(1)}static fromHex(t){return new e(parseInt(t.slice(0,2),16),parseInt(t.slice(2,4),16),parseInt(t.slice(4,6),16))}add(e){return typeof e==`number`?this.x+=e:this.x+=e.x,typeof e==`number`?this.y+=e:this.y+=e.y,typeof e==`number`?this.z+=e:this.z+=e.z,this}sub(e){return typeof e==`number`?this.x-=e:this.x-=e.x,typeof e==`number`?this.y-=e:this.y-=e.y,typeof e==`number`?this.z-=e:this.z-=e.z,this}mul(e){return this.x*=typeof e==`number`?e:e.x,this.y*=typeof e==`number`?e:e.y,this.z*=typeof e==`number`?e:e.z,this}div(e){return typeof e==`number`?this.x/=e:this.x/=e.x,typeof e==`number`?this.y/=e:this.y/=e.y,typeof e==`number`?this.z/=e:this.z/=e.z,this}dot(e){return this.x*e.x+this.y*e.y+this.z*e.z}cross(t){return new e(this.y*t.z-this.z*t.y,this.z*t.x-this.x*t.z,this.x*t.y-this.y*t.x)}map(e){return this.x=e(this.x),this.y=e(this.y),this.z=e(this.z),this}toArr(){return[this.x,this.y,this.z]}length(){return Math.hypot(this.x,this.y,this.z)}lengthSquared(){return this.x*this.x+this.y*this.y+this.z*this.z}normalize(){let e=this.length();return e!==0&&this.div(e),this}distance(e){let t=e.x-this.x,n=e.y-this.y,r=e.z-this.z;return Math.hypot(t,n,r)}clone(){return new e(this.x,this.y,this.z)}};e.Vec2=t,e.Vec3=n,e.add=(e,r)=>`z`in e&&`z`in r?new n(e.x+r.x,e.y+e.y,e.z+e.z):new t(e.x+r.x,e.y+e.y),e.approach=(e,t,n)=>e<t?Math.min(e+n,t):Math.max(e-n,t),e.chooseRand=e=>e[Math.floor(Math.random()*e.length)],e.clamp=(e,t,n)=>Math.max(Math.min(e,n),t),e.div=(e,r)=>`z`in e&&`z`in r?new n(e.x/r.x,e.y/e.y,e.z/e.z):new t(e.x/r.x,e.y/e.y),e.lerp=(e,t,n)=>e*(1-n)+t*n,e.mod=(e,t)=>(e%t+t)%t,e.mul=(e,r)=>`z`in e&&`z`in r?new n(e.x*r.x,e.y*e.y,e.z*e.z):new t(e.x*r.x,e.y*e.y),e.randRange=(e,t)=>e+Math.random()*(t-e),e.sub=(e,r)=>`z`in e&&`z`in r?new n(e.x-r.x,e.y-e.y,e.z-e.z):new t(e.x-r.x,e.y-e.y)});
|
package/dist/src/functions.d.ts
CHANGED
|
@@ -5,7 +5,19 @@ export declare const randRange: (min: number, max: number) => number;
|
|
|
5
5
|
export declare const clamp: (value: number, min: number, max: number) => number;
|
|
6
6
|
export declare const lerp: (a: number, b: number, t: number) => number;
|
|
7
7
|
export declare const approach: (current: number, target: number, dt: number) => number;
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated use `Vec2` || `Vec3` instead
|
|
10
|
+
*/
|
|
8
11
|
export declare const add: <V extends Vec2 | Vec3>(x: V, y: V) => V;
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated use `Vec2` || `Vec3` instead
|
|
14
|
+
*/
|
|
9
15
|
export declare const sub: <V extends Vec2 | Vec3>(x: V, y: V) => V;
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated use `Vec2` || `Vec3` instead
|
|
18
|
+
*/
|
|
10
19
|
export declare const mul: <V extends Vec2 | Vec3>(x: V, y: V) => V;
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated use `Vec2` || `Vec3` instead
|
|
22
|
+
*/
|
|
11
23
|
export declare const div: <V extends Vec2 | Vec3>(x: V, y: V) => V;
|
package/dist/src/vectors.d.ts
CHANGED
|
@@ -3,34 +3,114 @@ export declare class Vec2 {
|
|
|
3
3
|
y: number;
|
|
4
4
|
constructor(x: number, y: number);
|
|
5
5
|
static initial(v: number): Vec2;
|
|
6
|
-
static
|
|
6
|
+
static zero(): Vec2;
|
|
7
|
+
static one(): Vec2;
|
|
7
8
|
add(other: Vec2 | number): this;
|
|
8
9
|
sub(other: Vec2 | number): this;
|
|
9
10
|
mul(other: Vec2 | number): this;
|
|
10
11
|
div(other: Vec2 | number): this;
|
|
12
|
+
/**
|
|
13
|
+
* @returns Dot product of `this` and `other`
|
|
14
|
+
*/
|
|
15
|
+
dot(other: Vec2): number;
|
|
16
|
+
/**
|
|
17
|
+
* @returns the cross product of `this` and `other`
|
|
18
|
+
*/
|
|
19
|
+
cross(other: Vec2): number;
|
|
20
|
+
/**
|
|
21
|
+
* `cb` applied to the `x` and `y`
|
|
22
|
+
*/
|
|
11
23
|
map(cb: (v: number) => number): this;
|
|
24
|
+
/**
|
|
25
|
+
* @returns \[`x`, `y`, 0\]
|
|
26
|
+
*/
|
|
12
27
|
toArr(): [number, number, number];
|
|
28
|
+
/**
|
|
29
|
+
* @returns magnitude of the vector
|
|
30
|
+
*/
|
|
31
|
+
length(): number;
|
|
32
|
+
/**
|
|
33
|
+
* Consider using this over `length` if you want faster calculations
|
|
34
|
+
* @returns squared magnitude of the vector
|
|
35
|
+
*/
|
|
36
|
+
lengthSquared(): number;
|
|
37
|
+
/**
|
|
38
|
+
* Normalizes the vector
|
|
39
|
+
* @example
|
|
40
|
+
* const vec = new Vec2(5, 0)
|
|
41
|
+
* vec.normalize()
|
|
42
|
+
* console.assert(vec, new Vec2(1, 0))
|
|
43
|
+
*/
|
|
44
|
+
normalize(): this;
|
|
45
|
+
distance(target: Vec2): number;
|
|
46
|
+
/**
|
|
47
|
+
* @returns A deep copy of this vector
|
|
48
|
+
*/
|
|
49
|
+
clone(): Vec2;
|
|
13
50
|
}
|
|
14
|
-
export declare class Vec3
|
|
51
|
+
export declare class Vec3 {
|
|
52
|
+
x: number;
|
|
53
|
+
y: number;
|
|
15
54
|
z: number;
|
|
16
55
|
constructor(x: number, y: number, z: number);
|
|
17
56
|
static initial(v: number): Vec3;
|
|
18
|
-
static
|
|
57
|
+
static zero(): Vec3;
|
|
58
|
+
static one(): Vec3;
|
|
59
|
+
/**
|
|
60
|
+
*
|
|
61
|
+
* @param value HEX string without the char '#'
|
|
62
|
+
* @example
|
|
63
|
+
*
|
|
64
|
+
* const white = Vec3.fromHex("ffffff");
|
|
65
|
+
* const black = Vec3.fromHex("000000");
|
|
66
|
+
* const blue = Vec3.fromHex("0000ff");
|
|
67
|
+
*
|
|
68
|
+
* console.assert(white, new Vec3(255, 255, 255))
|
|
69
|
+
* console.assert(black, new Vec3(0, 0, 0))
|
|
70
|
+
* console.assert(blue, new Vec3(0, 0, 255))
|
|
71
|
+
*/
|
|
19
72
|
static fromHex(value: string): Vec3;
|
|
20
73
|
add(other: Vec3 | number): this;
|
|
21
74
|
sub(other: Vec3 | number): this;
|
|
22
75
|
mul(other: Vec3 | number): this;
|
|
23
76
|
div(other: Vec3 | number): this;
|
|
77
|
+
/**
|
|
78
|
+
* @returns Dot product of this vector
|
|
79
|
+
*/
|
|
80
|
+
dot(other: Vec3): number;
|
|
81
|
+
/**
|
|
82
|
+
* `Vec3.cross` doesn't update for the current value.
|
|
83
|
+
* Instead returns the calculated value
|
|
84
|
+
*/
|
|
85
|
+
cross(other: Vec3): Vec3;
|
|
86
|
+
/**
|
|
87
|
+
* `cb` applied to the `x`, `y` and `z`
|
|
88
|
+
*/
|
|
24
89
|
map(cb: (v: number) => number): this;
|
|
90
|
+
/**
|
|
91
|
+
* @returns \[`x`, `y`, `z`\]
|
|
92
|
+
*/
|
|
25
93
|
toArr(): [number, number, number];
|
|
94
|
+
/**
|
|
95
|
+
* @returns magnitude of the vector
|
|
96
|
+
*/
|
|
97
|
+
length(): number;
|
|
98
|
+
/**
|
|
99
|
+
* Consider using this over `length` if you want faster calculations
|
|
100
|
+
* @returns squared magnitude of the vector
|
|
101
|
+
*/
|
|
102
|
+
lengthSquared(): number;
|
|
103
|
+
/**
|
|
104
|
+
* Normalizes the vector
|
|
105
|
+
* @example
|
|
106
|
+
* const vec = new Vec2(5, 0, 0)
|
|
107
|
+
* vec.normalize()
|
|
108
|
+
* console.assert(vec, new Vec2(1, 0, 0))
|
|
109
|
+
*/
|
|
110
|
+
normalize(): this;
|
|
111
|
+
distance(target: Vec3): number;
|
|
112
|
+
/**
|
|
113
|
+
* @returns A deep copy of this vector
|
|
114
|
+
*/
|
|
115
|
+
clone(): Vec3;
|
|
26
116
|
}
|
|
27
|
-
export type VertexPositionColorTexture = {
|
|
28
|
-
pos: Vec2;
|
|
29
|
-
color: Vec3;
|
|
30
|
-
uv: Vec2;
|
|
31
|
-
alpha: number;
|
|
32
|
-
};
|
|
33
|
-
export type VertexPositionColor = {
|
|
34
|
-
pos: Vec2;
|
|
35
|
-
color: Vec3;
|
|
36
|
-
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devrals/math",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Vector2/3 Utils",
|
|
7
|
-
"repository": "https://github.com/devRals/webgl-backdrops/",
|
|
8
7
|
"files": [
|
|
9
8
|
"dist"
|
|
10
9
|
],
|
|
@@ -14,7 +13,8 @@
|
|
|
14
13
|
},
|
|
15
14
|
"keywords": [
|
|
16
15
|
"math",
|
|
17
|
-
"vec2"
|
|
16
|
+
"vec2",
|
|
17
|
+
"vec3"
|
|
18
18
|
],
|
|
19
19
|
"exports": {
|
|
20
20
|
".": {
|