HTMLGraph

Direct Edge Shape

The Direct edge shape can be configured by calling the setDefaults method on CanvasBuilder.

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

const canvas = new CanvasBuilder(element)
  .setDefaults({
    edges: {
      shape: {
        type: "direct",
      },
    },
  })
  .build();

  

In addition to type: "direct", the following options are available:

NameTypeDescriptionRequiredDefault
colorstringColor of the line and arrowsno"#777777"
widthnumberLine widthno1
arrowRendererArrowRendererArrow shapeno{}
arrowLengthnumberFull length of the arrowno20
sourceOffsetnumber | PortOffsetFnEmpty space distance from source port to an edgeno0
targetOffsetnumber | PortOffsetFnEmpty space distance from target port to an edgeno0
hasSourceArrowbooleanDraw an arrow near the source portnofalse
hasTargetArrowbooleanDraw an arrow near the target portnofalse

Alternatively, you can create a Direct shape by passing a factory function into the configuration.

    
import { DirectEdgeShape } from "@html-graph/html-graph";

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

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

  

All parameters are the same as specified in the table.

You can also apply the Direct shape to a specific edge using the addEdge and updateEdge methods.

    
import { DirectEdgeShape } from "@html-graph/html-graph";

canvas.addEdge({
  from: "port-1",
  to: "port-2",
  shape: new DirectEdgeShape(),
});

  
    
canvas.updateEdge("edge-1", {
  shape: new DirectEdgeShape(),
});

  

PortOffsetFn

PortOffsetFn is a function that calculates the arrow offset distance based on edge direction and port parameters. This determines how far from the port center the arrow should be placed.

Here is an example of such a function that positions arrows near the border of a rectangular port:

    
const boxPortOffsetFn = (params) => {
  const { direction, radius } = params;
  const { x, y } = direction;
  const { horizontal, vertical } = radius;
  const tg = y / x;

  const horX = Math.abs(vertical / tg);
  const vertY = Math.abs(horizontal * tg);

  const minX = Math.min(horizontal, horX);
  const minY = Math.min(vertical, vertY);

  return Math.sqrt(minX * minX + minY * minY);
};

  

boxPortOffsetFn is provided out-of-the-box by the library:

    
import { boxPortOffsetFn } from "@html-graph/html-graph";

canvas.addEdge({
  from: "port-1",
  to: "port-2",
  shape: new DirectEdgeShape({
    hasSourceArrow: true,
    hasTargetArrow: true,
    sourceOffset: boxPortOffsetFn,
    targetOffset: boxPortOffsetFn,
  }),
});