Selectable Nodes
To enable built-in selectable nodes, there are two CanvasBuilder configurations available:
enableUserSelectableNodes- when the user clicks inside a nodeenableUserSelectableCanvas- when the user clicks outside of any node
Call them separately or both as needed.
const element = document.getElementById("canvas");
const canvas = new CanvasBuilder(element)
.enableUserSelectableNodes({
onNodeSelected: (selectedNodeId) => {
canvas.graph.getAllNodeIds().forEach((nodeId) => {
const { element } = canvas.graph.getNode(nodeId);
element.classList.toggle("selected", nodeId === selectedNodeId);
});
}
})
.enableUserSelectableCanvas({
onCanvasSelected: () => {
canvas.graph.getAllNodeIds().forEach((nodeId) => {
const { element } = canvas.graph.getNode(nodeId);
element.classList.remove("selected");
});
}
})
.build();
Both methods require a configuration object.
Selectable Nodes Configuration Parameters
| Name | Type | Description | Required | Default |
|---|---|---|---|---|
onNodeSelected | (selectedNodeId) => void | Function to call when node is selected | yes | |
movementThreshold | number | Specifies maximum cursor travel, when user actions can still be regarded as selection | no | 10 |
mouseDownEventVerifier | (event) => boolean | Function to verify if mouse event should initiate node selection process | no | (event) => event.button === 0 |
mouseUpEventVerifier | (event) => boolean | Function to verify if mouse event should apply node selection | no | (event) => event.button === 0 |
Selectable Canvas Configuration Parameters
| Name | Type | Description | Required | Default |
|---|---|---|---|---|
onCanvasSelected | () => void | Function to call when canvas is selected | yes | |
movementThreshold | number | Specifies maximum cursor travel, when user actions can still be regarded as selection | no | 10 |
mouseDownEventVerifier | (event) => boolean | Function to verify if mouse event should initiate canvas selection process | no | (event) => event.button === 0 |
mouseUpEventVerifier | (event) => boolean | Function to verify if mouse event should apply canvas selection | no | (event) => event.button === 0 |