Skip to content

Instantly share code, notes, and snippets.

@benshimmin
Last active December 10, 2015 19:29
Show Gist options
  • Save benshimmin/4482073 to your computer and use it in GitHub Desktop.
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
# 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
@benshimmin
Copy link
Author

You could also put all the elements in a Raphaël set and do getBBox() on that, of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment