Skip to main content
Known Participant
June 21, 2020
質問

Confused about how coordinates work in scripts

  • June 21, 2020
  • 返信数 7.
  • 2713 ビュー

So strictly speaking inside of a script, the origin is the lower left corner of the artboard... Then say with the sample code here:

 

myDoc.pathItems.rectangle(8, 4, 4, 4);

 

This goes (x, y, x, y) ?  and the 2nd x, y is in relation to the first point, not to the origin?  And x being positive is always to the right, and y being positive is always up?

このトピックへの返信は締め切られました。

返信数 7

Maple Stirrup作成者
Known Participant
June 26, 2020

It seems if a script changes the artboard, the origin doesn't move, but if you manually resize the artboard, the origin moves with it... is that correct?

renél80416020
Inspiring
June 24, 2020

Salut!
Une méthode à l'ancienne pour ne pas se préoccuper de la position de l'origine.

Il suffit de faire un choix au départ du script? (se référer au bas ou au haut du plan de travail)

Mais depuis il y a d'autre méthode mais celle ci peut aider à comprendre...

// elleere Wed, 24 June 2020 08:11:52 GMT
// Traitement dans le plans de travail actif
// orignine fictive en haut à gauche avec rulerOrigin
var docRef = app.activeDocument;
var Origin = docRef.rulerOrigin;
var hauteur = docRef.height;
var origX = -Origin[0];
var origY = -Origin[1]+hauteur;

var top = origY-50, left = origX+50, width = 60, height = 40;
docRef.pathItems.rectangle(top, left, width, height);
// orignine fictive en bas à gauche avec rulerOrigin
    origX = -Origin[0];
    origY = -Origin[1];

    var  height = 40, top = origY+50+height, left = origX+50, width = 60;
docRef.pathItems.rectangle(top, left, width, height);

de elleere

 

renél80416020
Inspiring
June 24, 2020

Salut!
Une méthode à l'ancienne pour ne pas se préoccuper de la position de l'origine globale.

Il suffit de faire un choix au départ du script? (se référer au bas ou au haut du plan de travail)

Mais depuis il y a d'autres méthodes mais celle ci peut aider à comprendre...

 

 

// elleere Wed, 24 June 2020 08:11:52 GMT
// Traitement dans le plan de travail actif
// orignine fictive en haut à gauche avec rulerOrigin
var docRef = app.activeDocument;
var Origin = docRef.rulerOrigin;
var hauteur = docRef.height;
var origX = -Origin[0];
var origY = -Origin[1]+hauteur;

var top = origY-50, left = origX+50, width = 60, height = 40;
docRef.pathItems.rectangle(top, left, width, height);
// orignine fictive en bas à gauche avec rulerOrigin
    var origX = -Origin[0];
    var origY = -Origin[1];
var  height = 40, top = origY+50+height, left = origX+50, width = 60;
docRef.pathItems.rectangle(top, left, width, height);

 

 

de elleere

 

 

 

CarlosCanto
Community Expert
Community Expert
June 24, 2020

it is [left, top, right, bottom]

Maple Stirrup作成者
Known Participant
June 24, 2020

Okay does the artboard rect work the same as making a rectangle?  It's y, x, width, height?

Inspiring
June 24, 2020

Unfortunately no. artboardRect returns an array in the form [ x1, y1, x2, y2 ].

CarlosCanto
Community Expert
Community Expert
June 22, 2020

as if things weren't confusing enough, here's the kicker. If your script creates the document the origin is bottom/left. If your script references an already open document, the origin is top/left.

 

that might seem absolutely crazy, but it kinds of make sense. Adobe changed the origin from bottom/left to top/left in CS5. So in order to not break existing scripts, origin had to remain as it was (as in CS4) when the script creates documents, but accessing existing documents had to align with the new and shining Cartesian system (CS5).

m1b
Community Expert
Community Expert
June 22, 2020

Ouch! That is a horrible kicker. Thanks for the info.

Maple_Stirrup does your script create the document? My test was on a new blank document created via the normal UI.

Maple Stirrup作成者
Known Participant
June 22, 2020

Yeah my script did create the document, this is all great information, I appreciate it.

 

 

m1b
Community Expert
Community Expert
June 21, 2020

The coordinates in Illustrator are a bit confusing (confused?). In artboard coordinates, the 0,0 point is top left of artboard.

 

Try this:

var top = 10, left = 20, width = 30, height = 40;
app.activeDocument.pathItems.rectangle(-top, left, width, height);

 

Notice the minus sign before 'top'? This is because the illustrator Y axis starts at 0 at the top of artboard, and goes down. So -10 means 10 down from the top of the artboard.

 

FYI, I mentioned artboard coordinates earlier. This means that values are relative to the active artboard. So when drawing with a script, the artboard matters. If that's a problem for your script, look into document coordinates.

 

Regards,

Mark

Maple Stirrup作成者
Known Participant
June 21, 2020

I keep playing around with my numbers trying to wrap my head around things, and with my line of code I posted originally, all my shapes are appearing in the lower left corner of the artboard as I keep my numbers hovering around all 0's, giving me the impression that the lower left is the origin?

 

Oh, it's not x,y x,y .. it's y, x, width, height?

m1b
Community Expert
Community Expert
June 22, 2020

When I run my above code I get a box that's 10 down and 20 to the right of the top left of the artboard. Where does it appear when you run my code?

 

Yes, it is top, left, width, height. I find it helps during a learning phase to use different values for each (eg. 10,20,30,40), so I can get my head around it more easily.