Skip to content

Instantly share code, notes, and snippets.

@strokirk
Created November 23, 2020 11:03
Show Gist options
  • Select an option

  • Save strokirk/ba702e972a5fc7bc41eabcd694f6a28d to your computer and use it in GitHub Desktop.

Select an option

Save strokirk/ba702e972a5fc7bc41eabcd694f6a28d to your computer and use it in GitHub Desktop.
Calculate the viewBox of an SVG element
function calculateViewBox(svgString) {
// Get the bounding-box of a SVG element, formatted to use as it's viewBox
let d = document.createElement("div")
d.innerHTML = s
document.body.appendChild(d)
let r = d.firstElementChild.getBBox()
return `${r.x} ${r.y} ${r.width} ${r.height}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment