Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add forceExtent #163

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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][1] + 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