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",
|
||||
"main": "springy.js",
|
||||
"version": "2.3.0",
|
||||
"version": "2.7.1",
|
||||
"homepage": "https://github.com/dhotson/springy",
|
||||
"authors": [
|
||||
"Dennis Hotson <dennis@99designs.com>"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "springy",
|
||||
"version": "2.3.0",
|
||||
"version": "2.7.1",
|
||||
"description": "A force directed graph layout algorithm in JavaScript.",
|
||||
"main": "springy.js",
|
||||
"scripts": {
|
||||
|
|
83
springy.js
83
springy.js
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Springy v2.3.0
|
||||
* Springy v2.7.1
|
||||
*
|
||||
* Copyright (c) 2010-2013 Dennis Hotson
|
||||
*
|
||||
|
@ -24,22 +24,24 @@
|
|||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
(function (root, factory) {
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
// AMD. Register as an anonymous module.
|
||||
define(function () {
|
||||
return (root.returnExportsGlobal = factory());
|
||||
});
|
||||
} else if (typeof exports === 'object') {
|
||||
// Node. Does not work with strict CommonJS, but
|
||||
// only CommonJS-like enviroments that support module.exports,
|
||||
// like Node.
|
||||
module.exports = factory();
|
||||
} else {
|
||||
// Browser globals
|
||||
root.Springy = factory();
|
||||
}
|
||||
}(this, function() {
|
||||
|
||||
(function() {
|
||||
// Enable strict mode for EC5 compatible browsers
|
||||
"use strict";
|
||||
|
||||
// Establish the root object, `window` in the browser, or `global` on the server.
|
||||
var root = this;
|
||||
|
||||
// The top-level namespace. All public Springy classes and modules will
|
||||
// be attached to this. Exported for both CommonJS and the browser.
|
||||
var Springy;
|
||||
if (typeof exports !== 'undefined') {
|
||||
Springy = exports;
|
||||
} else {
|
||||
Springy = root.Springy = {};
|
||||
}
|
||||
var Springy = {};
|
||||
|
||||
var Graph = Springy.Graph = function() {
|
||||
this.nodeSet = {};
|
||||
|
@ -325,11 +327,13 @@
|
|||
|
||||
// -----------
|
||||
var Layout = Springy.Layout = {};
|
||||
Layout.ForceDirected = function(graph, stiffness, repulsion, damping) {
|
||||
Layout.ForceDirected = function(graph, stiffness, repulsion, damping, minEnergyThreshold, maxSpeed) {
|
||||
this.graph = graph;
|
||||
this.stiffness = stiffness; // spring stiffness constant
|
||||
this.repulsion = repulsion; // repulsion constant
|
||||
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.edgeSprings = {}; // keep track of springs associated with edges
|
||||
|
@ -448,6 +452,9 @@
|
|||
// Is this, along with updatePosition below, the only places that your
|
||||
// integration code exist?
|
||||
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);
|
||||
});
|
||||
};
|
||||
|
@ -473,14 +480,14 @@
|
|||
|
||||
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; // stolen from coffeescript, thanks jashkenas! ;-)
|
||||
|
||||
Springy.requestAnimationFrame = __bind(root.requestAnimationFrame ||
|
||||
root.webkitRequestAnimationFrame ||
|
||||
root.mozRequestAnimationFrame ||
|
||||
root.oRequestAnimationFrame ||
|
||||
root.msRequestAnimationFrame ||
|
||||
Springy.requestAnimationFrame = __bind(this.requestAnimationFrame ||
|
||||
this.webkitRequestAnimationFrame ||
|
||||
this.mozRequestAnimationFrame ||
|
||||
this.oRequestAnimationFrame ||
|
||||
this.msRequestAnimationFrame ||
|
||||
(function(callback, element) {
|
||||
root.setTimeout(callback, 10);
|
||||
}), root);
|
||||
this.setTimeout(callback, 10);
|
||||
}), this);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -497,18 +504,14 @@
|
|||
if (onRenderStart !== undefined) { onRenderStart(); }
|
||||
|
||||
Springy.requestAnimationFrame(function step() {
|
||||
t.applyCoulombsLaw();
|
||||
t.applyHookesLaw();
|
||||
t.attractToCentre();
|
||||
t.updateVelocity(0.03);
|
||||
t.updatePosition(0.03);
|
||||
t.tick(0.03);
|
||||
|
||||
if (render !== undefined) {
|
||||
render();
|
||||
}
|
||||
|
||||
// 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;
|
||||
if (onRenderStop !== undefined) { onRenderStop(); }
|
||||
} else {
|
||||
|
@ -521,6 +524,14 @@
|
|||
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
|
||||
Layout.ForceDirected.prototype.nearest = function(pos) {
|
||||
var min = {node: null, point: null, distance: null};
|
||||
|
@ -634,14 +645,16 @@
|
|||
* Renderer handles the layout rendering loop
|
||||
* @param onRenderStop optional callback function that gets executed whenever rendering stops.
|
||||
* @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.clear = clear;
|
||||
this.drawEdge = drawEdge;
|
||||
this.drawNode = drawNode;
|
||||
this.onRenderStop = onRenderStop;
|
||||
this.onRenderStart = onRenderStart;
|
||||
this.onRenderFrame = onRenderFrame;
|
||||
|
||||
this.layout.graph.addGraphListener(this);
|
||||
}
|
||||
|
@ -672,7 +685,9 @@
|
|||
t.layout.eachNode(function(node, point) {
|
||||
t.drawNode(node, point.p);
|
||||
});
|
||||
}, this.onRenderStart, this.onRenderStop);
|
||||
|
||||
if (t.onRenderFrame !== undefined) { t.onRenderFrame(); }
|
||||
}, this.onRenderStop, this.onRenderStart);
|
||||
};
|
||||
|
||||
Renderer.prototype.stop = function() {
|
||||
|
@ -715,4 +730,6 @@
|
|||
}
|
||||
return true;
|
||||
};
|
||||
}).call(this);
|
||||
|
||||
return Springy;
|
||||
}));
|
||||
|
|
|
@ -32,6 +32,7 @@ jQuery.fn.springy = function(params) {
|
|||
var stiffness = params.stiffness || 400.0;
|
||||
var repulsion = params.repulsion || 400.0;
|
||||
var damping = params.damping || 0.5;
|
||||
var minEnergyThreshold = params.minEnergyThreshold || 0.00001;
|
||||
var nodeSelected = params.nodeSelected || null;
|
||||
var nodeImages = {};
|
||||
var edgeLabelsUpright = true;
|
||||
|
@ -39,7 +40,7 @@ jQuery.fn.springy = function(params) {
|
|||
var canvas = this[0];
|
||||
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
|
||||
var currentBB = layout.getBoundingBox();
|
||||
|
@ -323,7 +324,7 @@ jQuery.fn.springy = function(params) {
|
|||
ctx.textAlign = "left";
|
||||
ctx.textBaseline = "top";
|
||||
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;
|
||||
ctx.fillText(text, s.x - contentWidth/2, s.y - contentHeight/2);
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue
Block a user