Get X and Y of <g> with ID even when zooming in

Asked

Viewed 59 times

2

I have a map of Brazil with several <g> svg. Each element of this is a municipality. I need to take the X and Y of this element in relation to the div that svg is in. The biggest problem is because I have a zoom, that by clicking for example on "More Zoom" he zoom changing the Matrix of <g>, Thus the map of Brazil is closer to the screen, but the X and Y of it remains the same in relation to div. So if the zoom is too far or too close, it doesn’t work, because the X and Y stay fixed.

I’m taking the X and the Y with the code:

var path      = document.getElementById(municipio).getBoundingClientRect();
var svggerado = document.getElementById('svggeradodiv').getBoundingClientRect();

var calculaX  = Math.round(path.left - (svggerado.left + 110));
var calculaY  = Math.round(path.top);
  • Show an example. Do you have a Jsfiddle of this? It is worth investing a few hours and understand well the functioning of matrix() and SVG coordinate systems (which can be changed for each element). Here’s a good [tutorial that describes the math in detail][http://www.useragentman.com/blog/2011/01/07/css3-matrix-transform-for-the-mathematically-challenged/]. Are you using D3? It’s much easier to manipulate zooms and maps using SVG through a framework like [D3.js][d3js.org]. It’s worth a look.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.