Skip to content

Instantly share code, notes, and snippets.

@jordanstephens
Last active January 5, 2016 22:34
Show Gist options
  • Save jordanstephens/af0d106731f56ef3ac0d to your computer and use it in GitHub Desktop.
Save jordanstephens/af0d106731f56ef3ac0d to your computer and use it in GitHub Desktop.
calculate bbox union of selected svg elements
// example usage:
// bBoxUnion("svg > *") => Object {x: 464.9, y: 521.0, width: 319.75, height: 75.5}
function bBoxUnion(selector) {
var nodeList = document.querySelectorAll(selector),
nodeArray = Array.prototype.slice.call(nodeList);
return nodeArray.reduce(function(memo, node) {
if (node.getBBox) {
var bBox = node.getBBox();
memo.x = Math.max(memo.x, bBox.x);
memo.y = Math.max(memo.y, bBox.y);
memo.width = Math.max(memo.width, bBox.width);
memo.height = Math.max(memo.height, bBox.height);
}
return memo;
}, { x: 0, y: 0, width: 0, height: 0 });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment