Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3.js v4 programmatic Pan+Zoom. How?

Tags:

d3.js

I did a small example of what i am trying to implement, here it is - https://jsfiddle.net/zeleniy/4sgqgcx0/. You can zoom and pan SVG image as usual. But i have no idea how to implement zoom programmatically when user click on "+" and "-" buttons. Especially if he already zoom in/out and pan original image. Could you help me?

On line 13 of the code you will find zoom event handler.

var zoom = d3.zoom()
    .on('zoom', function() {
        canvas.attr('transform', d3.event.transform);
    });

On lines 35 and 39 - zoom event handlers

d3.select('#zoom-in').on('click', function() {
    // what here?
});

d3.select('#zoom-out').on('click', function() {
    // what here?
});

So when user click on "+" app should zoom in as if mouse were at the center of SVG element. And the same with "-".

like image 513
zeleniy Avatar asked Mar 05 '17 19:03

zeleniy


People also ask

How do you get the current zoom level in Diablo 3?

As of D3 v4 there are two documented ways of retrieving the zoom state. To quote d3/d3-zoom README (under Zoom Transforms): To retrieve the zoom state, use event. transform on a current zoom event within a zoom event listener (see zoom.

Which of these methods help alter zoom modes in d3 JS?

With the help of the d3. zoom function you can alter zoom modes in d3 js.


1 Answers

Thanks to Fil. Here is an updated version of jsfiddle.

d3.select('#zoom-in').on('click', function() {
  // Smooth zooming
  zoom.scaleBy(svg.transition().duration(750), 1.3);
});

d3.select('#zoom-out').on('click', function() {
  // Ordinal zooming
  zoom.scaleBy(svg, 1 / 1.3);
});
like image 101
zeleniy Avatar answered Sep 26 '22 15:09

zeleniy