After Effects Expressions: Linking Opacity to Layer Position with linear()
Link a layer's opacity to another layer's horizontal position in After Effects using a JavaScript expression. Learn the exact attachment steps, a worked linear() example, and how to verify the behavior locally.
15 Dec 2025, 10:07 UTC

You want a layer to fade in and out automatically as another object moves across the frame, without keyframing opacity by hand. In After Effects, the direct answer is to attach a JavaScript expression to the Opacity property that reads the other layer's position and maps it to a percentage. The useful takeaway: Alt-click (Windows) or Option-click (macOS) the Opacity stopwatch on your target layer, then enter a linear() expression that references the driving layer's X coordinate.
Expressions are small JavaScript-based snippets bound to a single animatable property. They evaluate at every frame, which means the property can react to other layers, time, or built-in randomness without manual keyframes. This guide uses a concrete opacity-linking example to show how the mechanism works, where it breaks, and how to confirm the result in your own project.
How expressions attach to a property
Select the layer in the composition, reveal the property you want to drive in the Timeline panel, then Alt-click (Option-click on macOS) the property's stopwatch icon. Alternatively, choose Animation > Add Expression. An expression field appears next to the property. Any text you type there replaces the static value or modulates existing keyframes, depending on what the expression returns.
Because the expression engine is JavaScript-based, you can use variables, arithmetic, and built-in helper functions. You cannot, however, read external files, fetch network resources, or access data outside the current project. The engine operates only on the composition, layer, and effect data already loaded in After Effects.
Worked example: opacity follows horizontal position
Create a composition with two solid layers. Name the first Reference and the second Target. The goal is for Target to become fully opaque as Reference moves from the left edge to the right edge of a 1000-pixel-wide composition.
On the Target layer, Alt-click the Opacity stopwatch and enter:
pos = thisComp.layer('Reference').transform.position;
linear(pos[0], 0, 1000, 0, 100)
Here is what each part does. thisComp.layer('Reference') resolves the layer by its exact name in the Timeline. .transform.position returns that layer's position property, which is an array of X and Y values. pos[0] extracts the X coordinate. The linear() function maps an input range onto an output range: as pos[0] moves from 0 to 1000, the returned value moves from 0 to 100. Opacity is expressed as a percentage, so the layer fades from fully transparent to fully opaque.
If you prefer the value to stay within bounds even if the Reference layer moves outside that range, wrap the result in clamp():
pos = thisComp.layer('Reference').transform.position;
clamp(linear(pos[0], 0, 1000, 0, 100), 0, 100)
Other common built-ins include ease() for eased mapping, wiggle() for controlled randomness, loopOut() for cycling keyframed values, and valueAtTime() for sampling a property at a shifted time. Each is a standard part of the expression language, though the exact set of available methods can vary between After Effects releases.
Why property references break
References are resolved when the expression evaluates, not when you type it. That makes text-based references like thisComp.layer('Reference') convenient but fragile. If you rename the layer, move it into a different composition, or change its index in the layer stack, the expression cannot find the target and returns an error. Pick-whip references (created by dragging the pick-whip icon to another property) and index-based references such as thisComp.layer(1) are also brittle in different ways: index references shift when you reorder layers, and pick-whip links can silently point to the wrong property if you restructure the timeline.
A broken expression does not fall back to keyframes. The property stops animating and displays an error indicator in the Timeline. You must fix the reference or remove the expression to restore manual control.
Limits and common mistakes
- Version differences. Available expression methods, property names, and menu locations vary between After Effects releases. Confirm syntax against the version you actually run instead of assuming a tutorial matches your installation.
- Read-only properties. Some properties and effect parameters do not accept expressions, or accept them only in one direction. If the stopwatch does not respond to Alt-click, the property likely cannot be driven by an expression.
- Instances do not inherit. An expression attached to a layer in one composition does not automatically apply to instances of that layer in other compositions. Each instance must be edited separately.
- Performance cost. Expressions recalculate while scrubbing and during rendering. Heavy loops, per-frame sampling across many layers, or deeply nested references can slow previews. The usual mitigations are to simplify the math, cache the composition, or pre-render the driven layers before final output.
- Keyframe interaction. Adding an expression to a property that already has keyframes overrides the keyframed values while the expression is active. The keyframes remain in the Timeline but are ignored until you disable or delete the expression.
Verifying the behavior locally
To confirm the mechanism works in your project, follow these checks. They require only a standard After Effects installation and a composition you can edit.
- Create a new composition and add two solid layers. Rename them exactly
ReferenceandTarget, and confirm the names appear as written in the Timeline panel. - Alt-click (Option-click) the
Targetlayer's Opacity stopwatch. Paste the worked example above into the expression field. - Drag the
Referencelayer horizontally. Watch the Opacity value forTargetchange in step with the X position. If the composition is 1000 pixels wide, opacity should reach 100 when the layer is at the far right. - Scrub the timeline playhead. The value should update on every frame, not only when you drag the layer.
- Render a short frame range to confirm the expression is evaluated during output as well as during preview.
- Deliberately misspell the layer name in the expression, for example
Refernce. Confirm the property reports an error and stops animating, which demonstrates how reference failures surface.
If the opacity does not change, check that the layer names match exactly, that the expression field is active, and that you are viewing the correct composition. If the value changes but seems clamped or offset, verify the input and output ranges inside linear() match your composition dimensions.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.