So, I have various groups of SVG elements, wrapped in s and want the user to be able to drag around and resize these objects when they mouse over them.
I kind of got it going but when switching between gourps, by onMouseOut of one and onMouseOver of another there is weird stuff going on. The zoom state of the last group is carried over to the new target group and weirdness happens. The docs state:
The zoom behavior stores the zoom state on the element to which the zoom behavior was applied, not on the zoom behavior itself. This allows the zoom behavior to be applied to many elements simultaneously with independent zooming. The zoom state can change either on user interaction or programmatically via zoom.transform.
I figure what is going on is that the state is stored in the parent i.e. svg element usually, and the transform is applied to a sub element, using the parent relatively that the transform is calculated against.
some code
const svg = d3.select(svgRef.current); // frame of reference
const g = svg.select("g");// should be the main group
function defaultZoomed({ transform }) {
g.attr("transform", transform);
}
const zoomedFunc = zoomed ? zoomed : defaultZoomed;
// Reset to kill any previoud state
svg.on(".zoom", null);
// Create a new zoom behavior
svg.call(d3.zoom()
.extent([[0, 0], [dimensions.width, dimensions.height]])
.scaleExtent([0.01, 10])
.on("zoom", zoomedFunc));
Here zoomed
is set onMouseOver
and onMouseOut
of the various zoomable things (Iām doing all this in React to keep it interesting).
What I really need to do is be able to reset the zoom state which I assume is kept on the parent svg
element, but how?