Proximity Expression
Dynamic, Transform, Time Based Animation, Advanced Expressions
Proximity Expression
This expression scales a layer dynamically based on its proximity to a target, controlled via sliders for maximum scale and range.
This expression scales a layer dynamically based on its proximity to a target. For example, create a null named "Target" and another null named "Controller" with two sliders: Max Scale and Range. Apply the expression to a shape layer’s Scale property, and adjust the sliders to see the shape react to the target’s distance.
// Define the target and controller layers
target = thisComp.layer("Target");
controller = thisComp.layer("Controller");
// Get the maximum scale and range values from the controller
maxScale = controller.effect("Max Scale")("Slider");
range = controller.effect("Range")("Slider");
// Calculate the distance between the target and the current layer
dist = length(target.position, position);
// Smoothly interpolate the scale factor based on the distance
scaleFactor = ease(dist, 0, range, maxScale, 100);
// Return the scale value for both X and Y axes
[scaleFactor, scaleFactor];
How to Use This Expression?
Setup in After Effects: Add two layers and name them "Target" and "Controller".
Add a slider control effect to the "Controller" layer.
Rename the sliders to: "Max Scale": Set this slider to define the maximum scaling.
"Range": Set this slider to define the maximum distance for the effect.
Apply Expression: Select the Scale property of the layer you want to affect.
Alt + Click (Windows) or Option + Click (Mac) the stopwatch icon for the Scale property.
Paste the script into the expression editor.
Adjust Parameters: Modify the values of the Max Scale and Range sliders in the Controller layer to see the effect in action.
Max Scale = 150: Layers closest to the target will scale up to 150%.
Range = 500: The scaling effect will occur within a distance of 500 pixels from the target.