Last active
March 10, 2017 16:27
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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