Skip to main content
Inspiring
November 7, 2014
Answered

changing or scaling gradient length in javascript

  • November 7, 2014
  • 2 replies
  • 3223 views

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!

This topic has been closed for replies.
Correct answer CarlosCanto

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


 

2 replies

frax_Author
Inspiring
November 19, 2014

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

CarlosCanto
Community Expert
Community Expert
November 19, 2014

great!!, glad to help

Qwertyfly___
Legend
November 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???

frax_Author
Inspiring
November 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...    

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
November 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);