HTMLGraph

Selectable Entities

To enable built-in selectable entities, three CanvasBuilder configurations are available:

  • enableUserSelectableNodes - handles selection when the user clicks inside a node
  • enableUserSelectableEdges - handles selection when the user clicks inside an edge. Edges must be interactive for this feature to work. See the Interactive Edges tutorial.
  • enableUserSelectableCanvas - handles selection when the user clicks outside of any node or edge

Call them separately or all together 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);
      });
    }
  })
  .enableUserSelectableEdges({
    onEdgeSelected: (selectedEdgeId) => {
      canvas.graph.getAllEdgeIds().forEach((edgeId) => {
        const { shape } = canvas.graph.getEdge(edgeId);
        const width = edgeId === selectedEdgeId ? 2 : 1;

        shape.line.setAttribute("stroke-width", `${width}`);
      });
    },
  })
  .enableUserSelectableCanvas({
    onCanvasSelected: () => {
      canvas.graph.getAllNodeIds().forEach((nodeId) => {
        const { element } = canvas.graph.getNode(nodeId);

        element.classList.remove("selected");
      });

      canvas.graph.getAllEdgeIds().forEach((edgeId) => {
        const { shape } = canvas.graph.getEdge(edgeId);

        shape.line.setAttribute("stroke-width", "1");
      });
    },
  })
  .build();

  

All 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 Edges Configuration Parameters

NameTypeDescriptionRequiredDefault
onEdgeSelected(selectedEdgeId) => voidFunction to call when edge 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