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)
}

Notes ​

Notes take plain options, the same shape as data.notes, so a stored set can be drawn at any time, not only when the graph is built.

MethodReturnsDescription
addNote(options: NoteOptions | Note)Note | undefinedAdds and draws a note, and emits noteAdd. Throws if the id is taken. Returns undefined while UI.notes.enabled is false.
setNotes(notes: NoteOptions[])Note[]Replaces the notes, leaving nodes and edges alone. Notes are matched by id: a note already on the canvas is updated in place, one missing from the list is removed, and a new one is added.
note.toJSON()NoteOptionsThe note as plain options: id, x, y, width, height, content, color, surface and, when it has one, attachedElement.
getNotes() / getNote(id)Note[] / Note | undefinedThe notes on the graph.

toJSON() is what JSON.stringify calls, so saving the notes is one line, and so is drawing them on another graph:

js
const saved = JSON.stringify(graph.getNotes())

other.setNotes(JSON.parse(saved))
// or one at a time
other.addNote(graph.getNote('n1').toJSON())

A note attached to a node keeps its link when the node exists on the other graph. Whether notes are hidden is a view setting and stays out of toJSON().

The Note class is exported too, as import { Note } from 'pivotick' or Pivotick.Note in the browser build.