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

changing or scaling gradient length in javascript

Explorer ,
Nov 07, 2014 Nov 07, 2014

Hi,

I am trying to scale or change the length of a gradient fill. I managed to rotate it using the trick with .rotate without changing the shape, but I can't get the equivalent working with changing the length of the gradient - it just doesn't do anything!

I am on Illustrator 18 using JavaScript.

pathItem.fillColor.length doesn't work, and neither does .resize!

TOPICS
Scripting
2.5K
Translate
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 , Nov 17, 2014 Nov 17, 2014

indeed this is a very good question, it's been asked several times...this time based on the rotate trick Is it possible to transform a selected gradient in javascript? I got some results (trial and error), I'm not sure what's happening though, the script seems to be unable to transform a gradient two times, here's what I've got.

 

// transform gradients findings

// select a path item with a gradient applied to it before running the script

 

var idoc = app.activeDocument;

var ipath =  idoc.select

...
Translate
Adobe
Guide ,
Nov 11, 2014 Nov 11, 2014

I like this question.

I have been playing a little with it and it has me stumped.

in the javascript reference it states we should be able to set the Length, Angle, Origin etc of a gradient object.

a statement such as:

alert(pathItem.fillColor.origin);

works fine and displays the origin, this also works with length and Angle.

But when you try to set these values it does not work.

it says nothing about it being read only.

Does anyone have any light to shed on this???

Translate
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 ,
Nov 12, 2014 Nov 12, 2014

Hi, thanks for your response! What I don't get is why .resize doesn't work when .rotate does!

I came up with a workaround though - I was thinking I would have a path with the gradient the way I want it, then use a script to copy it behind the other shapes, one by one, and use the top shape in each group as a clipping mask, if you follow what I mean.

Translate
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 ,
Nov 12, 2014 Nov 12, 2014

Maybe one should report the problems as a bug btw!    

Translate
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
Guide ,
Nov 12, 2014 Nov 12, 2014

I Was thinking about that. But it makes the art a lot more complex. Especially if you want stroked shapes.

it should be simple.

I am happy to report a bug. But need to make sure it is a bug and not just something I'm not doing correctly.

Not it that it makes a lick of difference. If they paid any attention at all we would be able to run scripts from actions. Without having it break on restart!

Translate
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 ,
Nov 12, 2014 Nov 12, 2014

I just reported it as a bug - it probably doesn't hurt if you do it too. I didn't bother creating a test case - the read-onliness of those properties has been widely documented in the forums...    

Translate
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
Guide ,
Nov 12, 2014 Nov 12, 2014

I added a bug report on this, if these options are read-only, then the least they could do is say so.

sadly the scripting documentation is far from extensive.

Translate
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 ,
Nov 17, 2014 Nov 17, 2014

indeed this is a very good question, it's been asked several times...this time based on the rotate trick Is it possible to transform a selected gradient in javascript? I got some results (trial and error), I'm not sure what's happening though, the script seems to be unable to transform a gradient two times, here's what I've got.

 

// transform gradients findings

// select a path item with a gradient applied to it before running the script

 

var idoc = app.activeDocument;

var ipath =  idoc.selection[0];

var fillcolor = ipath.fillColor;

 

 

ipath.filled = false; // remove previous gradient transformations, to be able to apply new ones ??

 

 

ipath.fillColor = fillcolor; // re apply the same gradient color, without transformations.

ipath.fillOverprint = ipath.fillOverprint; // without this, the gradient won't move or scale, but it does rotate ??

 

 

var moveMatrix = app.getTranslationMatrix(50, 0);

var scale_moveMatrix = app.concatenateScaleMatrix (moveMatrix, 75, 50);

var move_scale_rotateMatrix = app.concatenateRotationMatrix (scale_moveMatrix, 15);

 

 

ipath.transform(move_scale_rotateMatrix,false,false,true,false,0, Transformation.CENTER);


 

Translate
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 ,
Nov 18, 2014 Nov 18, 2014

Thanks, I'll give it a shot and see if it works!

Translate
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
Guide ,
Nov 18, 2014 Nov 18, 2014

This is great, how ever did you decide to try ipath.fillOverprint = ipath.fillOverprint;

I found the script can edit the object again, if you change angle say to 25° and re-run then it re-fills and transforms to the 25°.

What I can't work out is how to read the newly set angle etc...

is there a way you know of to get the values of the matrix.

Translate
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 ,
Nov 18, 2014 Nov 18, 2014

using overprint wasn't my idea, it was on the link I posted, I wouldn't have thought of it either.

you might be able to work out the math in matrix values, all I know about it (not much), I got from here

senocular.com

Translate
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
Guide ,
Nov 18, 2014 Nov 18, 2014

in your link he uses ipath.fillOverprint = false;

not ipath.fillOverprint = ipath.fillOverprint;

but I tried setting it to false in your script and it still works.

Matrices look like quite a headache, could be that I've had a long day and my brain is a little soft right now.

I'll have to have a read at some point as they could be quite useful.

Thanks Carlos.

Translate
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 ,
Nov 18, 2014 Nov 18, 2014

I tried to keep the original overprint setting whether true of false...I don't remember if it worked, I tried sooo many things that day, I got frustrated, nothing seemed to work.

Translate
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
Guide ,
Nov 19, 2014 Nov 19, 2014

ahhh the "joys" of coding...!

Translate
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 ,
Nov 19, 2014 Nov 19, 2014

hahaha, right, the joys of coding...Illustrator

Translate
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
Advisor ,
Aug 18, 2021 Aug 18, 2021

What i do find weird in this method. If you reset the angle back to 0 deg. The gradient doesnt match the angle?!
Noticed how the angle is 0 but it still angled in a certain degree?!

gradient angle reset to 0gradient angle reset to 0expand image

What is also weird, if you do an Object > rotate. I see both the path and gradient angle get a rotation. Yet the gradient gizmo is parellel to the path object?!?!
both path angle and gradient angle show an angle of 15?!both path angle and gradient angle show an angle of 15?!expand image

Translate
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
Advisor ,
Sep 04, 2024 Sep 04, 2024
LATEST

I came back to this post again. Found an easy example on the documentation api page. This one runs just fine, no issue. 
https://ai-scripting.docsforadobe.dev/jsobjref/Matrix.html#jsobjref-matrix

I noticed your example gets illustrator funky and acting weird.

Translate
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 ,
Nov 19, 2014 Nov 19, 2014

I tried Carlos snippet, which i modified for the case I had - and it worked fine - thanks!    

Translate
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 ,
Nov 19, 2014 Nov 19, 2014

great!!, glad to help

Translate
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