Copy link to clipboard
Copied
How to reffer for center point of an object while positioning?
i.e. I need to position an object relating for it center point.
my apologies. i had the wrong math there. just swap the operator on each line before the variable halfOfMyItemStrokeWidth
like this:
myItem.left = myPosition[0] - myItem.width/2 - halfOfMyItemStrokeWidth;
myItem.top = myPosition[1] + myItem.height/2 + halfOfMyItemStrokeWidth;
Copy link to clipboard
Copied
you've gotta just use some math. there isn't a "center" property of any object, so you have to trick it into doing what you want.
try this out:
function test()
{
var docRef = app.activeDocument;
var myPosition = [100,-300];
var myItem = docRef.pageItems[0];
myItem.left = myPosition[0] - myItem.width/2;
myItem.top = myPosition[1] + myItem.height/2;
}
test();
Copy link to clipboard
Copied
What exactly do you want to do?
Copy link to clipboard
Copied
I want align one object to other, so that the center point of object 2 coincides the center point of object 1,
and the object 1 should stay in place.
Copy link to clipboard
Copied
You marked my suggestion helpful.. But not correct? Did mine not work for you?
Copy link to clipboard
Copied
williamadowling schrieb
You marked my suggestion helpful.. But not correct? Did mine not work for you?
In case of post #1 - yes. But (seeing the much more detailed post #3) - 'only' helpful.
Как вы думаете?
Copy link to clipboard
Copied
A few more questions:
Are the two objects already present?
Do these have unique names?
They are selected?
Which object should be aligned to which? (depends on what? by name or stacking order or position or …)
Are the objects simple paths?
Or compound paths, or groups or nested grouped groups and so on?
Or was the objects created through the script?
For two simple paths this should work (required: two simple paths which are selected):
var aDoc = app.activeDocument;
var aSel =app.selection;
aSel[0].left = aSel[1].left + aSel[1].width/2 - aSel[0].width/2;
aSel[0].top= aSel[1].top - aSel[1].height/2 + aSel[0].height/2;
The last created path will be centered by the penultimate drawn path middle center position.
Have fun
Copy link to clipboard
Copied
Thanks andy, for the feedback. But for the sake of being complete and accurate, i would definitely take some time to consider pixel's questions to help mitigate any problems you may have moving forward. My snippet was absolutely the most basic thing that you could use, and as such, is probably unsafe to use as is, at least in any meaningful way.
Copy link to clipboard
Copied
Well, I found that simple function suits my needs when I tried to solve my task. Those two objects were programmatically created. They do not have unique names but I have links to them from my script. As I was trying to explaine the second obj should be aligned to the first one. But I found that script moves `myItem` to the position not exactly when `myItem` has a stroke. When I disabled stroke then the script processes correct.
Copy link to clipboard
Copied
Give this a go. this takes into account half of the width of the stroke
function test()
{
var docRef = app.activeDocument;
var myPosition = [100,-300];
var myItem = docRef.pageItems[0];
var halfOfMyItemStrokeWidth = 0;
if(myItem.stroked)
{
halfOfMyItemStrokeWidth = myItem.strokeWidth / 2;
}
myItem.left = myPosition[0] - myItem.width/2 + halfOfMyItemStrokeWidth;
myItem.top = myPosition[1] + myItem.height/2 - halfOfMyItemStrokeWidth;
}
test();
Copy link to clipboard
Copied
this is what I get with the updated version and myPosition = [0,0];
Copy link to clipboard
Copied
my apologies. i had the wrong math there. just swap the operator on each line before the variable halfOfMyItemStrokeWidth
like this:
myItem.left = myPosition[0] - myItem.width/2 - halfOfMyItemStrokeWidth;
myItem.top = myPosition[1] + myItem.height/2 + halfOfMyItemStrokeWidth;
Copy link to clipboard
Copied
williamadowlingthakns!
Copy link to clipboard
Copied
any old time. sorry for the typo there. i always have to think hard about the xy directions, since i also write JS for HTML5 Canvas which uses opposite Y directions. in HTML5 Canvas, adding to the Y coordinate moves things down, instead of up.
Copy link to clipboard
Copied
I'd like to add that as noted, Left/Top properties include total width/height of objects including strokes (aka Visible Bounds). On the other hand Position uses Geometric Bounds for placement. Both are useful, depending on the needs.
for this project, it would be something like this
function test()
{
var docRef = app.activeDocument;
var myPosition = [114,-106.05];
var myItem = docRef.pageItems[0];
myItem.position = [myPosition[0] - myItem.width/2, myPosition[1] + myItem.height/2];
}
test();