diff --git a/docs/network/nodes.html b/docs/network/nodes.html index 2189b6ec..0a68ff55 100644 --- a/docs/network/nodes.html +++ b/docs/network/nodes.html @@ -809,7 +809,10 @@ network.setOptions(options); Number 1 The barnesHut physics model (which is enabled by default) is based on an inverted gravity model. By - increasing the mass of a node, you increase it's repulsion. Values lower than 1 are not recommended. + increasing the mass of a node, you increase it's repulsion. +

+ Values between 0 and 1 are not recommended.
+ Negative or zero values are not allowed. These will generate a console error and will be set to 1. diff --git a/lib/network/modules/NodesHandler.js b/lib/network/modules/NodesHandler.js index 3e363ed5..e7e918d5 100644 --- a/lib/network/modules/NodesHandler.js +++ b/lib/network/modules/NodesHandler.js @@ -129,6 +129,12 @@ class NodesHandler { x: undefined, y: undefined }; + + // Protect from idiocy + if (this.defaultOptions.mass <= 0) { + throw 'Internal error: mass in defaultOptions of NodesHandler may not be zero or negative'; + } + util.extend(this.options, this.defaultOptions); this.bindEventListeners(); diff --git a/lib/network/modules/components/Node.js b/lib/network/modules/components/Node.js index 296815d9..f42cdb3b 100644 --- a/lib/network/modules/components/Node.js +++ b/lib/network/modules/components/Node.js @@ -115,6 +115,8 @@ class Node { throw "Node must have an id"; } + Node.checkMass(options, this.id); + // set these options locally // clear x and y positions if (options.x !== undefined) { @@ -210,6 +212,8 @@ class Node { ]; util.selectiveNotDeepExtend(fields, parentOptions, newOptions, allowDeletion); + Node.checkMass(newOptions); + // merge the shadow options into the parent. util.mergeOptions(parentOptions, newOptions, 'shadow', allowDeletion, globalOptions); @@ -538,6 +542,24 @@ class Node { this.shape.boundingBox.bottom > obj.top ); } + + + /** + * Check valid values for mass + * + * The mass may not be negative or zero. If it is, reset to 1 + */ + static checkMass(options, id) { + if (options.mass !== undefined && options.mass <= 0) { + let strId = ''; + if (id !== undefined) { + strId = ' in node id: ' + id; + } + console.log('%cNegative or zero mass disallowed' + strId + + ', setting mass to 1.' , printStyle); + options.mass = 1; + } + } } export default Node;