Added method to stop layout calculations

This commit is contained in:
Dennis Hotson 2013-03-14 21:53:03 +11:00
parent 459904b833
commit 4c0a9e3e28
3 changed files with 12 additions and 3 deletions

View File

@ -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));

View File

@ -468,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();
@ -481,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 {
@ -490,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};
@ -628,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 ) {

View File

@ -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);
},