Skip to main content

vec2

Class

A two dimensional vector.

  • Available on all platforms
Platform details: Snapchat, SPECS, Camera Kit Web, Camera Kit Mobile fully supported.

Platform details

  • Snapchat — Fully supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Fully supported.

  • Camera Kit Mobile — Fully supported.

A two dimensional vector. Vectors can only store finite numbers in the range Number.MIN_VALUE to Number.MAX_VALUE.

Index

Example

// Set the scale of a sprite using vec2 methods
//@input Component.SpriteAligner spriteAligner
//@input float spriteScale

script.spriteAligner.size = vec2.one().uniformScale(script.spriteScale);

Constructors

Constructor

new vec2(x, y): vec2

Parameters

ParameterType
xnumber
ynumber

Returns

vec2

Properties

g

g: number

Alternate name for the y component.


length

length: number

Returns the length of the vector.


lengthSquared

lengthSquared: number

Returns the squared length of the vector.


r

r: number

Alternate name for the x component.


x

x: number

x component of the vec2.


y

y: number

y component of the vec2.

Methods

add()

add(vec): vec2

Returns the vector plus vec.

Parameters

ParameterType
vecvec2

Returns

vec2


addInPlace()

addInPlace(vec): void

Adds vec to this vector in place.

Parameters

ParameterType
vecvec2

Returns

void


angleTo()

angleTo(vec): number

Returns the angle between the vector and vec.

Parameters

ParameterType
vecvec2

Returns

number


clampLength()

clampLength(length): vec2

Returns a copy of the vector with its length clamped to length.

Parameters

ParameterType
lengthnumber

Returns

vec2


clampLengthInPlace()

clampLengthInPlace(length): void

Clamps this vector's length to length in place.

Parameters

ParameterType
lengthnumber

Returns

void


clone()

clone(): vec2

Returns a copy of this vector.

Returns

vec2


copyFrom()

copyFrom(source): void

Copies matching components from the source vector, ignoring extra source components. For example, copying a vec3 into a vec2 will update x and y but ignore z.

Parameters

ParameterType
sourcevec2 | vec3 | vec4

Returns

void


distance()

distance(vec): number

Returns the distance between the vector and the vector vec.

Parameters

ParameterType
vecvec2

Returns

number


distanceSquared()

distanceSquared(vec): number

Like distance(), but returns the squared distance between vectors.

Parameters

ParameterType
vecvec2

Returns

number


div()

div(vec): vec2

Returns the division of the vector by the vector vec.

Parameters

ParameterType
vecvec2

Returns

vec2


divInPlace()

divInPlace(vec): void

Divides this vector by vec component-wise in place.

Parameters

ParameterType
vecvec2

Returns

void


dot()

dot(vec): number

Returns the dot product of the vector and vec.

Parameters

ParameterType
vecvec2

Returns

number


down()

static down(): vec2

Returns the vector (0, -1).

Returns

vec2


equal()

equal(vec): boolean

Returns whether this is equal to vec.

Parameters

ParameterType
vecvec2

Returns

boolean


fill()

fill(scalar): void

Sets the x and y components of this vector to scalar.

Parameters

ParameterType
scalarnumber

Returns

void


left()

static left(): vec2

Returns the vector (-1, 0).

Returns

vec2


lerp()

static lerp(from, to, t): vec2

Linearly interpolates between the two vectors from and to by the factor t.

Parameters

ParameterType
fromvec2
tovec2
tnumber

Returns

vec2


lerpInPlace()

lerpInPlace(target, t): void

Linearly interpolates this vector towards target by factor t in place.

Parameters

ParameterType
targetvec2
tnumber

Returns

void


max()

static max(a, b): vec2

Returns a new vector containing the largest value of each component in the two vectors.

Parameters

ParameterType
avec2
bvec2

Returns

vec2


min()

static min(a, b): vec2

Returns a new vector containing the smallest value of each component in the two vectors.

Parameters

ParameterType
avec2
bvec2

Returns

vec2


moveTowards()

moveTowards(target, step): vec2

Returns a copy of the vector moved towards the point point by the amount magnitude.

Parameters

ParameterType
targetvec2
stepnumber

Returns

vec2


moveTowardsInPlace()

moveTowardsInPlace(point, magnitude): void

Moves this vector towards point by magnitude in place.

Parameters

ParameterType
pointvec2
magnitudenumber

Returns

void


mult()

mult(vec): vec2

Returns the component-wise multiplication product of the vector and vec.

Parameters

ParameterType
vecvec2

Returns

vec2


multInPlace()

multInPlace(vec): void

Multiplies this vector by vec component-wise in place.

Parameters

ParameterType
vecvec2

Returns

void


normalize()

normalize(): vec2

Returns a copy of the vector with its length scaled to 1.

Returns

vec2


normalizeInPlace()

normalizeInPlace(): void

Scales this vector's length to 1 in place.

Returns

void


one()

static one(): vec2

Returns the vector (1, 1).

Returns

vec2


project()

project(onto): vec2

Returns a copy of the vector projected onto the vector onto.

Parameters

ParameterType
ontovec2

Returns

vec2


projectInPlace()

projectInPlace(onto): void

Projects this vector onto the vector onto in place.

Parameters

ParameterType
ontovec2

Returns

void


projectOnPlane()

projectOnPlane(planeNormal): vec2

Projects the vector onto the plane defined by planeNormal.

Parameters

ParameterType
planeNormalvec2

Returns

vec2


projectOnPlaneInPlace()

projectOnPlaneInPlace(planeNormal): void

Projects this vector onto the plane defined by planeNormal in place.

Parameters

ParameterType
planeNormalvec2

Returns

void


randomUnitVector()

static randomUnitVector(): vec2

Generates a random 2D unit vector. Equivalent to a random point on a unit-radius circle.

Returns

vec2


reflect()

reflect(planeNormal): vec2

Returns a copy of the vector reflected across the plane defined by planeNormal.

Parameters

ParameterType
planeNormalvec2

Returns

vec2


reflectInPlace()

reflectInPlace(planeNormal): void

Reflects this vector across the plane defined by planeNormal in place.

Parameters

ParameterType
planeNormalvec2

Returns

void


static right(): vec2

Returns the vector (1, 0).

Returns

vec2


scale()

scale(vec): vec2

Returns the component-wise multiplication product of the vector and vec.

Parameters

ParameterType
vecvec2

Returns

vec2


scaleInPlace()

scaleInPlace(vec): void

Multiplies this vector by vec component-wise in place.

Parameters

ParameterType
vecvec2

Returns

void


setRandomUnitVector()

setRandomUnitVector(): void

Sets this vector to a random unit vector in place. Equivalent to a random point on a unit-radius circle.

Returns

void


setXY()

setXY(x?, y?): void

Sets the x and y components of this vector. Omit parameters or pass null or undefined to leave a component unchanged.

Parameters

ParameterType
x?number
y?number

Returns

void


sub()

sub(vec): vec2

Returns the vector minus vec.

Parameters

ParameterType
vecvec2

Returns

vec2


subInPlace()

subInPlace(vec): void

Subtracts vec from this vector in place.

Parameters

ParameterType
vecvec2

Returns

void


toString()

toString(): string

Returns a string representation of the vector.

Returns

string


uniformScale()

uniformScale(scale): vec2

Multiplies the components of this vector by scale.

Parameters

ParameterType
scalenumber

Returns

vec2


uniformScaleInPlace()

uniformScaleInPlace(scale): void

Multiplies the components of this vector by scale in place.

Parameters

ParameterType
scalenumber

Returns

void


up()

static up(): vec2

Returns the vector (0, 1).

Returns

vec2


zero()

static zero(): vec2

Returns the vector (0, 0).

Returns

vec2