@mocanvas/editor · class

Geometry2d

declare abstract class Geometry2d

Base class for shape geometry.


Methods · 31

get area(): number

The enclosed area, or 0 for anything open.

get bounds(): Box
get boundsVertices(): Vec[]

The four corners of Geometry2d.bounds.

get center(): Vec
constructor(opts: Geometry2dOptions)
distanceToLineSegment(A: VecLike, B: VecLike, filters?: Geometry2dFilters): number

The shortest distance from any point of this geometry to the segment A→B.

distanceToPoint(point: VecLike, hitInside?: boolean, filters?: Geometry2dFilters): number
getArea(): number

The enclosed area by the shoelace formula. Open geometry encloses nothing.

getBounds(): Box

The bounds this geometry contributes to its shape. Memoized by bounds.

getBoundsVertices(): Vec[]
getLength(_filters?: Geometry2dFilters): number

The total length of the outline, walked vertex to vertex.

getSvgPathData(first?: boolean): string

This geometry as one subpath of an SVG d attribute.

@param first - whether this is the first subpath in the d. A later one starts with L rather than M, so it joins what came before instead of lifting the pen.

abstract getVertices(filters?: Geometry2dFilters): Vec[]

Flattened outline vertices in shape-local space.

hitTestLineSegment(A: VecLike, B: VecLike, distance?: number, filters?: Geometry2dFilters): boolean

Whether the segment A→B comes within distance of this geometry.

hitTestPoint(point: VecLike, margin?: number, hitInside?: boolean, _filters?: Geometry2dFilters): boolean

Whether point hits this geometry, within margin.

The interior counts when the shape is **filled**, or when the caller asks for hitInside — the two are alternatives, not a requirement and a refinement. hitInside means "count the interior of a HOLLOW shape too", which is what a marquee or a drop target wants; it is not the only way the interior is ever reachable.

This used to test hitInside alone, so a solid rectangle could not be selected by clicking its middle — only its outline answered. Nothing failed and nothing warned: every feature built on hit testing simply returned nothing, which reads as several unrelated bugs rather than one.

An unfilled shape still misses in the middle. A hollow rectangle is a frame around empty space, and clicking the space inside it should reach whatever is behind.

ignoreHit(_point: VecLike): boolean

Whether a hit at _point should be discarded even though it landed. The default answer comes from the ignore flag; a subclass may look at where the hit was.

interpolateAlongEdge(t: number, _filters?: Geometry2dFilters): Vec

The point t of the way along the outline, t in [0, 1]. This is how a label, a handle or an arrow terminal is placed a fixed fraction along a shape's edge and stays there as the shape resizes.

intersectCircle(center: VecLike, radius: number, _filters?: Geometry2dFilters): VecLike[]
intersectLineSegment(A: VecLike, B: VecLike, _filters?: Geometry2dFilters): VecLike[]
intersectPolygon(polygon: VecLike[], _filters?: Geometry2dFilters): VecLike[]
intersectPolyline(polyline: VecLike[], _filters?: Geometry2dFilters): VecLike[]
isExcludedByFilter(filters?: Geometry2dFilters): boolean

Whether filters says to skip this geometry.

isPointInBounds(point: VecLike, margin?: number): boolean

Whether point lies within the bounding box, which is the cheap pre-test.

get length(): number

The length of the outline — the perimeter, for anything closed.

nearestPoint(point: VecLike, _filters?: Geometry2dFilters): Vec
overlapsPolygon(_polygon: VecLike[]): boolean

Whether _polygon overlaps this geometry at all — including the case where one lies entirely within the other, which no edge crossing would reveal.

abstract toPathWords(): number[]

Serialize to the engine's flat path encoding.

toSimpleSvgPath(): string

The whole geometry as a standalone SVG path d.

transform(transform: MatModel, opts?: TransformedGeometry2dOptions): Geometry2d

This geometry seen through transform — the form a parent needs when it asks a child for its outline in the parent's own space. The result is a view onto this geometry, not a copy of it, so transforming is cheap.

uninterpolateAlongEdge(point: VecLike, _filters?: Geometry2dFilters): number

The inverse of Geometry2d.interpolateAlongEdge: how far along the outline the nearest point to point sits, as a t in [0, 1].

get vertices(): Vec[]

Properties · 8

readonly debugColor: string | undefined
readonly excludeFromShapeBounds: boolean
readonly ignore: boolean
readonly isClosed: boolean
readonly isEmptyLabel: boolean
readonly isFilled: boolean
readonly isInternal: boolean
readonly isLabel: boolean

mocanvas is built by Symbio. Free for non-commercial, non-profit and development use; commercial use needs an agreement. Not affiliated with or endorsed by tldraw.