HTMLGraph

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