Skip to content

ctx.packComponents(spacing?, options?)

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

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

Preview · Extensions › Layout context › Writing positions and finishing

Read in the complete API reference · All API members

// Separate the scope's disconnected components: a one-call, translation-only post-pass
ctx.packComponents(spacing?: number, options?: { bodies?: boolean; includeLabels?: boolean; positions?: Float32Array | Float64Array; }): void

Separate the scope's disconnected components: a one-call, translation-only post-pass. Per-component bounding boxes at the current positions are shelf-packed largest-first with spacing between them, every member translated with its component, and the largest component's centre held fixed — the dominant structure keeps its place and the strays come to it.

The boxes are body boxes (nodeDimensions(), labels included by the run's option), so two singleton components end up spacing apart edge to edge rather than centre to centre; bodies: false restores the 87.1 point boxes.

Components are computed over the scope's own edges (an edge with an endpoint outside the scope connects nothing here), and only the nodeSlots() nodes move — a locked node neither moves nor holds its component in place, so a scope mixing locked and unlocked members of one component can separate them. The write lands through setPositions (one dirty span).

Parameters
NameDescription
spacing: number?Default: 40

the gap between packed component boxes (default 40, the force layout's componentSpacing default)

options: { bodies?: boolean; includeLabels?: boolean; positions?: Float32Array | Float64Array; }?Default: {}

bodies (default true) packs the nodes' boxes rather than their positions; includeLabels overrides the run's nodeDimensionsIncludeLabels for those boxes; positions packs that array (2n, parallel to nodeSlots()) in place instead of the store — for a layout that lands through finish() and must not write positions before the tween starts

Fields of options
NameDescription
bodies: boolean?
includeLabels: boolean?
positions: Float32Array | Float64Array?
Returns void

Example

cy.layout({ impl: { run(ctx) {
ctx.packComponents(20);
console.log(ctx.components().count); // 1
} } }).run();

Example graph and handle setup