Skip to content

Geometry

Geometry exports model shapes emitted by MouseDraw and provide point containment checks for selection workflows.

The detailed API pages are generated from JSDoc in src/components/geometry.ts. Use this page as a map to the generated reference.

Types

ExportDescription
PointCoordinate pair: { x, y }.
ShapeTypeShape discriminator union.
AnyShapeUnion of all built-in geometry shapes.

Classes

ExportDescription
Shape<TType>Abstract base class implemented by all geometry classes.
RectAxis-aligned rectangle.
EllipseAxis-aligned ellipse.
PolygonPolygon defined by ordered vertices.
CircularSectorCircular sector used by pie slices.
AnnularSectorRing-shaped circular sector used by donut slices.

Selection Workflow

Use the type discriminator to narrow emitted shapes.

ts
function handleShape(shape: AnyShape) {
  if (shape.type === 'rect') {
    console.log(shape.width)
  }
}

All built-in shapes expose contains(point) and svgPath(). Boundary points are included for rectangles, ellipses, and polygons. Ellipses with non-positive radii reject all points in contains(). Sector shapes assume non-negative inner radii, positive outer radii, and positive sweep angles for containment checks. svgPath() passes raw radius values into SVG path data, so invalid SVG radius values may not render. Polygons with fewer than three vertices reject all points.

Use svgPath() when a shape should be rendered directly as an SVG <path>.

The built-in geometry classes are intentionally lightweight: they model selection shapes and answer simple containment questions. For robust computational geometry, spatial indexing, or geospatial workflows, use a specialized package and convert results to Rect, Ellipse, Polygon, or your own application shape type at the boundary.

PackageUse it forNotes
polygon-clippingPolygon union, intersection, difference, and xor.Use for boolean operations on polygons and multipolygons.
geometricLightweight point, line, polygon, and angle utilities.Good when you want plain JavaScript array geometry primitives with TypeScript declarations.
@turf/turf or individual Turf modulesGeospatial analysis with GeoJSON.Best when coordinates represent geographic data rather than plain SVG/data-space geometry.
rbushFast 2D spatial indexing for points and rectangles.Useful when hit-testing many shapes or filtering candidates before exact contains checks.
d3-delaunayDelaunay triangulation, Voronoi diagrams, and nearest-point lookup.Useful for nearest-neighbor interactions on scatter plots.
d3-polygonPolygon area, centroid, hull, length, and containment helpers.Good for lightweight planar polygon math in the D3 ecosystem.

Prefer these libraries when correctness around self-intersections, holes, topology, projections, or large shape sets matters.