Skip to content

Commit

Permalink
add forceExtent (by @gka)
Browse files Browse the repository at this point in the history
fixes #89

note that the extent's definition in this PR is [[*xmin*, *ymin*], [*xmax*, *ymax*]], more in line with, for example zoom.extent. Defaults to [[0,0], [960,500]].
  • Loading branch information
Fil committed Jun 25, 2020
1 parent 194dba9 commit 09d7c4a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ If *x* is specified, sets the *x*-coordinate of the centering position to the sp

If *y* is specified, sets the *y*-coordinate of the centering position to the specified number and returns this force. If *y* is not specified, returns the current *y*-coordinate, which defaults to zero.

<a name="forceExtent" href="#forceExtent">#</a> d3.<b>forceExtent</b>([<i>extent</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/extent.js#L1 "Source")

Creates a new extent force with the specified extent as [[xmin, ymin], [xmax, ymax]]. If *extent* is not specified, its defaults to [⟨0,0⟩, ⟨960,500⟩]. This force maintains the nodes inside the extent, taking into account their radius (if the property is set).

<a name="extent_extent" href="#extent_extent">#</a> <i>extent</i>.<b>extent</b>([<i>extent</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/extent.js#L1 "Source")

If *extent* is specified, sets the extent and returns this force. If *extent* is not specified, returns the current extent.

#### Collision

The collision force treats nodes as circles with a given [radius](#collide_radius), rather than points, and prevents nodes from overlapping. More formally, two nodes *a* and *b* are separated so that the distance between *a* and *b* is at least *radius*(*a*) + *radius*(*b*). To reduce jitter, this is by default a “soft” constraint with a configurable [strength](#collide_strength) and [iteration count](#collide_iterations).
Expand Down
25 changes: 25 additions & 0 deletions src/extent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default function(extent) {
var nodes;

if (extent === undefined) extent = [[0, 0], [960, 500]];

function force() {
for (var i = 0; i < nodes.length; ++i) {
var node = nodes[i], r = node.radius || 0;
node.x = clamp(node.x, extent[0][0] - r, extent[1][0] + r);
node.y = clamp(node.y, extent[0][1] - r, extent[1][0] + r);
}
}

force.initialize = function(_) { nodes = _; };

force.extent = function(_) {
return arguments.length ? (extent = _, force) : extent;
};

return force;
}

function clamp(x, min, max) {
return Math.max(min, Math.min(max, x));
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export {default as forceCenter} from "./center.js";
export {default as forceCollide} from "./collide.js";
export {default as forceExtent} from "./extent.js";
export {default as forceLink} from "./link.js";
export {default as forceManyBody} from "./manyBody.js";
export {default as forceRadial} from "./radial.js";
Expand Down

0 comments on commit 09d7c4a

Please sign in to comment.