Vector Math in After Effects Expressions

Vector math in Adobe After Effects expressions allows you to perform basic arithmetic operations—addition, subtraction, multiplication, and division—on multidimensional values such as 2D/3D positions, scale dimensions, and color arrays. Because standard JavaScript arithmetic operators like + or - fail when applied directly to arrays, After Effects includes native global vector functions: add(), sub(), mul(), and div(). This guide covers how these four functions work, why they are essential, and how to implement them in your project workflows.

Why Standard Math Operators Fail on Arrays

In After Effects, multidimensional properties like Position [x, y] or Color [r, g, b, a] are treated as JavaScript arrays. If you attempt to add two position arrays using the standard plus operator:

[100, 200] + [50, 50];

JavaScript treats this as string concatenation, producing "100,20050,50" instead of a valid coordinate. To perform math on each corresponding dimension (element-by-element), you must either write manual loops, destructure each axis individually, or use built-in vector math functions.

1. add(vec1, vec2) — Vector Addition

The add() function takes two vectors of the same dimension and adds their corresponding values together.

  • Use Case: Offsetting a layer's position based on another layer's coordinates.
  • Syntax: add(vector1, vector2)
// Add an offset of 50px X and 100px Y to the current position
var offset = [50, 100];
add(value, offset);

Result: A layer at [960, 540] moves to [1010, 640].

2. sub(vec1, vec2) — Vector Subtraction

The sub() function subtracts the second vector from the first vector.

  • Use Case: Finding the difference vector between two layers to calculate direction, or preparing a vector for the length() function to measure distance.
  • Syntax: sub(vector1, vector2)
// Calculate the vector pointing from Layer B to Layer A
var targetPos = thisComp.layer("Target").transform.position;
var diff = sub(targetPos, transform.position);

Result: If Target is at [500, 300] and the current layer is at [200, 100], the result is [300, 200].

3. mul(vec, amount) — Vector Multiplication

The mul() function scales a vector by a scalar (a single number) or multiplies two vectors component-by-component.

  • Use Case: Scaling values uniformly, multiplying an animated wiggle, or modifying scale percentages dynamically.
  • Syntax: mul(vector, scalar) or mul(vector1, vector2)
// Halve the current layer's position or scale
mul(value, 0.5);

// Multiply two vectors component-by-component
mul([10, 20], [2, 3]); // Returns [20, 60]

4. div(vec, amount) — Vector Division

The div() function divides a vector by a scalar value or performs component-by-component division between two vectors.

  • Use Case: Normalizing scale factors, dividing dimensions evenly, or creating relative responsive spacing across screen layouts.
  • Syntax: div(vector, scalar) or div(vector1, vector2)
// Divide a 3D position vector by 2
div(transform.position, 2);

// Component-wise division
div([1920, 1080], [2, 2]); // Returns [960, 540]

Summary Comparison

Function Operation Example Input Example Output
add(v1, v2) Component-wise addition add([10, 20], [5, 5]) [15, 25]
sub(v1, v2) Component-wise subtraction sub([10, 20], [5, 5]) [5, 15]
mul(v, s) Scalar multiplication mul([10, 20], 2) [20, 40]
div(v, s) Scalar division div([10, 20], 2) [5, 10]