Dynamic Mograph Effects: Control Position, Rotation, Scale, Opacity, and Color by Proximity

Unlock the power of dynamic motion graphics with these After Effects expressions. Learn how to control position, rotation, scale, opacity, and color based on the distance from a reference layer, bringing your animations to life with interactive, responsive effects.

Proximity-Based Position Adjustment

This expression is designed to move a layer based on its distance from an "Effector" layer. The expression calculates the position offset for the current layer and gradually moves it closer to or farther from its current position defined by the "Effector" layer.

How does it work?

1. Getting the Position of the Current Layer:

var p1 = thisLayer.transform.position;

This line retrieves the position of the current layer to which the expression is applied. The position is stored as an array of X, Y, and Z coordinates.

2. Getting the Position of the "Effector" Layer:

var p2 = thisComp.layer("Effector").transform.position;

This line gets the position of the "Effector" layer in the composition. The "Effector" layer is used as a reference to determine how the current layer will be influenced.

3. Retrieving Slider Controls from the "Effector" Layer:

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

These lines fetch values from two slider controls on the "Effector" layer: Radius and Radius Strength. Radius Strength defines the minimum distance where the effect starts, and Radius defines the maximum distance over which the movement effect will occur.

4. Getting Position Offsets from the "Effector" Layer:

var MoveX = thisComp.layer("Effector").effect("Position XYZ")(1)[0];

var MoveY = thisComp.layer("Effector").effect("Position XYZ")(1)[1];

var MoveZ = thisComp.layer("Effector").effect("Position XYZ")(1)[2];

var MoveY = thisComp.layer("Effector").effect("Position XYZ")(1)[1];

var MoveZ = thisComp.layer("Effector").effect("Position XYZ")(1)[2];

These lines retrieve the X, Y, and Z offsets from a control named "Position XYZ" on the "Effector" layer. This control defines the maximum amount by which the position of the current layer should be adjusted in each direction.

5. Calculating the Distance Between the Layers:

var d = length(p1, p2);

This line calculates the distance d between the current layer (p1) and the "Effector" layer (p2). The length function measures the straight-line distance between the two points.

6. Mapping the Distance to Position Offsets:

var x = linear(d, Radius, RadiusStrength, MoveX, 0);

var y = linear(d, Radius, RadiusStrength, MoveY, 0);

var z = linear(d, Radius, RadiusStrength, MoveZ, 0);

var y = linear(d, Radius, RadiusStrength, MoveY, 0);

var z = linear(d, Radius, RadiusStrength, MoveZ, 0);

The linear function is used to map the distance d to a range that determines how much the current layer will move along each axis (X, Y, Z). When the distance d is equal to RadiusStrength, the offsets (x, y, z) will be at their maximum (MoveX, MoveY, MoveZ). As d approaches Radius, the offsets will gradually decrease to zero.

7. Calculating the New Position:

var xPos = p1[0] + x;

var yPos = p1[1] + y;

var zPos = p1[2] + z;

var yPos = p1[1] + y;

var zPos = p1[2] + z;

These lines add the calculated offsets (x, y, z) to the current position (p1). This effectively moves the layer closer to or farther from the original position based on the distance from the "Effector" layer.

8. Returning the Final Position:

[xPos, yPos, zPos];

The final step returns an array representing the new position of the layer, taking into account the distance-based offsets.

__________________________________________

Here's the complete expression you can use in After Effects, just copy and paste on the position property:

var p1 = thisLayer.transform.position;

var p2 = thisComp.layer("Effector").transform.position;

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var p2 = thisComp.layer("Effector").transform.position;

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var MoveX = thisComp.layer("Effector").effect("Position XYZ")(1)[0];

var MoveY = thisComp.layer("Effector").effect("Position XYZ")(1)[1];

var MoveZ = thisComp.layer("Effector").effect("Position XYZ")(1)[2];

var MoveY = thisComp.layer("Effector").effect("Position XYZ")(1)[1];

var MoveZ = thisComp.layer("Effector").effect("Position XYZ")(1)[2];

var d = length(p1, p2);

var x = linear(d, Radius, RadiusStrength, MoveX, 0);

var y = linear(d, Radius, RadiusStrength, MoveY, 0);

var z = linear(d, Radius, RadiusStrength, MoveZ, 0);

var y = linear(d, Radius, RadiusStrength, MoveY, 0);

var z = linear(d, Radius, RadiusStrength, MoveZ, 0);

var xPos = p1[0] + x;

var yPos = p1[1] + y;

var zPos = p1[2] + z;

[xPos, yPos, zPos];

var yPos = p1[1] + y;

var zPos = p1[2] + z;

[xPos, yPos, zPos];

Unlock Stunning Motion Graphics in Seconds:

You can use the free expressions provided in this blog, but if you are in a hurry you can get Our All-in-One Presets for Just 12 Euro!

Effortlessly Control Position, Rotation, Scale, Opacity, and Color with One Click — No Need for Complex Expressions!

Proximity-Based Scale Adjustment

This expression dynamically adjusts the scale of a layer based on its distance from another layer, the "Effector." The expression calculates the distance between the two layers and then uses that distance to interpolate between two scale values: the current scale of the layer (ValueFrom) and a target scale (ValueTo) defined on the "Effector" layer. The interpolation occurs within a range specified by the Radius and Radius Strength controls.

Here's the complete expression you can use in After Effects, just copy and paste on the Scale Property:

var pos1 = thisLayer.transform.position;

var pos2 = thisComp.layer("Effector").transform.position;

var ValueFrom = transform.scale[0];

var ValueTo = thisComp.layer("Effector").effect("Scale To")(1);

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var d = length(pos1, pos2);

var finalValue = linear(d, Radius, RadiusStrength, ValueTo, ValueFrom);

[finalValue, finalValue, finalValue];

var pos2 = thisComp.layer("Effector").transform.position;

var ValueFrom = transform.scale[0];

var ValueTo = thisComp.layer("Effector").effect("Scale To")(1);

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var d = length(pos1, pos2);

var finalValue = linear(d, Radius, RadiusStrength, ValueTo, ValueFrom);

[finalValue, finalValue, finalValue];

Proximity-Based Rotation Adjustment

These expressions dynamically adjust the X, Y, and Z rotations of a layer based on its distance from another layer called the "Effector." The expressions calculate the distance between the two layers and use this distance to interpolate between the current rotation value (ValueFrom) and a target rotation value (ValueTo) defined on the "Effector" layer. The interpolation is controlled by a range set by Radius and Radius Strength parameters, allowing for a smooth rotational transition effect as the distance changes.

Here's the complete expression you can use in After Effects, just copy and paste on the Rotation Properties:

X Rotation:

var p1 = thisLayer.transform.position;

var p2 = thisComp.layer("Effector").transform.position;

var ValueTo = thisComp.layer("Effector").effect("Rotation XYZ")(1)[0];

var ValueFrom = transform.xRotation;

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var d = length(p1, p2);

var t = linear(d, Radius, RadiusStrength, 0, 1);

var r = linear(t, 0, 1, ValueFrom + ValueTo, ValueFrom);

r;

var p2 = thisComp.layer("Effector").transform.position;

var ValueTo = thisComp.layer("Effector").effect("Rotation XYZ")(1)[0];

var ValueFrom = transform.xRotation;

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var d = length(p1, p2);

var t = linear(d, Radius, RadiusStrength, 0, 1);

var r = linear(t, 0, 1, ValueFrom + ValueTo, ValueFrom);

r;

Y Rotation:

var p1 = thisLayer.transform.position;

var p2 = thisComp.layer("Effector").transform.position;

var ValueTo = thisComp.layer("Effector").effect("Rotation XYZ")(1)[1];

var ValueFrom = transform.yRotation;

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var d = length(p1, p2);

var t = linear(d, Radius, RadiusStrength, 0, 1);

var r = linear(t, 0, 1, ValueFrom + ValueTo, ValueFrom);

r;

var p2 = thisComp.layer("Effector").transform.position;

var ValueTo = thisComp.layer("Effector").effect("Rotation XYZ")(1)[1];

var ValueFrom = transform.yRotation;

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var d = length(p1, p2);

var t = linear(d, Radius, RadiusStrength, 0, 1);

var r = linear(t, 0, 1, ValueFrom + ValueTo, ValueFrom);

r;

Z Rotation:

var p1 = thisLayer.transform.position;

var p2 = thisComp.layer("Effector").transform.position;

var ValueTo = thisComp.layer("Effector").effect("Rotation XYZ")(1)[2];

var ValueFrom = transform.zRotation;

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var d = length(p1, p2);

var t = linear(d, Radius, RadiusStrength, 0, 1);

var r = linear(t, 0, 1, ValueFrom + ValueTo, ValueFrom);

r;

var p2 = thisComp.layer("Effector").transform.position;

var ValueTo = thisComp.layer("Effector").effect("Rotation XYZ")(1)[2];

var ValueFrom = transform.zRotation;

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var d = length(p1, p2);

