@devrals/math 0.1.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/LICENSE +21 -0
- package/dist/math.js +71 -0
- package/dist/math.umd.cjs +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/vec.d.ts +46 -0
- package/package.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 devRals
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/math.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
//#region src/vec.ts
|
|
2
|
+
var e = class e {
|
|
3
|
+
constructor(e, t) {
|
|
4
|
+
this.x = e, this.y = t;
|
|
5
|
+
}
|
|
6
|
+
static initial(t) {
|
|
7
|
+
return new e(t, t);
|
|
8
|
+
}
|
|
9
|
+
static empty() {
|
|
10
|
+
return new e(0, 0);
|
|
11
|
+
}
|
|
12
|
+
add(e) {
|
|
13
|
+
return typeof e == "number" ? this.x += e : this.x += e.x, typeof e == "number" ? this.y += e : this.y += e.y, this;
|
|
14
|
+
}
|
|
15
|
+
sub(e) {
|
|
16
|
+
return typeof e == "number" ? this.x -= e : this.x -= e.x, typeof e == "number" ? this.y -= e : this.y -= e.y, this;
|
|
17
|
+
}
|
|
18
|
+
mul(e) {
|
|
19
|
+
return typeof e == "number" ? this.x *= e : this.x *= e.x, typeof e == "number" ? this.y *= e : this.y *= e.y, this;
|
|
20
|
+
}
|
|
21
|
+
div(e) {
|
|
22
|
+
return typeof e == "number" ? this.x /= e : this.x /= e.x, typeof e == "number" ? this.y /= e : this.y /= e.y, this;
|
|
23
|
+
}
|
|
24
|
+
map(e) {
|
|
25
|
+
return this.x = e(this.x), this.y = e(this.y), this;
|
|
26
|
+
}
|
|
27
|
+
toArr() {
|
|
28
|
+
return [
|
|
29
|
+
this.x,
|
|
30
|
+
this.y,
|
|
31
|
+
0
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
}, t = class t extends e {
|
|
35
|
+
constructor(e, t, n) {
|
|
36
|
+
super(e, t), this.z = n;
|
|
37
|
+
}
|
|
38
|
+
static initial(e) {
|
|
39
|
+
return new t(e, e, e);
|
|
40
|
+
}
|
|
41
|
+
static empty() {
|
|
42
|
+
return new t(0, 0, 0);
|
|
43
|
+
}
|
|
44
|
+
static fromHex(e) {
|
|
45
|
+
return new t(parseInt(e.slice(0, 2), 16), parseInt(e.slice(2, 4), 16), parseInt(e.slice(4, 6), 16));
|
|
46
|
+
}
|
|
47
|
+
add(e) {
|
|
48
|
+
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;
|
|
49
|
+
}
|
|
50
|
+
sub(e) {
|
|
51
|
+
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;
|
|
52
|
+
}
|
|
53
|
+
mul(e) {
|
|
54
|
+
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;
|
|
55
|
+
}
|
|
56
|
+
div(e) {
|
|
57
|
+
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
|
+
}
|
|
59
|
+
map(e) {
|
|
60
|
+
return this.x = e(this.x), this.y = e(this.y), this.z = e(this.z), this;
|
|
61
|
+
}
|
|
62
|
+
toArr() {
|
|
63
|
+
return [
|
|
64
|
+
this.x,
|
|
65
|
+
this.y,
|
|
66
|
+
this.z
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
}, n = (e) => e[Math.floor(Math.random() * e.length)], r = (e, t) => (e % t + t) % t, i = (e, t) => e + Math.random() * t, 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
|
+
//#endregion
|
|
71
|
+
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 };
|
|
@@ -0,0 +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 empty(){return new e(0,0)}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}map(e){return this.x=e(this.x),this.y=e(this.y),this}toArr(){return[this.x,this.y,0]}},n=class e extends t{constructor(e,t,n){super(e,t),this.z=n}static initial(t){return new e(t,t,t)}static empty(){return new e(0,0,0)}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}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]}};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.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)});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './vec.js';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export declare class Vec2 {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
constructor(x: number, y: number);
|
|
5
|
+
static initial(v: number): Vec2;
|
|
6
|
+
static empty(): Vec2;
|
|
7
|
+
add(other: Vec2 | number): this;
|
|
8
|
+
sub(other: Vec2 | number): this;
|
|
9
|
+
mul(other: Vec2 | number): this;
|
|
10
|
+
div(other: Vec2 | number): this;
|
|
11
|
+
map(cb: (v: number) => number): this;
|
|
12
|
+
toArr(): [number, number, number];
|
|
13
|
+
}
|
|
14
|
+
export declare class Vec3 extends Vec2 {
|
|
15
|
+
z: number;
|
|
16
|
+
constructor(x: number, y: number, z: number);
|
|
17
|
+
static initial(v: number): Vec3;
|
|
18
|
+
static empty(): Vec3;
|
|
19
|
+
static fromHex(value: string): Vec3;
|
|
20
|
+
add(other: Vec3 | number): this;
|
|
21
|
+
sub(other: Vec3 | number): this;
|
|
22
|
+
mul(other: Vec3 | number): this;
|
|
23
|
+
div(other: Vec3 | number): this;
|
|
24
|
+
map(cb: (v: number) => number): this;
|
|
25
|
+
toArr(): [number, number, number];
|
|
26
|
+
}
|
|
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
|
+
};
|
|
37
|
+
export declare const chooseRand: <T>(arr: T[]) => T;
|
|
38
|
+
export declare const mod: (x: number, m: number) => number;
|
|
39
|
+
export declare const randRange: (x: number, y: number) => number;
|
|
40
|
+
export declare const clamp: (value: number, min: number, max: number) => number;
|
|
41
|
+
export declare const lerp: (a: number, b: number, t: number) => number;
|
|
42
|
+
export declare const approach: (current: number, target: number, dt: number) => number;
|
|
43
|
+
export declare const add: <V extends Vec2 | Vec3>(x: V, y: V) => V;
|
|
44
|
+
export declare const sub: <V extends Vec2 | Vec3>(x: V, y: V) => V;
|
|
45
|
+
export declare const mul: <V extends Vec2 | Vec3>(x: V, y: V) => V;
|
|
46
|
+
export declare const div: <V extends Vec2 | Vec3>(x: V, y: V) => V;
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@devrals/math",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "Vector2/3 Utils",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "devRals",
|
|
12
|
+
"url": "https://devrals.github.io/"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"math",
|
|
16
|
+
"vec2"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/src/index.d.ts",
|
|
21
|
+
"default": "./dist/math.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "vite build"
|
|
26
|
+
}
|
|
27
|
+
}
|