Last active
December 10, 2015 19:29
-
-
Save benshimmin/4482073 to your computer and use it in GitHub Desktop.
How to get the bounds of *all* elements on your Raphaël paper
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
# I'm pretty sure there isn't a built-in way of doing this; | |
# the below is one (simple) way... | |
# This assumes `paper` is your Raphael paper | |
getElementsBounds = -> | |
bottom = paper.bottom | |
elements = [] | |
while bottom | |
elements.push bottom | |
bottom = bottom.next | |
minX = Number.MAX_VALUE | |
minY = Number.MAX_VALUE | |
maxWidth = 0 | |
maxHeight = 0 | |
for elem in elements | |
bb = elem.getBBox() | |
minX = Math.min minX, bb.x | |
minY = Math.min minY, bb.y | |
maxWidth = Math.max maxWidth, (bb.x + bb.width) | |
maxHeight = Math.max maxHeight, (bb.y + bb.height) | |
x : minX | |
y : minY | |
width : maxWidth | |
height : maxHeight |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could also put all the elements in a Raphaël set and do
getBBox()
on that, of course.