• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to reffer for center point while positioning

Explorer ,
May 01, 2018 May 01, 2018

Copy link to clipboard

Copied

How to reffer for center point of an object while positioning?

вапрв.png

i.e. I need to position an object relating for it center point.

TOPICS
Scripting

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 02, 2018 May 02, 2018

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;

Votes

Translate

Translate
Adobe
Community Expert ,
May 01, 2018 May 01, 2018

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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 01, 2018 May 01, 2018

Copy link to clipboard

Copied

What exactly do you want to do?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 01, 2018 May 01, 2018

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.

проапо.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 01, 2018 May 01, 2018

Copy link to clipboard

Copied

You marked my suggestion helpful.. But not correct? Did mine not work for you?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 01, 2018 May 01, 2018

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.

Как вы думаете?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 01, 2018 May 01, 2018

Copy link to clipboard

Copied

andyf65867865​

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 01, 2018 May 01, 2018

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 01, 2018 May 01, 2018

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 02, 2018 May 02, 2018

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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

this is what I get with the updated version and myPosition = [0,0];

проапо.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 02, 2018 May 02, 2018

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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 02, 2018 May 02, 2018

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

LATEST

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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines