@emasoft/svg-matrix 1.0.9 → 1.0.10
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 +124 -33
- package/package.json +1 -1
- package/src/index.js +1 -1
package/README.md
CHANGED
|
@@ -56,41 +56,88 @@ node test/benchmark-precision.js
|
|
|
56
56
|
|
|
57
57
|
### Node.js (Local Installation)
|
|
58
58
|
|
|
59
|
-
**
|
|
59
|
+
**What does "local installation" mean?** It means downloading the library to your computer so you can use it in your Node.js projects. This is what most developers do.
|
|
60
|
+
|
|
61
|
+
#### Step 1: Install the package
|
|
62
|
+
|
|
63
|
+
Open your terminal and run:
|
|
60
64
|
|
|
61
65
|
```bash
|
|
62
66
|
npm install @emasoft/svg-matrix
|
|
63
67
|
```
|
|
64
68
|
|
|
65
|
-
|
|
69
|
+
This downloads the library into a folder called `node_modules` in your project.
|
|
70
|
+
|
|
71
|
+
#### Step 2: Create a JavaScript file
|
|
72
|
+
|
|
73
|
+
Create a new file called `example.js` in your project folder.
|
|
74
|
+
|
|
75
|
+
#### Step 3: Import what you need
|
|
66
76
|
|
|
67
|
-
|
|
77
|
+
There are two ways to import the library:
|
|
78
|
+
|
|
79
|
+
**Way 1: Pick only what you need (recommended)**
|
|
80
|
+
|
|
81
|
+
This is like ordering specific items from a menu:
|
|
68
82
|
|
|
69
83
|
```js
|
|
84
|
+
// example.js
|
|
85
|
+
|
|
86
|
+
// Import only the modules you need
|
|
70
87
|
import { Matrix, Vector, Transforms2D } from '@emasoft/svg-matrix';
|
|
71
88
|
|
|
72
|
-
|
|
73
|
-
|
|
89
|
+
// Now you can use them directly
|
|
90
|
+
const v = Vector.from([1, 2, 3]);
|
|
91
|
+
const rotation = Transforms2D.rotate(Math.PI / 4); // 45 degrees
|
|
92
|
+
|
|
93
|
+
console.log('Vector:', v.toNumberArray());
|
|
94
|
+
console.log('Rotation matrix created!');
|
|
74
95
|
```
|
|
75
96
|
|
|
76
|
-
|
|
97
|
+
**Way 2: Import everything at once**
|
|
98
|
+
|
|
99
|
+
This is like getting the entire toolbox:
|
|
77
100
|
|
|
78
101
|
```js
|
|
102
|
+
// example.js
|
|
103
|
+
|
|
104
|
+
// Import EVERYTHING as one big object called "SVGMatrix"
|
|
79
105
|
import * as SVGMatrix from '@emasoft/svg-matrix';
|
|
80
106
|
|
|
81
|
-
// Now use SVGMatrix.ModuleName
|
|
107
|
+
// Now use SVGMatrix.ModuleName to access each tool
|
|
82
108
|
const v = SVGMatrix.Vector.from([1, 2, 3]);
|
|
83
109
|
const m = SVGMatrix.Matrix.identity(3);
|
|
84
110
|
const rotation = SVGMatrix.Transforms2D.rotate(Math.PI / 4);
|
|
85
111
|
const pathData = SVGMatrix.GeometryToPath.circleToPathData(100, 100, 50);
|
|
112
|
+
|
|
113
|
+
// See everything that's available:
|
|
114
|
+
console.log('Available modules:', Object.keys(SVGMatrix));
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**What's inside the toolbox?** When you import everything, you get:
|
|
118
|
+
- `SVGMatrix.Matrix` - For matrix math
|
|
119
|
+
- `SVGMatrix.Vector` - For vector math
|
|
120
|
+
- `SVGMatrix.Transforms2D` - For 2D rotations, translations, scaling
|
|
121
|
+
- `SVGMatrix.Transforms3D` - For 3D transformations
|
|
122
|
+
- `SVGMatrix.SVGFlatten` - For flattening SVG transforms
|
|
123
|
+
- `SVGMatrix.GeometryToPath` - For converting shapes to paths
|
|
124
|
+
- `SVGMatrix.PolygonClip` - For polygon boolean operations
|
|
125
|
+
- ...and many more!
|
|
126
|
+
|
|
127
|
+
#### Step 4: Run your code
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
node example.js
|
|
86
131
|
```
|
|
87
132
|
|
|
88
|
-
|
|
133
|
+
#### Complete Working Example
|
|
89
134
|
|
|
90
|
-
Save this as `example.js
|
|
135
|
+
Save this as `my-first-example.js`:
|
|
91
136
|
|
|
92
137
|
```js
|
|
93
|
-
// example.js
|
|
138
|
+
// my-first-example.js
|
|
139
|
+
// A complete example showing the main features
|
|
140
|
+
|
|
94
141
|
import {
|
|
95
142
|
Matrix,
|
|
96
143
|
Vector,
|
|
@@ -99,46 +146,72 @@ import {
|
|
|
99
146
|
SVGFlatten
|
|
100
147
|
} from '@emasoft/svg-matrix';
|
|
101
148
|
|
|
102
|
-
|
|
149
|
+
console.log('=== Vector Example ===');
|
|
103
150
|
const v = Vector.from([1, 2, 3]);
|
|
104
151
|
const w = Vector.from([4, 5, 6]);
|
|
105
|
-
console.log('
|
|
106
|
-
console.log('
|
|
152
|
+
console.log('Vector v:', v.toNumberArray());
|
|
153
|
+
console.log('Vector w:', w.toNumberArray());
|
|
154
|
+
console.log('Dot product (v · w):', v.dot(w).toString());
|
|
107
155
|
|
|
108
|
-
|
|
156
|
+
console.log('\n=== Matrix Example ===');
|
|
109
157
|
const A = Matrix.from([[1, 2], [3, 4]]);
|
|
158
|
+
console.log('Matrix A:', A.toNumberArray());
|
|
110
159
|
console.log('Determinant:', A.determinant().toString());
|
|
111
|
-
console.log('Inverse:', A.inverse().toNumberArray());
|
|
112
160
|
|
|
113
|
-
|
|
114
|
-
|
|
161
|
+
console.log('\n=== Transform Example ===');
|
|
162
|
+
// Create a transform: move 100 right, rotate 45°, scale 2x
|
|
163
|
+
const transform = Transforms2D.translation(100, 0)
|
|
115
164
|
.mul(Transforms2D.rotate(Math.PI / 4))
|
|
116
165
|
.mul(Transforms2D.scale(2));
|
|
117
166
|
|
|
118
|
-
|
|
119
|
-
|
|
167
|
+
// Apply to a point
|
|
168
|
+
const [x, y] = Transforms2D.applyTransform(transform, 10, 0);
|
|
169
|
+
console.log(`Point (10, 0) after transform: (${x.toFixed(2)}, ${y.toFixed(2)})`);
|
|
120
170
|
|
|
121
|
-
|
|
171
|
+
console.log('\n=== SVG Path Example ===');
|
|
172
|
+
// Convert a circle to a path
|
|
122
173
|
const circlePath = GeometryToPath.circleToPathData(0, 0, 50);
|
|
123
|
-
|
|
124
|
-
|
|
174
|
+
console.log('Circle as path:', circlePath.substring(0, 50) + '...');
|
|
175
|
+
|
|
176
|
+
console.log('\nDone! Everything works!');
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Run it:
|
|
180
|
+
```bash
|
|
181
|
+
node my-first-example.js
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### Troubleshooting
|
|
185
|
+
|
|
186
|
+
**"Cannot use import statement outside a module"**
|
|
187
|
+
|
|
188
|
+
Add `"type": "module"` to your `package.json`:
|
|
189
|
+
|
|
190
|
+
```json
|
|
191
|
+
{
|
|
192
|
+
"name": "my-project",
|
|
193
|
+
"type": "module"
|
|
194
|
+
}
|
|
125
195
|
```
|
|
126
196
|
|
|
127
|
-
|
|
197
|
+
Or rename your file from `.js` to `.mjs`.
|
|
128
198
|
|
|
129
|
-
|
|
199
|
+
**Using older Node.js syntax (require)?**
|
|
200
|
+
|
|
201
|
+
This library uses modern ES modules. If you have old code using `require()`:
|
|
130
202
|
|
|
131
203
|
```js
|
|
132
|
-
//
|
|
204
|
+
// OLD WAY (doesn't work directly)
|
|
205
|
+
// const { Matrix } = require('@emasoft/svg-matrix'); // ❌ Error!
|
|
206
|
+
|
|
207
|
+
// NEW WAY (use dynamic import)
|
|
133
208
|
async function main() {
|
|
134
209
|
const { Matrix, Vector } = await import('@emasoft/svg-matrix');
|
|
210
|
+
|
|
135
211
|
const v = Vector.from([1, 2, 3]);
|
|
136
212
|
console.log(v.norm().toString());
|
|
137
213
|
}
|
|
138
214
|
main();
|
|
139
|
-
|
|
140
|
-
// Option 2: Add "type": "module" to your package.json
|
|
141
|
-
// Then you can use import statements directly
|
|
142
215
|
```
|
|
143
216
|
|
|
144
217
|
### Browser Usage (CDN)
|
|
@@ -200,25 +273,43 @@ Popular CDN with good caching.
|
|
|
200
273
|
</script>
|
|
201
274
|
```
|
|
202
275
|
|
|
203
|
-
#### Import Everything
|
|
276
|
+
#### Import Everything at Once
|
|
204
277
|
|
|
205
|
-
|
|
278
|
+
**What if I don't know what I need yet?** You can import the entire library as one big object. This is like getting the whole toolbox instead of picking individual tools.
|
|
206
279
|
|
|
207
280
|
```html
|
|
208
281
|
<script type="module">
|
|
209
|
-
// Import
|
|
282
|
+
// Import EVERYTHING as one object called "SVGMatrix"
|
|
283
|
+
// The "* as" means "everything as"
|
|
210
284
|
import * as SVGMatrix from 'https://esm.sh/@emasoft/svg-matrix';
|
|
211
285
|
|
|
212
|
-
// Now use SVGMatrix.ModuleName
|
|
286
|
+
// Now use SVGMatrix.ModuleName to access each tool
|
|
213
287
|
const v = SVGMatrix.Vector.from([1, 2, 3]);
|
|
214
288
|
const m = SVGMatrix.Matrix.identity(3);
|
|
215
289
|
const rotation = SVGMatrix.Transforms2D.rotate(Math.PI / 4);
|
|
216
290
|
const [x, y] = SVGMatrix.Transforms2D.applyTransform(rotation, 10, 0);
|
|
217
291
|
|
|
218
|
-
|
|
292
|
+
// See everything that's available:
|
|
293
|
+
console.log('All modules:', Object.keys(SVGMatrix));
|
|
294
|
+
// Output: ['Matrix', 'Vector', 'Transforms2D', 'Transforms3D', 'SVGFlatten', ...]
|
|
219
295
|
</script>
|
|
220
296
|
```
|
|
221
297
|
|
|
298
|
+
**What's inside?** Here's everything you get:
|
|
299
|
+
|
|
300
|
+
| Module | What it does |
|
|
301
|
+
|--------|--------------|
|
|
302
|
+
| `SVGMatrix.Matrix` | Matrix math (multiply, inverse, determinant) |
|
|
303
|
+
| `SVGMatrix.Vector` | Vector math (add, dot product, cross product) |
|
|
304
|
+
| `SVGMatrix.Transforms2D` | 2D transforms (rotate, scale, translate) |
|
|
305
|
+
| `SVGMatrix.Transforms3D` | 3D transforms (rotate around axes) |
|
|
306
|
+
| `SVGMatrix.SVGFlatten` | Flatten SVG transforms into paths |
|
|
307
|
+
| `SVGMatrix.GeometryToPath` | Convert shapes (circle, rect) to paths |
|
|
308
|
+
| `SVGMatrix.PolygonClip` | Boolean operations on polygons |
|
|
309
|
+
| `SVGMatrix.ClipPathResolver` | Resolve SVG clipPath elements |
|
|
310
|
+
| `SVGMatrix.BrowserVerify` | Verify against browser's SVG engine |
|
|
311
|
+
| `SVGMatrix.Logger` | Control library logging |
|
|
312
|
+
|
|
222
313
|
#### Pin to a Specific Version
|
|
223
314
|
|
|
224
315
|
To avoid breaking changes, pin to a specific version:
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -49,7 +49,7 @@ import { Logger, LogLevel, setLogLevel, getLogLevel as getLoggerLevel, enableFil
|
|
|
49
49
|
* Library version
|
|
50
50
|
* @constant {string}
|
|
51
51
|
*/
|
|
52
|
-
export const VERSION = '1.0.
|
|
52
|
+
export const VERSION = '1.0.10';
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
55
|
* Default precision for path output (decimal places)
|