I'm using D3's drag behavior to drag circle elements in a force layout with circleGroup.call(force.drag)
, where force.drag
is a drag behavior and circleGroup
is a selection of all circle elements. This works fine for dragging a single element.
How can I drag an arbitrary selection of multiple circle elements all at once?
Note that because the selection must be arbitrary, I don't think I can just just group the ones I want to drag together in a <g>
tag.
I've looked at these questions and still haven't been able to get it working.
You could do something like this... It's not a force layout, but you should be able to extend it to that pretty easily.
Click on a circle to select it, and then click and drag somewhere else to move them around.
Basically, I'm keeping track of the index of the selected circles in an array, and updating the corresponding data in the
drag
handler. After the data is updated, I then modify thecx
andcy
attributes of the selected circles.Note: the
drag
handler is attached to a transparentrect
that covers the entire SVG, and I'm using CSS styles to get the events to cascade to the respective SVG elements correctly withpointer-events: all;
applied to therect
.