HTMLGraph logo HTMLGraph

Interactive Edges

By default, edges in the graph are not interactive. To enable interaction with edges, you need to use the InteractiveEdgeShape decorator.

First, create a canvas with desired edge shape using factory function:

    
import { CanvasBuilder, BezierEdgeShape } from "@html-grapg/html-graph";

const element = document.getElementById("canvas");

const canvas = new CanvasBuilder(element)
  .setDefaults({
    edges: {
      shape: () => {
        return new BezierEdgeShape({
          hasTargetArrow: true,
        });
      },
    },
  })
  .build();

  

The InteractiveEdgeShape decorator provides an invisible interaction area around the visible edge. This example shows how to handle mouse and touch events:

    
import {
  CanvasBuilder,
  BezierEdgeShape,
  InteractiveEdgeShape,
} from "@html-grapg/html-graph";

const element = document.getElementById("canvas");

const canvas = new CanvasBuilder(element)
  .setDefaults({
    edges: {
      shape: (edgeId) => {
        const baseShape = new BezierEdgeShape({
          hasTargetArrow: true,
        });

        const interactiveEdge = new InteractiveEdgeShape(baseShape, {
          width: 20,
        });

        const handler = () => {
            console.log(`clicked on edge with id: ${edgeId}`);
        };

        interactiveEdge.handle.addEventListener("mousedown", (event) => {
          event.stopPropagation();
          handler();
        });

        interactiveEdge.handle.addEventListener("touchstart", (event) => {
          event.stopPropagation();
          handler();
        });

        return interactiveEdge;
      },
    },
  })
  .build();

  

Decorator Parameters

NameTypeDescriptionRequiredDefault
baseShapeStructuredEdgeShapeThe shape to make interactiveYes
interactiveParametersInteractiveParamsConfiguration for interactive behaviorNo{}

Interactive Parameters

NameTypeDescriptionRequiredDefault
widthnumberWidth of the interactive areaNo10

Try out this demo to understand the mechanics of edge interaction:

Interactive edges

When used with connectable ports it is recommended to set edge priority below node priority to ensure ports remain accessible:

    
const canvas = new CanvasBuilder(element)
  .setDefaults({
    nodes: {
      priority: 1,
    },
    edges: {
      priority: 0,
      shape: (edgeId) => {
        const baseShape = new BezierEdgeShape({
          hasTargetArrow: true,
        });

        const interactiveEdge = new InteractiveEdgeShape(baseShape, {
          width: 10,
        });

        // ... event handlers ...

        return interactiveEdge;
      },
    },
  })
  .enableUserDraggableNodes({
    moveOnTop: false,
  })
  .enableUserTransformableViewport()
  .enableBackground()
  .build();

  

Key configuration:

  • Nodes: priority: 1 (higher z-index)
  • Edges: priority: 0 (lower z-index)
  • Draggable nodes: moveOnTop: false maintains priority order during interaction