vector-math
Vector Math
Section titled “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.
Example
Section titled “Example”This expression returns [11, 22, 3]
:
add([10, 20], [1, 2, 3])
Methods
Section titled “Methods”add(vec1, vec2)
Description
Section titled “Description”Adds two vectors.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
vec1 | Array | First vector to add. |
vec2 | Array | Second vector to add. |
Returns
Section titled “Returns”Array
clamp()
Section titled “clamp()”clamp(value, limit1, limit2)
Description
Section titled “Description”The value of each component of value
is constrained to fall between the values of the corresponding values of limit1
and limit2
.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
value | Number or Array | Value to clamp. |
limit1 | Number or Array | Minimum amount to clamp value to. |
limit2 | Number or Array | Maximum amount to clamp value to. |
Returns
Section titled “Returns”Number or Array
Example
Section titled “Example”Ensure that a wiggled amount never exceeds the 0-100 range:
const wiggled = wiggle(0.5, 500);clamp(wiggled, 0, 500);
cross()
Section titled “cross()”cross(vec1, vec2)
Description
Section titled “Description”Returns the vector cross product of vec1
and vec2
.
Refer to a math reference or JavaScript guide for more information.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
vec1 | Array (2- or 3-dimensional) | First vector to calculate cross product of. |
vec2 | Array (2- or 3-dimensional) | Second vector to calculate cross product of. |
Returns
Section titled “Returns”Array (2- or 3-dimensional)
div(vec, amount)
Description
Section titled “Description”Divides every element of the vector by the amount.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
vec | Array | The vector to divide |
amount | Number | The amount to divide by |
Returns
Section titled “Returns”Array
dot(vec1, vec2)
Description
Section titled “Description”Returns the dot (inner) product of the vector arguments.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
vec1 | Array | First vector to calculate dot product of. |
vec2 | Array | Second vector to calculate dot product of. |
Returns
Section titled “Returns”Number
length()
Section titled “length()”length(vec[, point2])
Description
Section titled “Description”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.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
vec | Array | The vector to normalize, or the first point to measure from. |
point2 | Array | Optional. The second point to measure to. |
Returns
Section titled “Returns”Number
Example
Section titled “Example”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()
Section titled “lookAt()”lookAt(fromPoint, atPoint)
Description
Section titled “Description”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.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
fromPoint | Array (3-dimensional) | The location (in world space) of the layer to orient. |
atPoint | Array (3-dimensional) | The point (in world space) you want to point the layer at. |
Returns
Section titled “Returns”Array (3-dimensional)
Example
Section titled “Example”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)
Description
Section titled “Description”Multiplies every element of the vector by the amount.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
vec | Array | The vector to multiply |
amount | Number | The amount to multiply by |
Returns
Section titled “Returns”Array
normalize()
Section titled “normalize()”normalize(vec)
Description
Section titled “Description”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))
.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
vec | Array | The vector to normalize |
Returns
Section titled “Returns”Array
sub(vec1, vec2)
Description
Section titled “Description”Subtracts two vectors.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
vec1 | Array | The first vector |
vec2 | Array | The second vector |
Returns
Section titled “Returns”Array