Lock camera zoom
Lock the camera at its current zoom level by collapsing zoomSteps to a single value.
import { DEFAULT_CAMERA_OPTIONS, Tldraw, TLUiOverrides } from 'tldraw'
import 'tldraw/tldraw.css'
// There's a guide at the bottom of this file!
const overrides: TLUiOverrides = {
actions(editor, actions) {
actions.lockCameraZoom = {
id: 'lock-camera-zoom',
kbd: 'shift+k',
onSelect() {
// [1]
const isLocked = editor.getCameraOptions().zoomSteps.length === 1
editor.setCameraOptions({
zoomSteps: isLocked ? DEFAULT_CAMERA_OPTIONS.zoomSteps : [editor.getZoomLevel()],
})
},
}
return actions
},
}
export default function LockCameraZoomExample() {
return (
<div className="tldraw__editor">
<Tldraw persistenceKey="lock-camera-zoom-example" overrides={overrides} />
</div>
)
}
/*
[1]
The camera can only zoom to values in `zoomSteps` (or between the smallest and largest of them). With a
single step equal to the current zoom, every zoom gesture, shortcut, and menu action is a no-op, so the
zoom is effectively locked. Restoring the default steps unlocks it. We use `zoomSteps.length === 1` as
the "is locked" flag so the same shortcut toggles both ways.
*/
The camera's zoomSteps option defines the zoom levels the camera can move between; the smallest and largest steps also bound the zoom range. Setting zoomSteps to a single value equal to the current zoom (editor.setCameraOptions({ zoomSteps: [editor.getZoomLevel()] })) means every zoom gesture, shortcut, and menu action has nowhere to go, so the zoom stays fixed while panning still works. Restoring DEFAULT_CAMERA_OPTIONS.zoomSteps unlocks it.
Press Shift+K to lock the camera at its current zoom, then try scrolling, pinching, or using the zoom menu. Press Shift+K again to unlock.
Is this page helpful?
Prev
Focus the editorNext
Search text on the canvas