HTMLGraph

Selectable Nodes

To enable built-in selectable nodes, there are two CanvasBuilder configurations available:

  • enableUserSelectableNodes - when the user clicks inside a node
  • enableUserSelectableCanvas - 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

NameTypeDescriptionRequiredDefault
onNodeSelected(selectedNodeId) => voidFunction to call when node is selectedyes
movementThresholdnumberSpecifies maximum cursor travel, when user actions can still be regarded as selectionno10
mouseDownEventVerifier(event) => booleanFunction to verify if mouse event should initiate node selection processno(event) => event.button === 0
mouseUpEventVerifier(event) => booleanFunction to verify if mouse event should apply node selectionno(event) => event.button === 0

Selectable Canvas Configuration Parameters

NameTypeDescriptionRequiredDefault
onCanvasSelected() => voidFunction to call when canvas is selectedyes
movementThresholdnumberSpecifies maximum cursor travel, when user actions can still be regarded as selectionno10
mouseDownEventVerifier(event) => booleanFunction to verify if mouse event should initiate canvas selection processno(event) => event.button === 0
mouseUpEventVerifier(event) => booleanFunction to verify if mouse event should apply canvas selectionno(event) => event.button === 0