After defining the d.fx
and d.fy
properties, the nodes will stay in place even if d.fixed
is set differently. The setting of these properties can be seen here:
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
This information is stated in the d3v4 API documentation:
If you want to fix a node at a specific position, you need to specify two additional properties:
fx - the fixed x-position of the node
fy - the fixed y-position of the node
At the end of each tick, when forces have been applied, a node with a defined node.fx will have its position reset to this value and its velocity on the x-axis (vx) set to zero. Similarly, a node with a defined node.fy will have its position reset to this value and its velocity on the y-axis (vy) set to zero. To unfix a node that was previously fixed, simply nullify node.fx and node.fy, or delete these properties.
I only discovered this after creating my demo which was based on your block. Interestingly, I removed the values of d.fx
and d.fy
upon double-clicking, allowing them to be repositioned according to the force diagram.
The reason why you may encounter the use of d.fixed to fix positions is because this was the method utilized in v3:
fixed - a boolean indicating whether the node's position is locked.