Skip to content

ctx.setPositions(slots, xy)

ctx.setPositions — Extensions > Layout context > Writing positions and finishing. Cytoscape.js signatures, parameters and return contract.

ctx.setPositions — Extensions › Layout context › Writing positions and finishing

Preview · Extensions › Layout context › Writing positions and finishing

Read in the complete API reference · All API members

// Write model-space coordinates for the supplied node slots
ctx.setPositions(slots: number[], xy: number[] | Float32Array): void

Write model-space coordinates for the supplied node slots. For each index i, xy[i * 2] and xy[i * 2 + 1] are the x and y values for slots[i]. Use this helper to keep rendering and compound bounds up to date.

Parameters
NameDescription
slots: number[]

the node slots to move

xy: number[] | Float32Array

the packed positions: xy[i*2], xy[i*2+1] land on slots[i]

Returns void

Example

// Inside a custom layout, ctx is supplied by the library.
cy.layout({ impl: { run(ctx) {
const slots = Array.from(ctx.nodeSlots());
ctx.setPositions(slots, slots.flatMap((slot, i) => [i * 40, 0]));
} } }).run();
console.log(cy.getElementById('b').position('x')); // 40

Example graph and handle setup