Accepted answer

The zoom scale is stored in your zoom object. I'm guessing you have a line of code that looks like:

var zoom = d3.behavior.zoom() 

Getting the scale from that object is simple:


To zoom out x2:

zoom.scale( zoom.scale()/2 )

Translation works the same way, with zoom.translate() and zoom.translate( [x, y] ) to get and set.

To keep the display transform in sync with the zoom, just make sure that when you update one, the other is also updated.


There's a easier way in D3 v5. You can call, d3.zoomIdentity); to reset the zoom, and even apply a smooth transition like selection.transition().duration(750).call(zoom.transform, d3.zoomIdentity);.


Related Query

More Query from same tag