Hi all,
I'm searching for a script that will change the stroke opacity of a line based on its length (distance between two points).
I would be really glad for any suggestions how to do this.
Have a nice day.
Jakub.
Hi Jakub, I just wrote this:
var maxLen = 100 var selPaths = document.getItems({type: Path, selected: true}) for(var i = 0; i < selPaths.length; i++){ var path = selPath[i] var pathLen = path.length var alpha = pathLen / maxLen path.strokeColor.alpha = alpha < 1 ? alpha : 1 }
You could try this out. Have some paths (lines) selected and then run this script.
Not 100% sure it works as I'm writing this on my iPhone and can't test it properly. Let me know if it is working or not and I shall fix it.
Best wishes
Ok, again, I'm sorry.
I got it all mixed up and used the alpha value instead of opacity. However, this changes the whole objects opacity, not just the stroke. So, here goes:
var maxLen = 100 var selPaths = document.getItems({type: Path, selected: true}) for(var i = 0; i < selPaths.length; i++){ var path = selPaths[i] var pathLen = path.length var opacity = pathLen / maxLen path.opacity = opacity < 1 ? 1 - opacity : 1 }
Oh, yes, I also got the math all wrong in the last line, but this has been tested and it works for me anyway.
So sorry for any inconvenience!
Thank you.
This generates really interesting results. However, I was playing with maxLen value but couldn't get the script to execute the lines as in attached image: to make the longest selected line with 0% opacity and the shortest with 100% (so everything in between is derived from that).
Do you have any idea how to approach this?
Again, thanks a lot for your support.
What I use in the 'Objects-on-paths' script is to scale the opacity ...
objInstance.opacity = (Math.abs(pathLength- pos) / pathLength) * (1-fadeLimit) + fadeLimit;
The abs is for when I work backwards along a path
In your case: select all the lines, find the max and min length and then set the opacity for a line of length L to
(L - Lmin)/(Lmax - Lmin) which gives a range of 0 to 1
You may also want a minimum opacity ( not completely vanishing ) so something like :
var fadeLimit = 0.1;
var opacity = (L - Lmin) / (Lmax - Lmin) * (1-fadeLimit) + fadeLimit;
Hi again,
I see that Gareth has help you out a bit as well.
I re-wrote this just now. You no longer have to use a maxLen value as the script sorts all the selected lines in ascending order.
But as Gareth says, the longest line WILL disappear due to having an opacity of 0. This is easily compensated for by changing the variable in the first line to true.
var compensate = false var selPaths = document.getItems({type: Path, selected: true}) if(selPaths){ selPaths.sort(function(a, b){return a.length - b.length}) var opacity = 1 / (compensate ? selPaths.length : (selPaths.length - 1)) for(var i = 0; i < selPaths.length; i++){ selPaths[i].opacity = 1 - opacity * i } }