Skip to content

Instantly share code, notes, and snippets.

@bignimbus
Created November 29, 2018 20:05
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 bignimbus/56b13326c1ffd54cff84f78fda6197b3 to your computer and use it in GitHub Desktop.
Save bignimbus/56b13326c1ffd54cff84f78fda6197b3 to your computer and use it in GitHub Desktop.
const cropSvgToContentOnly = (svgElement) => {
const {
x,
y,
width,
height,
} = svgElement.getBBox();
const viewBoxValue = [x, y, width, height].join(' ');
svgElement.setAttribute('viewBox', viewBoxValue);
};
// example:
// cropSvgToContentOnly(document.querySelector('svg'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment