Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions springy.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@
// accepts variable number of arguments, where each argument
// is a string that becomes both node identifier and label
for (var i = 0; i < arguments.length; i++) {
var name = arguments[i];
var node = new Node(name, {label:name});
this.addNode(node);
var e = arguments[i];
var name = e[0];
var attr = e[1];
this.newNode(name, attr);
}
};

Expand Down Expand Up @@ -145,8 +146,8 @@
}
};

Graph.prototype.newNode = function(data) {
var node = new Node(this.nextNodeId++, data);
Graph.prototype.newNode = function(name, data) {
var node = new Node(name, Object.assign( {label:name},data) );
this.addNode(node);
return node;
};
Expand Down Expand Up @@ -329,7 +330,7 @@
var Layout = Springy.Layout = {};
Layout.ForceDirected = function(graph, stiffness, repulsion, damping, minEnergyThreshold, maxSpeed) {
this.graph = graph;
this.stiffness = stiffness; // spring stiffness constant
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
Expand All @@ -342,15 +343,18 @@
Layout.ForceDirected.prototype.point = function(node) {
if (!(node.id in this.nodePoints)) {
var mass = (node.data.mass !== undefined) ? node.data.mass : 1.0;
this.nodePoints[node.id] = new Layout.ForceDirected.Point(Vector.random(), mass);
// this.nodePoints[node.id] = new Layout.ForceDirected.Point(Vector.random(), mass);
var vector = (node.data.xpos !== undefined) ? new Vector(node.data.xpos,-node.data.ypos) : Vector.random();
this.nodePoints[node.id] = new Layout.ForceDirected.Point(vector, mass);
}

return this.nodePoints[node.id];
};

Layout.ForceDirected.prototype.spring = function(edge) {
if (!(edge.id in this.edgeSprings)) {
var length = (edge.data.length !== undefined) ? edge.data.length : 1.0;
var d = this.point(edge.source).p.subtract(this.point(edge.target).p);
var length = (edge.data.length !== undefined) ? edge.data.length : d.magnitude();

var existingSpring = false;

Expand Down Expand Up @@ -525,9 +529,9 @@
}

Layout.ForceDirected.prototype.tick = function(timestep) {
this.applyCoulombsLaw();
// this.applyCoulombsLaw();
this.applyHookesLaw();
this.attractToCentre();
// this.attractToCentre();
this.updateVelocity(timestep);
this.updatePosition(timestep);
};
Expand All @@ -550,9 +554,8 @@

// returns [bottomleft, topright]
Layout.ForceDirected.prototype.getBoundingBox = function() {
var bottomleft = new Vector(-2,-2);
var topright = new Vector(2,2);

var bottomleft = new Vector(Infinity,Infinity);
var topright = new Vector(-Infinity,-Infinity);
this.eachNode(function(n, point) {
if (point.p.x < bottomleft.x) {
bottomleft.x = point.p.x;
Expand Down
6 changes: 3 additions & 3 deletions springyui.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Copyright (c) 2010 Dennis Hotson
jQuery.fn.springy = function(params) {
var graph = this.graph = params.graph || new Springy.Graph();
var nodeFont = "16px Verdana, sans-serif";
var edgeFont = "8px Verdana, sans-serif";
var edgeFont = "14px Verdana, sans-serif";
var stiffness = params.stiffness || 400.0;
var repulsion = params.repulsion || 400.0;
var damping = params.damping || 0.5;
Expand All @@ -44,7 +44,7 @@ jQuery.fn.springy = function(params) {

// calculate bounding box of graph layout.. with ease-in
var currentBB = layout.getBoundingBox();
var targetBB = {bottomleft: new Springy.Vector(-2, -2), topright: new Springy.Vector(2, 2)};
var targetBB = {bottomleft: new Springy.Vector(0, 0), topright: new Springy.Vector(1000, 1000)};

// auto adjusting bounding box
Springy.requestAnimationFrame(function adjust() {
Expand Down Expand Up @@ -287,7 +287,7 @@ jQuery.fn.springy = function(params) {
var textPos = s1.add(s2).divide(2).add(normal.multiply(displacement));
ctx.translate(textPos.x, textPos.y);
ctx.rotate(angle);
ctx.fillText(text, 0,-2);
ctx.fillText(text, 0, -8);
ctx.restore();
}

Expand Down