Layer Proximity Effect
A reactive effect that scales or modifies properties based on the distance between layers.
Code
/* broadcastGEMs - Roland Kahlenberg - June 26, 2024.
Proximity Effect based on Layer Boundary
Usually used in Opacity/Scale properties to create a Reactive Effect.
This Expression Rig assumes and requires the Moving Layer and this ProximityFX Layer
have their Anchor Points centered to their respective Rectangular Boundaries.
*/
// Declare the Moving/Target Layers
const targetLayer1 = thisComp.layer("Moving Layer 1");
const targetLayer2 = thisComp.layer("Moving Layer 2");
// Replace the Layer Names with relevant layer names in your Composition
// Get CompLayer with the ProximityFX Slider Controls
const compLayer = thisComp.layer("Controller");
// Get Maximum Distance and Scale Controls
const maxDistance = compLayer.effect("Proximity - Container - Max Distance")(1);
const minScale = compLayer.effect("Proximity - Container - Min Scale")(1);
const maxScale = compLayer.effect("Proximity - Container - Max Scale")(1);
// Function to calculate distance to a target layer
function calculateDistance(targetLayer) {
const targetLayerPos = targetLayer.transform.position;
const targetLayerRect = targetLayer.sourceRectAtTime(time, false);
const targetLayerScale = targetLayer.transform.scale;
const targetLayerWidth = targetLayerRect.width * targetLayerScale[0] / 100;
const targetLayerHeight = targetLayerRect.height * targetLayerScale[1] / 100;
const halfWidthTargetLayer = targetLayerWidth / 2;
const halfHeightTargetLayer = targetLayerHeight / 2;
const distanceX = Math.max(Math.abs(targetLayerPos[0] - myLayerPos[0]) - (halfWidthThisLayer + halfWidthTargetLayer), 0);
const distanceY = Math.max(Math.abs(targetLayerPos[1] - myLayerPos[1]) - (halfHeightThisLayer + halfHeightTargetLayer), 0);
return Math.sqrt(distanceX * distanceX + distanceY * distanceY);
}
// Declare this Layer's Position and dimensions
const myLayer = thisLayer;
const myLayerPos = myLayer.transform.position;
const myLayerRect = myLayer.sourceRectAtTime(time, false);
const myLayerScale = myLayer.transform.scale;
const myLayerWidth = myLayerRect.width * myLayerScale[0] / 100;
const myLayerHeight = myLayerRect.height * myLayerScale[1] / 100;
// Calculate the half-width and half-height of this layer's Rect Boundary
const halfWidthThisLayer = myLayerWidth / 2;
const halfHeightThisLayer = myLayerHeight / 2;
// Calculate distances to both target layers
const distance1 = calculateDistance(targetLayer1);
const distance2 = calculateDistance(targetLayer2);
// Use the smaller of the two distances
const distance = Math.min(distance1, distance2);
// Calculate the scale factor based on the distance
const scaleFactor = ease(distance, 0, maxDistance, maxScale, minScale);
// Return the new scale value
[scaleFactor, scaleFactor]
// EndOfExpression
animationproximity effectreactive effectafter effects
How to Use
This expression calculates the proximity between two layers based on their rectangular boundaries, which enhances the accuracy of proximity effects for non-uniform shapes. Apply this expression to the Scale property of a layer in After Effects to create reactive effects, such as scaling an object as it approaches another. Customize by renaming 'Moving Layer 1' and 'Moving Layer 2' to match layer names in your composition, and adjust the proximity threshold in the Controller layer.