Skip to content

Instantly share code, notes, and snippets.

@audinue
Created December 14, 2021 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audinue/2de1c615bb843e5b6d3852a1ebc0ec76 to your computer and use it in GitHub Desktop.
Save audinue/2de1c615bb843e5b6d3852a1ebc0ec76 to your computer and use it in GitHub Desktop.
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