@emasoft/svg-matrix 1.0.1 → 1.0.3
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 +264 -12
- package/package.json +17 -3
- package/src/index.js +25 -1
- package/src/matrix.js +263 -35
- package/src/transforms2d.js +120 -2
- package/src/transforms3d.js +214 -4
- package/src/vector.js +174 -30
- package/.github/workflows/publish.yml +0 -34
- package/.github/workflows/release.yml +0 -37
- package/.github/workflows/test.yml +0 -21
- package/CLAUDE.md +0 -60
- package/SVG-MATRIX/README.md +0 -22
- package/SVG-MATRIX/package.json +0 -33
- package/emasoft-svg-matrix-1.0.1.tgz +0 -0
- package/npm-shrinkwrap.json +0 -22
- package/scripts/bootstrap_repo.sh +0 -99
- package/test/example.js +0 -18
- package/test/examples.js +0 -50
package/README.md
CHANGED
|
@@ -1,25 +1,277 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @emasoft/svg-matrix
|
|
2
2
|
|
|
3
3
|
Arbitrary-precision matrix, vector and affine transformation library for JavaScript using decimal.js.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
- Decimal-backed Matrix and Vector classes.
|
|
7
|
-
- 2D (3x3) and 3D (4x4) transform helpers.
|
|
5
|
+
## Features
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
- **Decimal-backed** Matrix and Vector classes for high-precision geometry
|
|
8
|
+
- **2D transforms** (3x3 homogeneous matrices): translation, rotation, scale, skew, reflection
|
|
9
|
+
- **3D transforms** (4x4 homogeneous matrices): translation, rotation (X/Y/Z axis), scale, reflection
|
|
10
|
+
- **Linear algebra**: LU/QR decomposition, determinant, inverse, solve, matrix exponential
|
|
11
|
+
- Works in **Node.js** and **browsers** (via CDN)
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
### npm
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @emasoft/svg-matrix
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { Decimal, Matrix, Vector, Transforms2D, Transforms3D } from '@emasoft/svg-matrix';
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### CDN (Browser)
|
|
26
|
+
|
|
27
|
+
Using esm.sh (recommended - auto-resolves dependencies):
|
|
28
|
+
|
|
29
|
+
```html
|
|
30
|
+
<script type="module">
|
|
31
|
+
import { Decimal, Matrix, Vector, Transforms2D, Transforms3D } from 'https://esm.sh/@emasoft/svg-matrix';
|
|
32
|
+
|
|
33
|
+
Decimal.set({ precision: 80 });
|
|
34
|
+
|
|
35
|
+
// Create and compose 2D transforms
|
|
36
|
+
const M = Transforms2D.translation(2, 3)
|
|
37
|
+
.mul(Transforms2D.rotate(Math.PI / 4))
|
|
38
|
+
.mul(Transforms2D.scale(1.5));
|
|
39
|
+
|
|
40
|
+
// Apply to a point
|
|
41
|
+
const [x, y] = Transforms2D.applyTransform(M, 1, 0);
|
|
42
|
+
console.log('Transformed:', x.toString(), y.toString());
|
|
43
|
+
</script>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Using Skypack:
|
|
47
|
+
|
|
48
|
+
```html
|
|
49
|
+
<script type="module">
|
|
50
|
+
import { Matrix, Vector } from 'https://cdn.skypack.dev/@emasoft/svg-matrix';
|
|
51
|
+
</script>
|
|
10
52
|
```
|
|
11
|
-
|
|
53
|
+
|
|
54
|
+
Using import maps:
|
|
55
|
+
|
|
56
|
+
```html
|
|
57
|
+
<script type="importmap">
|
|
58
|
+
{
|
|
59
|
+
"imports": {
|
|
60
|
+
"@emasoft/svg-matrix": "https://esm.sh/@emasoft/svg-matrix"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
</script>
|
|
64
|
+
<script type="module">
|
|
65
|
+
import { Matrix, Vector, Transforms2D } from '@emasoft/svg-matrix';
|
|
66
|
+
</script>
|
|
12
67
|
```
|
|
13
68
|
|
|
14
|
-
Usage
|
|
69
|
+
## Usage Examples
|
|
70
|
+
|
|
71
|
+
### Vector Operations
|
|
72
|
+
|
|
15
73
|
```js
|
|
16
|
-
import {
|
|
74
|
+
import { Vector, Decimal } from '@emasoft/svg-matrix';
|
|
75
|
+
|
|
17
76
|
Decimal.set({ precision: 80 });
|
|
18
77
|
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
|
|
78
|
+
const v = Vector.from([1, 2, 3]);
|
|
79
|
+
const w = Vector.from([4, 5, 6]);
|
|
80
|
+
|
|
81
|
+
// Basic operations
|
|
82
|
+
console.log('Add:', v.add(w).toNumberArray()); // [5, 7, 9]
|
|
83
|
+
console.log('Scale:', v.scale(2).toNumberArray()); // [2, 4, 6]
|
|
84
|
+
console.log('Dot:', v.dot(w).toString()); // 32
|
|
85
|
+
console.log('Cross:', v.cross(w).toNumberArray()); // [-3, 6, -3]
|
|
86
|
+
|
|
87
|
+
// Geometry
|
|
88
|
+
console.log('Norm:', v.norm().toString()); // 3.7416...
|
|
89
|
+
console.log('Normalized:', v.normalize().toNumberArray());
|
|
90
|
+
console.log('Angle:', v.angleBetween(w).toString()); // radians
|
|
91
|
+
|
|
92
|
+
// Projection
|
|
93
|
+
const proj = v.projectOnto(w);
|
|
94
|
+
console.log('Projection:', proj.toNumberArray());
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Matrix Operations
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
import { Matrix } from '@emasoft/svg-matrix';
|
|
101
|
+
|
|
102
|
+
const A = Matrix.from([[1, 2], [3, 4]]);
|
|
103
|
+
const B = Matrix.from([[5, 6], [7, 8]]);
|
|
104
|
+
|
|
105
|
+
// Basic operations
|
|
106
|
+
console.log('Multiply:', A.mul(B).toNumberArray());
|
|
107
|
+
console.log('Transpose:', A.transpose().toNumberArray());
|
|
108
|
+
console.log('Determinant:', A.determinant().toString()); // -2
|
|
109
|
+
console.log('Trace:', A.trace().toString()); // 5
|
|
110
|
+
|
|
111
|
+
// Linear algebra
|
|
112
|
+
const inv = A.inverse();
|
|
113
|
+
console.log('Inverse:', inv.toNumberArray());
|
|
114
|
+
|
|
115
|
+
// Solve Ax = b
|
|
116
|
+
const x = A.solve([1, 1]);
|
|
117
|
+
console.log('Solution:', x.toNumberArray());
|
|
118
|
+
|
|
119
|
+
// Decompositions
|
|
120
|
+
const { L, U, P } = A.lu();
|
|
121
|
+
const { Q, R } = A.qr();
|
|
122
|
+
|
|
123
|
+
// Matrix exponential
|
|
124
|
+
const expA = A.exp();
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### 2D Transforms
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
import { Transforms2D } from '@emasoft/svg-matrix';
|
|
131
|
+
|
|
132
|
+
// Basic transforms
|
|
133
|
+
const T = Transforms2D.translation(10, 20);
|
|
134
|
+
const R = Transforms2D.rotate(Math.PI / 4); // 45 degrees
|
|
135
|
+
const S = Transforms2D.scale(2, 3); // non-uniform
|
|
136
|
+
const Su = Transforms2D.scale(2); // uniform
|
|
137
|
+
|
|
138
|
+
// Rotation around a point
|
|
139
|
+
const Rp = Transforms2D.rotateAroundPoint(Math.PI / 2, 5, 5);
|
|
140
|
+
|
|
141
|
+
// Skew and stretch
|
|
142
|
+
const Sk = Transforms2D.skew(0.5, 0);
|
|
143
|
+
const St = Transforms2D.stretchAlongAxis(1, 0, 2); // stretch 2x along X
|
|
144
|
+
|
|
145
|
+
// Reflections
|
|
146
|
+
const Rx = Transforms2D.reflectX(); // flip Y
|
|
147
|
+
const Ry = Transforms2D.reflectY(); // flip X
|
|
148
|
+
const Ro = Transforms2D.reflectOrigin(); // flip both
|
|
149
|
+
|
|
150
|
+
// Compose transforms (right-to-left: S first, then R, then T)
|
|
151
|
+
const M = T.mul(R).mul(S);
|
|
152
|
+
|
|
153
|
+
// Apply to point
|
|
154
|
+
const [x, y] = Transforms2D.applyTransform(M, 1, 0);
|
|
155
|
+
|
|
156
|
+
// Inverse transform
|
|
157
|
+
const Minv = M.inverse();
|
|
158
|
+
const [xBack, yBack] = Transforms2D.applyTransform(Minv, x, y);
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### 3D Transforms
|
|
162
|
+
|
|
163
|
+
```js
|
|
164
|
+
import { Transforms3D } from '@emasoft/svg-matrix';
|
|
165
|
+
|
|
166
|
+
// Basic transforms
|
|
167
|
+
const T = Transforms3D.translation(1, 2, 3);
|
|
168
|
+
const S = Transforms3D.scale(2, 2, 2);
|
|
169
|
+
|
|
170
|
+
// Axis rotations (radians, right-hand rule)
|
|
171
|
+
const Rx = Transforms3D.rotateX(Math.PI / 2);
|
|
172
|
+
const Ry = Transforms3D.rotateY(Math.PI / 4);
|
|
173
|
+
const Rz = Transforms3D.rotateZ(Math.PI / 6);
|
|
174
|
+
|
|
175
|
+
// Rotation around arbitrary axis
|
|
176
|
+
const Raxis = Transforms3D.rotateAroundAxis(1, 1, 0, Math.PI / 3);
|
|
177
|
+
|
|
178
|
+
// Rotation around a point
|
|
179
|
+
const Rpt = Transforms3D.rotateAroundPoint(0, 0, 1, Math.PI / 2, 5, 5, 0);
|
|
180
|
+
|
|
181
|
+
// Reflections
|
|
182
|
+
const RfXY = Transforms3D.reflectXY(); // flip Z
|
|
183
|
+
const RfXZ = Transforms3D.reflectXZ(); // flip Y
|
|
184
|
+
const RfYZ = Transforms3D.reflectYZ(); // flip X
|
|
185
|
+
const RfO = Transforms3D.reflectOrigin(); // flip all
|
|
186
|
+
|
|
187
|
+
// Compose and apply
|
|
188
|
+
const M = T.mul(Rx).mul(S);
|
|
189
|
+
const [x, y, z] = Transforms3D.applyTransform(M, 1, 0, 0);
|
|
22
190
|
```
|
|
23
191
|
|
|
24
|
-
|
|
192
|
+
## API Reference
|
|
193
|
+
|
|
194
|
+
### Vector
|
|
195
|
+
|
|
196
|
+
| Method | Description |
|
|
197
|
+
|--------|-------------|
|
|
198
|
+
| `Vector.from(arr)` | Create vector from array |
|
|
199
|
+
| `add(v)` | Element-wise addition |
|
|
200
|
+
| `sub(v)` | Element-wise subtraction |
|
|
201
|
+
| `scale(s)` | Scalar multiplication |
|
|
202
|
+
| `negate()` | Negate all components |
|
|
203
|
+
| `dot(v)` | Dot product |
|
|
204
|
+
| `cross(v)` | Cross product (3D only) |
|
|
205
|
+
| `outer(v)` | Outer product (returns 2D array) |
|
|
206
|
+
| `norm()` | Euclidean length |
|
|
207
|
+
| `normalize()` | Unit vector |
|
|
208
|
+
| `angleBetween(v)` | Angle in radians |
|
|
209
|
+
| `projectOnto(v)` | Vector projection |
|
|
210
|
+
| `orthogonal()` | Perpendicular vector |
|
|
211
|
+
| `distance(v)` | Euclidean distance |
|
|
212
|
+
| `equals(v, tol?)` | Equality check with optional tolerance |
|
|
213
|
+
| `toArray()` | Get Decimal array |
|
|
214
|
+
| `toNumberArray()` | Get number array |
|
|
215
|
+
| `toStringArray()` | Get string array |
|
|
216
|
+
|
|
217
|
+
### Matrix
|
|
218
|
+
|
|
219
|
+
| Method | Description |
|
|
220
|
+
|--------|-------------|
|
|
221
|
+
| `Matrix.from(arr)` | Create from 2D array |
|
|
222
|
+
| `Matrix.zeros(r, c)` | Zero matrix |
|
|
223
|
+
| `Matrix.identity(n)` | Identity matrix |
|
|
224
|
+
| `add(M)` | Element-wise addition |
|
|
225
|
+
| `sub(M)` | Element-wise subtraction |
|
|
226
|
+
| `mul(M)` | Matrix multiplication |
|
|
227
|
+
| `div(s)` | Scalar division |
|
|
228
|
+
| `negate()` | Negate all elements |
|
|
229
|
+
| `transpose()` | Transpose |
|
|
230
|
+
| `trace()` | Sum of diagonal |
|
|
231
|
+
| `determinant()` | Determinant |
|
|
232
|
+
| `inverse()` | Matrix inverse |
|
|
233
|
+
| `solve(b)` | Solve Ax = b |
|
|
234
|
+
| `lu()` | LU decomposition |
|
|
235
|
+
| `qr()` | QR decomposition |
|
|
236
|
+
| `exp()` | Matrix exponential |
|
|
237
|
+
| `applyToVector(v)` | Matrix-vector product |
|
|
238
|
+
| `equals(M, tol?)` | Equality check |
|
|
239
|
+
| `isSquare()` | Check if square |
|
|
240
|
+
| `toNumberArray()` | Get number 2D array |
|
|
241
|
+
| `toArrayOfStrings()` | Get string 2D array |
|
|
242
|
+
|
|
243
|
+
### Transforms2D
|
|
244
|
+
|
|
245
|
+
| Function | Description |
|
|
246
|
+
|----------|-------------|
|
|
247
|
+
| `translation(tx, ty)` | Translation matrix |
|
|
248
|
+
| `scale(sx, sy?)` | Scale matrix (uniform if sy omitted) |
|
|
249
|
+
| `rotate(theta)` | Rotation matrix (radians) |
|
|
250
|
+
| `rotateAroundPoint(theta, px, py)` | Rotation around point |
|
|
251
|
+
| `skew(ax, ay)` | Skew/shear matrix |
|
|
252
|
+
| `stretchAlongAxis(ux, uy, k)` | Stretch along direction |
|
|
253
|
+
| `reflectX()` | Reflect across X axis |
|
|
254
|
+
| `reflectY()` | Reflect across Y axis |
|
|
255
|
+
| `reflectOrigin()` | Reflect through origin |
|
|
256
|
+
| `applyTransform(M, x, y)` | Apply matrix to point |
|
|
257
|
+
|
|
258
|
+
### Transforms3D
|
|
259
|
+
|
|
260
|
+
| Function | Description |
|
|
261
|
+
|----------|-------------|
|
|
262
|
+
| `translation(tx, ty, tz)` | Translation matrix |
|
|
263
|
+
| `scale(sx, sy?, sz?)` | Scale matrix |
|
|
264
|
+
| `rotateX(theta)` | Rotation around X axis |
|
|
265
|
+
| `rotateY(theta)` | Rotation around Y axis |
|
|
266
|
+
| `rotateZ(theta)` | Rotation around Z axis |
|
|
267
|
+
| `rotateAroundAxis(ux, uy, uz, theta)` | Rotation around arbitrary axis |
|
|
268
|
+
| `rotateAroundPoint(ux, uy, uz, theta, px, py, pz)` | Rotation around point |
|
|
269
|
+
| `reflectXY()` | Reflect across XY plane |
|
|
270
|
+
| `reflectXZ()` | Reflect across XZ plane |
|
|
271
|
+
| `reflectYZ()` | Reflect across YZ plane |
|
|
272
|
+
| `reflectOrigin()` | Reflect through origin |
|
|
273
|
+
| `applyTransform(M, x, y, z)` | Apply matrix to point |
|
|
274
|
+
|
|
275
|
+
## License
|
|
25
276
|
|
|
277
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@emasoft/svg-matrix",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Arbitrary-precision matrix, vector and affine transformation library for JavaScript using decimal.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -22,11 +22,25 @@
|
|
|
22
22
|
"affine",
|
|
23
23
|
"transform",
|
|
24
24
|
"svg",
|
|
25
|
-
"geometry"
|
|
25
|
+
"geometry",
|
|
26
|
+
"esm",
|
|
27
|
+
"browser",
|
|
28
|
+
"cdn",
|
|
29
|
+
"2d",
|
|
30
|
+
"3d",
|
|
31
|
+
"rotation",
|
|
32
|
+
"translation",
|
|
33
|
+
"homogeneous"
|
|
26
34
|
],
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"import": "./src/index.js",
|
|
38
|
+
"default": "./src/index.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
27
41
|
"author": "Emasoft",
|
|
28
42
|
"license": "MIT",
|
|
29
43
|
"dependencies": {
|
|
30
44
|
"decimal.js": "^10.6.0"
|
|
31
45
|
}
|
|
32
|
-
}
|
|
46
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,7 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @emasoft/svg-matrix - Arbitrary-precision matrix, vector and affine transformation library.
|
|
3
|
+
*
|
|
4
|
+
* This library provides Decimal-backed Matrix and Vector classes along with
|
|
5
|
+
* 2D (3x3) and 3D (4x4) transform helpers for geometry operations requiring high precision.
|
|
6
|
+
*
|
|
7
|
+
* @module @emasoft/svg-matrix
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* import { Decimal, Matrix, Vector, Transforms2D, Transforms3D } from '@emasoft/svg-matrix';
|
|
11
|
+
*
|
|
12
|
+
* // Set precision for all operations
|
|
13
|
+
* Decimal.set({ precision: 80 });
|
|
14
|
+
*
|
|
15
|
+
* // Create and compose 2D transforms
|
|
16
|
+
* const M = Transforms2D.translation(2, 3)
|
|
17
|
+
* .mul(Transforms2D.rotate(Math.PI / 4))
|
|
18
|
+
* .mul(Transforms2D.scale(1.5));
|
|
19
|
+
*
|
|
20
|
+
* // Apply to a point
|
|
21
|
+
* const [x, y] = Transforms2D.applyTransform(M, 1, 0);
|
|
22
|
+
* console.log('Transformed:', x.toString(), y.toString());
|
|
23
|
+
*/
|
|
24
|
+
|
|
1
25
|
import Decimal from 'decimal.js';
|
|
2
26
|
import { Matrix } from './matrix.js';
|
|
3
27
|
import { Vector } from './vector.js';
|
|
4
28
|
import * as Transforms2D from './transforms2d.js';
|
|
5
29
|
import * as Transforms3D from './transforms3d.js';
|
|
6
30
|
|
|
7
|
-
export { Decimal, Matrix, Vector, Transforms2D, Transforms3D };
|
|
31
|
+
export { Decimal, Matrix, Vector, Transforms2D, Transforms3D };
|