@emasoft/svg-matrix 1.0.8 → 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.
Files changed (3) hide show
  1. package/README.md +190 -1
  2. package/package.json +1 -1
  3. package/src/index.js +1 -1
package/README.md CHANGED
@@ -54,12 +54,164 @@ node test/benchmark-precision.js
54
54
 
55
55
  ## Installation
56
56
 
57
+ ### Node.js (Local Installation)
58
+
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:
64
+
57
65
  ```bash
58
66
  npm install @emasoft/svg-matrix
59
67
  ```
60
68
 
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
76
+
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:
82
+
83
+ ```js
84
+ // example.js
85
+
86
+ // Import only the modules you need
87
+ import { Matrix, Vector, Transforms2D } from '@emasoft/svg-matrix';
88
+
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!');
95
+ ```
96
+
97
+ **Way 2: Import everything at once**
98
+
99
+ This is like getting the entire toolbox:
100
+
61
101
  ```js
62
- import { Matrix, Vector, Transforms2D, Transforms3D, SVGFlatten } from '@emasoft/svg-matrix';
102
+ // example.js
103
+
104
+ // Import EVERYTHING as one big object called "SVGMatrix"
105
+ import * as SVGMatrix from '@emasoft/svg-matrix';
106
+
107
+ // Now use SVGMatrix.ModuleName to access each tool
108
+ const v = SVGMatrix.Vector.from([1, 2, 3]);
109
+ const m = SVGMatrix.Matrix.identity(3);
110
+ const rotation = SVGMatrix.Transforms2D.rotate(Math.PI / 4);
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
131
+ ```
132
+
133
+ #### Complete Working Example
134
+
135
+ Save this as `my-first-example.js`:
136
+
137
+ ```js
138
+ // my-first-example.js
139
+ // A complete example showing the main features
140
+
141
+ import {
142
+ Matrix,
143
+ Vector,
144
+ Transforms2D,
145
+ GeometryToPath,
146
+ SVGFlatten
147
+ } from '@emasoft/svg-matrix';
148
+
149
+ console.log('=== Vector Example ===');
150
+ const v = Vector.from([1, 2, 3]);
151
+ const w = Vector.from([4, 5, 6]);
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());
155
+
156
+ console.log('\n=== Matrix Example ===');
157
+ const A = Matrix.from([[1, 2], [3, 4]]);
158
+ console.log('Matrix A:', A.toNumberArray());
159
+ console.log('Determinant:', A.determinant().toString());
160
+
161
+ console.log('\n=== Transform Example ===');
162
+ // Create a transform: move 100 right, rotate 45°, scale 2x
163
+ const transform = Transforms2D.translation(100, 0)
164
+ .mul(Transforms2D.rotate(Math.PI / 4))
165
+ .mul(Transforms2D.scale(2));
166
+
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)})`);
170
+
171
+ console.log('\n=== SVG Path Example ===');
172
+ // Convert a circle to a path
173
+ const circlePath = GeometryToPath.circleToPathData(0, 0, 50);
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
+ }
195
+ ```
196
+
197
+ Or rename your file from `.js` to `.mjs`.
198
+
199
+ **Using older Node.js syntax (require)?**
200
+
201
+ This library uses modern ES modules. If you have old code using `require()`:
202
+
203
+ ```js
204
+ // OLD WAY (doesn't work directly)
205
+ // const { Matrix } = require('@emasoft/svg-matrix'); // ❌ Error!
206
+
207
+ // NEW WAY (use dynamic import)
208
+ async function main() {
209
+ const { Matrix, Vector } = await import('@emasoft/svg-matrix');
210
+
211
+ const v = Vector.from([1, 2, 3]);
212
+ console.log(v.norm().toString());
213
+ }
214
+ main();
63
215
  ```
64
216
 
65
217
  ### Browser Usage (CDN)
@@ -121,6 +273,43 @@ Popular CDN with good caching.
121
273
  </script>
122
274
  ```
123
275
 
276
+ #### Import Everything at Once
277
+
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.
279
+
280
+ ```html
281
+ <script type="module">
282
+ // Import EVERYTHING as one object called "SVGMatrix"
283
+ // The "* as" means "everything as"
284
+ import * as SVGMatrix from 'https://esm.sh/@emasoft/svg-matrix';
285
+
286
+ // Now use SVGMatrix.ModuleName to access each tool
287
+ const v = SVGMatrix.Vector.from([1, 2, 3]);
288
+ const m = SVGMatrix.Matrix.identity(3);
289
+ const rotation = SVGMatrix.Transforms2D.rotate(Math.PI / 4);
290
+ const [x, y] = SVGMatrix.Transforms2D.applyTransform(rotation, 10, 0);
291
+
292
+ // See everything that's available:
293
+ console.log('All modules:', Object.keys(SVGMatrix));
294
+ // Output: ['Matrix', 'Vector', 'Transforms2D', 'Transforms3D', 'SVGFlatten', ...]
295
+ </script>
296
+ ```
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
+
124
313
  #### Pin to a Specific Version
125
314
 
126
315
  To avoid breaking changes, pin to a specific version:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emasoft/svg-matrix",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
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",
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.8';
52
+ export const VERSION = '1.0.10';
53
53
 
54
54
  /**
55
55
  * Default precision for path output (decimal places)