HTMLGraph logo HTMLGraph

Force-Directed Layout

A force-directed layout is utilized by default whenever the enableLayout method is invoked on a CanvasBuilder instance.

You can also explicitly specify it through its type.

    
const element = document.getElementById('canvas');

const canvas = new CanvasBuilder(element)
  .enableLayout({
    algorithm: {
      type: "forceDirected",
    },
  })
  .build();

  

The force-directed layout supports optional configuration parameters.

    
const canvas = new CanvasBuilder(element)
  .enableLayout({
    algorithm: {
      type: "forceDirected",
      dtSec: 0.02,
      nodeCharge: 10000,
      nodeMass: 1,
      effectiveDistance: 1000,
      edgeEquilibriumLength: 300,
      edgeStiffness: 100,
      maxIterations: 100,
      convergenceDelta: 0.001,
      seed: "HTMLGraph is awesome",
    },
  })
  .build();

  
NameTypeDescriptionRequiredDefault
dtSecnumberTime step duration in seconds per iterationNo0.02
nodeChargenumberRepresents electrostatic-like repulsive forces between nodes. Higher values increase repulsionNo10000
nodeMassnumberDetermines node’s inertia during movement. Larger masses slow down convergence but enhance stabilityNo1
effectiveDistancenumberCutoff distance beyond which nodes’ interactions become negligibleNo1000
edgeEquilibriumLengthnumberPerfect edge length when attractive and repulsive forces are absentNo300
edgeStiffnessnumberMeasures spring-like behavior of edges. High stiffness enforces equilibrium lengths strictlyNo100
maxIterationsnumberMaximum allowed iterations before stopping the layout processNo100
convergenceDeltanumberThreshold determining whether nodes have settled sufficiently after moving distances smaller than this threshold within an iterationNo0.001
seedstringA randomization seed used to initialize nodes’ starting positions if they lack predefined coordinatesNo"HTMLGraph is awesome"