A force type for the d3-force simulation engine to hard limit nodes positions to a specified range. See demo.
It can be used, for example to keep nodes within boundaries.
It also features a cushioning force that repels nodes away from the boundaries. This can be useful to prevent nodes from accumulating at the edge of the canvas.
This force works best if it is used as the last force in the simulation engine chain.
See also d3.forceSurface.
import d3ForceLimit from 'd3-force-limit';
or using a script tag
<script src="https://github.com/vasturiano/d3-force-limit/raw/master//unpkg.com/d3-force-limit"></script>
then
d3.forceSimulation()
.nodes(<myNodes>)
.force('limit', d3.forceLimit()
.x0(20)
.x1(45)
);
Method | Description | Default |
---|---|---|
radius([num or fn]) | Getter/setter for the node radius accessor function (fn(node) ) or a constant (num ) for all nodes. |
1 |
x0([num or fn]) | Getter/setter for the minimum X accessor function (fn(node) ) or a constant (num ) for all nodes. |
-Infinity |
x1([num or fn]) | Getter/setter for the maximum X accessor function (fn(node) ) or a constant (num ) for all nodes. |
Infinity |
y0([num or fn]) | Getter/setter for the minimum Y accessor function (fn(node) ) or a constant (num ) for all nodes. |
-Infinity |
y1([num or fn]) | Getter/setter for the maximum Y accessor function (fn(node) ) or a constant (num ) for all nodes. |
Infinity |
z0([num or fn]) | Getter/setter for the minimum Z accessor function (fn(node) ) or a constant (num ) for all nodes. Only applicable when using a 3-dimensional force engine. |
-Infinity |
z1([num or fn]) | Getter/setter for the maximum Z accessor function (fn(node) ) or a constant (num ) for all nodes. Only applicable when using a 3-dimensional force engine. |
Infinity |
cushionWidth([num]) | Getter/setter for the thickness (in px ) of the cushioning force that repels nodes away from the specified boundaries. Nodes outside this cushion margin will not be affected. |
0 |
cushionStrength([num]) | Getter/setter for the intensity or elasticity (measured in px/tick² ) of the boundary cushion. The higher this value the stronger nodes within the cushion margin will be pushed back. The intensity of this force increases linearly with the portion of cushion that has been crossed, being at full intensity when the node is touching the boundary. |
0.01 |