Class representing a 2D vector. A 2D vector is an ordered pair of numbers (labeled x and y), which can be used to represent a number of things, such as:

  • A point in 2D space (i.e. a position on a plane).
  • A direction and length across a plane. In three.js the length will always be the Euclidean distance (straight-line distance) from (0, 0) to (x, y) and the direction is also measured from (0, 0) towards (x, y).
  • Any arbitrary ordered pair of numbers.

There are other things a 2D vector can be used to represent, such as momentum vectors, complex numbers and so on. However these are the most common uses in three.js.

Iterating through a Vector2 instance will yield its components (x, y) in the corresponding order.

Example

const a = new THREE.Vector2( 0, 1 );

//no arguments; will be initialised to (0, 0)
const b = new THREE.Vector2( );

const d = a.distanceTo( b );

Hierarchy

  • Base
    • Vector2

Implements

Constructors

  • new Vector2(x?: number, y?: number): Vector2
  • Creates a new Vector2.

    Parameters

    • x: number = 0

      (Optional) The x value of this vector. Default is 0.

    • y: number = 0

      (Optional) The y value of this vector. Default is 0.

    Returns Vector2

Properties

x: number
y: number

Accessors

  • get height(): number
  • set height(value: number): void
  • Alias for y.

    Returns number

  • Parameters

    • value: number

    Returns void

  • get isBox2(): boolean
  • Returns boolean

  • get isBox3(): boolean
  • Returns boolean

  • get isColor(): boolean
  • Returns boolean

  • get isCylindrical(): boolean
  • Returns boolean

  • get isEuler(): boolean
  • Returns boolean

  • get isLine3(): boolean
  • Returns boolean

  • get isMatrix3(): boolean
  • Returns boolean

  • get isMatrix4(): boolean
  • Returns boolean

  • get isPlane(): boolean
  • Returns boolean

  • get isQuaternion(): boolean
  • Returns boolean

  • get isRay(): boolean
  • Returns boolean

  • get isSphere(): boolean
  • Returns boolean

  • get isSpherical(): boolean
  • Returns boolean

  • get isTriangle(): boolean
  • Returns boolean

  • get isVector(): boolean
  • Read-only flag to check if a given object is of type Vector.

    Returns boolean

  • get isVector2(): boolean
  • Read-only flag to check if a given object is of type Vector2.

    Returns boolean

  • get isVector3(): boolean
  • Returns boolean

  • get isVector4(): boolean
  • Returns boolean

  • get width(): number
  • set width(value: number): void
  • Alias for x.

    Returns number

  • Parameters

    • value: number

    Returns void

