Class Vector2

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

  • Creates a new Vector2.

    Parameters

    • Optional x: number

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

    • Optional y: number

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

    Returns Vector2

Properties

x: number
y: number

Accessors

  • get height(): number
  • Alias for y.

    Returns number

  • set height(value: number): void
  • 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
  • Alias for x.

    Returns number

  • set width(value: number): void
  • Parameters

    • value: number

    Returns void

Methods

  • Returns IterableIterator<number>

  • 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

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

    Returns

    The angle (radians)

    Returns number

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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.

    • Optional offset: number

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

    Returns Vector2

  • 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

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

    Returns

    The length.

    Returns 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

  • Computes the Manhattan distance from this vector to v.

    Returns

    The distance.

    Parameters

    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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • Returns an array [x, y], or copies x and y into the provided array.

    Returns

    An array.

    Parameters

    • Optional array: number[]

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

    • Optional offset: number

      (optional) optional offset into the array.

    Returns number[]