@mocanvas/editor · class

SnapManager

declare class SnapManager

Edge and center snapping against nearby shapes. Candidate shapes are taken from the engine's spatial index (viewport query), so cost does not grow with page size.


Methods · 10

clearIndicators(): void

Drop both the explicit indicators and the derived lines.

clearLines(): void
constructor(editor: Editor)
getCurrentCommonAncestor(): ShapeId | undefined

The parent every selected shape shares, or undefined when they do not share one.

This is what scopes snapping to a frame: a selection entirely inside one frame snaps to that frame's other children, and a selection spanning two frames snaps to nothing, because there is no coordinate space both halves agree on.

getIndicators(): SnapIndicator[]

What the canvas should draw to explain the current snap.

Kept separate from getLines because the two are different vocabularies: a *line* is this renderer's primitive, an *indicator* is the documented, renderer-agnostic description. Setting indicators explicitly replaces the derived ones for as long as they stand.

getLines(): SnapLine[]
getSnappableShapes(): UnknownShape[]

The shapes a drag may snap against.

Siblings under getCurrentCommonAncestor only. Snapping a shape to something in a different frame would align it with a thing it cannot overlap, and the guide line would run through a clipping boundary.

getSnapThreshold(): number

The snap distance in *page* units: the screen threshold divided by the zoom.

setIndicators(indicators: SnapIndicator[]): void

Publish indicators directly, for a tool that snaps something the manager does not.

snapTranslate(moving: BoxLike, exclude: ReadonlySet<ShapeId>, opts?: {

Snap a moving box (already offset by the proposed delta) to nearby shapes. Returns the extra nudge to apply and the guide lines to show.


Properties · 4

readonly editor: Editor
readonly handles: HandleSnaps

Handle snapping: finding a landing point for a dragged endpoint. See HandleSnaps.

readonly shapeBounds: BoundsSnaps

Bounds snapping: aligning a dragged or resized selection with its neighbours. See BoundsSnaps.

threshold: number

Snap distance in screen pixels.

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.