Compare commits
25 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
9654b64f85 | ||
|
8beaeff267 | ||
|
73ecbc773c | ||
|
73fd49c58a | ||
|
6158298c9e | ||
|
e490969ea0 | ||
|
559a400331 | ||
|
921b3e83e5 | ||
|
6f99308b44 | ||
|
fd785d8b10 | ||
|
6716fab883 | ||
|
322a7bae8b | ||
|
4480a8e3ff | ||
|
c41ff98d3c | ||
|
d3c3be9325 | ||
|
487afff1b2 | ||
|
db74df106c | ||
|
c14da4feae | ||
|
f51be2fc48 | ||
|
b3145ce522 | ||
|
0a588deed6 | ||
|
441ccfcc2b | ||
|
f64bda19bc | ||
|
1d51239af1 | ||
|
50eed3e039 |
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "Springy",
|
"name": "Springy",
|
||||||
"main": "springy.js",
|
"main": "springy.js",
|
||||||
"version": "2.3.0",
|
"version": "2.7.1",
|
||||||
"homepage": "https://github.com/dhotson/springy",
|
"homepage": "https://github.com/dhotson/springy",
|
||||||
"authors": [
|
"authors": [
|
||||||
"Dennis Hotson <dennis@99designs.com>"
|
"Dennis Hotson <dennis@99designs.com>"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "springy",
|
"name": "springy",
|
||||||
"version": "2.3.0",
|
"version": "2.7.1",
|
||||||
"description": "A force directed graph layout algorithm in JavaScript.",
|
"description": "A force directed graph layout algorithm in JavaScript.",
|
||||||
"main": "springy.js",
|
"main": "springy.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
81
springy.js
81
springy.js
|
@ -1,5 +1,5 @@
|
||||||
/**
|
/**
|
||||||
* Springy v2.3.0
|
* Springy v2.7.1
|
||||||
*
|
*
|
||||||
* Copyright (c) 2010-2013 Dennis Hotson
|
* Copyright (c) 2010-2013 Dennis Hotson
|
||||||
*
|
*
|
||||||
|
@ -24,22 +24,24 @@
|
||||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
* OTHER DEALINGS IN THE SOFTWARE.
|
* OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
(function (root, factory) {
|
||||||
(function() {
|
if (typeof define === 'function' && define.amd) {
|
||||||
// Enable strict mode for EC5 compatible browsers
|
// AMD. Register as an anonymous module.
|
||||||
"use strict";
|
define(function () {
|
||||||
|
return (root.returnExportsGlobal = factory());
|
||||||
// Establish the root object, `window` in the browser, or `global` on the server.
|
});
|
||||||
var root = this;
|
} else if (typeof exports === 'object') {
|
||||||
|
// Node. Does not work with strict CommonJS, but
|
||||||
// The top-level namespace. All public Springy classes and modules will
|
// only CommonJS-like enviroments that support module.exports,
|
||||||
// be attached to this. Exported for both CommonJS and the browser.
|
// like Node.
|
||||||
var Springy;
|
module.exports = factory();
|
||||||
if (typeof exports !== 'undefined') {
|
|
||||||
Springy = exports;
|
|
||||||
} else {
|
} else {
|
||||||
Springy = root.Springy = {};
|
// Browser globals
|
||||||
|
root.Springy = factory();
|
||||||
}
|
}
|
||||||
|
}(this, function() {
|
||||||
|
|
||||||
|
var Springy = {};
|
||||||
|
|
||||||
var Graph = Springy.Graph = function() {
|
var Graph = Springy.Graph = function() {
|
||||||
this.nodeSet = {};
|
this.nodeSet = {};
|
||||||
|
@ -325,11 +327,13 @@
|
||||||
|
|
||||||
// -----------
|
// -----------
|
||||||
var Layout = Springy.Layout = {};
|
var Layout = Springy.Layout = {};
|
||||||
Layout.ForceDirected = function(graph, stiffness, repulsion, damping) {
|
Layout.ForceDirected = function(graph, stiffness, repulsion, damping, minEnergyThreshold, maxSpeed) {
|
||||||
this.graph = graph;
|
this.graph = graph;
|
||||||
this.stiffness = stiffness; // spring stiffness constant
|
this.stiffness = stiffness; // spring stiffness constant
|
||||||
this.repulsion = repulsion; // repulsion constant
|
this.repulsion = repulsion; // repulsion constant
|
||||||
this.damping = damping; // velocity damping factor
|
this.damping = damping; // velocity damping factor
|
||||||
|
this.minEnergyThreshold = minEnergyThreshold || 0.01; //threshold used to determine render stop
|
||||||
|
this.maxSpeed = maxSpeed || Infinity; // nodes aren't allowed to exceed this speed
|
||||||
|
|
||||||
this.nodePoints = {}; // keep track of points associated with nodes
|
this.nodePoints = {}; // keep track of points associated with nodes
|
||||||
this.edgeSprings = {}; // keep track of springs associated with edges
|
this.edgeSprings = {}; // keep track of springs associated with edges
|
||||||
|
@ -448,6 +452,9 @@
|
||||||
// Is this, along with updatePosition below, the only places that your
|
// Is this, along with updatePosition below, the only places that your
|
||||||
// integration code exist?
|
// integration code exist?
|
||||||
point.v = point.v.add(point.a.multiply(timestep)).multiply(this.damping);
|
point.v = point.v.add(point.a.multiply(timestep)).multiply(this.damping);
|
||||||
|
if (point.v.magnitude() > this.maxSpeed) {
|
||||||
|
point.v = point.v.normalise().multiply(this.maxSpeed);
|
||||||
|
}
|
||||||
point.a = new Vector(0,0);
|
point.a = new Vector(0,0);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -473,14 +480,14 @@
|
||||||
|
|
||||||
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; // stolen from coffeescript, thanks jashkenas! ;-)
|
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; // stolen from coffeescript, thanks jashkenas! ;-)
|
||||||
|
|
||||||
Springy.requestAnimationFrame = __bind(root.requestAnimationFrame ||
|
Springy.requestAnimationFrame = __bind(this.requestAnimationFrame ||
|
||||||
root.webkitRequestAnimationFrame ||
|
this.webkitRequestAnimationFrame ||
|
||||||
root.mozRequestAnimationFrame ||
|
this.mozRequestAnimationFrame ||
|
||||||
root.oRequestAnimationFrame ||
|
this.oRequestAnimationFrame ||
|
||||||
root.msRequestAnimationFrame ||
|
this.msRequestAnimationFrame ||
|
||||||
(function(callback, element) {
|
(function(callback, element) {
|
||||||
root.setTimeout(callback, 10);
|
this.setTimeout(callback, 10);
|
||||||
}), root);
|
}), this);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -497,18 +504,14 @@
|
||||||
if (onRenderStart !== undefined) { onRenderStart(); }
|
if (onRenderStart !== undefined) { onRenderStart(); }
|
||||||
|
|
||||||
Springy.requestAnimationFrame(function step() {
|
Springy.requestAnimationFrame(function step() {
|
||||||
t.applyCoulombsLaw();
|
t.tick(0.03);
|
||||||
t.applyHookesLaw();
|
|
||||||
t.attractToCentre();
|
|
||||||
t.updateVelocity(0.03);
|
|
||||||
t.updatePosition(0.03);
|
|
||||||
|
|
||||||
if (render !== undefined) {
|
if (render !== undefined) {
|
||||||
render();
|
render();
|
||||||
}
|
}
|
||||||
|
|
||||||
// stop simulation when energy of the system goes below a threshold
|
// stop simulation when energy of the system goes below a threshold
|
||||||
if (t._stop || t.totalEnergy() < 0.01) {
|
if (t._stop || t.totalEnergy() < t.minEnergyThreshold) {
|
||||||
t._started = false;
|
t._started = false;
|
||||||
if (onRenderStop !== undefined) { onRenderStop(); }
|
if (onRenderStop !== undefined) { onRenderStop(); }
|
||||||
} else {
|
} else {
|
||||||
|
@ -521,6 +524,14 @@
|
||||||
this._stop = true;
|
this._stop = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Layout.ForceDirected.prototype.tick = function(timestep) {
|
||||||
|
this.applyCoulombsLaw();
|
||||||
|
this.applyHookesLaw();
|
||||||
|
this.attractToCentre();
|
||||||
|
this.updateVelocity(timestep);
|
||||||
|
this.updatePosition(timestep);
|
||||||
|
};
|
||||||
|
|
||||||
// Find the nearest point to a particular position
|
// Find the nearest point to a particular position
|
||||||
Layout.ForceDirected.prototype.nearest = function(pos) {
|
Layout.ForceDirected.prototype.nearest = function(pos) {
|
||||||
var min = {node: null, point: null, distance: null};
|
var min = {node: null, point: null, distance: null};
|
||||||
|
@ -634,14 +645,16 @@
|
||||||
* Renderer handles the layout rendering loop
|
* Renderer handles the layout rendering loop
|
||||||
* @param onRenderStop optional callback function that gets executed whenever rendering stops.
|
* @param onRenderStop optional callback function that gets executed whenever rendering stops.
|
||||||
* @param onRenderStart optional callback function that gets executed whenever rendering starts.
|
* @param onRenderStart optional callback function that gets executed whenever rendering starts.
|
||||||
|
* @param onRenderFrame optional callback function that gets executed after each frame is rendered.
|
||||||
*/
|
*/
|
||||||
var Renderer = Springy.Renderer = function(layout, clear, drawEdge, drawNode, onRenderStop, onRenderStart) {
|
var Renderer = Springy.Renderer = function(layout, clear, drawEdge, drawNode, onRenderStop, onRenderStart, onRenderFrame) {
|
||||||
this.layout = layout;
|
this.layout = layout;
|
||||||
this.clear = clear;
|
this.clear = clear;
|
||||||
this.drawEdge = drawEdge;
|
this.drawEdge = drawEdge;
|
||||||
this.drawNode = drawNode;
|
this.drawNode = drawNode;
|
||||||
this.onRenderStop = onRenderStop;
|
this.onRenderStop = onRenderStop;
|
||||||
this.onRenderStart = onRenderStart;
|
this.onRenderStart = onRenderStart;
|
||||||
|
this.onRenderFrame = onRenderFrame;
|
||||||
|
|
||||||
this.layout.graph.addGraphListener(this);
|
this.layout.graph.addGraphListener(this);
|
||||||
}
|
}
|
||||||
|
@ -672,7 +685,9 @@
|
||||||
t.layout.eachNode(function(node, point) {
|
t.layout.eachNode(function(node, point) {
|
||||||
t.drawNode(node, point.p);
|
t.drawNode(node, point.p);
|
||||||
});
|
});
|
||||||
}, this.onRenderStart, this.onRenderStop);
|
|
||||||
|
if (t.onRenderFrame !== undefined) { t.onRenderFrame(); }
|
||||||
|
}, this.onRenderStop, this.onRenderStart);
|
||||||
};
|
};
|
||||||
|
|
||||||
Renderer.prototype.stop = function() {
|
Renderer.prototype.stop = function() {
|
||||||
|
@ -715,4 +730,6 @@
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
}).call(this);
|
|
||||||
|
return Springy;
|
||||||
|
}));
|
||||||
|
|
|
@ -32,6 +32,7 @@ jQuery.fn.springy = function(params) {
|
||||||
var stiffness = params.stiffness || 400.0;
|
var stiffness = params.stiffness || 400.0;
|
||||||
var repulsion = params.repulsion || 400.0;
|
var repulsion = params.repulsion || 400.0;
|
||||||
var damping = params.damping || 0.5;
|
var damping = params.damping || 0.5;
|
||||||
|
var minEnergyThreshold = params.minEnergyThreshold || 0.00001;
|
||||||
var nodeSelected = params.nodeSelected || null;
|
var nodeSelected = params.nodeSelected || null;
|
||||||
var nodeImages = {};
|
var nodeImages = {};
|
||||||
var edgeLabelsUpright = true;
|
var edgeLabelsUpright = true;
|
||||||
|
@ -39,7 +40,7 @@ jQuery.fn.springy = function(params) {
|
||||||
var canvas = this[0];
|
var canvas = this[0];
|
||||||
var ctx = canvas.getContext("2d");
|
var ctx = canvas.getContext("2d");
|
||||||
|
|
||||||
var layout = this.layout = new Springy.Layout.ForceDirected(graph, stiffness, repulsion, damping);
|
var layout = this.layout = new Springy.Layout.ForceDirected(graph, stiffness, repulsion, damping, minEnergyThreshold);
|
||||||
|
|
||||||
// calculate bounding box of graph layout.. with ease-in
|
// calculate bounding box of graph layout.. with ease-in
|
||||||
var currentBB = layout.getBoundingBox();
|
var currentBB = layout.getBoundingBox();
|
||||||
|
@ -323,7 +324,7 @@ jQuery.fn.springy = function(params) {
|
||||||
ctx.textAlign = "left";
|
ctx.textAlign = "left";
|
||||||
ctx.textBaseline = "top";
|
ctx.textBaseline = "top";
|
||||||
ctx.font = (node.data.font !== undefined) ? node.data.font : nodeFont;
|
ctx.font = (node.data.font !== undefined) ? node.data.font : nodeFont;
|
||||||
ctx.fillStyle = "#000000";
|
ctx.fillStyle = (node.data.color !== undefined) ? node.data.color : "#000000";
|
||||||
var text = (node.data.label !== undefined) ? node.data.label : node.id;
|
var text = (node.data.label !== undefined) ? node.data.label : node.id;
|
||||||
ctx.fillText(text, s.x - contentWidth/2, s.y - contentHeight/2);
|
ctx.fillText(text, s.x - contentWidth/2, s.y - contentHeight/2);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user