Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/tldraw/tldraw/llms.txt

Use this file to discover all available pages before exploring further.

The Editor class is the heart of tldraw. It manages the infinite canvas, handles shape operations, processes user input, and maintains the application state through a reactive store.

Constructor

Create a new Editor instance with the required configuration.
const editor = new Editor({
  store: myStore,
  shapeUtils: [RectangleUtil, CircleUtil],
  bindingUtils: [ArrowBindingUtil],
  tools: [SelectTool, DrawTool],
  getContainer: () => document.getElementById('canvas')!
})
options
TLEditorOptions
required
Configuration object for the editor
store
TLStore
required
The Store instance containing the editor’s data
shapeUtils
TLAnyShapeUtilConstructor[]
required
Array of shape utility classes for creating and managing shapes
bindingUtils
TLAnyBindingUtilConstructor[]
required
Array of binding utility classes for managing shape relationships
tools
TLStateNodeConstructor[]
required
Array of tool classes for handling user interactions
getContainer
() => HTMLElement
required
Function that returns the HTML container element for the editor
user
TLUser
Custom user configuration
initialState
string
The editor’s initial active tool or state node id
autoFocus
boolean
Whether to automatically focus the editor when it mounts
options
Partial<TldrawOptions>
Additional editor options including camera and text configuration

Core properties

store

readonly store: TLStore
The editor’s reactive store containing all records (shapes, pages, assets, etc.).

history

readonly history: HistoryManager<TLRecord>
Manager for undo/redo operations.

user

readonly user: UserPreferencesManager
Manager for user preferences and settings.

inputs

readonly inputs: InputsManager
Manager for tracking mouse, touch, and keyboard input state.

snaps

readonly snaps: SnapManager
Manager for snapping shapes to guides and other shapes.

Shape operations

getShape

Get a shape by its id.
getShape<T extends TLShape = TLShape>(id: TLShapeId): T | undefined
id
TLShapeId
required
The id of the shape to get
shape
T | undefined
The shape with the given id, or undefined if not found
Example:
const shape = editor.getShape('shape:abc123')
if (shape) {
  console.log('Shape type:', shape.type)
}

getShapes

Get multiple shapes by their ids.
getShapes(ids: TLShapeId[]): TLShape[]

createShape

Create a new shape.
createShape<T extends TLShape>(partial: TLCreateShapePartial<T>): this
Example:
editor.createShape({
  type: 'geo',
  x: 100,
  y: 100,
  props: {
    w: 200,
    h: 100,
    geo: 'rectangle'
  }
})

updateShape

Update an existing shape.
updateShape<T extends TLShape>(
  partial: TLShapePartial<T> | null | undefined
): this
Example:
editor.updateShape({
  id: 'shape:abc123',
  x: 150,
  y: 200
})

updateShapes

Update multiple shapes.
updateShapes<T extends TLShape>(partials: (TLShapePartial<T> | null | undefined)[]): this

deleteShape

Delete a shape by its id.
deleteShape(id: TLShapeId): this

deleteShapes

Delete multiple shapes.
deleteShapes(ids: TLShapeId[]): this

getShapeUtil

Get the shape utility for a given shape type.
getShapeUtil<S extends TLShape>(shape: S | TLShapePartial<S> | S['type']): ShapeUtil<S>

Selection

getSelectedShapes

Get all currently selected shapes.
getSelectedShapes(): TLShape[]
shapes
TLShape[]
Array of selected shapes

getSelectedShapeIds

Get the ids of all selected shapes.
getSelectedShapeIds(): TLShapeId[]

setSelectedShapes

Set the selected shapes.
setSelectedShapes(shapes: TLShapeId[] | TLShape[]): this
Example:
// Select by ids
editor.setSelectedShapes(['shape:abc', 'shape:def'])

// Select by shapes
const shapes = editor.getShapesOnPage()
editor.setSelectedShapes(shapes.slice(0, 2))

select

Select one or more shapes.
select(...shapes: TLShapeId[] | TLShape[]): this

selectAll

Select all shapes on the current page.
selectAll(): this

selectNone

Clear the selection.
selectNone(): this

getSelectionPageBounds

Get the bounding box of the current selection.
getSelectionPageBounds(): Box | null

History operations

undo

Undo to the last mark.
undo(): this

redo

Redo to the next mark.
redo(): this

canUndo

Check if undo is available.
canUndo(): boolean

canRedo

Check if redo is available.
canRedo(): boolean

markHistoryStoppingPoint

Create a new mark in the undo/redo history.
markHistoryStoppingPoint(name?: string): string
name
string
Optional name for the mark (useful for debugging)
markId
string
Unique id for the mark that can be used with squashToMark or bailToMark
Example:
const mark = editor.markHistoryStoppingPoint('before rotation')
editor.rotateShapes(selectedShapes, Math.PI / 4)
// If needed, can bail back to the mark
// editor.bailToMark(mark)

run

Run a function in a transaction with optional history control.
run(fn: () => void, opts?: TLEditorRunOptions): this
fn
() => void
required
The callback function to run
opts
TLEditorRunOptions
Options for the transaction
history
'record' | 'record-preserveRedoStack' | 'ignore'
How to handle history for this transaction
ignoreShapeLock
boolean
Whether to allow changes to locked shapes
Example:
// Update without adding to history
editor.run(() => {
  editor.updateShape({ id: myShape.id, x: 100 })
}, { history: 'ignore' })

// Force changes to locked shapes
editor.run(() => {
  editor.deleteShape(lockedShape.id)
}, { ignoreShapeLock: true })

Camera operations

getCamera

Get the current camera state.
getCamera(): TLCamera

setCamera

Set the camera position and zoom.
setCamera(point: VecLike, options?: TLCameraMoveOptions): this

zoomIn

Zoom in the camera.
zoomIn(point?: VecLike, options?: TLCameraMoveOptions): this

zoomOut

Zoom out the camera.
zoomOut(point?: VecLike, options?: TLCameraMoveOptions): this

zoomToFit

Zoom to fit all shapes on the current page.
zoomToFit(options?: TLCameraMoveOptions): this

zoomToSelection

Zoom to fit the current selection.
zoomToSelection(options?: TLCameraMoveOptions): this

Page operations

getCurrentPage

Get the current page.
getCurrentPage(): TLPage

getCurrentPageId

Get the current page id.
getCurrentPageId(): TLPageId

setCurrentPage

Set the current page.
setCurrentPage(page: TLPageId | TLPage): this

getPages

Get all pages.
getPages(): TLPage[]

createPage

Create a new page.
createPage(options?: { name?: string, id?: TLPageId }): this

deletePage

Delete a page.
deletePage(page: TLPageId | TLPage): this

State and tools

getCurrentTool

Get the current active tool.
getCurrentTool(): StateNode

getCurrentToolId

Get the current active tool’s id.
getCurrentToolId(): string

setCurrentTool

Set the active tool.
setCurrentTool(id: string, info?: {}): this
Example:
editor.setCurrentTool('select')
editor.setCurrentTool('draw', { date: Date.now() })

getPath

Get the current path of active states.
getPath(): string
path
string
Path of active states separated by periods (e.g., “select.idle” or “draw.drawing”)

isIn

Check if the editor is in a specific state.
isIn(path: string): boolean
Example:
if (editor.isIn('select.brushing')) {
  console.log('User is brush selecting')
}

Editing

getEditingShape

Get the shape currently being edited.
getEditingShape(): TLShape | undefined

getEditingShapeId

Get the id of the shape currently being edited.
getEditingShapeId(): TLShapeId | null

setEditingShape

Set the shape to edit.
setEditingShape(shape: TLShapeId | TLShape | null): this

Events

The Editor extends EventEmitter and emits various events:
// Listen to all changes
editor.on('change', (changes) => {
  console.log('Store changed:', changes)
})

// Listen to editor updates
editor.on('update', () => {
  console.log('Editor updated')
})

// Listen to tick events
editor.on('tick', (elapsed) => {
  console.log('Tick:', elapsed, 'ms')
})

Lifecycle

dispose

Dispose of the editor and clean up resources.
dispose(): void
Example:
// When unmounting
editor.dispose()

Build docs developers (and LLMs) love