Replace a built-in overlay

Replace the built-in selection brush by subclassing BrushOverlayUtil and overriding its render method.

import { Tldraw } from 'tldraw'
import 'tldraw/tldraw.css'
import { DashedBrushOverlayUtil } from './DashedBrushOverlayUtil'

// There's a guide at the bottom of this file!

// [1]
const overlayUtils = [DashedBrushOverlayUtil]

export default function ReplaceBrushOverlayExample() {
	return (
		<div className="tldraw__editor">
			<Tldraw overlayUtils={overlayUtils} />
		</div>
	)
}

/*
The canvas overlays (brush, scribble, snap indicators, selection handles, and so on) are all
`OverlayUtil` subclasses. To replace one, subclass the built-in util and override the methods you
want to change.

`DashedBrushOverlayUtil.ts` extends `BrushOverlayUtil` and overrides `render` to draw a dashed purple
rectangle. `getOverlays`, `isActive`, and the static `type` are inherited, so it activates exactly when
the built-in brush would. Try drag-selecting a region on the canvas.

[1]
`<Tldraw>` merges the overlay utils you pass with the defaults, and a custom util whose static `type`
matches a default replaces it. Since our subclass inherits `type = 'brush'`, passing it alone is enough
to swap out the built-in brush. `shapeUtils` and `bindingUtils` merge by static `type` the same
way; `tools` merge by static `id`.
*/

Every canvas overlay (the selection brush, scribble, snap indicators, shape handles) is an OverlayUtil subclass you can replace. This example extends the built-in BrushOverlayUtil and overrides render to draw a dashed purple rectangle instead of the default. Because the subclass inherits the brush type, passing it in overlayUtils replaces the default brush; <Tldraw> merges custom utils over the defaults by type.

Drag-select on the canvas to see it.

Is this page helpful?
Prev
Reactive inputs
Next
Toggle focus mode