@emasoft/svg-matrix 1.0.7 → 1.0.9
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 +245 -3
- package/package.json +1 -1
- package/src/index.js +1 -1
package/README.md
CHANGED
|
@@ -54,22 +54,264 @@ node test/benchmark-precision.js
|
|
|
54
54
|
|
|
55
55
|
## Installation
|
|
56
56
|
|
|
57
|
+
### Node.js (Local Installation)
|
|
58
|
+
|
|
59
|
+
**Step 1: Install the package**
|
|
60
|
+
|
|
57
61
|
```bash
|
|
58
62
|
npm install @emasoft/svg-matrix
|
|
59
63
|
```
|
|
60
64
|
|
|
65
|
+
**Step 2: Import what you need**
|
|
66
|
+
|
|
67
|
+
You can import specific modules (recommended - smaller bundle):
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
import { Matrix, Vector, Transforms2D } from '@emasoft/svg-matrix';
|
|
71
|
+
|
|
72
|
+
const rotation = Transforms2D.rotate(Math.PI / 4);
|
|
73
|
+
console.log(rotation);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Or import everything as a single namespace:
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
import * as SVGMatrix from '@emasoft/svg-matrix';
|
|
80
|
+
|
|
81
|
+
// Now use SVGMatrix.ModuleName
|
|
82
|
+
const v = SVGMatrix.Vector.from([1, 2, 3]);
|
|
83
|
+
const m = SVGMatrix.Matrix.identity(3);
|
|
84
|
+
const rotation = SVGMatrix.Transforms2D.rotate(Math.PI / 4);
|
|
85
|
+
const pathData = SVGMatrix.GeometryToPath.circleToPathData(100, 100, 50);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Complete Node.js Example**
|
|
89
|
+
|
|
90
|
+
Save this as `example.js` and run with `node example.js`:
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
// example.js
|
|
94
|
+
import {
|
|
95
|
+
Matrix,
|
|
96
|
+
Vector,
|
|
97
|
+
Transforms2D,
|
|
98
|
+
GeometryToPath,
|
|
99
|
+
SVGFlatten
|
|
100
|
+
} from '@emasoft/svg-matrix';
|
|
101
|
+
|
|
102
|
+
// 1. Vector operations
|
|
103
|
+
const v = Vector.from([1, 2, 3]);
|
|
104
|
+
const w = Vector.from([4, 5, 6]);
|
|
105
|
+
console.log('Dot product:', v.dot(w).toString());
|
|
106
|
+
console.log('Cross product:', v.cross(w).toNumberArray());
|
|
107
|
+
|
|
108
|
+
// 2. Matrix operations
|
|
109
|
+
const A = Matrix.from([[1, 2], [3, 4]]);
|
|
110
|
+
console.log('Determinant:', A.determinant().toString());
|
|
111
|
+
console.log('Inverse:', A.inverse().toNumberArray());
|
|
112
|
+
|
|
113
|
+
// 3. 2D Transforms
|
|
114
|
+
const transform = Transforms2D.translation(100, 50)
|
|
115
|
+
.mul(Transforms2D.rotate(Math.PI / 4))
|
|
116
|
+
.mul(Transforms2D.scale(2));
|
|
117
|
+
|
|
118
|
+
const [x, y] = Transforms2D.applyTransform(transform, 10, 10);
|
|
119
|
+
console.log(`Transformed point: (${x}, ${y})`);
|
|
120
|
+
|
|
121
|
+
// 4. SVG path transformation
|
|
122
|
+
const circlePath = GeometryToPath.circleToPathData(0, 0, 50);
|
|
123
|
+
const transformedPath = SVGFlatten.transformPathData(circlePath, transform);
|
|
124
|
+
console.log('Transformed circle path:', transformedPath);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**Using with CommonJS (require)**
|
|
128
|
+
|
|
129
|
+
This library uses ES modules. If you need CommonJS:
|
|
130
|
+
|
|
61
131
|
```js
|
|
62
|
-
|
|
132
|
+
// Option 1: Dynamic import (recommended)
|
|
133
|
+
async function main() {
|
|
134
|
+
const { Matrix, Vector } = await import('@emasoft/svg-matrix');
|
|
135
|
+
const v = Vector.from([1, 2, 3]);
|
|
136
|
+
console.log(v.norm().toString());
|
|
137
|
+
}
|
|
138
|
+
main();
|
|
139
|
+
|
|
140
|
+
// Option 2: Add "type": "module" to your package.json
|
|
141
|
+
// Then you can use import statements directly
|
|
63
142
|
```
|
|
64
143
|
|
|
65
|
-
### CDN
|
|
144
|
+
### Browser Usage (CDN)
|
|
145
|
+
|
|
146
|
+
You can use this library directly in a web browser without installing anything. Just add a `<script>` tag to your HTML file.
|
|
147
|
+
|
|
148
|
+
**What is a CDN?** A CDN (Content Delivery Network) hosts the library files so you can load them directly in your browser. No `npm install` needed!
|
|
149
|
+
|
|
150
|
+
#### Option 1: esm.sh (Recommended)
|
|
151
|
+
|
|
152
|
+
Best for modern browsers. Automatically handles dependencies.
|
|
153
|
+
|
|
154
|
+
```html
|
|
155
|
+
<!DOCTYPE html>
|
|
156
|
+
<html>
|
|
157
|
+
<head>
|
|
158
|
+
<title>SVG Matrix Example</title>
|
|
159
|
+
</head>
|
|
160
|
+
<body>
|
|
161
|
+
<script type="module">
|
|
162
|
+
// Import the modules you need
|
|
163
|
+
import { Matrix, Vector, Transforms2D } from 'https://esm.sh/@emasoft/svg-matrix';
|
|
164
|
+
|
|
165
|
+
// Now you can use them!
|
|
166
|
+
const rotation = Transforms2D.rotate(Math.PI / 4); // 45 degrees
|
|
167
|
+
const [x, y] = Transforms2D.applyTransform(rotation, 10, 0);
|
|
168
|
+
|
|
169
|
+
console.log(`Point (10, 0) rotated 45 degrees = (${x}, ${y})`);
|
|
170
|
+
</script>
|
|
171
|
+
</body>
|
|
172
|
+
</html>
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
#### Option 2: unpkg
|
|
176
|
+
|
|
177
|
+
Another reliable CDN option.
|
|
66
178
|
|
|
67
179
|
```html
|
|
68
180
|
<script type="module">
|
|
69
|
-
import {
|
|
181
|
+
import { SVGFlatten, GeometryToPath } from 'https://unpkg.com/@emasoft/svg-matrix/src/index.js';
|
|
182
|
+
|
|
183
|
+
// Convert a circle to a path
|
|
184
|
+
const pathData = GeometryToPath.circleToPathData(100, 100, 50);
|
|
185
|
+
console.log(pathData);
|
|
70
186
|
</script>
|
|
71
187
|
```
|
|
72
188
|
|
|
189
|
+
#### Option 3: jsDelivr
|
|
190
|
+
|
|
191
|
+
Popular CDN with good caching.
|
|
192
|
+
|
|
193
|
+
```html
|
|
194
|
+
<script type="module">
|
|
195
|
+
import { Matrix, Vector } from 'https://cdn.jsdelivr.net/npm/@emasoft/svg-matrix/src/index.js';
|
|
196
|
+
|
|
197
|
+
const v = Vector.from([1, 2, 3]);
|
|
198
|
+
const w = Vector.from([4, 5, 6]);
|
|
199
|
+
console.log('Dot product:', v.dot(w).toString());
|
|
200
|
+
</script>
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
#### Import Everything as a Namespace
|
|
204
|
+
|
|
205
|
+
If you want all modules under one name:
|
|
206
|
+
|
|
207
|
+
```html
|
|
208
|
+
<script type="module">
|
|
209
|
+
// Import everything as "SVGMatrix"
|
|
210
|
+
import * as SVGMatrix from 'https://esm.sh/@emasoft/svg-matrix';
|
|
211
|
+
|
|
212
|
+
// Now use SVGMatrix.ModuleName
|
|
213
|
+
const v = SVGMatrix.Vector.from([1, 2, 3]);
|
|
214
|
+
const m = SVGMatrix.Matrix.identity(3);
|
|
215
|
+
const rotation = SVGMatrix.Transforms2D.rotate(Math.PI / 4);
|
|
216
|
+
const [x, y] = SVGMatrix.Transforms2D.applyTransform(rotation, 10, 0);
|
|
217
|
+
|
|
218
|
+
console.log('All modules available:', Object.keys(SVGMatrix));
|
|
219
|
+
</script>
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
#### Pin to a Specific Version
|
|
223
|
+
|
|
224
|
+
To avoid breaking changes, pin to a specific version:
|
|
225
|
+
|
|
226
|
+
```html
|
|
227
|
+
<!-- esm.sh with version -->
|
|
228
|
+
<script type="module">
|
|
229
|
+
import { Transforms2D } from 'https://esm.sh/@emasoft/svg-matrix@1.0.7';
|
|
230
|
+
</script>
|
|
231
|
+
|
|
232
|
+
<!-- unpkg with version -->
|
|
233
|
+
<script type="module">
|
|
234
|
+
import { Matrix } from 'https://unpkg.com/@emasoft/svg-matrix@1.0.7/src/index.js';
|
|
235
|
+
</script>
|
|
236
|
+
|
|
237
|
+
<!-- jsDelivr with version -->
|
|
238
|
+
<script type="module">
|
|
239
|
+
import { Vector } from 'https://cdn.jsdelivr.net/npm/@emasoft/svg-matrix@1.0.7/src/index.js';
|
|
240
|
+
</script>
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
#### Complete Working Example
|
|
244
|
+
|
|
245
|
+
Save this as `example.html` and open it in your browser:
|
|
246
|
+
|
|
247
|
+
```html
|
|
248
|
+
<!DOCTYPE html>
|
|
249
|
+
<html>
|
|
250
|
+
<head>
|
|
251
|
+
<title>SVG Matrix - Complete Example</title>
|
|
252
|
+
<style>
|
|
253
|
+
body { font-family: sans-serif; padding: 20px; }
|
|
254
|
+
svg { border: 1px solid #ccc; }
|
|
255
|
+
pre { background: #f5f5f5; padding: 10px; }
|
|
256
|
+
</style>
|
|
257
|
+
</head>
|
|
258
|
+
<body>
|
|
259
|
+
<h1>SVG Matrix Demo</h1>
|
|
260
|
+
|
|
261
|
+
<h2>Original Circle</h2>
|
|
262
|
+
<svg width="200" height="200">
|
|
263
|
+
<circle id="original" cx="100" cy="100" r="40" fill="blue" opacity="0.5"/>
|
|
264
|
+
</svg>
|
|
265
|
+
|
|
266
|
+
<h2>Transformed (rotate 45 + scale 1.5)</h2>
|
|
267
|
+
<svg width="200" height="200">
|
|
268
|
+
<path id="transformed" fill="red" opacity="0.5"/>
|
|
269
|
+
</svg>
|
|
270
|
+
|
|
271
|
+
<h2>Generated Path Data</h2>
|
|
272
|
+
<pre id="output"></pre>
|
|
273
|
+
|
|
274
|
+
<script type="module">
|
|
275
|
+
import {
|
|
276
|
+
Transforms2D,
|
|
277
|
+
GeometryToPath,
|
|
278
|
+
SVGFlatten
|
|
279
|
+
} from 'https://esm.sh/@emasoft/svg-matrix';
|
|
280
|
+
|
|
281
|
+
// Step 1: Convert circle to path
|
|
282
|
+
const circlePath = GeometryToPath.circleToPathData(100, 100, 40);
|
|
283
|
+
|
|
284
|
+
// Step 2: Create a combined transform (rotate 45 degrees, then scale 1.5x)
|
|
285
|
+
const transform = Transforms2D.scale(1.5)
|
|
286
|
+
.mul(Transforms2D.rotate(Math.PI / 4));
|
|
287
|
+
|
|
288
|
+
// Step 3: Apply transform to the path
|
|
289
|
+
const transformedPath = SVGFlatten.transformPathData(circlePath, transform);
|
|
290
|
+
|
|
291
|
+
// Step 4: Display the result
|
|
292
|
+
document.getElementById('transformed').setAttribute('d', transformedPath);
|
|
293
|
+
document.getElementById('output').textContent = transformedPath;
|
|
294
|
+
</script>
|
|
295
|
+
</body>
|
|
296
|
+
</html>
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
#### Troubleshooting
|
|
300
|
+
|
|
301
|
+
**"Cannot use import statement outside a module"**
|
|
302
|
+
Make sure you have `type="module"` in your script tag:
|
|
303
|
+
```html
|
|
304
|
+
<script type="module"> <!-- This is required! -->
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
**CORS errors when opening HTML file directly**
|
|
308
|
+
Some browsers block CDN imports when opening files with `file://`. Solutions:
|
|
309
|
+
1. Use a local server: `npx serve .` or `python -m http.server`
|
|
310
|
+
2. Or use a code playground like CodePen, JSFiddle, or StackBlitz
|
|
311
|
+
|
|
312
|
+
**Want to use with older browsers?**
|
|
313
|
+
This library requires ES modules (modern browsers). For IE11 or very old browsers, you'll need a bundler like Webpack or Rollup.
|
|
314
|
+
|
|
73
315
|
## CLI
|
|
74
316
|
|
|
75
317
|
The library includes a command-line interface for batch processing SVG files.
|
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.9';
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
55
|
* Default precision for path output (decimal places)
|