Skip to content

Instantly share code, notes, and snippets.

@igodorogea
Created March 25, 2016 15:25
Show Gist options
  • Save igodorogea/c1d44f3cfbe56274dbe6 to your computer and use it in GitHub Desktop.
Save igodorogea/c1d44f3cfbe56274dbe6 to your computer and use it in GitHub Desktop.
$($0).find('path').each(function(idx, path) {
var coords = $(path)
.attr('d')
.split(' ')
.filter(function(el) {
return /^(M|L)/.test(el);
})
.map(function(el) {
return el.replace(/^(M|L)/, '').split(',');
});
var minX = parseFloat(coords[0][0]), minY = parseFloat(coords[0][1]), maxX = 0, maxY = 0, x, y, avgX, avgY;
coords.forEach(function(coord) {
x = parseFloat(coord[0]);
y = parseFloat(coord[1]);
if (x > maxX) {
maxX = x;
}
if (y > maxY) {
maxY = y;
}
if (x < minX) {
minX = x;
}
if (y < minY) {
minY = y;
}
});
avgX = ((minX + maxX) / 2).toFixed(1);
avgY = ((minY + maxY) / 2).toFixed(1);
// $(path).attr('limits', minX.toFixed(1) + ',' + minY.toFixed(1) + ',' + maxX.toFixed(1) + ',' + maxY.toFixed(1));
// $(path).attr('center', avgX.toFixed(1) + ',' + avgY.toFixed(1));
console.log(avgX, avgY);
$($0).append('<text rel="' + path.id + '" text-anchor="middle" x="' + avgX + '" y="' + avgY + '"></text>');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment