Vec

A two-dimensional vector.

Vectors can be used to represent positions, normals, tangents, offsets and translations.

Constructors

Vec(x,y)→Vec

Constructs a vector from two numbers.

Vec(3, 5)
// Result: Vec(3, 5)
x
number
optional

The x component

y
number
optional

The y component

Vec.fromAngle(angle)→Vec

Constructs a unit-length vector from an angle.

angle
number

An angle specified in degrees

Vec.fromAngleRadians(angle)→Vec

Constructs a unit-length vector from an angle.

angle
number

An angle specified in radians

Properties

.xnumber

The x component

.ynumber

The y component

Methods

.clone()→Vec

Returns a copy of this vector.

.set(x,y)→Vecchainable

Sets the the x and y components of this vector.

x
number
y
number
.copy(v)→Vecchainable

Copies x and y components into this vector from v.

.affineTransform(affineMatrix)→Vecchainable

Transforms this vector by the affine matrix affineMatrix.

Use this when transforming a Vec that represents a point or position.

affineMatrix

An affine matrix representing a transformation

.affineTransformWithoutTranslation(affineMatrix)→Vecchainable

Transforms this vector by the affine matrix affineMatrix, ignoring translation.

Use this instead of affineTransform() when transforming a Vec that represents a normal or a tangent.

affineMatrix

An affine matrix representing a transformation

.transform(transform)→Vecchainable

Transforms this vector.

vec.transform({
  position: Vec(1, 2),
  rotation: 45,
  scale: Vec(1, 0.5), // Scale can also be a number
  skew: 10,
  origin: Vec(0, 0), // The center of rotation and scale
})
transform

An object reprenting a transform.

.add(v)→Vecchainable

Adds the vector v to this vector.

.addScalar(x)→Vecchainable

Adds the scalar x to both components of this vector.

x
number
.sub(v)→Vecchainable

Subtracts the vector v from this vector.

.subScalar(x)→Vecchainable

Subtracts the scalar x from both components of this vector.

x
number
.mul(v)→Vecchainable

Multiplies this vector by the vector v.

.mulScalar(x)→Vecchainable

Multiplies both components of this vector by the scalar x.

x
number
.div(v)→Vecchainable

Divides this vector by the vector v.

.divScalar(x)→Vecchainable

Divides both components of this vector by the scalar x;

x
number
.negate()→Vecchainable

Multiplies both components of this vector by -1. This is functionally the same as .mulScalar(-1).

.equals(v)→boolean

Tests if this vector exactly equals the vector v.

Returns true if the vectors are exactly equal, false otherwise.

.floor()→Vecchainable

Rounds the components of this vector to the next-lowest integer.

let v = Vec(3.141, -1.618);
v.floor();
// Result: Vec(3, -2)
.ceil()→Vecchainable

Rounds the components of this vector to the next-highest integer.

let v = Vec(3.141, -1.618);
v.ceil();
// Result: Vec(4, -1)
.round()→Vecchainable

Rounds the components of this vector to the closest integer.

let v = Vec(3.141, 3.618);
v.round();
// Result: Vec(3, 4)
.min(v)→Vecchainable

Compares the components of this vector and v and sets this vector's to the lesser of the two.

let a = Vec(1, 4);
let b = Vec(2, 3);
a.min(b);
// Result: Vec(1, 3)
v

The vector to compare against

.minScalar(x)→Vecchainable

Compares this vector's components to x and sets them to the lesser of the two.

let v = Vec(1, 2);
v.minScalar(3);
// Result: Vec(1, 2)
x
number

The value to compare against

.max(v)→Vecchainable

Compares the components of this vector and v and sets this vector's to the greater of the two.

let a = Vec(1, 4);
let b = Vec(2, 3);
a.max(b);
// Result: Vec(2, 4)
v

The vector to compare against

.maxScalar(x)→Vecchainable

Compares this vector's components to x and sets them to the greater of the two.

let v = Vec(1, 2);
v.maxScalar(3);
// Result: Vec(3, 3)
x
number

The value to compare against

.mix(v,t)→Vecchainable

Linearly interpolates this vector to the vector v by the mixing factor t.

v

The vector to interpolate to

t
number

The mixing factor

.dot(v)→number

Returns the dot product between this vector and the vector v.

.cross(v)→number

Returns the cross product between this vector and the vector v.

.normalize()→Vecchainable

Scales this vector so that its length is equal to 1.

Note the this vector must already have a non-zero length.

.rotate(angle)→Vecchainable

Rotates this vector clockwise by an angle specified in degrees.

angle
number

An angle in degrees

.rotateRadians(angleRadians)→Vecchainable

Rotates this vector clockwise by an angle specified in radians.

angleRadians
number

An angle in radians

.rotate90()→Vecchainable

Rotates this vector clockwise by exactly 90°.

This is functionally the same as writing vec.rotate(90) but is computationally simpler.

.rotateNeg90()→Vecchainable

Rotates this vector counter-clockwise by exactly 90°.

This is functionally the same as writing vec.rotate(-90) but is computationally simpler.

.projectOnto(v)→Vecchainable

The vector projection of this vector onto a non-zero vector v, (also known as the vector component or vector resolution of a in the direction of b), is the orthogonal projection of this onto a straight line parallel to v.

v

A vector representing a line to project onto

.closestPoint(v)→

A trivial closest point implementation to maintain a uniform interface with Geometry.

Returns a ClosestPointResult with itself as the position.

.angle()→number

Returns the angle of this vector in degrees.

.angleRadians()→number

Returns the angle of this vector in radians.

.setFromAngle(angle)→Vecchainable

Sets this vector to a unit-length vector at angle.

angle
number

An angle specified in degrees

.setFromAngleRadians(angle)→Vecchainable

Sets this vector to a unit-length vector at angle.

angle
number

An angle specified in radians

.isClockwiseFrom(v)→boolean
v

The vector to compare against

Returns true if this vector lies in the 180° region clockwise from v.

.length()→number

Returns the length of this vector. The length of a vector is also sometimes referred to as its "magnitude".

.lengthSquared()→number

Returns the squared length of this vector.

This variation avoids the sqrt() used in length() and can be computationally simpler when comparing the length of two vectors.

.distance(v)→number

Returns the distance from this vector to v.

.distanceSquared(v)→number

Returns the squared distance from this vector to v.

This variation avoids the sqrt() used in distance() and can be computationally simpler when comparing distances.

.isZero()→boolean

Returns true if both components of this vector are 0, false otherwise.

.isFinite()→boolean

Returns true if both components of this vector are finite real numbers, meaning not NaN or Infinity.