Sg2.9
Get bounds of multiple select...
Recent RSS
Get bounds of multiple selected items?
From:  Zed
Date:  12. February 2012, 15:46

Hello there,

I'm trying to get the bounds for, say, 4 rectangles on screen, just as if they were a group, but they're not, they're separate elements, just selected together.

I can get the bounds of all of them separately, then get the highest and lowest values, and find the rectangle enclosing them.

I could also just group them together, get the bounds for the group and then remove them from the group, but this feels a bit too clumsy to me.

How would you do this, is there any easier way?

Thanks!

Re: Get bounds of multiple selected items?
Date:  13. February 2012, 15:14

Hi Zed,

you are right about both of the methods you speak of.
Unfortunately, the document.selectedItems returns an array and not an object so there is no way of getting the bounding box that way. The simplest solution I have come up with is this:

var selectedItems = document.selectedItems

if(selectedItems.length > 0){
			var xArray = [], yArray = []
		
			for(i = 0; i < selectedItems.length; i++){
				xArray.push(selectedItems[i].bounds.left, selectedItems[i].bounds.right)
				yArray.push(selectedItems[i].bounds.top, selectedItems[i].bounds.bottom)
			}
		
			var boundingBox = new Rectangle(new Point(Math.min.apply(null, xArray), Math.min.apply(null, yArray)), new Point(Math.max.apply(null, xArray), Math.max.apply(null, yArray)))
}

This way you could easily make a path of the bounding box by adding this line:

var rectangle = new Path.Rectangle(boundingBox)

between line 11-12.
Hope that helps.

Best wishes
/ Håkan

Re: Get bounds of multiple selected items?
From:  Zed
Date:  14. February 2012, 13:13

Håkan,

that's what i feared :)

Many thanks for taking the time to write the code. That's a very clever use of 'apply' right there! Never read about that function.

Since I'm still starting on javascript, I think I'll take the longer but more readable (to me) course until I'm more comfortable with js and Sg:

var selectedItems = document.selectedItems;
var item;
var minX = 99999999;
var minY = 99999999;
var maxX = -99999999;
var maxY = -99999999;
					
for (var i = 0; i<selectedItems.length; i++)
{
	if (selectedItems[i].strokeBounds.left < minX) minX = selectedItems[i].strokeBounds.left;
	if (selectedItems[i].strokeBounds.right > maxX) maxX = selectedItems[i].strokeBounds.right;
	if (selectedItems[i].strokeBounds.top < minY) minY = selectedItems[i].strokeBounds.top;
	if (selectedItems[i].strokeBounds.bottom > maxY) maxY = selectedItems[i].strokeBounds.bottom;
}
					
item = new Rectangle(new Point(minX,minY), new Point(maxX,maxY));

Thanks for the idea and the code! I'll think of the apply method next time I face a situation like this, that's for sure.

Re: Get bounds of multiple selected items?
Date:  14. February 2012, 16:07

Hello again,
just to be clear, it wasn't me who came up with that solution. I merely found the apply-function here as I googled a way of finding the max/min value in an array of numbers...

Re: Get bounds of multiple selected items?
From:  Zed
Date:  14. February 2012, 19:30

Good find anyway, thanks!

Here's the script I was working on, finally: Crop Marks

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Hexagonal Rasterizer 02.03.14