Created
December 14, 2021 10:46
-
-
Save audinue/2de1c615bb843e5b6d3852a1ebc0ec76 to your computer and use it in GitHub Desktop.
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 textBBox (text, attributes) { | |
var ns = 'http://www.w3.org/2000/svg' | |
var svg = document.createElementNS(ns, 'svg') | |
var svgText = document.createElementNS(ns, 'text') | |
for (var name in attributes) { | |
svgText.setAttribute(name, attributes[name]) | |
} | |
svgText.textContent = text | |
svg.appendChild(svgText) | |
svg.style.position = 'fixed' | |
document.body.appendChild(svg) | |
var bbox = svgText.getBBox() | |
document.body.removeChild(svg) | |
return bbox | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment