Last active
August 29, 2015 14:28
-
-
Save GeoffCox/49c44d700724b9b41cc1 to your computer and use it in GitHub Desktop.
Measure text in SVG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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