2016-04-23 00:45:03 +08:00
|
|
|
import {dispatch} from "d3-dispatch";
|
|
|
|
|
import {map} from "d3-collection";
|
|
|
|
|
import {timer} from "d3-timer";
|
|
|
|
|
|
2016-04-24 05:24:37 +08:00
|
|
|
export function x(d) {
|
|
|
|
|
return d.x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function y(d) {
|
|
|
|
|
return d.y;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-06 04:50:36 +08:00
|
|
|
export default function(nodes) {
|
2016-04-08 02:32:34 +08:00
|
|
|
var simulation,
|
|
|
|
|
iteration = 0,
|
2016-04-08 02:55:45 +08:00
|
|
|
alpha = 1,
|
2016-04-07 04:51:59 +08:00
|
|
|
alphaMin = 0.0001,
|
|
|
|
|
alphaDecay = -0.02,
|
2016-04-26 01:42:19 +08:00
|
|
|
drag = 0.5,
|
2016-04-23 00:45:03 +08:00
|
|
|
force = map(),
|
|
|
|
|
ticker = timer(tick),
|
|
|
|
|
event = dispatch("start", "tick", "end");
|
2016-04-06 04:50:36 +08:00
|
|
|
|
2016-04-26 08:04:51 +08:00
|
|
|
if (nodes == null) nodes = [];
|
|
|
|
|
|
2016-04-07 04:21:23 +08:00
|
|
|
function start() {
|
2016-04-07 04:51:59 +08:00
|
|
|
if (iteration < Infinity) {
|
2016-04-08 02:55:45 +08:00
|
|
|
iteration = 0, alpha = 1;
|
|
|
|
|
} else {
|
|
|
|
|
iteration = 0, alpha = 1;
|
2016-04-23 00:45:03 +08:00
|
|
|
event.call("start", simulation);
|
|
|
|
|
ticker.restart(tick);
|
2016-04-07 04:21:23 +08:00
|
|
|
}
|
2016-04-08 02:32:34 +08:00
|
|
|
return simulation;
|
2016-04-07 04:21:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stop() {
|
2016-04-07 04:51:59 +08:00
|
|
|
if (iteration < Infinity) {
|
2016-04-08 02:55:45 +08:00
|
|
|
iteration = Infinity, alpha = 0;
|
2016-04-23 00:45:03 +08:00
|
|
|
event.call("end", simulation);
|
|
|
|
|
ticker.stop();
|
2016-04-06 04:50:36 +08:00
|
|
|
}
|
2016-04-08 02:32:34 +08:00
|
|
|
return simulation;
|
2016-04-06 04:50:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function tick() {
|
2016-04-08 02:32:34 +08:00
|
|
|
alpha = Math.exp(++iteration * alphaDecay);
|
2016-04-07 04:51:59 +08:00
|
|
|
if (!(alpha > alphaMin)) return stop();
|
2016-04-08 02:32:34 +08:00
|
|
|
force.each(apply);
|
2016-04-06 04:50:36 +08:00
|
|
|
|
|
|
|
|
for (var i = 0, n = nodes.length, node; i < n; ++i) {
|
|
|
|
|
node = nodes[i];
|
2016-04-26 01:42:19 +08:00
|
|
|
node.x += node.vx *= drag;
|
|
|
|
|
node.y += node.vy *= drag;
|
2016-04-06 04:50:36 +08:00
|
|
|
}
|
|
|
|
|
|
2016-04-23 00:45:03 +08:00
|
|
|
event.call("tick", simulation);
|
2016-04-06 04:50:36 +08:00
|
|
|
}
|
|
|
|
|
|
2016-04-21 06:24:49 +08:00
|
|
|
function initializeNodes() {
|
|
|
|
|
for (var i = 0, n = nodes.length, node; i < n; ++i) {
|
|
|
|
|
node = nodes[i], node.index = i;
|
|
|
|
|
if (isNaN(node.x)) node.x = Math.random() * 100 - 50;
|
|
|
|
|
if (isNaN(node.y)) node.y = Math.random() * 100 - 50;
|
|
|
|
|
if (isNaN(node.vx)) node.vx = 0;
|
|
|
|
|
if (isNaN(node.vy)) node.vy = 0;
|
2016-04-08 23:32:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 06:24:49 +08:00
|
|
|
function initializeForce(force) {
|
2016-04-26 12:47:24 +08:00
|
|
|
if (force.initialize) force.initialize(nodes);
|
2016-04-21 03:53:35 +08:00
|
|
|
return force;
|
2016-04-08 02:32:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function apply(force) {
|
2016-04-22 04:44:52 +08:00
|
|
|
force(alpha);
|
2016-04-08 02:32:34 +08:00
|
|
|
}
|
|
|
|
|
|
2016-04-26 08:04:51 +08:00
|
|
|
initializeNodes();
|
2016-04-08 23:32:24 +08:00
|
|
|
|
2016-04-08 02:32:34 +08:00
|
|
|
return simulation = {
|
|
|
|
|
start: start,
|
|
|
|
|
stop: stop,
|
|
|
|
|
tick: tick,
|
2016-04-22 06:52:14 +08:00
|
|
|
|
2016-04-06 04:50:36 +08:00
|
|
|
nodes: function(_) {
|
2016-04-21 06:24:49 +08:00
|
|
|
return arguments.length ? (nodes = _, initializeNodes(), force.each(initializeForce), simulation) : nodes;
|
2016-04-06 04:50:36 +08:00
|
|
|
},
|
2016-04-22 06:52:14 +08:00
|
|
|
|
2016-04-07 04:51:59 +08:00
|
|
|
alphaMin: function(_) {
|
2016-04-08 02:32:34 +08:00
|
|
|
return arguments.length ? (alphaMin = _, simulation) : alphaMin;
|
2016-04-06 04:50:36 +08:00
|
|
|
},
|
2016-04-22 06:52:14 +08:00
|
|
|
|
2016-04-06 04:50:36 +08:00
|
|
|
alphaDecay: function(_) {
|
2016-04-23 01:04:24 +08:00
|
|
|
return arguments.length ? (alphaDecay = -_, iteration = alphaDecay ? Math.round(Math.log(alpha) / alphaDecay) : 0, simulation) : -alphaDecay;
|
2016-04-06 04:50:36 +08:00
|
|
|
},
|
2016-04-22 06:52:14 +08:00
|
|
|
|
2016-04-26 01:42:19 +08:00
|
|
|
drag: function(_) {
|
|
|
|
|
return arguments.length ? (drag = 1 - _, simulation) : 1 - drag;
|
2016-04-08 02:32:34 +08:00
|
|
|
},
|
2016-04-22 06:52:14 +08:00
|
|
|
|
2016-04-08 02:32:34 +08:00
|
|
|
force: function(name, _) {
|
2016-04-21 06:24:49 +08:00
|
|
|
return arguments.length > 1 ? ((_ == null ? force.remove(name) : force.set(name, initializeForce(_))), simulation) : force.get(name);
|
2016-04-06 04:50:36 +08:00
|
|
|
},
|
2016-04-22 06:52:14 +08:00
|
|
|
|
2016-04-06 04:50:36 +08:00
|
|
|
on: function(name, _) {
|
2016-04-23 00:45:03 +08:00
|
|
|
return arguments.length > 1 ? (event.on(name, _), simulation) : event.on(name);
|
2016-04-06 04:50:36 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|