Skip to main content
Inspiring
April 10, 2015
Question

Confused on creating a rectangle and validating its size & position afterwards

  • April 10, 2015
  • 1 reply
  • 857 views

If one creates a rectangle in a layer like this:

var childLayer = parentLayer.pathItems.rectangle(top, left, someWidth, someHeight);

childLayer.stroked = false;

childLayer.filled = true;

After creating the rectangle, would one read it back like this:

var bounds = childLayer.geometricBounds;

alert("bounds: "+bounds[0]+","+bounds[1]+","+bounds[2]+","+bounds[3]);

or should I use visibleBounds, or controlBounds? Or something else?

I know if stroke not used or can exclude can use geometric bounds? Or rather all 3 bounds could be the same in that case?

I'm confused right now because I'm assuming the bounds represent the four corners of the rectangle's position in the document and when read back out, the values (bounds[0] and bounds[1]) don't match against the top & left values originally passed in to create the rectangle. Shouldn't they be the same?

If not, how am I supposed to validate that the layer/object uses the same top & left values for it's position as when the rectangle was created (for say validation purposes)?

Also I assume I should be able to fetch the position of one corner using childLayer.top & childLayer.left as well right? For some reason, that doesn't match with top & left either.

This topic has been closed for replies.

1 reply

Qwertyfly___
Legend
April 11, 2015

it all worked fine for me...

I tested with this.

var doc = app.activeDocument;

var Top = 0;

var Left = 50;

var Width = 20;

var Height = 100;

var rect = doc.pathItems.rectangle(Top,Left,Width,Height);

rect.stroked = false;

rect.filled = true;

var bounds = rect.geometricBounds;

alert("bounds: "+bounds[0]+","+bounds[1]+","+bounds[2]+","+bounds[3]);

the alert reports: "bounds: 50,0,70,-100"

so your bounds are as follows:

bounds[0] = Left = 50

bounds[1] = Top = 0

bounds[2] = Right = 70 (Left + Width)

bounds[3] = Bottom = -100 (Top - Height)

if this is not the case for you I would like to know what version your are using.

Qwertyfly___
Legend
April 11, 2015

as far as geometric vs visible vs control...

var doc = app.activeDocument; 

var Top = 0; 

var Left = 50; 

var Width = 20; 

var Height = 100; 

var rect = doc.pathItems.rectangle(Top,Left,Width,Height); 

rect.stroked = true;

rect.strokeWidth = 10;

rect.filled = true; 

var bounds = rect.geometricBounds;  

alert("bounds: "+bounds[0]+","+bounds[1]+","+bounds[2]+","+bounds[3]); 

var bounds = rect.visibleBounds; 

alert("bounds: "+bounds[0]+","+bounds[1]+","+bounds[2]+","+bounds[3]); 

var bounds = rect.controlBounds;  

alert("bounds: "+bounds[0]+","+bounds[1]+","+bounds[2]+","+bounds[3]); 

you can see that geometric does not include stroke while visible does.

Control measures the control box.

although that is not what the documentation says...

controlBounds

array of 4 numbers Read-only.

The bounds of the object including stroke width and controls.