modern-path2d 1.6.0 → 1.7.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/README.md CHANGED
@@ -26,6 +26,10 @@
26
26
 
27
27
  - Path triangulate (fill、stroke)
28
28
 
29
+ - Hit testing (point in fill / stroke, holes-aware)
30
+
31
+ - Analytical bounding box (lines / beziers / arcs / ellipses)
32
+
29
33
  - Parse SVG to Path2DSet
30
34
 
31
35
  - TypeScript
@@ -97,4 +101,55 @@ console.log(path.fillTriangulate())
97
101
 
98
102
  // triangulate for stroke
99
103
  console.log(path.strokeTriangulate())
104
+
105
+ /**
106
+ * Hit testing
107
+ */
108
+
109
+ // point in fill — holes are honored, fillRule defaults to 'nonzero'
110
+ path.isPointInFill({ x: 75, y: 75 })
111
+
112
+ // concise PathKit-style shorthand for fill containment
113
+ path.contains(75, 75)
114
+
115
+ // point on stroke — within strokeWidth / 2 + tolerance
116
+ path.isPointInStroke({ x: 110, y: 75 }, { strokeWidth: 4, tolerance: 1 })
117
+
118
+ // hit test a whole set top-to-bottom; returns the hit Path2D or undefined
119
+ const set = new Path2DSet([path])
120
+ const hit = set.hitTest({ x: 75, y: 75 }) // Path2D | undefined
121
+
122
+ /**
123
+ * Bounding box
124
+ */
125
+
126
+ // analytical bounds, tight for lines / beziers / arcs / ellipses
127
+ const { x, y, width, height } = path.getBoundingBox()
128
+
129
+ // geometry only, ignoring stroke width
130
+ path.getBoundingBox(false)
131
+ ```
132
+
133
+ ### Low-level geometry helpers
134
+
135
+ Pure functions over flat `[x0, y0, x1, y1, ...]` vertices, useful for building your own hit
136
+ testing. Vertices are treated as an implicitly closed ring.
137
+
138
+ ```ts
139
+ import {
140
+ pointInPolygon,
141
+ pointInPolygons,
142
+ pointToPolylineDistance,
143
+ pointToSegmentDistance,
144
+ } from 'modern-path2d'
145
+
146
+ // single ring (fillRule: 'nonzero' | 'evenodd', default 'nonzero')
147
+ pointInPolygon({ x: 5, y: 5 }, [0, 0, 10, 0, 10, 10, 0, 10]) // true
148
+
149
+ // multi-ring shape with holes — sums winding / crossings across all rings
150
+ pointInPolygons({ x: 5, y: 5 }, [outerRing, innerHole]) // false (in the hole)
151
+
152
+ // distance to a segment / polyline (for stroke hit testing)
153
+ pointToSegmentDistance({ x: 5, y: 1 }, { x: 0, y: 0 }, { x: 10, y: 0 }) // 1
154
+ pointToPolylineDistance({ x: 5, y: 1 }, [0, 0, 10, 0, 10, 10], true) // closed polyline
100
155
  ```