> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ploterr.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SnapEngine

> SnapEngine finds the nearest snap point or grid point.

It does the maths only. Your tool converts the pointer position and draws the indicator.

## Import

```typescript theme={null}
import { SnapEngine, drawingSnapTargets } from '@opengeometry/floorkit';
```

## Example

```typescript theme={null}
import { SnapEngine } from '@opengeometry/floorkit';

const snapper = new SnapEngine(5, 3);
const targets = [
  { x: 20, y: 15 },
  { x: 40, y: 15 },
];
const drafting = snapper.snap({ x: 21, y: 16 }, targets);
const dimensionEndpoint = snapper.snapToTargets({ x: 21, y: 16 }, targets);
snapper.setGridSpacing(10);
snapper.setSnapRadius(2);
console.log(drafting, dimensionEndpoint);
```

## Notes

* Give the points, the grid spacing and the radius in the same unit. Paper tools use millimetres.
* `snap()` tries the nearest target first, then the grid. If it finds none, it returns your point with `snapped: false`.
* `snapToTargets()` does not use the grid.
* Convert screen pixels to your unit before the call.

## Constructor

```typescript theme={null}
constructor(gridSpacing?: number, snapRadius?: number);
```

## Methods

### snap

```typescript theme={null}
snap(point: Point2, targets?: Point2[]): SnapResult;
```

Snap a point.

### snapToTargets

```typescript theme={null}
snapToTargets(point: Point2, targets: Point2[]): SnapResult;
```

Endpoint-only snap (NO grid): the nearest target within `snapRadius`, else the point unchanged.

### setGridSpacing

```typescript theme={null}
setGridSpacing(spacing: number): void;
```

### setSnapRadius

```typescript theme={null}
setSnapRadius(radius: number): void;
```

Set the snap radius (paper mm).

## drawingSnapTargets

Snap candidates from a drawing's primitives: line ends, arc centres, region corners, and optionally segment midpoints.

```typescript theme={null}
function drawingSnapTargets(drawing: VectorDrawing, options?: SnapTargetOptions): Point2[];
```

## Types

### SnapResult

```typescript theme={null}
interface SnapResult {
  point: Point2;
  snapped: boolean;
  snapType?: 'grid' | 'endpoint' | 'midpoint';
}
```

### SnapTargetOptions

The points in a drawing that a snap can lock onto — the geometry a dimension or a dragged vertex should land exactly on.

```typescript theme={null}
interface SnapTargetOptions {
  midpoints?: boolean;
  excludeOgid?: string;
}
```

<ResponseField name="midpoints" type="boolean">
  Include the midpoint of every segment as well as its ends.
</ResponseField>

<ResponseField name="excludeOgid" type="string">
  Skip primitives belonging to this element — a vertex must not snap to the very geometry it is
  dragging.
</ResponseField>

## Related

* [`Point2`](/floorkit/api/annotations/dimension-geometry#point2)
* [`VectorDrawing`](/floorkit/api/drawings/draw-project-view#vectordrawing)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.