Skip to content

Instantly share code, notes, and snippets.

@steveruizok
Created November 12, 2023 10:02
Show Gist options
  • Save steveruizok/8fc84900334028b705abcb724c8f98fb to your computer and use it in GitHub Desktop.
Save steveruizok/8fc84900334028b705abcb724c8f98fb to your computer and use it in GitHub Desktop.
get the bounding box of a text shape?
function getTextBoundingBox(text: SVGTextElement) {
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
svg.appendChild(text)
document.body.appendChild(svg)
const bbox = text.getBoundingClientRect()
document.body.removeChild(svg)
return bbox
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment