I am trying to fine tune the zoom behavior for a graph and notice something curious.
Here is the code:
var zoom = d3.zoom()
.scaleExtent([1 / 2, 2])
.on("zoom", function() {
var duration = 250;
if (d3.event.sourceEvent.type == "mousemove") {
duration = 0;
}
g.transition().duration(duration).attr("transform", d3.event.transform);
});
svg.call(zoom)
.on(“dblclick.zoom”, function() {
svg.call(zoom.transform, d3.zoomIdentity);
});
Apologies for the difficult to read formatting. I wanted to override the double click behavior so that it resets the graph to the original transform.
What’s odd is that for the d3.zoom function I set the g node to have the transform (note that g is the direct child of the svg element where I’m putting all of my graph elements). Where as in the “dblclick” handler I am calling the zoom.transform function on the svg element. When I inspect the page though, both cases are actually modifying the transform of the g node.
Can someone explain what’s going on? I’m having trouble grasping exactly how the zoom mechanics work and while the zooming behavior is behaving as I want it to I’d like to know why it’s doing what it’s doing.