Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting zooming properties d3

Tags:

zooming

d3.js

I'm trying to find a way to reset the zoom property of my svg whenever I click on an object and leave it as is. For example in this jsfiddle if you zoom out and click on a square, the zoom gets reset but then when I try to pan the screen the zoom goes back to what it was before I clicked on the square. Is there a way such that when I click on a square, the zoom goes back to 100% and stays at 100% even when panning?

JSFiddle: http://jsfiddle.net/nrabinowitz/p3m8A/

Here is my zoom call:

svg.call(d3.behavior.zoom().on("zoom", redraw));
like image 745
user2534068 Avatar asked Jul 02 '13 20:07

user2534068


1 Answers

The key is to tell the zoom behaviour that you're resetting scale and translation. To achieve this, simply save it in a variable and set the values appropriately as you're setting the transform attribute of the SVG.

var zoom = d3.behavior.zoom();
svg.call(zoom.on("zoom", redraw));
// ...
.on("click", function () {
        counter++;
        canvas
          .transition()
          .duration(1000)
          .attr('transform', "translate(" + (offset * counter) + ",0)");
        zoom.scale(1);
        zoom.translate([offset * counter, 0]);
     drawBox(x, y);
    });

Updated jsfiddle here.

like image 92
Lars Kotthoff Avatar answered Nov 08 '22 17:11

Lars Kotthoff