HTMLGraph logo HTMLGraph

Canvas

The Canvas API provides a set of methods and properties for managing graph visualization.

Canvas uses a fluent interface, allowing methods to be chained for concise configuration. For example:

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

const canvas = new CanvasBuilder(element).build();

canvas
  .addNode({ x: 100, y: 100, element: document.createElement('div') })
  .addNode({ x: 200, y: 200, element: document.createElement('div') })
  .addNode({ x: 300, y: 300, element: document.createElement('div') })
  .patchViewportMatrix({ x: -100, y: -100 });

  

Below is a list of all available methods


Add Node

Adds a node to the canvas with customizable properties.

    
canvas.addNode({
  id: "node-1",
  x: 100,
  y: 100,
  element: document.createElement('div'),
});

  

For convenience, static ports can be defined directly during node creation.

    
const node = document.createElement('div');
const port = document.createElement('div');
node.appendChild(port);

canvas.addNode({
  id: "node-1",
  element: node,
  x: 100,
  y: 100,
  ports: [
    {
      id: "port-1",
      element: port,
    },
  ],
});

  

All available parameters are demonstrated in the following example:

    
const node = document.createElement('div');
const port = document.createElement('div');
node.appendChild(port);

canvas.addNode({
  id: "node-1",
  element: node,
  x: 100,
  y: 100,
  ports: [
    {
      id: "port-1",
      element: port,
      direction: 0,
    },
  ],
  centerFn: (w, h) => ({ x: w / 2, y: h / 2 }),
  priority: 1,
});

  

Parameters

NameTypeDescriptionRequiredDefault
idanyUnique identifier for the nodenoautogenerated
elementHTMLElementHTML element for the nodeyes
xnumberX-coordinate of the nodeyes
ynumberY-coordinate of the nodeyes
portsArray<Port>Array of portsno[]
centerFnfunctionFunction to determine the node’s centerno(w, h) => ({x: w/2, y: h/2})
prioritynumberZ-index of the nodeno0

Port Parameters

NameTypeDescriptionRequiredDefault
idanyUnique identifier for the portnoautogenerated
elementHTMLElementHTML element for the portyes
directionnumberRadian angle for edge directionno0

Update Node

Updates specified properties of a node.

    
canvas.updateNode(&#34;node-1&#34;, {
  x: 100,
  y: 100,
});

  

All available parameters are demonstrated in the following example:

    
canvas.updateNode(&#34;node-1&#34;, {
  x: 100,
  y: 100,
  priority: 10,
  centerFn: () =&gt; ({ x: 0, y: 0 }),
});

  

