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.
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.
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);
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];
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);
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;
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.