Skip to content

vector-math

Vector Math functions are global methods that perform operations on arrays, treating them as mathematical vectors.

Unless otherwise specified, Vector Math methods are lenient about dimensions and return a value that is the dimension of the largest input Array object, filling in missing elements with zeros.

Unlike built-in JavaScript methods, such as Math.sin, these methods are not used with the Math prefix.

This expression returns [11, 22, 3]:

add([10, 20], [1, 2, 3])

add(vec1, vec2)

Adds two vectors.

ParameterTypeDescription
vec1ArrayFirst vector to add.
vec2ArraySecond vector to add.

Array


clamp(value, limit1, limit2)

The value of each component of value is constrained to fall between the values of the corresponding values of limit1 and limit2.

ParameterTypeDescription
valueNumber or ArrayValue to clamp.
limit1Number or ArrayMinimum amount to clamp value to.
limit2Number or ArrayMaximum amount to clamp value to.

Number or Array

Ensure that a wiggled amount never exceeds the 0-100 range:

const wiggled = wiggle(0.5, 500);
clamp(wiggled, 0, 500);

cross(vec1, vec2)

Returns the vector cross product of vec1 and vec2.

Refer to a math reference or JavaScript guide for more information.

ParameterTypeDescription
vec1Array (2- or 3-dimensional)First vector to calculate cross product of.
vec2Array (2- or 3-dimensional)Second vector to calculate cross product of.

Array (2- or 3-dimensional)


div(vec, amount)

Divides every element of the vector by the amount.

ParameterTypeDescription
vecArrayThe vector to divide
amountNumberThe amount to divide by

Array


dot(vec1, vec2)

Returns the dot (inner) product of the vector arguments.

ParameterTypeDescription
vec1ArrayFirst vector to calculate dot product of.
vec2ArraySecond vector to calculate dot product of.

Number


length(vec[, point2])

Returns the length of vector vec.

If a second argument is provided, instead treats the first argument as a point and returns the distance between the two points.

ParameterTypeDescription
vecArrayThe vector to normalize, or the first point to measure from.
point2ArrayOptional. The second point to measure to.

Number

For example, add this expression to the Focus Distance property of a camera to lock the focal plane to the camera’s point of interest so that the point of interest is in focus:

length(position, pointOfInterest)

lookAt(fromPoint, atPoint)

Orients a layer to look at a given point, from a given point.

The return value can be used as an expression for the Orientation property, making the z-axis of the layer point at atPoint.

This method is especially useful for cameras and lights.

ParameterTypeDescription
fromPointArray (3-dimensional)The location (in world space) of the layer to orient.
atPointArray (3-dimensional)The point (in world space) you want to point the layer at.

Array (3-dimensional)

This expression on the Orientation property of a spot light makes the light point at the anchor point of layer number 1 in the same composition:

lookAt(position, thisComp.layer(1).position)

mul(vec, amount)

Multiplies every element of the vector by the amount.

ParameterTypeDescription
vecArrayThe vector to multiply
amountNumberThe amount to multiply by

Array


normalize(vec)

Normalizes the vector so that its length is 1.0.

Using the normalize method is a short way of performing the operation div(vec, length(vec)).

ParameterTypeDescription
vecArrayThe vector to normalize

Array


sub(vec1, vec2)

Subtracts two vectors.

ParameterTypeDescription
vec1ArrayThe first vector
vec2ArrayThe second vector

Array