Animated Layouts
Iterative layout algorithms allow visualizing intermediate results while calculations are ongoing. Users can observe the evolving graph layout in real-time without having to wait until all nodes have reached their final positions.
To enable animated nodes positioning, call the enableAnimatedLayout method on CanvasBuilder.
const element = document.getElementById("canvas");
const canvas = new CanvasBuilder(element)
.enableAnimatedLayout()
.build();
As shown below, coordinates are calculated automatically and do not need to be specified explicitly when calling the addNode method.
const element = document.createElement('div');
canvas.addNode({
id: "node-1",
element: element,
ports: [
{ id: "node-1", element: element }
],
});
The enableAnimatedLayout method accepts optional configuration parameters:
const element = document.getElementById("canvas");
const canvas = new CanvasBuilder(element)
.enableAnimatedLayout({
algorithm: {
type: "forceDirected",
},
staticNodeResolver: (nodeId) => false,
events: {
onBeforeApplied: () => {
console.log("Layout is about to be applied");
},
onAfterApplied: () => {
console.log("Layout has been applied");
},
},
})
.build();
Configuration Parameters
| Name | Type | Description | Required | Default |
|---|---|---|---|---|
algorithm | AlgorithmConfig | Specifies the layout algorithm to apply | no | { type: "forceDirected" } |
staticNodeResolver | (nodeId) => boolean | Function that determines whether a node is static | no | () => false |
events | EventsConfig | Handlers for available events | no | {} |
AlgorithmConfig Options
| Algorithm | Configuration | Example |
|---|---|---|
| ForceDirected | ForceDirected | { type: "forceDirected" } |
| Custom | Custom | { type: "custom", instance: new MyAlgorithm() } |
Events Configuration
| Name | Type | Description | Required | Default |
|---|---|---|---|---|
onBeforeApplied | () => void | Function to call before layout applied | No | () => {} |
onAfterApplied | () => void | Function to call after layout has been applied | No | () => {} |