Skip to content

ctx.layoutPositions(fn, overrides?)

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

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

Preview · Extensions › Layout context › Writing positions and finishing

Read in the complete API reference · All API members

// The discrete layout finisher over the scope — spacingFactor, transform, animate (with the fit-at-final-positions viewport animation), fit/zoom/pan, and the layoutready/layoutstop events
ctx.layoutPositions(fn: (node: Collection, i: number) => Position, overrides?: LayoutBaseOptions): void

The discrete layout finisher over the scope — spacingFactor, transform, animate (with the fit-at-final-positions viewport animation), fit/zoom/pan, and the layoutready/layoutstop events. The wrapper's layoutstart covers the start (no double emit).

Parameters
NameDescription
fn: (node: Collection, i: number) => Position

called per scoped node with the node and its index, returning the model position to place it at

overrides: LayoutBaseOptions?Default: {}

an impl's own defaults, merged over the run's options Shared options apply consistently to built-in and custom layouts. ready and stop are never overridden — the wrapper's stop is what resolves promise()

Fields of overrides
NameDescription
eles: unknown?

the elements to lay out (set automatically by eles.layout()); defaults to the whole graph

fit: boolean?
padding: number?
boundingBox: BoundingBoxInput?
nodeDimensionsIncludeLabels: boolean?

include labels in the node dimensions a layout spaces by — default false. Every built-in's overlap avoidance reads the same boxes, so true keeps labels apart too. Headless label dimensions are estimates.

spacingFactor: number?
transform: (node: unknown, position: Position) => Position?

transform a computed position (e.g. to flip an axis)

animate: boolean?

whether to animate node positions into place

animationDuration: number?
animationEasing: string?
animateFilter: (node: unknown, i: number) => boolean?

which nodes animate (others jump); all by default

ready: () => void?

callback on layoutready

stop: () => void?

callback on layoutstop

zoom: number?

zoom to set when fit is false

pan: Position?

pan to set when fit is false

Returns void

Example

// Inside a custom layout, ctx is supplied by the library.
cy.layout({ impl: { run(ctx) {
ctx.layoutPositions((node, i) => ({ x: i * 40, y: 0 }), { fit: false });
} } }).run();
console.log(cy.getElementById('b').position('x')); // 40

Example graph and handle setup