Compare commits

...

2 Commits

Author SHA1 Message Date
Dennis Hotson
f13bd76552 Option to manually tick simulation forward 2013-03-14 23:19:16 +11:00
Dennis Hotson
d7a26171b0 Fixed shitespace 2013-03-14 23:02:23 +11:00

View File

@ -44,21 +44,21 @@ var Node = function(id, data) {
this.data = (data !== undefined) ? data : {}; this.data = (data !== undefined) ? data : {};
// Data fields used by layout algorithm in this file: // Data fields used by layout algorithm in this file:
// this.data.mass // this.data.mass
// Data used by default renderer in springyui.js // Data used by default renderer in springyui.js
// this.data.label // this.data.label
}; };
var Edge = function(id, source, target, data) { var Edge = function(id, source, target, data) {
this.id = id; this.id = id;
/** @type {Node} */ /** @type {Node} */
this.source = source; this.source = source;
this.target = target; this.target = target;
this.data = (data !== undefined) ? data : {}; this.data = (data !== undefined) ? data : {};
// Edge data field used by layout alorithm // Edge data field used by layout alorithm
// this.data.length // this.data.length
// this.data.type // this.data.type
}; };
Graph.prototype.addNode = function(node) { Graph.prototype.addNode = function(node) {
@ -73,13 +73,13 @@ Graph.prototype.addNode = function(node) {
}; };
Graph.prototype.addNodes = function() { Graph.prototype.addNodes = function() {
// accepts variable number of arguments, where each argument // accepts variable number of arguments, where each argument
// is a string that becomes both node identifier and label // is a string that becomes both node identifier and label
for (var i = 0; i < arguments.length; i++) { for (var i = 0; i < arguments.length; i++) {
var name = arguments[i]; var name = arguments[i];
var node = new Node(name, {label:name}); var node = new Node(name, {label:name});
this.addNode(node); this.addNode(node);
} }
}; };
Graph.prototype.addEdge = function(edge) { Graph.prototype.addEdge = function(edge) {
@ -113,22 +113,22 @@ Graph.prototype.addEdge = function(edge) {
}; };
Graph.prototype.addEdges = function() { Graph.prototype.addEdges = function() {
// accepts variable number of arguments, where each argument // accepts variable number of arguments, where each argument
// is a triple [nodeid1, nodeid2, attributes] // is a triple [nodeid1, nodeid2, attributes]
for (var i = 0; i < arguments.length; i++) { for (var i = 0; i < arguments.length; i++) {
var e = arguments[i]; var e = arguments[i];
var node1 = this.nodeSet[e[0]]; var node1 = this.nodeSet[e[0]];
if (node1 == undefined) { if (node1 == undefined) {
throw new TypeError("invalid node name: " + e[0]); throw new TypeError("invalid node name: " + e[0]);
} }
var node2 = this.nodeSet[e[1]]; var node2 = this.nodeSet[e[1]];
if (node2 == undefined) { if (node2 == undefined) {
throw new TypeError("invalid node name: " + e[1]); throw new TypeError("invalid node name: " + e[1]);
} }
var attr = e[2]; var attr = e[2];
this.newEdge(node1, node2, attr); this.newEdge(node1, node2, attr);
} }
}; };
Graph.prototype.newNode = function(data) { Graph.prototype.newNode = function(data) {
@ -152,20 +152,20 @@ Springy's simple JSON format for graphs.
historically, Springy uses separate lists historically, Springy uses separate lists
of nodes and edges: of nodes and edges:
{ {
"nodes": [ "nodes": [
"center", "center",
"left", "left",
"right", "right",
"up", "up",
"satellite" "satellite"
], ],
"edges": [ "edges": [
["center", "left"], ["center", "left"],
["center", "right"], ["center", "right"],
["center", "up"] ["center", "up"]
] ]
} }
**/ **/
// parse if a string is passed (EC5+ browsers) // parse if a string is passed (EC5+ browsers)
@ -462,6 +462,14 @@ Layout.requestAnimationFrame = __bind(window.requestAnimationFrame ||
}, window); }, window);
Layout.ForceDirected.prototype.tick = function(timestep) {
this.applyCoulombsLaw();
this.applyHookesLaw();
this.attractToCentre();
this.updateVelocity(timestep);
this.updatePosition(timestep);
};
// start simulation // start simulation
Layout.ForceDirected.prototype.start = function(render, done) { Layout.ForceDirected.prototype.start = function(render, done) {
var t = this; var t = this;
@ -471,15 +479,12 @@ Layout.ForceDirected.prototype.start = function(render, done) {
this._stop = false; this._stop = false;
Layout.requestAnimationFrame(function step() { Layout.requestAnimationFrame(function step() {
t.applyCoulombsLaw();
t.applyHookesLaw(); t.tick(0.03);
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() < 0.01) {
@ -492,7 +497,7 @@ Layout.ForceDirected.prototype.start = function(render, done) {
}; };
Layout.ForceDirected.prototype.stop = function() { Layout.ForceDirected.prototype.stop = function() {
this._stop = true; this._stop = true;
} }
// Find the nearest point to a particular position // Find the nearest point to a particular position
@ -634,34 +639,34 @@ Renderer.prototype.start = function() {
}; };
Renderer.prototype.stop = function() { Renderer.prototype.stop = function() {
this.layout.stop(); this.layout.stop();
}; };
// Array.forEach implementation for IE support.. // Array.forEach implementation for IE support..
//https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach //https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
if ( !Array.prototype.forEach ) { if ( !Array.prototype.forEach ) {
Array.prototype.forEach = function( callback, thisArg ) { Array.prototype.forEach = function( callback, thisArg ) {
var T, k; var T, k;
if ( this == null ) { if ( this == null ) {
throw new TypeError( " this is null or not defined" ); throw new TypeError( " this is null or not defined" );
} }
var O = Object(this); var O = Object(this);
var len = O.length >>> 0; // Hack to convert O.length to a UInt32 var len = O.length >>> 0; // Hack to convert O.length to a UInt32
if ( {}.toString.call(callback) != "[object Function]" ) { if ( {}.toString.call(callback) != "[object Function]" ) {
throw new TypeError( callback + " is not a function" ); throw new TypeError( callback + " is not a function" );
} }
if ( thisArg ) { if ( thisArg ) {
T = thisArg; T = thisArg;
} }
k = 0; k = 0;
while( k < len ) { while( k < len ) {
var kValue; var kValue;
if ( k in O ) { if ( k in O ) {
kValue = O[ k ]; kValue = O[ k ];
callback.call( T, kValue, k, O ); callback.call( T, kValue, k, O );
} }
k++; k++;
} }
}; };
} }