Skip to content

Instantly share code, notes, and snippets.

@gka
Last active March 10, 2017 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gka/b535cb0be34d276f836d6cd1f82c9542 to your computer and use it in GitHub Desktop.
Save gka/b535cb0be34d276f836d6cd1f82c9542 to your computer and use it in GitHub Desktop.
forces particles in a d3.forceSimulation into a [[x1, x2], [y1, y2]] extent
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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment