svg-path-commander 2.3.5 → 2.4.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/llms.txt ADDED
@@ -0,0 +1,100 @@
1
+ # SVGPathCommander
2
+
3
+ > A TypeScript sourced toolkit for manipulating the `d` (description) attribute of SVG `<path>` elements. Parses any path string into a `PathArray`, provides chainable instance and static methods for normalizing, optimizing, converting, transforming, reversing and morphing paths, plus accurate geometry queries. Works in browser and Node.js, ships strong TypeScript definitions and is verified against real Chromium by a Vitest + Playwright suite with 100% coverage.
4
+
5
+ ## Links
6
+
7
+ - [GitHub Repository](https://github.com/thednp/svg-path-commander)
8
+ - [Live Demo](https://thednp.github.io/svg-path-commander)
9
+ - [README](README.md)
10
+ - [Changelog](CHANGELOG.md)
11
+ - [Agent Guidance](AGENTS.md)
12
+ - [NPM Package](https://www.npmjs.com/package/svg-path-commander)
13
+ - [JSR Package](https://jsr.io/@thednp/svg-path-commander)
14
+ - [CDN](https://www.jsdelivr.com/package/npm/svg-path-commander)
15
+
16
+ ## About
17
+
18
+ `svg-path-commander` is a modern set of TypeScript tools for manipulating the `d` attribute of `SVGPathElement` items. The library:
19
+
20
+ - parses any valid SVG path string (including shorthand commands) into a `PathArray` and serializes it with configurable precision (four decimals by default, or exact values with `round: "off"`);
21
+ - is implemented as a chainable `SVGPathCommander` class with instance methods, while every tool is also exposed as a standalone static method;
22
+ - converts between absolute / relative command sets, to cubic Bézier only (`pathToCurve`), and from other SVG shapes (`<circle>`, `<rect>`, `<polygon>`, `<polyline>`, `<line>`, `<ellipse>`) into `<path>` (`shapeToPath`);
23
+ - applies any CSS / SVG transform function to path commands via the modern `DOMMatrix` API (`@thednp/dommatrix` shim), including 3D to 2D projections, with SVG-consistent transform origins;
24
+ - reverses path draw direction without altering the path (`reversePath`), including specialized variants for `C`-only paths and for other shapes;
25
+ - provides more reliable and more accurate built-in `getBBox`, `getPointAtLength` and `getTotalLength` than the native methods, plus community tools like `getPropertiesAtLength`, `getSegmentOfPoint`, `isPointInStroke`, `pathsIntersection` and `boundingBoxIntersect`;
26
+ - returns the frame of the path at a given length — `getPropertiesAtLength` answers with the point, the unit tangent vector, the unit normal vector (the left normal of the tangent) and the angle of the tangent in degrees, next to the segment metadata;
27
+ - simplifies a path while preserving curves — `simplifyPath` reduces straight-line runs with Douglas-Peucker, fits curve runs with adaptive Schneider cubic Béziers, and retains arcs and original curves whenever fitting would not reduce the segment count; every source sub-path and closure is preserved;
28
+ - resamples every sub-path of a path to a given number of points — `resamplePath` distributes the points along the length of each sub-path and keeps the source segment boundaries and corners when the requested count allows it, so the sub-paths stay separate and the shape of the path keeps its vertices;
29
+ - removes a redundant closing segment without changing the source — `fixPath` returns a fixed copy of the path;
30
+ - keeps complete geometry while morphing multi-subpath paths — outer contours only match outer contours and holes only match holes, whole-path reversal preserves the outer/hole relationships, and unmatched contours collapse at their own centroids instead of disappearing or taking the opposite fill role; closed contours keep their `Z` commands by default and `close: false` opts out;
31
+ - offsets a path along its normals — `offsetPath` shifts the path to one side or outlines the stroke around it, with the `miter`, `bevel` and `round` joins and the `butt`, `square` and `round` caps of SVG; the side is literal, the left side is the left normal of the direction of travel, and the offset is not cleaned up, so a distance larger than the local radius of curvature produces a self-crossing curve;
32
+ - joins and splits the sub-paths of a path — `joinPath` concatenates two paths into one, `splitPath` cuts a path into its sub-paths and `reverseSubPath` reverses one of them;
33
+ - tells if a point is inside a filled shape — `isPointInPath` tests the point against the flattened path with the `nonzero` or the `evenodd` fill rule of the SVG `fill-rule` attribute, and a point on the boundary of the shape is inside of it, the way the browser answers it with `isPointInFill()`;
34
+ - tells if a point is along a path — `isPointInStroke` answers for the path itself, and with the `width` option for the band a stroke paints around it;
35
+ - supports path morphing via `equalize` / `equalizePaths` / `equalizeSegments`; the `equalize` facade is the recommended entry point, it aligns the points of the two paths, normalizes their winding and places their points with the `segment` or the `arcLength` strategy;
36
+ - serves every geometry query from a shared length table — `getPropertiesAtLength`, `getPointAtLength` and the closest point search are linear in the number of segments instead of quadratic (a 4 000 segment path answers a length query in about half a millisecond and a closest point query in under two milliseconds);
37
+ - has a defined answer for an empty or malformed path: a zeroed bounding box, a `{x:0,y:0}` point, `false`, `undefined` or a synthetic `["M", 0, 0]` segment, depending on the tool — the copy-based path tools leave their source path unchanged; `iterate()` is the explicit in-place helper, and the chainable instance methods intentionally update their instance;
38
+ - is built with [tsdown](https://tsdown.dev), producing an ESM bundle (`index.js`), a UMD bundle (`index.min.js`, global `SVGPathCommander`) and one tree-shakeable utility bundle (`util.js`) into `dist/`;
39
+ - ships two entry points, so a bundle only carries the tools it uses: `svg-path-commander` holds the class, its static methods and the public types, while `svg-path-commander/util` holds every standalone tool, including the morphing and intersection engines; the JSR package has the same utility entry;
40
+ - is performance-oriented: a single-pass fast path for 2D transforms, reduced allocations in the parser and string converter, and benchmarks against [SvgPath](https://github.com/fontello/svgpath) via `vpr bench` (results in the README);
41
+ - is JSR-ready: `deno.json` publishes the raw TypeScript source as `@thednp/svg-path-commander` with clean `deno check` / `deno lint` / `deno publish --dry-run`.
42
+
43
+ ## Usage
44
+
45
+ ```ts
46
+ import SVGPathCommander from "svg-path-commander";
47
+
48
+ const path = new SVGPathCommander("M0 0L100 0L50 100");
49
+ path.flipX().toString(); // "M100 0H0L50 100"
50
+ ```
51
+
52
+ ```ts
53
+ import { getPathBBox, pathToCurve } from "svg-path-commander/util";
54
+
55
+ getPathBBox("M0 0L100 0L50 100"); // { x: 0, y: 0, x2: 100, y2: 100, width: 100, height: 100, cx: 50, cy: 50, cz: 150 }
56
+ ```
57
+
58
+ ```ts
59
+ import { equalize } from "svg-path-commander/util";
60
+
61
+ const [eq1, eq2] = equalize("M0 0L100 0L50 100Z", "M0 0L100 0L100 100L0 100Z");
62
+ ```
63
+
64
+ ```ts
65
+ import { pathsIntersection } from "svg-path-commander/util";
66
+
67
+ pathsIntersection("M0 50C0 0,100 0,100 50", "M50 0C100 0,100 100,50 100", false);
68
+ ```
69
+
70
+ ```ts
71
+ import { offsetPath } from "svg-path-commander/util";
72
+
73
+ // the left side is the left normal of the direction of travel, "both" is the
74
+ // outline of the stroke
75
+ offsetPath("M0 0L100 0", 10); // [["M", 0, 10], ["L", 100, 10]]
76
+ offsetPath("M0 0L100 0", 10, { side: "both", join: "round", cap: "round" });
77
+ ```
78
+
79
+ ## API Overview
80
+
81
+ **Constructor** — accepts a non-empty SVG path string and optional instance options (`round` and `origin`), validated against the default options.
82
+
83
+ **Instance methods** — chainable path processors and queries: `toAbsolute`, `toRelative`, `toCurve`, `reverse`, `reverseSubPath`, `join`, `normalize`, `optimize`, `simplify`, `offset`, `transform`, `flipX`, `flipY`; query methods: `getBBox`, `getTotalLength`, `getPointAtLength`, `getPropertiesAtLength`, `isPointInPath`, `isPointInStroke`; plus `toString` and `dispose`.
84
+
85
+ **Getters** — `bbox` and `length`.
86
+
87
+ **Static methods** — the class exposes the parser, conversion, processing, geometry, shape, morphing and intersection tools as static fields, including `parsePathString`, `pathToString`, `pathToAbsolute`, `pathToRelative`, `pathToCurve`, `normalizePath`, `optimizePath`, `simplifyPath`, `offsetPath`, `reversePath`, `reverseSubPath`, `joinPath`, `transformPath`, `roundPath`, `splitPath`, `fixPath`, `getPathBBox`, `getPathArea`, `getDrawDirection`, `getTotalLength`, `getPointAtLength`, `getSegmentAtLength`, `getSegmentOfPoint`, `getPropertiesAtLength`, `getPropertiesAtPoint`, `getClosestPoint`, `isPointInStroke`, `isPointInPath`, `isValidPath`, `equalize`, `equalizePaths`, `equalizeSegments`, `resamplePath`, `pathsIntersection`, `boundingBoxIntersect` and `isPointInsideBBox`. The low-level tools are also available from the `svg-path-commander/util` subpath entry.
88
+
89
+ **Entry points** — `svg-path-commander` holds the class, every static method and the public types; `svg-path-commander/util` holds every standalone tool, including the morphing engine (`equalize`, `equalizePaths`, `equalizeSegments`, `resamplePath`, `fixPath`, `splitCubicSegment`) and the intersection engine (`pathsIntersection`, `boundingBoxIntersect`, `isPointInsideBBox`).
90
+
91
+ **Types** — `PathArray`, `PathSegment`, `PathCommand`, `AbsoluteCommand`, `RelativeCommand`, `NormalSegment`, `MorphPathArray`, `IteratorCallback`, `Options`, `TransformObject`, `PathBBox`, `ShapeTypes`, `ShapeOps`, `ShapeParams`, `OffsetOptions`, `OffsetSide`, `OffsetJoin`, `OffsetCap`, `PointInPathOptions`, `FillRule`, `StrokeOptions`, `EqualizationOptions`, `PathsEqualizationOptions`, `EqualizeOptions`, and per-command segment aliases (`MSegment`, `LSegment`, `CSegment`, `ArcSegment`, ...).
92
+
93
+ ## Key Differences
94
+
95
+ - Works in **Node.js** and browsers — no `SVGPathElement` required for path computation (the `@thednp/dommatrix` shim covers `DOMMatrix`).
96
+ - All transformations are consistent with the SVG coordinate system, including an explicit transform origin for rotation.
97
+ - The built-in `getBBox`, `getTotalLength` and `getPointAtLength` are implemented mathematically (arcs via Gauss-Legendre quadrature) and are more accurate than the native methods.
98
+ - Static methods return safe defaults for empty / invalid input (e.g. `getPathBBox("")` returns a zeroed box) while the constructor throws a `TypeError` on invalid path strings.
99
+ - The class API and the standalone tools share their implementations, but their defaults can differ: static `optimizePath()` rounds to 2 decimals, while instance `optimize()` uses the instance `round` option and falls back to 2 decimals when rounding is off.
100
+ - The version is exposed as `SVGPathCommander.version` and follows `package.json`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svg-path-commander",
3
- "version": "2.3.5",
3
+ "version": "2.4.0",
4
4
  "description": "🛹 Modern TypeScript tools for SVG",
5
5
  "keywords": [
6
6
  "absolute",
@@ -33,7 +33,9 @@
33
33
  "package.json",
34
34
  "README.md",
35
35
  "AGENTS.md",
36
+ "CLAUDE.md",
36
37
  "CHANGELOG.md",
38
+ "llms.txt",
37
39
  "LICENSE"
38
40
  ],
39
41
  "type": "module",
@@ -58,12 +60,12 @@
58
60
  "happy-dom": "^21.0.4",
59
61
  "playwright": "^1.65.1",
60
62
  "typescript": "^7.0.2",
61
- "vite-plus": "1.6.0",
63
+ "vite-plus": "1.7.0",
62
64
  "vitest": "5.1.0"
63
65
  },
64
66
  "catalog": {
67
+ "vite-plus": "1.7.0",
65
68
  "vitest": "5.1.0",
66
- "vite-plus": "1.6.0",
67
69
  "@vitest/browser-playwright": "5.1.0",
68
70
  "@vitest/coverage-istanbul": "5.1.0",
69
71
  "@vitest/ui": "5.1.0"