diff --git a/demo.html b/demo.html index 380d637..f44b0b6 100644 --- a/demo.html +++ b/demo.html @@ -29,7 +29,7 @@ graph.newEdge(dennis, bianca, {color: '#CC333F'}); graph.newEdge(bianca, monty, {color: '#EB6841'}); jQuery(function(){ - var springy = jQuery('#springydemo').springy({ + var springy = window.springy = jQuery('#springydemo').springy({ graph: graph, nodeSelected: function(node){ console.log('Node selected: ' + JSON.stringify(node.data)); diff --git a/springy.js b/springy.js index ca75a8e..9bb9871 100644 --- a/springy.js +++ b/springy.js @@ -168,6 +168,10 @@ of nodes and edges: } **/ + // parse if a string is passed (EC5+ browsers) + if (typeof json == 'string' || json instanceof String) { + json = JSON.parse( json ); + } if ('nodes' in json || 'edges' in json) { this.addNodes.apply(this, json['nodes']); @@ -464,6 +468,7 @@ Layout.ForceDirected.prototype.start = function(render, done) { if (this._started) return; this._started = true; + this._stop = false; Layout.requestAnimationFrame(function step() { t.applyCoulombsLaw(); @@ -477,7 +482,7 @@ Layout.ForceDirected.prototype.start = function(render, done) { } // stop simulation when energy of the system goes below a threshold - if (t.totalEnergy() < 0.01) { + if (t._stop || t.totalEnergy() < 0.01) { t._started = false; if (done !== undefined) { done(); } } else { @@ -486,6 +491,10 @@ Layout.ForceDirected.prototype.start = function(render, done) { }); }; +Layout.ForceDirected.prototype.stop = function() { + this._stop = true; +} + // Find the nearest point to a particular position Layout.ForceDirected.prototype.nearest = function(pos) { var min = {node: null, point: null, distance: null}; @@ -624,6 +633,10 @@ Renderer.prototype.start = function() { }); }; +Renderer.prototype.stop = function() { + this.layout.stop(); +}; + // Array.forEach implementation for IE support.. //https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach if ( !Array.prototype.forEach ) { diff --git a/springyui.js b/springyui.js index 771dec9..8f80a3a 100755 --- a/springyui.js +++ b/springyui.js @@ -131,7 +131,7 @@ jQuery.fn.springy = function(params) { return 20; }; - var renderer = new Renderer(layout, + var renderer = this.renderer = new Renderer(layout, function clear() { ctx.clearRect(0,0,canvas.width,canvas.height); },