D3 v4: Reset zoom state

前端 未结 5 2093
谎友^
谎友^ 2021-02-19 07:26

I have a SVG which can be zoomed and also a \"reset\" button. I\'m resetting the zoom with

zoom.transform(selection, d3.zoomIdentity)

This wor

5条回答
  •  没有蜡笔的小新
    2021-02-19 08:05

    This gist, which points to this working sample, ended up pointing me to the solution at least for my situation. As far as I can tell, it should fix your issue too.

    What you're missing is that you need to tell zoomIdentity what the initial x, y, and k states should be. The x and y state is set using .translate(x,y) and the scale is set using .scale(k). The code below should work.

    $("#reset-zoom-button").click(() => {
      zoom.transform(container, d3.zoomIdentity.translate(0,0).scale(0.7));
    })
    

    From what I can tell, translating the zoomIdentity to 0,0 should always reset the svg to the initial settings position settings, but it's possible you'll have to play around with this.

    Let me know if this works for you.

提交回复
热议问题