[D3][SVG] zoom to object

Fen1kz

could you please help me with zooming to SVG objects. no idea how to do this. i need to zoom and center by click on object, i've made a test plunkr, so please take a look: http://plnkr.co/edit/ZQxhQ8VVoIXjMvdFIvQF
here's full code:

$(function(){
  svg = d3.select("#svg");
  svg_group = d3.select('#outer_group');

  zoom = d3.behavior.zoom()
      .translate([0, 0])
      .scale(1)
      .scaleExtent([.5, 20])
      .on("zoom", zoomed);

  svg.call(zoom);

  function zoomed() {
    svg_group.style("stroke-width", 1.5 / d3.event.scale + "px");
    svg_group.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
    $zoomService.$broadcast('zoom', {
        scale: d3.event.scale
    });
  }

  $('.sector').click(function(){
    //zoom to somehow??
  });
});
musically_ut

You have to use call zoom.event explicitly on the correct element after setting the desired translation and scaling.

  var zoomed = false;
  $('.sector').click(function(){
    var bbox = this.getBBox();
    var scale = 4;

    // Manually calculating the position to which to transition to
    // Will differ on a case by case basis.
    svg
    .call(zoom
          .translate([ (- bbox.x - bbox.width / 2) * scale
                     , (- bbox.y - bbox.height / 2) * scale
                     ])
          .scale(scale).event
    );
  });

Demo: http://plnkr.co/edit/h1UP87dfQneRCFye9Xtu?p=preview

In the demo, I changed the position of the polygons and the viewBox on the svg to make it easier to calculate the exact coordinates to transition to for the zoom to stay centered. I also added some transitions and zoom-to-zero behavior not shown in the code excerpt above.


Sidenote: You don't have to use jQuery here to bind to click events; D3's selection.on can provide that function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

[D3][SVG] zoom to object

From Dev

How to zoom in and center on an object with fabricjs?

From Dev

autocad zoom object to center screen

From Dev

How to zoom out on an object with glTranslatef?

From Dev

D3 geometric zoom when there is no SVG element under the cursor

From Dev

D3.js map zoom behaviors in canvas and svg

From Dev

d3: svg image in zoom circle packing

From Dev

Plugin to zoom and pan SVG

From Dev

d3 stacking and zoom

From Dev

D3 How to zoom in on SVG text that stays within an SVG Rectangle?

From Dev

D3 manually zoom,how to set the translate for zoom

From Dev

D3 zoom is not properly reseting zoom state and translated location

From Dev

d3.js change zoom behavior to semantic zoom

From Dev

Zoom in OpenGL ES 2.0 - object disappearing

From Dev

Zoom in OpenGL ES 2.0 - object disappearing

From Dev

SVG zoom in on mouse - mathematical model

From Java

how to zoom in on a complex svg structure

From Dev

SVG Opera 12 ctrl zoom

From Dev

D3 Zoom in version 3

From Dev

d3js attach object to svg

From Dev

How can I get a SVG path shape to scale on zoom in D3?

From Dev

Select SVG elements using rectangle select box not working during zoom : d3.js

From Dev

Apply zoom event on existing SVG (d3.js v4)

From Dev

How to make SVG:A elements clickable when using D3.js Pan & Zoom behaviour?

From Dev

D3.js zoom with nested svg breaks viewport in Internet Explorer

From Dev

Zoom to bounding box of path on externally loaded svg using D3

From Dev

Position D3 SVG Canvas x/y and Zoom Level On Load

From Dev

D3.js zoom with nested svg breaks viewport in Internet Explorer

From Dev

Select SVG elements using rectangle select box not working during zoom : d3.js

Related Related

HotTag

Archive