HTMLGraph

Force-Directed Layout

A force-directed layout is utilized by default whenever the enableLayout method is invoked on a CanvasBuilder instance. This type of layout also comes with an animated version.

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.01,
      nodeCharge: 10000,
      nodeMass: 1,
      nodeForceCoefficient: 1,
      maxForce: 10000000,
      edgeEquilibriumLength: 300,
      edgeStiffness: 1000,
      maxIterations: 1000,
      convergenceVelocity: 10,
      barnesHut: {
        theta: 1,
        areaRadiusThreshold: 0.01,
      },
      seed: "HTMLGraph is awesome",
    },
  })
  .build();

  
NameTypeDescriptionRequiredDefault
dtSecnumberTime step duration in seconds per iterationNo0.01
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
nodeForceCoefficientnumberMultiplier for the repulsive force between nodes. A higher value increases the repulsion effect given the same node charges.No1
maxForcenumberSets the maximum amount of force applicable to a node. Useful when nodes are very close together, preventing repulsive forces from becoming infinitely large.No10000000
edgeEquilibriumLengthnumberPerfect edge length when attractive and repulsive forces are absentNo300
edgeStiffnessnumberMeasures spring-like behavior of edges. High stiffness enforces equilibrium lengths strictlyNo1000
maxIterationsnumberMaximum allowed iterations before stopping the layout processNo1000
convergenceVelocitynumberThreshold determining whether nodes have stabilized sufficiently based on their movement speed (measured as absolute content distance per second) being less than this value during an iterationNo10
barnesHutBarnesHutConfigConfiguration settings for approximating node forces calculations.No{}
seedstringA randomization seed used to initialize nodes’ starting positions if they lack predefined coordinatesNo"HTMLGraph is awesome"

Barnes-Hut approximation configuration

NameTypeDescriptionRequiredDefault
thetanumberControls the trade-off between optimization and accuracy. Higher values lead to greater optimization but reduced accuracy. With theta=0, the algorithm’s complexity is O(n²); with theta=1, the complexity becomes O(n*log(n)).no1
areaRadiusThresholdnumberSpecifies the radius of the mesh squares where further subdivision stops. Smaller values produce a finer mesh.no0.01