What is gl-vec2?
The gl-vec2 npm package provides a set of functions for performing vector operations in 2D space. It is part of the gl-matrix library, which is widely used for matrix and vector math in WebGL applications. The package includes functions for creating, manipulating, and performing mathematical operations on 2D vectors.
What are gl-vec2's main functionalities?
Vector Creation
This feature allows you to create a new 2D vector. The `create` function initializes a vector with default values [0, 0].
const vec2 = require('gl-vec2');
let v = vec2.create(); // Creates a new, empty vector
console.log(v); // Output: [0, 0]
Vector Addition
This feature allows you to add two 2D vectors. The `add` function takes two vectors and returns their sum.
const vec2 = require('gl-vec2');
let v1 = vec2.fromValues(1, 2);
let v2 = vec2.fromValues(3, 4);
let result = vec2.add([], v1, v2);
console.log(result); // Output: [4, 6]
Vector Scaling
This feature allows you to scale a 2D vector by a scalar value. The `scale` function multiplies each component of the vector by the scalar.
const vec2 = require('gl-vec2');
let v = vec2.fromValues(1, 2);
let scaled = vec2.scale([], v, 2);
console.log(scaled); // Output: [2, 4]
Vector Dot Product
This feature allows you to compute the dot product of two 2D vectors. The `dot` function returns the scalar product of the vectors.
const vec2 = require('gl-vec2');
let v1 = vec2.fromValues(1, 2);
let v2 = vec2.fromValues(3, 4);
let dotProduct = vec2.dot(v1, v2);
console.log(dotProduct); // Output: 11
Vector Length
This feature allows you to compute the length (magnitude) of a 2D vector. The `length` function returns the Euclidean length of the vector.
const vec2 = require('gl-vec2');
let v = vec2.fromValues(3, 4);
let length = vec2.length(v);
console.log(length); // Output: 5
Other packages similar to gl-vec2
gl-matrix
The gl-matrix package is a comprehensive library for matrix and vector operations in WebGL. It includes support for 2D, 3D, and 4D vectors, as well as various types of matrices. It is more extensive than gl-vec2, which focuses solely on 2D vectors.
vec2
The vec2 package provides basic 2D vector operations similar to gl-vec2. However, it is less feature-rich and not as optimized for performance as gl-vec2, which is part of the gl-matrix suite.
vector2d
The vector2d package offers a simple API for 2D vector math. It includes basic operations like addition, subtraction, scaling, and normalization. While it covers similar functionalities, it is not as optimized for WebGL use cases as gl-vec2.
gl-vec2
Part of a fork of @toji's
gl-matrix split into smaller pieces: this
package contains glMatrix.vec2
.
Usage
vec2 = require('gl-vec2')
Will load all of the module's functionality and expose it on a single
object. Note that any of the methods may also be required directly
from their files.
For example, the following are equivalent:
var scale = require('gl-vec2').scale
var scale = require('gl-vec2/scale')
API
add(out:vec2, a:vec2, b:vec2)
Adds two vec2's
ceil(out:vec2, a:vec2)
Math.ceil
the components of a vec2
clone(a:vec2)
Creates a new vec2 initialized with values from an existing vector
copy(out:vec2, a:vec2)
Copy the values from one vec2 to another
create()
Creates a new, empty vec2
cross(out:vec3, a:vec2, b:vec2)
Computes the cross product of two vec2's
Note that the cross product must by definition produce a 3D vector
distance(a:vec2, b:vec2)
Calculates the euclidian distance between two vec2's. Aliased as dist
.
divide(out:vec2, a:vec2, b:vec2)
Divides two vec2's. Aliased as div
.
dot(a:vec2, b:vec2)
Calculates the dot product of two vec2's
equals(a:vec2, b:vec2)
Returns whether or not the vectors have approximately the same elements in the same position.
exactEquals(a:vec2, b:vec2)
Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===)
floor(out:vec2, a:vec2)
Math.floor
the components of a vec2
forEach(a:Array, stride:Number, offset:Number, count:Number, fn:Function, [arg]:Object)
Perform some operation over an array of vec2s.
fromValues(x:Number, y:Number)
Creates a new vec2 initialized with the given values
inverse(out:vec2, a:vec2)
Returns the inverse of the components of a vec2
length(a:vec2)
Calculates the length of a vec2. Aliased as len
.
lerp(out:vec2, a:vec2, b:vec2, t:Number)
Performs a linear interpolation between two vec2's
limit(out:vec2, a:vec2, max:Number)
Limit the magnitude of this vector to the value used for the max
parameter
max(out:vec2, a:vec2, b:vec2)
Returns the maximum of two vec2's
min(out:vec2, a:vec2, b:vec2)
Returns the minimum of two vec2's
multiply(out:vec2, a:vec2, b:vec2)
Multiplies two vec2's. Aliased as mul
.
negate(out:vec2, a:vec2)
Negates the components of a vec2
normalize(out:vec2, a:vec2)
Normalize a vec2
random(out:vec2, [scale]:Number)
Generates a random vector with the given scale
round(out:vec2, a:vec2)
Math.round
the components of a vec2
rotate(out:vec2, a:vec2, angle:Number)
Rotates a vec2 by an angle (in radians)
scale(out:vec2, a:vec2, b:Number)
Scales a vec2 by a scalar number
scaleAndAdd(out:vec2, a:vec2, b:vec2, scale:Number)
Adds two vec2's after scaling the second operand by a scalar value
set(out:vec2, x:Number, y:Number)
Set the components of a vec2 to the given values
squaredDistance(a:vec2, b:vec2)
Calculates the squared euclidian distance between two vec2's. Aliased as sqrDist
.
squaredLength(a:vec2)
Calculates the squared length of a vec2. Aliased as sqrLen
.
subtract(out:vec2, a:vec2, b:vec2)
Subtracts vector b from vector a. Aliased as sub
.
transformMat2(out:vec2, a:vec2, m:mat2)
Transforms the vec2 with a mat2
transformMat2d(out:vec2, a:vec2, m:mat2d)
Transforms the vec2 with a mat2d
transformMat3(out:vec2, a:vec2, m:mat3)
Transforms the vec2 with a mat3
3rd vector component is implicitly '1'
transformMat4(out:vec2, a:vec2, m:mat4)
Transforms the vec2 with a mat4
3rd vector component is implicitly '0'
4th vector component is implicitly '1'
License
zlib. See LICENSE.md for details.