When called without parameters, the coordinates of all edges connected to the node will be updated.

    
canvas.updateNode(&#34;node-1&#34;);

  

Parameters for updateNode:

NameTypeDescriptionRequiredDefault
idanyIdentifier of the node to updateyes
updateRequestNodeUpdateRequestProperties to updateno{}

NodeUpdateRequest Parameters

NameTypeDescriptionRequired
xnumberX-coordinate of the nodeno
ynumberY-coordinate of the nodeno
prioritynumberZ-index of the nodeno
centerFnfunctionFunction to determine the node’s centerno

Remove Node

Removes a specified node from the canvas. All associated ports will be unmarked, and any edges connected to the node will also be removed.

    
canvas.removeNode(&#34;node-1&#34;);

  

Parameters for removeNode:

NameTypeDescriptionRequired
idanyIdentifier of the node to removeyes

Mark Port

Marks an element as a port for a specific node.

    
const portElement = document.createElement(&#39;div&#39;);

nodeElement.appendChild(portElement);

canvas.markPort({
  nodeId: &#34;node-1&#34;,
  element: portElement,
});

  

All available parameters are demonstrated in the following example:

    
canvas.markPort({
  id: &#34;port-1&#34;,
  nodeId: &#34;node-1&#34;,
  element: portElement,
  direction: -Math.PI,
});

  

Parameters for markPort:

NameTypeDescriptionRequiredDefault
idanyUnique identifier for the portnoautogenerated
nodeIdanyIdentifier of the nodeyes
elementHTMLElementHTML element for the portyes
directionnumberRadian angle for edge directionno0

Static ports can also be defined directly within the addNode method.


Update Port

Updates specified properties of a port.

    
canvas.updatePort(&#34;port-1&#34;, {
  direction: -Math.PI,
});

  

When called without parameters, the coordinates of all edges connected to the port will be updated.

    
canvas.updatePort(&#34;port-1&#34;);

  

Parameters for updatePort:

NameTypeDescriptionRequiredDefault
idanyIdentifier of the port to updateyes
updateRequestPortUpdateRequestProperties to updateno{}

PortUpdateRequest Parameters

NameTypeDescriptionRequired
directionnumberRadian angle for edge directionno

Unmark Port

Unmarks a specified port, removing relation to the node. All edges connected to the port will also be removed.

    
canvas.unmarkPort(&#34;port-1&#34;);

  

Parameters for unmarkPort:

NameTypeDescriptionRequired
idanyIdentifier of the port to unmarkyes

Add Edge

Adds an edge between two ports on the canvas.

    
canvas.addEdge({
  id: &#34;edge-1&#34;,
  from: &#34;port-1&#34;,
  to: &#34;port-2&#34;,
});

  

All available parameters are demonstrated in the following example:

    
import { BezierEdgeShape } from &#34;@html-graph/html-graph&#34;;

canvas.addEdge({
  id: &#34;edge-1&#34;,
  from: &#34;port-1&#34;,
  to: &#34;port-2&#34;,
  shape: new BezierEdgeShape(),
  priority: 5,
});

  

Parameters:

NameTypeDescriptionRequiredDefault
idanyUnique identifier for the edgenoautogenerated
fromanyIdentifier of the source portyes
toanyIdentifier of the target portyes
shapeEdgeShapeShape of the edgenoBezierEdgeShape
prioritynumberZ-index of the edgeno0

Update Edge

Updates specified properties of an edge.

    
canvas.updateEdge(&#34;edge-1&#34;, {
  from: &#34;port-3&#34;,
  to: &#34;port-4&#34;,
});

  

All available parameters are demonstrated in the following example:

    
canvas.updateEdge(&#34;edge-1&#34;, {
  from: &#34;port-3&#34;,
  to: &#34;port-4&#34;,
  shape: new HorizontalEdgeShape(),
  priority: 10,
});

  

When called without parameters, the edge’s coordinates will be recalculated.

    
canvas.updateEdge(&#34;edge-1&#34;);

  

Parameters for updateEdge:

NameTypeDescriptionRequiredDefault
idanyIdentifier of the edge to updateyes
updateRequestEdgeUpdateRequestProperties to updateno{}

EdgeUpdateRequest Parameters

NameTypeDescriptionRequired
fromanyIdentifier of source portno
toanyIdentifier of target portno
shapeEdgeShapeEdge shapeno
prioritynumberZ-index of the edgeno

Remove Edge

Removes a specified edge from the canvas.

    
canvas.removeEdge(&#34;edge-1&#34;);

  

Parameters for removeEdge:

NameTypeDescriptionRequired
idanyIdentifier of the edge to removeyes

Clear

Removes all nodes and edges from the canvas, allowing it to be reused.

    
canvas.clear();

  

Destroy

Destroys the canvas. The provided HTML element gets rolled back to its initial state. Once this method is called, the canvas cannot be reused.

    
canvas.destroy();

  

Patch Viewport Matrix

The viewport transformation matrix consists of three values: scale, x, and y.

  • Scale: Represents the current scaling factor of the viewport. A larger scale zooms out, making the content appear smaller, while a smaller scale zooms in, making the content appear larger.
  • X: The horizontal distance from the absolute zero point to the top-left corner of the viewport.
  • Y: The vertical distance from the absolute zero point to the top-left corner of the viewport.

To better understand how these values work, experiment with the demo below.

Viewport Matrix

To update the viewport matrix values, use the patchViewportMatrix method. All parameters are optional.

    
canvas.patchViewportMatrix({
  scale: 0.5,
  x: -50,
  y: -50,
});

  

This matrix is the inverse of the content matrix.


Patch Content Matrix

The content transformation matrix consists of three values: scale, x, and y.

  • Scale: Represents the current scaling factor of the content. A larger scale makes the content appear bigger, while a smaller scale zooms out, making the content appear smaller.
  • X: The horizontal distance from the top-left corner of the viewport to the absolute zero point.
  • Y: The vertical distance from the top-left corner of the viewport to the absolute zero point.

To better understand how these values work, experiment with the demo below.

Content Matrix

To update the content matrix values, use the patchContentMatrix method. All parameters are optional.

    
canvas.patchContentMatrix({
  scale: 2,
  x: 100,
  y: 100,
});

  

This matrix is the inverse of the viewport matrix.