Sg2.9
Stroke opacity
Recent RSS
Stroke opacity
From:  Jakub
Date:  4. August 2012, 17:13

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.

Re: Stroke opacity
Date:  6. August 2012, 13:13

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

Re: Stroke opacity
Date:  6. August 2012, 13:54

Oh, by the way.
You can change the variable 'maxLen' to whatever value you like. It controls how long a path is before it is no longer visible.

Re: Stroke opacity
From:  Jakub
Date:  6. August 2012, 17:13

Håkan,

thank you but unfortunately, it gives me this:

Re: Stroke opacity
Date:  6. August 2012, 18:16

Oh, sorry!

I've made a type-o in line 5. The variable is called selPaths, with an 's' at the end. Please exchange the entire line with this instead:

   var path = selPaths[i]

Hope that works, I'm still stuck with a touch screen. It makes it all the more difficult...

Re: Stroke opacity
From:  Jakub
Date:  6. August 2012, 20:18

It doesn't report any error now but it seems it's doing nothing...

Re: Stroke opacity
Date:  6. August 2012, 22:00

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!

Re: Stroke opacity
From:  Jakub
Date:  7. August 2012, 01:35

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.

Re: Stroke opacity
Date:  7. August 2012, 08:52

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;

Re: Stroke opacity
Date:  7. August 2012, 09:22

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
   }
}
Re: Stroke opacity
From:  Jakub
Date:  7. August 2012, 17:50

Well, this is what I was looking for.

Thank you gentlemen!

My best,
Jakub

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Area Type Resizer 03.08.12