diff --git a/springy.js b/springy.js index 0bf5ba4..f8c1553 100644 --- a/springy.js +++ b/springy.js @@ -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); } }; @@ -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; }; @@ -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 @@ -342,7 +343,9 @@ 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]; @@ -350,7 +353,8 @@ 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; @@ -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); }; @@ -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; diff --git a/springyui.js b/springyui.js index acc35eb..1e34562 100755 --- a/springyui.js +++ b/springyui.js @@ -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; @@ -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() { @@ -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(); }