Selection
With the lasso
Set the tool to { type: 'lasso' } and draw a loop around strokes to select them. Drag the selection to move it.
From code
CanvasController.setSelection(strokes) selects strokes without the lasso; strokes that are not in the canvas's drawing are ignored. PencilKit has no equivalent.
const canvas = useRef<CanvasController>(null)
// Select every marker stroke.
const markers = drawing.getStrokes().filter((stroke) => stroke.ink.type === 'marker')
canvas.current?.setSelection(markers)
Acting on the selection
| Method | Effect |
|---|---|
getSelection() | The selected strokes, back to front. |
selectedStrokeCount | How many strokes are selected. |
deleteSelection() | Removes the selected strokes as one undo step. |
duplicateSelection() | Copies the selection 16 points down and right, and selects the copies. |
setSelectionColor(color) | Recolours the selection (0xRRGGBBAA) as one undo step. |
Following the selection
onSelectionChange (or controller.addOnSelectionChangeListener) receives the number of selected strokes whenever it changes. Use it to show selection actions:
const [selected, setSelected] = useState(0)
<SketchCanvas ref={canvas} tool={tool} onSelectionChange={setSelected} layout={{ flex: 1 }} />
{selected > 0 ? (
<Button title="Delete" onPress={() => canvas.current?.deleteSelection()} />
) : null}