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:
(0, 0) to (x, y) and the direction is also measured from (0, 0) towards (x, y).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 vector instance will yield its components (x, y) in the corresponding order.
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 );
Constructs a new 2D vector.
x
The x value of this vector.
Default is 0.
y
The y value of this vector.
Default is 0.
Alias for Vector2#y.
This flag can be used for type testing.
Default is true.
Alias for Vector2#x.
The x value of this vector.
The y value of this vector.
Adds the given vector to this instance.
v
The vector to add.
Returns: A reference to this vector.
Adds the given scalar value to all components of this instance.
s
The scalar to add.
Returns: A reference to this vector.
Adds the given vector scaled by the given factor to this instance.
v
The vector.
s
The factor that scales v.
Returns: A reference to this vector.
Adds the given vectors and stores the result in this instance.
a
The first vector.
b
The second vector.
Returns: A reference to this vector.
Computes the angle in radians of this vector with respect to the positive x-axis.
Returns: The angle in radians.
Returns the angle between the given vector and this instance in radians.
v
The vector to compute the angle with.
Returns: The angle in radians.
Multiplies this vector (with an implicit 1 as the 3rd component) by the given 3x3 matrix.
m
The matrix to apply.
Returns: A reference to this vector.
The components of this vector are rounded up to the nearest integer value.
Returns: A reference to this vector.
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.
min
The minimum x and y values.
max
The maximum x and y values in the desired range.
Returns: A reference to this vector.
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.
min
The minimum value the vector length will be clamped to.
max
The maximum value the vector length will be clamped to.
Returns: A reference to this vector.
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.
minVal
The minimum value the components will be clamped to.
maxVal
The maximum value the components will be clamped to.
Returns: A reference to this vector.
Returns a new vector with copied values from this instance.
Returns: A clone of this instance.
Copies the values of the given vector to this instance.
v
The vector to copy.
Returns: A reference to this vector.
Calculates the cross product of the given vector with this instance.
v
The vector to compute the cross product with.
Returns: The result of the cross product.
Computes the distance from the given vector to this instance.
v
The vector to compute the distance to.
Returns: The distance.
Computes the squared distance from the given vector to this instance. 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.
v
The vector to compute the squared distance to.
Returns: The squared distance.
Divides this instance by the given vector.
v
The vector to divide.
Returns: A reference to this vector.
Divides this vector by the given scalar.
scalar
The scalar to divide.
Returns: A reference to this vector.
Calculates the dot product of the given vector with this instance.
v
The vector to compute the dot product with.
Returns: The result of the dot product.
Returns true if this vector is equal with the given one.
v
The vector to test for equality.
Returns: Whether this vector is equal with the given one.
The components of this vector are rounded down to the nearest integer value.
Returns: A reference to this vector.
Sets this vector's x value to be array[ offset ] and y value to be array[ offset + 1 ].
array
An array holding the vector component values.
offset
The offset into the array.
Default is 0.
Returns: A reference to this vector.
Sets the components of this vector from the given buffer attribute.
attribute
The buffer attribute holding vector data.
index
The index into the attribute.
Returns: A reference to this vector.
Returns the value of the vector component which matches the given index.
index
The component index. 0 equals to x, 1 equals to y.
Returns: A vector component value.
Computes the Euclidean length (straight-line length) from (0, 0) to (x, y).
Returns: The length of this vector.
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 square length of this vector.
Linearly interpolates between the given vector and this instance, where alpha is the percent distance along the line - alpha = 0 will be this vector, and alpha = 1 will be the given one.
v
The vector to interpolate towards.
alpha
The interpolation factor, typically in the closed interval [0, 1].
Returns: A reference to this vector.
Linearly interpolates between the given vectors, where alpha is the percent distance along the line - alpha = 0 will be first vector, and alpha = 1 will be the second one. The result is stored in this instance.
v1
The first vector.
v2
The second vector.
alpha
The interpolation factor, typically in the closed interval [0, 1].
Returns: A reference to this vector.
Computes the Manhattan distance from the given vector to this instance.
v
The vector to compute the Manhattan distance to.
Returns: The Manhattan distance.
Computes the Manhattan length of this vector.
Returns: The length of this vector.
If this vector's x or y value is less than the given vector's x or y value, replace that value with the corresponding max value.
v
The vector.
Returns: A reference to this vector.
If this vector's x or y value is greater than the given vector's x or y value, replace that value with the corresponding min value.
v
The vector.
Returns: A reference to this vector.
Multiplies the given vector with this instance.
v
The vector to multiply.
Returns: A reference to this vector.
Multiplies the given scalar value with all components of this instance.
scalar
The scalar to multiply.
Returns: A reference to this vector.
Inverts this vector - i.e. sets x = -x and y = -y.
Returns: A reference to this vector.
Converts this vector to a unit vector - that is, sets it equal to a vector with the same direction as this one, but with a vector length of 1.
Returns: A reference to this vector.
Sets each component of this vector to a pseudo-random value between 0 and 1, excluding 1.
Returns: A reference to this vector.
Rotates this vector around the given center by the given angle.
center
The point around which to rotate.
angle
The angle to rotate, in radians.
Returns: A reference to this vector.
The components of this vector are rounded to the nearest integer value
Returns: A reference to this vector.
The components of this vector are rounded towards zero (up if negative, down if positive) to an integer value.
Returns: A reference to this vector.
Sets the vector components.
x
The value of the x component.
y
The value of the y component.
Returns: A reference to this vector.
Allows to set a vector component with an index.
index
The component index. 0 equals to x, 1 equals to y.
value
The value to set.
Returns: A reference to this vector.
Sets this vector to a vector with the same direction as this one, but with the specified length.
length
The new length of this vector.
Returns: A reference to this vector.
Sets the vector components to the same value.
scalar
The value to set for all vector components.
Returns: A reference to this vector.
Sets the vector's x component to the given value
x
The value to set.
Returns: A reference to this vector.
Sets the vector's y component to the given value
y
The value to set.
Returns: A reference to this vector.
Subtracts the given vector from this instance.
v
The vector to subtract.
Returns: A reference to this vector.
Subtracts the given scalar value from all components of this instance.
s
The scalar to subtract.
Returns: A reference to this vector.
Subtracts the given vectors and stores the result in this instance.
a
The first vector.
b
The second vector.
Returns: A reference to this vector.
Writes the components of this vector to the given array. If no array is provided, the method returns a new instance.
array
The target array holding the vector components.
Default is [].
offset
Index of the first element in the array.
Default is 0.
Returns: The vector components.