From e490969ea09b6f04add733cb5ee20d80988d6101 Mon Sep 17 00:00:00 2001 From: mwcz Date: Tue, 19 Jul 2016 14:38:01 -0400 Subject: [PATCH] add maxSpeed parameter to Layout.ForceDirected constructor --- springy.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/springy.js b/springy.js index 392f94b..c93c399 100644 --- a/springy.js +++ b/springy.js @@ -327,12 +327,13 @@ // ----------- var Layout = Springy.Layout = {}; - Layout.ForceDirected = function(graph, stiffness, repulsion, damping, minEnergyThreshold) { + Layout.ForceDirected = function(graph, stiffness, repulsion, damping, minEnergyThreshold, maxSpeed) { this.graph = graph; this.stiffness = stiffness; // spring stiffness constant this.repulsion = repulsion; // repulsion constant this.damping = damping; // velocity damping factor this.minEnergyThreshold = minEnergyThreshold || 0.01; //threshold used to determine render stop + this.maxSpeed = maxSpeed || Infinity; // nodes aren't allowed to exceed this speed this.nodePoints = {}; // keep track of points associated with nodes this.edgeSprings = {}; // keep track of springs associated with edges @@ -451,6 +452,9 @@ // Is this, along with updatePosition below, the only places that your // integration code exist? point.v = point.v.add(point.a.multiply(timestep)).multiply(this.damping); + if (point.v.magnitude() > this.maxSpeed) { + point.v = point.v.normalise().multiply(this.maxSpeed); + } point.a = new Vector(0,0); }); };