Skip to main content
j.khakase
Inspiring
August 23, 2022
Answered

round number at x and y cordinate,

  • August 23, 2022
  • 4 replies
  • 1774 views

Dear all, please help me on this

Need Illustrator scipt to round number at x and y cordinate, so that I can export artboards with exact dimension

 

 

This topic has been closed for replies.
Correct answer femkeblanco

Thanks @Ton Frederiks.  In which case, this should round the left and top positions of artboards. 

 

var ABs = app.activeDocument.artboards;
for (var i = 0; i < ABs.length; i++) {
    var ABR = ABs[i].artboardRect;
    var dx = Math.round(ABR[0]) - ABR[0];
    var dy = Math.round(ABR[1]) - ABR[1];
    ABs[i].artboardRect = [
        Math.round(ABR[0]), Math.round(ABR[1]), 
        ABR[2] + dx, ABR[3] + dy
    ];
}

 

4 replies

chrisg11235813
Participating Frequently
August 23, 2022

It might be too late for this, but here is a video that might work. you can quickly "snap to" the pixel grid. moving artwork with your artboards. 

https://www.youtube.com/watch?v=K-KdA0E4Y64

chrisg11235813
Participating Frequently
August 23, 2022

basically, select artboards. Object>Make Pixel Perfect

Ton Frederiks
Community Expert
Community Expert
August 23, 2022

The problem with this is that it not only moves but resizes your artboards. A 540 pixel artboard not aligned to the grid can become a 541 pixel artboard when made pixel perfect.

femkeblanco
Legend
August 23, 2022

You seem to be using pixels. Illustrator scripting uses points. Converting points to pixels involves multiplying by a repeating decimal and you'll always end up with a decimal. So what you want is not possible.

Ton Frederiks
Community Expert
Community Expert
August 23, 2022

1 point in Illustrator equals 1 pixel.

The default is 72 ppi which is the same as 72 points in an inch.

No complex conversions are necessary.

j.khakase
j.khakaseAuthor
Inspiring
August 25, 2022

Thank you so much @femkeblanco and all for your valuable comments.

The above pice of code works fine for me. and I get the exact what I want.


I just observe one more thing, the artwork doesn't move even respective check mark selected before run the script. and get 1 and half pixcel gap at edges as shown below image

 

chrisg11235813
Participating Frequently
August 23, 2022

The X and Y cordinates don't actually affect the size of your output. However, you can use the Align tool to align your artboard(s) to another artboard (Select both artboards and the align options appear)

Ton Frederiks
Community Expert
Community Expert
August 23, 2022
quote

The X and Y cordinates don't actually affect the size of your output. However, you can use the Align tool to align your artboard(s) to another artboard (Select both artboards and the align options appear)


By @chrisg11235813



They do, if they contain fractions, rounding will occur and additional pixels will be generated.

chrisg11235813
Participating Frequently
August 23, 2022

I always figured that when you move the X or Y positions, the Width and Height shift with the dimentions remaining static (?)

Ton Frederiks
Community Expert
Community Expert
August 23, 2022

Rearrange All Artboards will align them to round pixel coordinates. You could make an action and record: Object > Artboards > Rearrange All Artboards.

j.khakase
j.khakaseAuthor
Inspiring
August 23, 2022

Rearranging all artboards disrub my visual placement of other artobards as well, looking something will work on selected artboards by the script