var t = linear(d, Radius, RadiusStrength, 0, 1);

var r = linear(t, 0, 1, ValueFrom + ValueTo, ValueFrom);

r;

These expressions control the X, Y, and Z rotations of a layer based on its proximity to the "Effector" layer, allowing for dynamic rotational effects in your After Effects projects.

Proximity-Based Opacity Adjustment

This expression dynamically adjusts the opacity of a layer based on its distance from another layer called the "Effector." The expression calculates the distance between the two layers and uses this distance to interpolate between the current opacity (ValueFrom) and a target opacity (ValueTo) defined on the "Effector" layer. The interpolation is controlled by the Radius and Radius Strength parameters, creating a smooth transition in opacity as the distance changes.

Here's the complete expression you can use in After Effects, just copy and paste on the Opacity Property:

var pos1 = thisLayer.transform.position;

var pos2 = thisComp.layer("Effector").transform.position;

var ValueFrom = transform.opacity;

var ValueTo = thisComp.layer("Effector").effect("Opacity To")(1);

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var d = length(pos1, pos2);

var finalValue = linear(d, Radius, RadiusStrength, ValueTo, ValueFrom);

[finalValue];

var pos2 = thisComp.layer("Effector").transform.position;

var ValueFrom = transform.opacity;

var ValueTo = thisComp.layer("Effector").effect("Opacity To")(1);

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var d = length(pos1, pos2);

var finalValue = linear(d, Radius, RadiusStrength, ValueTo, ValueFrom);

[finalValue];

Distance-Based Opacity Control: This expression controls the opacity of a layer depending on its distance from an "Effector" layer, allowing for dynamic fading effects in your After Effects compositions.

Premium Tools for Stunning Animations

Discover a Collection of After Effects products, templates, and toolkits designed to streamline your animation projects.

Whether you’re crafting intricate character animations or looking for ready-to-use templates, our library has everything you need to bring your creative vision to life with ease and efficiency.

Explore our offerings and find the perfect tools to enhance your animations today.

Proximity-Based Opacity Adjustment

This expression adjusts the color of a layer based on its distance from another layer named the "Effector." The expression calculates the distance between the two layers and uses this distance to interpolate between two colors: the starting color (ColorFrom) and the target color (ColorTo). The interpolation is controlled by the Radius and Radius Strength parameters, resulting in a smooth color transition effect as the distance changes.

Here's the complete expression you can use in After Effects, just copy and paste on any Color Property:

var p1 = thisLayer.transform.position;

var p2 = thisComp.layer("Effector").transform.position;

var p2 = thisComp.layer("Effector").transform.position;

var ColorFrom = thisComp.layer("Effector").effect("Color From")(1);

var ColorTo = thisComp.layer("Effector").effect("Color To")(1);

var ColorTo = thisComp.layer("Effector").effect("Color To")(1);

var RadiusStrength = thisComp.layer("Effector").effect("Radius Strength")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var Radius = thisComp.layer("Effector").effect("Radius")(1);

var d = length(p1, p2);

var t = linear(d, Radius, RadiusStrength, 0, 1);

var r = linear(t, 0, 1, ColorTo[0], ColorFrom[0]);

var g = linear(t, 0, 1, ColorTo[1], ColorFrom[1]);

var b = linear(t, 0, 1, ColorTo[2], ColorFrom[2]);

var g = linear(t, 0, 1, ColorTo[1], ColorFrom[1]);

var b = linear(t, 0, 1, ColorTo[2], ColorFrom[2]);

[r, g, b, 1];

Proximity-Based Color Transition: This expression dynamically changes the color of a layer based on its distance from an "Effector" layer, allowing for smooth color blending effects in your After Effects projects.

3D to 2D Position Mapping:

This expression converts a 3D position from the "Effector" layer into a 2D screen space position within the current composition. It uses the toComp function to transform the 3D coordinates [0,0,0] of the "Effector" layer into 2D coordinates that represent its location in the composition's 2D view. This is useful for linking 2D properties (like position) to a 3D layer's screen-space representation.

temp = thisComp.layer("Effector").toComp([0, 0, 0]);

[temp[0], temp[1]];

[temp[0], temp[1]];

3D to 2D Position Mapping: This expression converts a 3D layer's position into 2D screen space, allowing a 2D layer to follow or respond to the 3D layer's movement within the composition.

Explore More After Effects Tips and Tricks! 🌟

Visit our YouTube channel for free tutorials and insights on mastering motion graphics. Subscribe now for easy-to-follow guides and creative inspiration!"