Reputation: 8066
This is a polygon in my svg file.
<polygon id="UKCYEW" fill="#09252E" points="840.218,415.85 696.468,415.85 696.468,373.974 817.176,373.558 "/>
Through D3.js I can get all the points in the polygon and get the center of this polygon
var points = d3.select('#'+name).attr('points');
if(points !=null){
var temps = [], posx = 0, posy = 0;
temps = points.split(' ');
//console.log('.................................. temperary positions',temps);
temps.forEach(function(e){
// console.log(e);
var arr = e.split(',');
posx += Number(arr[0]), posy +=Number(arr[1]);
})
posx = posx/temps.length , posy = posy/temps.length;
}
now the point for me, is how to compute the size of polygon
Upvotes: 0
Views: 528
Reputation: 123995
document.getElementById("UKCYEW")[0].getBBox()
will get the x, y, width and height. The size is the width and height and the centre is x + width / 2, y + height / 2. Isn't that simpler?
Upvotes: 3