d3-force
d3-force copied to clipboard
add d3.forceExtent
I find this useful:
d3.forceExtent = function(extent) {
var nodes;
if (extent == null) extent=[[0,800], [0,500]];
function force() {
var i,
n = nodes.length,
node,
r = 0;
for (i = 0; i < n; ++i) {
node = nodes[i];
r = (node.radius || 0);
node.x = Math.max(Math.min(node.x, extent[0][1]-r), extent[0][0]+r);
node.y = Math.max(Math.min(node.y, extent[1][1]-r), extent[1][0]+r);
}
}
force.initialize = function(_) { nodes = _; };
force.extent = function(_) {
return arguments.length ? (extent = _, force) : extent;
};
return force;
};
Thank you, @gka. Your code saved me some time!
+1 for adding this. This kind of force helped me to capture orphan nodes and prevent them to fly away out of screen bounds
This would certainly be useful, I've made PR #163 from your proposal, however changing the way the extent is defined and its defaults to [[xmin, ymin], [xmax, ymax]] = [[0,0], [960, 500]].