Vector3.js is a 3D vector library for JavaScript, providing a variety of vector operations used in 3D graphics, physics simulations, and geometric computations.
- Basic vector operations: addition, subtraction, scaling, negation
- Geometric functions: dot product, cross product, projection
- Utility functions: normalization, magnitude, distance, linear interpolation (lerp)
- Matrix transformations and function applications on vectors
- Support for creating vectors from arrays or objects
You can install Vector3.js via npm:
npm install @rawify/vector3Or with yarn:
yarn add @rawify/vector3Alternatively, download or clone the repository:
git clone https://github.com/rawify/Vector3.jsInclude the vector3.min.js file in your project:
<scriptsrc="path/to/vector3.min.js"></script>Or in a Node.js project:
constVector3=require('@rawify/vector3');or
importVector3from'@rawify/vector3';Vectors can be created using new Vector3 or the Vector3 function:
letv1=Vector3(1,2,3);letv2=newVector3(4,5,6);You can also initialize vectors from arrays or objects:
letv3=newVector3([1,2,3]);letv4=newVector3({x: 4,y: 5,z: 6});Adds the vector v to the current vector.
letv1=newVector3(1,2,3);letv2=newVector3(4,5,6);letresult=v1.add(v2);//{x: 5, y: 7, z: 9}Subtracts the vector v from the current vector.
letresult=v1.sub(v2);//{x: -3, y: -3, z: -3}Negates the current vector (flips the direction).
letresult=v1.neg();//{x: -1, y: -2, z: -3}Scales the current vector by a scalar s.
letresult=v1.scale(2);//{x: 2, y: 4, z: 6}Calculates the Hadamard (element-wise) product of the current vector and v.
letresult=v1.prod(v2);//{x: 4, y: 10, z: 18}Computes the dot product between the current vector and v.
letresult=v1.dot(v2);// 32Calculates the 3D cross product between the current vector and v.
letresult=v1.cross(v2);//{x: -3, y: 6, z: -3}Projects the current vector onto the vector v using vector projection.
letresult=v1.projectTo(v2);// Projection of v1 onto v2Finds the orthogonal vector rejection of the current vector from the vector v.
Determines the vector reflection of the current vector across the vector n.
Determines the vector refraction of the current unit vector across a surface with unit normaln, using the index ratio η = ηin / ηout (like from air η_in=1.0 to water η_out=1.33).
letn=newVector3(0,1,0);// Surface normal pointing upleteta=1.0/1.33;// Air to glassletresult=v1.refract(n,eta);// Refraction of v1 across nReturns a new unit vector representing the refracted direction, or null if total internal reflection occurs.
Returns the magnitude or length (Euclidean norm) of the current vector.
letresult=v1.norm();// 3.741Returns the squared magnitude or length (norm squared) of the current vector.
letresult=v1.norm2();// 14Returns a normalized vector (unit vector) of the current vector.
letresult=v1.normalize();//{x: 0.267, y: 0.534, z: 0.801}Calculates the Euclidean distance between the current vector and v.
letresult=v1.distance(v2);// 5.196Sets the values of the current vector to match the vector v.
v1.set(v2);// v1 is now{x: 4, y: 5, z: 6}Rotates the vector around the X-axis by the given angle (in radians):
letv=newVector3(1,2,3);v.rotateX(Math.PI/2);// Rotates v 90° around the X-axisRotates the vector around the Y-axis by the given angle (in radians):
letv=newVector3(1,2,3);v.rotateY(Math.PI/2);// Rotates v 90° around the Y-axisRotates the vector around the Z-axis by the given angle (in radians):
letv=newVector3(1,2,3);v.rotateZ(Math.PI/2);// Rotates v 90° around the Z-axisApplies a transformation matrix M to the current vector.
letmatrix=[[1,0,0,0],[0,1,0,0],[0,0,1,0]];letresult=v1.applyMatrix(matrix);// Applies matrix transformationIf you need to make more CSS related matrix transforms, have a look at UnifiedTransform.js.
Applies a function fn (such as Math.abs, Math.min, Math.max) to the components of the current vector and an optional vector v.
letresult1=v1.apply(Math.min,v2);// Determines the minimum of v1 and v2 on each componentletresult2=v1.apply(Math.max,v2);// Determines the maximum of v1 and v2 on each componentletresult3=v1.apply(Math.round);// Rounds the components of the vectorletresult4=v1.apply(Math.floor);// Floors the components of the vectorletresult4=v1.apply(x=>Math.min(upper,Math.max(lower,x)));// Clamps the component to the interval [lower, upper]Returns the current vector as an array [x, y, z].
letresult=v1.toArray();// [1, 2, 3]Returns a clone of the current vector.
letresult=v1.clone();// A new vector with the same x, y, and z values as v1Checks if the current vector is equal to the vector v.
letresult=v1.equals(v2);// falseDetermines if the current vector is a normalized unit vector.
Performs a linear interpolation between the current vector and v by the factor t.
letresult=v1.lerp(v2,0.5);//{x: 2.5, y: 3.5, z: 4.5}Gets a string representation of the current vector.
Generates a vector with random x, y, and z values between 0 and 1.
letrandomVector=Vector3.random();//{x: 0.67, y: 0.45, z: 0.12}Creates a vector from two points a and b.
letresult=Vector3.fromPoints({x: 1,y: 1,z: 1},{x: 4,y: 5,z: 6});//{x: 3, y: 4, z: 5}Given a triangle (A, B, C) and a barycentric coordinate (u, v[, w = 1 - u - v]) calculate the cartesian coordinate in R³.
Like all my libraries, Vector3.js is written to minimize size after compression with Google Closure Compiler in advanced mode. The code style is optimized to maximize compressibility. If you extend the library, please preserve this style.
After cloning the Git repository run:
npm install npm run build Testing the source against the shipped test suite is as easy as
npm run test Copyright (c) 2025, Robert Eisele Licensed under the MIT license.