Last active
January 5, 2016 22:34
-
-
Save jordanstephens/af0d106731f56ef3ac0d to your computer and use it in GitHub Desktop.
calculate bbox union of selected svg elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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