Sg2.9
Changing Path Size
Recent RSS
Changing Path Size
From:  Ray
Date:  17. January 2013, 12:01

This is a very basic question but I'm having trouble getting a path to change size. In this example I want to change the selected paths to an absolute size.

var paths = document.getItems({
selected: true
});

if (paths.length > 0 ){
for(i=0; i<paths.length; i++) {
var path = paths[i];
path.size = new Size(100);
}
}

This script doesn't do anything. I can add points to the paths and change their color without any trouble. I've used path.scale to scale the objects but I can't seem to get them to size to one value.

I'll continue to look through the provided scripts until I find one that uses this function but just in case I can't find anything, I'd love some help.
Thanks

Re: Changing Path Size
From:  pqbd
Date:  17. January 2013, 21:18

A path does not have a size property. Can you describe what it is you're trying to accomplish?

Re: Changing Path Size
From:  Ray
Date:  17. January 2013, 22:00

I'm trying to make the selected paths all have the same size. The paths will be closed, (squares, circles, stars, etc).

I'm working on a tool similar to "object raster" but it will be applied after the objects are already drawn, similar to "colorizer." I want to take the selected paths, make them one unit size, and then scale them by raster.getAverageColor(path). I have something working now, but it only scales by the average value, it doesn't make the objects a uniform size first.

Re: Changing Path Size
From:  pqbd
Date:  17. January 2013, 22:28

I'm assuming you mean the size of the bounding box enclosing each path. You can take the square root of the mean area of all paths, and scale each path by its deviation from the mean.

sel = document.selectedItems;
var mean = 0;
for (i=0; i<sel.length; i++) {
var boundArea = sel[i].bounds.width*sel[i].bounds.height;
mean+=boundArea;
}
mean /= sel.length;
mean = Math.sqrt(mean);
for (i=0; i<sel.length; i++) {
sel[i].scale(mean/Math.sqrt(sel[i].bounds.width * sel[i].bounds.height));
}

Re: Changing Path Size
From:  Ray
Date:  17. January 2013, 23:12

Yes, the bounding box is a better description.

I'll review your provided example when I have a free moment but is there some simple way to change a selected items bounding box to an absolute value, like 100x100?

Thanks a lot for the reply.

Re: Changing Path Size
From:  pqbd
Date:  18. January 2013, 00:28

Well, since some polygons like stars only have a square bounding box at specific orientations, you probably don't want to scale an item's height and width independently to match a square configuration.

You could use the same method as above, but using a target value instead an average:

sel = document.selectedItems;
var targetValue = 100;
for (i=0; i<sel.length; i++) {
sel[i].scale(targetValue/Math.sqrt(sel[i].bounds.width * sel[i].bounds.height));
}

Re: Changing Path Size
From:  Ray
Date:  18. January 2013, 03:24

Thanks. That works great. I was able to implement it into the script I was working on without any trouble. I also learned quite a bit from those 2 examples.

I noticed that you can set the width and height bounds directly with:

sel[i].bounds.width = 10;
sel[i].bounds.height = 10;

Is there a way to set them both with one line? I thought maybe

sel[i].bounds = 10;
or
sel[i].bounds = 10, 10;

would work but it doesn't do anything. The reference section of this site is great but I'm still very new to this.

Re: Changing Path Size
Date:  18. January 2013, 10:23

Hi,
Yes you can set it with only one command. This is where you use the Size constructor.
Just use it like this:

var path = new Path.Rectangle(new Point(0, 0), new Size(100, 10));
path.bounds.size = new Size(100, 100);

If you want you could check each objects area and scale it using that value. That way you'd be able to get objects that are exactly the same size in terms of area. Though, the result might be a little peculiar since an arbitrarily formed object could get strange size proportions...

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
Parallel Arrangement 07.04.12