d3-force-gravity
Implement gravitational attraction (or force-field-like repulsion) using d3-force
Go here for a live demo and here for the code used in the demo
Installation
Using npm :
$ npm install --save d3-force-gravity
If you want to use this directly on your browser, use the contents of d3-force-gravity.min.js
, or, include this script after the d3 library in your HTML file :
Usage
Using npm :
const width = 200const height = 400 //First, require the moduleconst forceGravity = //Add it to your d3 force simulation, just like other forcesd3
Using directly in the browser :
- Follow installation steps above, and include the appropriate script.
- This force will be made available on
d3.forceGravity
. - Be sure to include the d3 library first
Documentation
forceGravity([x, y])
Creates a new gravitational force centered on the coordinates x
and y
.
Defaults :
x : 0
y : 0
Returns a new gravity
force
gravity.strength([strength])
If strength
is specified, sets the strength of the force to its value, and returns the force.
If it is not specified, returns the current value of the forces strength.
If the strength is negative, it will lead to replusion of all nodes from the center of the force.
Default : 10000
gravity.minRadius([minRadius])
The minimum radius outside of which this force is applied. Any node closer in distance to the center than this value, is not affected by the force. This is to prevent the enormous amount of force that would result from particles getting too close (tending to infinity at the center)
If minRadius
is specified, sets the minRadius of the force to its value, and returns the force.
If it is not specified, returns the current value of the forces minRadius.
Default : 40
gravity.x([x])
Set the x coordinate of the center of the force.
If x
is specified, sets the x coordinate of the force to its value, and returns the force.
If it is not specified, returns the current value of the forces x coordinate.
gravity.y([y])
Set the y coordinate of the center of the force.
If y
is specified, sets the y coordinate of the force to its value, and returns the force.
If it is not specified, returns the current value of the forces y coordinate.
License
MIT © sohamkamani