Skip to main content
shaig khaligli
Known Participant
January 10, 2018
Answered

Center object inside another object in illustrator script

  • January 10, 2018
  • 1 reply
  • 2186 views

I have a simple problem, but I haven't found a solution it.

I have a object and rectangle grouped together and want to scale the object proportionally and have it centered inside the rectangle.

I'm looking for an automated solution to this because I have a lot of objects that need to be scaled and centered like this.

The art I'm working with is similar to this.

This topic has been closed for replies.
Correct answer Disposition_Dev

I guess icon's height has to be 80% of rectangle's height. Actually I want to resize all my icons in artboard proportionally / keeping the aspect ratio. Let's say my rectangle should be 500x500px. My icon's height should be not more than 400px.


here's some pseudo code that may help..

var icon = the icon artwork

var background = the background square

var iconDimension,scale;

//get the largest dimension of the icon

if icon.width > icon.height

    iconDimension = icon.width;

else

    iconDimension = icon.height;

//get the scale percentage

if iconDimension < background.width

    scale = (iconDimension / background.width) * 100

else

    scale = (background.width / iconDimension) * 100

//get 80% of the scale

scale = scale*.8;

//resize the icon

icon.resize(scale,scale);

//make sure the icon is still centered in the background box

icon.left = (background.left + background.width/2) - icon.width/2

icon.top = (background.top - background.width/2) + icon.height/2

1 reply

Silly-V
Brainiac
January 10, 2018

You simply do pageItem.resize(w, h).

#target illustrator

function test(){

  var doc = app.activeDocument;

  var obj = doc.pathItems.getByName("obj");

  obj.resize(150, 150);

};

test();

Before:

After:

shaig khaligli
Known Participant
January 10, 2018

Silly-V​ thanks for response !

But I need different solution. I have different sizes of vector objects in artboard. I want to add each of them to groupped standart sized rectangles and make the icons be center and fit the rectangle. I just need script to make below " Before - After " thing.

Before:

After:

Silly-V
Brainiac
January 10, 2018

How do you decide what % of height is to be smaller than the rectangle, which gives it such amount of padding room?