Methods

  • [iterator](): IterableIterator<number>
  • Returns IterableIterator<number>

  • Adds a vector to this vector.

    Returns

    This instance.

    Parameters

    • v: Vector2

      The vector to add to this vector.

    Returns Vector2

  • Adds the scalar value s to this vector's x and y values.

    Returns

    This instance.

    Parameters

    • s: number

      The scalar to add to this vector.

    Returns Vector2

  • Adds the multiple of v and s to this vector.

    Returns

    This instance.

    Parameters

    • v: Vector2

      The vector to add to this vector.

    • s: number

      The scalar to multiply this vectory by.

    Returns Vector2

  • angle(): number
  • Computes the angle in radians of this vector with respect to the positive x-axis.

    Returns

    The angle (radians)

    Returns number

  • Multiplies this vector (with an implicit 1 as the 3rd component) by m.

    Returns

    This instance.

    Parameters

    Returns Vector2

  • The x and y components of this vector are rounded up to the nearest integer value.

    Returns

    This instance.

    Returns Vector2

  • If this vector's x or y value is greater than the max vector's x or y value, it is replaced by the corresponding value.

    If this vector's x or y value is less than the min vector's x or y value, it is replaced by the corresponding value.

    Returns

    This instance.

    Parameters

    • min: Vector2

      The minimum x and y values.

    • max: Vector2

      The maximum x and y values in the desired range

    Returns Vector2

  • clampLength(min: number, max: number): Vector2
  • If this vector's length is greater than the max value, it is replaced by the max value.

    If this vector's length is less than the min value, it is replaced by the min value.

    Returns

    This instance.

    Parameters

    • min: number

      The minimum value the length will be clamped to

    • max: number

      The maximum value the length will be clamped to

    Returns Vector2

  • clampScalar(minVal: number, maxVal: number): Vector2
  • If this vector's x or y values are greater than the max value, they are replaced by the max value.

    If this vector's x or y values are less than the min value, they are replaced by the min value.

    Returns

    This instance.

    Parameters

    • minVal: number

      the minimum value the components will be clamped to

    • maxVal: number

      the maximum value the components will be clamped to

    Returns Vector2

  • Create a Vector2 with the same x and y values as this one.

    Returns

    A new Vector2 instance.

    Returns Vector2

  • Copies the values of the passed Vector2's x and y properties to this Vector2.

    Returns

    This instance.

    Parameters

    Returns Vector2

  • Calculates the cross product of this vector and v. Note that a 'cross-product' in 2D is not well-defined. This function computes a geometric cross-product often used in 2D graphics

    Returns

    The cross product.

    Parameters

    Returns number

  • Computes the distance from this vector to v.

    Returns

    The distance.

    Parameters

    • v: Vector2

      The vector to compute destance to.

    Returns number

  • distanceToSquared(v: Vector2): number
  • Computes the squared distance from this vector to v. If you are just comparing the distance with another distance, you should compare the distance squared instead as it is slightly more efficient to calculate.

    Returns

    The distance squared.

    Parameters

    Returns number

  • Divides this vector by v.

    Returns

    This instance.

    Parameters

    • v: Vector2

      The vector to device into this vector.

    Returns Vector2

  • divideScalar(scalar: number): Vector2
  • Divides this vector by scalar s.

    Returns

    This instance.

    Parameters

    • scalar: number

    Returns Vector2

  • Calculates the dot product of this vector and v.

    Returns

    The dot product.

    Parameters

    Returns number

  • Compare a vector with this vector for equality.

    Returns

    Returns true if the components of this vector and v are strictly equal; false otherwise.

    Parameters

    Returns boolean

  • The components of this vector are rounded down to the nearest integer value.

    Returns

    This instance.

    Returns Vector2

  • fromArray(array: number[], offset?: number): Vector2
  • Sets this vector's x value to be array[ offset ] and y value to be array[ offset + 1 ].

    Returns

    This instance.

    Parameters

    • array: number[]

      The source array.

    • offset: number = 0

      (Optional) Offset into the array. Default is 0.

    Returns Vector2

  • getComponent(index: number): number
  • Get the component (x or y) indicated by index parameter.

    Returns

    The component value

    Parameters

    • index: number

      If index equals 0 returns the x value. If index equals 1 returns the y value.

    Returns number

  • length(): number
  • Computes the Euclidean length (straight-line length) from (0, 0) to (x, y).

    Returns

    The length.

    Returns number

  • lengthSq(): number
  • Computes the square of the Euclidean length (straight-line length) from (0, 0) to (x, y). If you are comparing the lengths of vectors, you should compare the length squared instead as it is slightly more efficient to calculate.

    Returns

    The length squared.

    Returns number

  • Linearly interpolates between this vector and v, where alpha is the percent distance along the line - alpha = 0 will be this vector, and alpha = 1 will be v.

    Returns

    This instance.

    Parameters

    • v: Vector2

      Vector2 to interpolate towards.

    • alpha: number

      interpolation factor, typically in the closed interval [0, 1].

    Returns Vector2

  • Sets this vector to be the vector linearly interpolated between v1 and v2 where alpha is the percent distance along the line connecting the two vectors - alpha = 0 will be v1, and alpha = 1 will be v2.

    Returns

    This instance.

    Parameters

    • v1: Vector2

      the starting Vector2.

    • v2: Vector2

      Vector2 to interpolate towards.

    • alpha: number

      interpolation factor, typically in the closed interval [0, 1].

    Returns Vector2

  • manhattanDistanceTo(v: Vector2): number
  • Computes the Manhattan distance from this vector to v.

    Returns

    The distance.

    Parameters

    Returns number

  • manhattanLength(): number
  • Computes the Manhattan length of this vector.

    Returns

    The manhattan length.

    Returns number

  • If this vector's x or y value is less than v's x or y value, replace that value with the corresponding max value.

    Returns

    This instance.

    Parameters

    Returns Vector2

  • If this vector's x or y value is greater than v's x or y value, replace that value with the corresponding min value.

    Returns

    This instance.

    Parameters

    Returns Vector2

  • Multiplies this vector by v.

    Returns

    This instance.

    Parameters

    • v: Vector2

      The vector to multiply this vector by.

    Returns Vector2

  • multiplyScalar(scalar: number): Vector2
  • Multiplies this vector by scalar s.

    Returns

    This instance.

    Parameters

    • scalar: number

      The scalar to multiple this vector by.

    Returns Vector2

  • Inverts this vector - i.e. sets x = -x and y = -y.

    Returns

    This instance.

    Returns Vector2

  • Converts this vector to a unit vector - that is, sets it equal to a vector with the same direction as this one, but length 1.

    Returns

    This instance.

    Returns Vector2

  • Set the component of this vector to random values between 0-1.

    Returns

    This instance.

    Returns Vector2

  • Rotates this vector around center by angle radians.

    Returns

    This instance.

    Parameters

    • center: Vector2

      The point around which to rotate.

    • angle: number

      The angle to rotate, in radians.

    Returns Vector2

  • The components of this vector are rounded to the nearest integer value.

    Returns

    This instance.

    Returns Vector2

  • The components of this vector are rounded towards zero (up if negative, down if positive) to an integer value.

    Returns

    This instance.

    Returns Vector2

  • set(x: number, y: number): Vector2
  • Sets the x and y components of this vector.

    Returns

    This instance.

    Parameters

    • x: number

      The new x component

    • y: number

      The new y component

    Returns Vector2

  • setComponent(index: number, value: number): Vector2
  • Update the component value.

    Returns

    This instance.

    Parameters

    • index: number

      If index equals 0 set x to value. If index equals 1 set y to value

    • value: number

      The new component value

    Returns Vector2

  • setLength(length: number): Vector2
  • Sets this vector to a vector with the same direction as this one, but length l.

    Returns

    This instance.

    Parameters

    • length: number

      The new length of this vector.

    Returns Vector2

  • setScalar(scalar: number): Vector2
  • Sets the x and y values of this vector both equal to scalar.

    Returns

    This instance.

    Parameters

    • scalar: number

      The scalar value to set.

    Returns Vector2

  • Parameters

    • x: number

    Returns Vector2

  • Parameters

    • y: number

    Returns Vector2

  • Subtracts v from this vector.

    Returns

    This instance.

    Parameters

    • v: Vector2

      The vector to subtract from this vector.

    Returns Vector2

  • Subtracts s from this vector's x and y components.

    Returns

    This instance.

    Parameters

    • s: number

      The scalar to subtract from each of this vector's components.

    Returns Vector2

  • toArray(array?: number[], offset?: number): number[]
  • Returns an array [x, y], or copies x and y into the provided array.

    Returns

    An array.

    Parameters

    • array: number[] = []

      (optional) array to store this vector to. If this is not provided, a new array will be created.

    • offset: number = 0

      (optional) optional offset into the array.

    Returns number[]