layout
Layout is the process of assigning world-space positions (x, y, z) to every node in the graph. The layout namespace groups all position-related operations: geometric layouts that place nodes in structured patterns, spatial transforms that move or reshape an existing arrangement, and direct position read/write for custom algorithms.
Categories
Geometric layouts
These methods compute a new position for every node (or an explicit subset) based on a mathematical pattern:
- layout.circle — evenly spaced around a ring on the XY plane.
- layout.grid — arranged in rows and columns.
- layout.line — strung along a straight axis.
- layout.spiral — wound outward in a coil.
- layout.sphere — distributed uniformly on a sphere surface.
- layout.cube — placed at the vertices of a cube.
- layout.scatter — scattered randomly within a bounding box.
- layout.ego — radial tree rooted at one or more hub nodes.
Pass undefined as the first argument to apply the layout to all nodes, or pass an array of node ids to restrict it to a subset.
Spatial transforms
These methods adjust the positions of an already-arranged graph:
- layout.rotate — rotate around an axis by a given angle in degrees.
- layout.scale — expand or compress positions relative to their centroid.
- layout.shift — translate all nodes by an offset vector.
- layout.alignBy — flatten positions onto a single value along one axis.
Position read / write
- layout.getPosition — synchronously read the current
{ x, y, z }of one node. - layout.setPositions — write absolute positions for any set of nodes, bypassing layout computation. Useful for restoring a saved layout or driving positions from an external algorithm.
Animation and timing
By default all layout methods animate from current to target positions over 500 ms using an ease-in-out tween (LayoutTween). The method's returned Promise resolves when the animation completes. Call layout.setTweenDuration to change the duration globally:
api.layout.setTweenDuration(0); // instant — positions snap with no animation
api.layout.setTweenDuration(1000); // slow 1-second tweens
All layout operations are recorded in the undo/redo history. After each layout call, affected nodes are automatically pinned so a subsequent force layout cannot overwrite them.
Fitting the camera after layout
After repositioning nodes the camera's view frustum may not cover the new bounding box. Follow any layout call with api.camera.flyToAll() to refit:
await api.layout.circle();
await api.camera.flyToAll();
Named dispatch
layout.applyByName lets you trigger any geometric layout by a string key — useful when the layout name comes from user input or configuration data:
await api.layout.applyByName("sphere");
Supported names: "circle", "grid", "line", "spiral", "sphere", "cube", "scatter", "ego", "scale-spacing-up", "scale-spacing-down".