From 985693e123cd5c743dab4a03293ccad1c4045367 Mon Sep 17 00:00:00 2001 From: Alex de Mulder Date: Fri, 11 Sep 2015 18:36:52 +0200 Subject: [PATCH] - Fixed stabilized event not firing if layout algorithm does very well. --- HISTORY.md | 1 + dist/vis.js | 23 +++++++++++++++++++---- lib/network/modules/LayoutEngine.js | 5 +++-- lib/network/modules/PhysicsEngine.js | 19 ++++++++++++++++--- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 8a7c43d7..c6473cdd 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -8,6 +8,7 @@ http://visjs.org - Fixed Phantom Edges during clustering. - Fixed scaling not doing anything to edges. - Fixed setting font to null so the network won't crash anymore. +- Fixed stabilized event not firing if layout algorithm does very well. ## 2015-09-07, version 4.8.1 diff --git a/dist/vis.js b/dist/vis.js index d66bfd45..f9af6577 100644 --- a/dist/vis.js +++ b/dist/vis.js @@ -33323,9 +33323,11 @@ return /******/ (function(modules) { // webpackBootstrap value: function _emitStabilized() { var _this2 = this; + var amountOfIterations = arguments.length <= 0 || arguments[0] === undefined ? this.stabilizationIterations : arguments[0]; + if (this.stabilizationIterations > 1) { setTimeout(function () { - _this2.body.emitter.emit('stabilized', { iterations: _this2.stabilizationIterations }); + _this2.body.emitter.emit('stabilized', { iterations: amountOfIterations }); _this2.stabilizationIterations = 0; }, 0); } @@ -33697,6 +33699,12 @@ return /******/ (function(modules) { // webpackBootstrap }, { key: '_stabilizationBatch', value: function _stabilizationBatch() { + // this is here to ensure that there is at least one start event. + if (this.startedStabilization === false) { + this.body.emitter.emit('startStabilizing'); + this.startedStabilization = true; + } + var count = 0; while (this.stabilized === false && count < this.options.stabilization.updateInterval && this.stabilizationIterations < this.targetIterations) { this.physicsTick(); @@ -33731,7 +33739,13 @@ return /******/ (function(modules) { // webpackBootstrap this.body.emitter.emit('_requestRedraw'); if (this.stabilized === true) { - this._emitStabilized(); + // I want at least one stabilized event if there is nothing to stabilize. + if (this.stabilizationIterations < 2) { + this.stabilizationIterations = 2; + this._emitStabilized(0); // the zero overrules the iterations + } else { + this._emitStabilized(); + } } else { this.startSimulation(); } @@ -39261,9 +39275,10 @@ return /******/ (function(modules) { // webpackBootstrap this._shiftToCenter(); // perturb the nodes a little bit to force the physics to kick in + var offset = 70; for (var i = 0; i < this.body.nodeIndices.length; i++) { - this.body.nodes[this.body.nodeIndices[i]].x += (0.5 - this.seededRandom()) * 50; - this.body.nodes[this.body.nodeIndices[i]].y += (0.5 - this.seededRandom()) * 50; + this.body.nodes[this.body.nodeIndices[i]].x += (0.5 - this.seededRandom()) * offset; + this.body.nodes[this.body.nodeIndices[i]].y += (0.5 - this.seededRandom()) * offset; } // uncluster all clusters diff --git a/lib/network/modules/LayoutEngine.js b/lib/network/modules/LayoutEngine.js index 1c5fde68..a3ce9f42 100644 --- a/lib/network/modules/LayoutEngine.js +++ b/lib/network/modules/LayoutEngine.js @@ -224,9 +224,10 @@ class LayoutEngine { this._shiftToCenter(); // perturb the nodes a little bit to force the physics to kick in + let offset = 70; for (let i = 0; i < this.body.nodeIndices.length; i++) { - this.body.nodes[this.body.nodeIndices[i]].x += (0.5 - this.seededRandom())*50; - this.body.nodes[this.body.nodeIndices[i]].y += (0.5 - this.seededRandom())*50; + this.body.nodes[this.body.nodeIndices[i]].x += (0.5 - this.seededRandom())*offset; + this.body.nodes[this.body.nodeIndices[i]].y += (0.5 - this.seededRandom())*offset; } // uncluster all clusters diff --git a/lib/network/modules/PhysicsEngine.js b/lib/network/modules/PhysicsEngine.js index 16169f81..edc3a44c 100644 --- a/lib/network/modules/PhysicsEngine.js +++ b/lib/network/modules/PhysicsEngine.js @@ -273,10 +273,10 @@ class PhysicsEngine { * trigger the stabilized event. * @private */ - _emitStabilized() { + _emitStabilized(amountOfIterations = this.stabilizationIterations) { if (this.stabilizationIterations > 1) { setTimeout(() => { - this.body.emitter.emit('stabilized', {iterations: this.stabilizationIterations}); + this.body.emitter.emit('stabilized', {iterations: amountOfIterations}); this.stabilizationIterations = 0; }, 0); } @@ -631,6 +631,12 @@ class PhysicsEngine { * @private */ _stabilizationBatch() { + // this is here to ensure that there is at least one start event. + if (this.startedStabilization === false) { + this.body.emitter.emit('startStabilizing'); + this.startedStabilization = true; + } + var count = 0; while (this.stabilized === false && count < this.options.stabilization.updateInterval && this.stabilizationIterations < this.targetIterations) { this.physicsTick(); @@ -665,7 +671,14 @@ class PhysicsEngine { this.body.emitter.emit('_requestRedraw'); if (this.stabilized === true) { - this._emitStabilized(); + // I want at least one stabilized event if there is nothing to stabilize. + if (this.stabilizationIterations < 2) { + this.stabilizationIterations = 2; + this._emitStabilized(0); // the zero overrules the iterations + } + else { + this._emitStabilized(); + } } else { this.startSimulation();