d3 / d3-zoom

Pan and zoom SVG, HTML or Canvas using mouse or touch input.
https://d3js.org/d3-zoom
ISC License
507 stars 143 forks source link

Disable zoom behaviour #156

Closed oufresh closed 5 years ago

oufresh commented 6 years ago

Hello,

I'm using d3-zoom 1.7.3 and d3 5.5. Which is the correct way to disable the zoom behaviour applied to a selection? I would like to enable and disable it on my svg runtime with a switch.

the init funcion is:

initZoom(scale, x, y) {
this.zoom = d3Zoom().scaleExtent(scaleExtent);
    this.svg.call(this.zoom);
    this.zoom.on("zoom", this.zoomed);
    this.svg.call(
      this.zoom.transform,
      zoomIdentity.translate(x, y).scale(scale)
    );
}

To enable/disable depending on a flag is correct to do:

if (enableZoom == true && this.zoom == null) {
      this.initZoom(
        this.props.transform.currentExpScale,
        this.props.transform.transformX,
        this.props.transform.transformY
      );
    }
    if (enableZoom == false && this.zoom != null) {
      this.zoom.on("zoom", null);
      this.svg.on(".zoom", null);
      this.zoom = null;
    }

or there are problems with memory leaks, eventListeners and so on?

Thank you very much.

oufresh

mbostock commented 5 years ago

Per the README, you can remove the zoom listeners like so:

selection.on(".zoom", null);