Centroid of svg path element

450 views
Skip to first unread message

ALEI REYES

unread,
Feb 9, 2015, 11:41:02 AM2/9/15
to raph...@googlegroups.com
Is there an easy way to calculate the centroid of the svg path element?
currently, I am using this approximation:

function getCentroid(el){ 
    var pah = el.attr("path");
    var cnt = 0;
    var ans={x:0,y:0};
    for(var i=0;i<path.length;i++){
        if(path[i][0]=='M' || path[i][0]=='L'){
            ans.x+=path[i][1];
            ans.y+=path[i][2];
            cnt++;
        }
    }
    ans.x/=cnt;
    ans.y/=cnt;
    return ans;
}
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages