Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unregister zoom listener and restore scroll ability in D3.js

I'm currently working D3.js. This is the code to register zoom listener for my svg tag

var zoomListener = d3.behavior.zoom().scaleExtent([0.1, 3]).on("zoom", zoom);
function zoom() {
  vis.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
svg.call(zoomListener)

Now when I scroll the mouse on the svg element, it is zoomed. But when I unbind the zoom event like this

zoomListener.on("zoom", null);

only the svg is not zoomed but I cannot scroll the page while the mouse is over the svg element.

How to unbind the zoom listener and restore the scroll ability for the mouse? thanks!

like image 919
tmtxt Avatar asked Mar 10 '14 14:03

tmtxt


People also ask

How to use zoom () function in D3 JS?

The d3.zoom () Function in D3.js is used to create a new zoom behaviour. It is used to apply the zoom transformation on a selected element. Parameters: This function does not accept any parameter. Return Value: This function returns the zoom behaviour. Below programs illustrate the d3.zoom () function in D3.js

How to prevent scrolling on wheel input regardless of scale extent?

The user can try to zoom by wheeling, when already at the corresponding limit of the scale extent. If we want to prevent scrolling on wheel input regardless of the scale extent, register a wheel event listener to prevent the browser default behavior. If the extent is specified, it sets the translate extent to the specified array of points.

How to use zooming API methods?

Following are some of the most commonly used Zooming API Methods. Let us go through all these Zooming API methods in brief. It creates a new zoom behavior. We can access it using the script below. It is used to apply the zoom transformation on a selected element. For example, you can instantiate a mousedown.zoom behavior using the following syntax.

What is the difference between interpolate and listener in D3?

If interpolate is not specified, it returns the current interpolation factory, which defaults to d3.interpolateZoom. If the listener is specified, it sets the event listener for the specified typenames and returns the zoom behavior. The typenames is a string containing one or more typename separated by whitespace.


1 Answers

To completely disable the zoom behaviour, you need to unregister all event handlers that it has installed on the element you've called it on:

svg.on("mousedown.zoom", null);
svg.on("mousemove.zoom", null);
svg.on("dblclick.zoom", null);
svg.on("touchstart.zoom", null);
svg.on("wheel.zoom", null);
svg.on("mousewheel.zoom", null);
svg.on("MozMousePixelScroll.zoom", null);
like image 156
Lars Kotthoff Avatar answered Oct 15 '22 12:10

Lars Kotthoff