Maintain Stroke Width After Effects Expression - What It Does & How To Use It

To maintain a consistent stroke width in After Effects when scaling shape layers, apply the "Maintain Stroke Width" expression to the Stroke Width property. Alternatively, adjust the scale within the "Transform Shape" property of the shape layer for the same effect.

October 28, 2023
Maintain Stroke Width After Effects Expression - What It Does & How To Use It
Most Affordable All-in-one Subscription for Unlimited Downloads
Access our ever-growing Subscription Catalog of over 14 million stock content for After Effects, Premiere Pro, FCP & DaVinci!

How To Maintain Stroke Width In After Effects - Expression

After Effects Expressions

Basic Expression

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

Apply this expression to the "Stroke Width" property of a shape layer to maintain the visual thickness of the stroke, regardless of the scaling applied to the layer.

Alternative Method (Using Average Scale Values)

sv = transform.scale;
avScale = (sv[0] + sv[1])/2;
sFactor = avScale * .01;
value / sFactor;

This method averages the X and Y scale values and adjusts the stroke width based on the average scale factor.

Maintaining Stroke with Parented Layer

strokeSize = DESIRED_VALUE; // Replace DESIRED_VALUE with your desired stroke width
try {
    strokeSize / Math.abs(thisLayer.parent.transform.scale[0]/100);
} catch {
    strokeSize / Math.abs(transform.scale[0]/100);
}

This expression tries to use the parented layer for scale. If there's no parented layer, it uses the current layer's scale instead.

What It Does

Now, if you've played around with shape layers, you might have noticed that when you scale them up or down, the thickness of their outline, known as the stroke, changes too. This can be a bit of a nuisance, especially when you want that stroke to remain consistent. That's where our Maintain Stroke Width expression comes into play.

Imagine you've drawn a simple circle with a 5-pixel wide stroke. As you scale the circle to make it bigger, that 5-pixel stroke might become 10 pixels wide, altering its appearance. The "Maintain Stroke Width" expression cleverly adjusts the stroke width based on how much you're scaling the layer. It's like having an invisible assistant who ensures that no matter how much you resize your shape, the stroke remains visually the same.

How To Use It

  1. Locate the Shape Layer: In your After Effects timeline, find the shape layer you're working with. If you've named your layers (which is always a good practice!), it'll be easier to spot.
  2. Access the Stroke Width Property: Expand the shape layer by clicking on the small triangle next to its name. Dive deeper by expanding the "Contents" section, then the specific shape (like "Ellipse 1" if it's a circle), and finally, you'll find the "Stroke 1" section. Here, you'll see the "Stroke Width" property.
  3. Activate Expressions: Alt-click (or Option-click on Mac) on the stopwatch icon next to "Stroke Width." This will open up a space for you to type in or paste an expression.
  4. Enter the Expression: In the space provided, type in the "Maintain Stroke Width" expression. Once you've done this, any changes you make to the layer's scale will automatically adjust the stroke width to maintain its appearance.

A Word on Scaling with Different X and Y Values

Now, a quick heads-up. If you've tried scaling your shape differently in width (X) and height (Y), you might find the stroke behaving a bit unpredictably. This is because the expression has to figure out how to adjust the stroke when the shape is being stretched more in one direction than the other. A simple workaround is to average the X and Y scale values and use that average to adjust the stroke width. It's a bit of math magic that ensures your stroke looks just right.

Alternative Approach: Using Transform Shape

If you're looking for another method, here's a pro tip. Instead of scaling the entire layer, you can adjust the scale within the "Transform Shape" property of the shape layer. This way, the stroke remains below the transformation, ensuring it doesn't get affected by the scaling. To do this:

  1. Find the Transform Shape Property: Inside your shape layer, expand the "Contents" section. Beneath your specific shape (like "Ellipse 1"), you'll find the "Transform: Shape Name" option.
  2. Adjust the Scale: Here, instead of scaling the entire layer, adjust the scale within this property. You'll notice the stroke remains consistent, giving you the desired effect.