Skip to content

Pivotick API

The list of methods is available online.

Getting nodes and edges

Pivotick provides multiple ways to access nodes and edges. The methods for nodes are listed below (edges work the same way):

MethodReturnsDescription
getNode(id: string | Node)Node | undefinedReturns a deep copy of the node with the given ID.
getNodes()Node[]Returns a deep copy of all nodes.
getMutableNode(id: string | Node)Node | undefinedReturns the actual node object used internally by Pivotick.
getMutableNodes()Node[]Returns all internal node objects.

TIP

Use the standard getters (getNode / getNodes) whenever possible. They provide safe clones that can be modified without affecting the engine’s internal state.

DANGER

Modifying nodes or edges directly via the mutable getters may lead to unexpected behavior. Only use mutable access if you understand the internal engine mechanics; otherwise, prefer the cloned versions.

Adding nodes and edges

You can use the functions addNode() and addEdge() on the graph instance to create new nodes and edges.

js
async function addNode(counter, graph) {
    const newNode = new Node(`n-${counter}`, { label: `Node ${counter}` })
    
    const existingNodes = graph.getMutableNodes()
    const randomIndex = Math.floor(Math.random() * existingNodes.length)
    const randomNode = existingNodes[randomIndex]

    const newEdge = new Edge(`e-${counter}`, randomNode, newNode)
    
    graph.addNode(newNode)
    graph.addEdge(newEdge)

    // Restart physics by nudging the graph a bit
    graph.simulation.reheat(0.7)
    await graph.simulation.waitForSimulationStop()
    graph.renderer.fitAndCenter()
}
js
function loaded(graph) {
    let i = graph.getMutableNodes().length
    addNode(i++, graph)
    setInterval(() => {
        if (i < 10) {
            addNode(i++, graph)
        }
    }, 1500)
}