Skip to content

Instantly share code, notes, and snippets.

@GeoffCox
Last active August 29, 2015 14:28
Show Gist options
  • Save GeoffCox/49c44d700724b9b41cc1 to your computer and use it in GitHub Desktop.
Save GeoffCox/49c44d700724b9b41cc1 to your computer and use it in GitHub Desktop.
Measure text in SVG
function measureSvgText(text, className) {
if (!text || text.length === 0) {
return { width: 0, height: 0 };
}
var svg = d3.select('body').append('svg');
if (className) {
svg.attr('class', className);
}
svg.append('text').attr({ x: -1000, y: -1000 }).text(text);
var bounds = svg.node().getBBox();
svg.remove();
return { width: bounds.width, height: bounds.height };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment