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!
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
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With