tldraw/tldraw
Geometry
The geometry primitives that the editor uses for hit-testing, snapping, layout, and rendering. They live in packages/editor/src/lib/primitives/.
Key types
| Type | File | Purpose |
|---|---|---|
Vec |
packages/editor/src/lib/primitives/Vec.ts |
2D point/vector with arithmetic helpers (add, sub, dist, rot, lerp, …). |
Box |
packages/editor/src/lib/primitives/Box.ts |
Axis-aligned bounding box with translate/scale/expand/contains. |
Mat |
packages/editor/src/lib/primitives/Mat.ts |
3x3 affine matrix (translate/rotate/scale/compose/invert). |
Geometry2d |
packages/editor/src/lib/primitives/geometry/ |
Abstract geometry: Polygon2d, Polyline2d, Ellipse2d, Rectangle2d, Group2d, Circle2d, Edge2d. |
intersect* |
packages/editor/src/lib/primitives/intersect.ts |
Segment / polygon / circle intersection helpers. |
easings |
packages/editor/src/lib/primitives/easings.ts |
Standard easing curves used in animations. |
lerp, pointInPolygon, etc. |
packages/editor/src/lib/primitives/utils.ts |
General math helpers. |
How shapes use geometry
Every shape util returns a Geometry2d from getGeometry(). The editor uses the geometry for:
- Hit-testing — "is the pointer inside the shape?"
- Snapping — "does this edge align with a sibling shape?"
- Selection box — "what shapes lie within this rectangle?"
- Edge scrolling — "is the camera near the viewport edge?"
- SVG export — geometry → SVG path string.
graph LR
Shape -->|getGeometry()| Geom[Geometry2d]
Geom --> HitTest[hit-testing]
Geom --> Snap[snapping]
Geom --> Brush[brush selection]
Geom --> SVG[SVG path output]
Geom --> SpatialIndex[SpatialIndexManager bounds]SpatialIndexManager (packages/editor/src/lib/editor/managers/SpatialIndexManager/) wraps rbush to make broad-phase queries (which shapes might be in this rect?) fast even with thousands of shapes.
Camera and screen-to-page
Mat is the workhorse for converting between page space and screen space. The camera is a record (TLCamera) holding { x, y, z }. The screen-to-page matrix and its inverse are computed in the editor and exposed via editor.screenToPage(...), editor.pageToScreen(...).
Tests
Vec.test.ts, Box.test.ts, Mat.test.ts, and intersect.test.ts are some of the most thorough test files in the repo (the intersect test alone is 30 KB).
Related
- @tldraw/editor — uses these primitives in every manager.
- Shapes and tools — how shape utils plug geometry into the editor.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.