Expression Copied to Clipboard! πŸ—‚
← Back to Expressions

Maintain Stroke Width - After Effects Expression

Maintain stroke width while scaling layers. Apply to stroke width property

Maintain Stroke Width

value / length(toComp([0,0]), toComp([0.7071,0.7071])) || 0.001;

‍

Copied to Clipboard!

How to maintain stroke width when scaling shape layers.

If you're animating shape layers in After Effects, you will likely run into the scenario where you want to scale the shape layer, but keep the stroke width the same.

If so, this expression, by Adam Plouff (who understands math) is what you need.

value / length(toComp([0,0]), toComp([0.7071,0.7071])) || 0.001;

Apply this expression to the Stroke Width property of any shape layer to maintain the visual thickness of the stroke, no matter what scaling is applied to the layer.

How to use this expression in 60 seconds:

‍

How does this expression work?

value / length(toComp([0,0]), toComp([0.7071,0.7071])) || 0.001;

Alright, let's break down this down:

  • value: This refers to the current value of the Stroke Width property before the expression is applied.
  • toComp([0,0]): This part gets the position of the layer's anchor point and translates it to the composition's space, which is basically the main working area in After Effects.
  • toComp([0.7071,0.7071]): This is similar to the above but for a point that is diagonally offset from the anchor point. The numbers [0.7071,0.7071] are the x and y coordinates of a point that's at a 45-degree angle from the anchor point, assuming the anchor point is at [0,0].
  • length(): This function calculates the distance between two points. So in this expression, it's finding the distance between the layer's anchor point and the diagonal point in composition space.
  • value / length(...): Here's where the magic happens. By dividing the original stroke width by the length we just calculated, we're adjusting the stroke width based on the layer's scaling. If the layer is scaled up or down, the length will change, and thus the stroke width will adjust to maintain its visual thickness.
  • || 0.001: The || is a logical operator that means "or." This part is a safety net. It says, "If the result of the division is 0 or 'null' (which can happen if the layer's scale is set to 0), then use 0.001 instead." It prevents the expression from returning an error or a stroke width of 0, which would make the stroke invisible.

How to Use:

Just apply this whole expression to the Stroke Width property of your shape layer. Now, no matter how you transform the layer (like scaling it up or down), the stroke's visual thickness stays the same, looking sharp and consistent. Cool, right?

More expressions you might like...

Credit

Expression by the genius Adam Plouff