@aliser/vector2 / Exports
Install with npm.
npm install @aliser/vector2Import as a module.
import { Vector2, RAD2DEG, DEG2RAD } from '@aliser/vector2';Enjoy!
Below you can find the docs for all of this.
Note: the static and the instance methods are mixed. Sorry for that.
- custom interpolation functions for the
static lerp- slide with ease! - maybe some static damping function - it'll make the target vector experience some kind of repulsion (the magnitude of which is controller by some specified function) on approaching the end point.
- getters for directions - up, down, right, west, east, forward, backward, sideways, the 5- and 11- dimensions and other.
- implement the stuff that other libs like this one are providing: reflect, normal, moveTowards.
@aliser/vector2 / Exports / Vector2
- Vector2
- IVector2
- add
- ceil
- clamp
- copy
- div
- floor
- isEquals
- lerp
- mod
- mult
- negate
- normalize
- rotateBy
- rotateTo
- round
- set
- setMag
- sub
- add
- angleBetween
- angleBetweenSigned
- dist
- div
- dot
- fromAngle
- lerp
- mult
- random
- randomWithLength
- sub
- zero
+ new Vector2(x?: number, y?: number): Vector2
Creates an instance.
| Name | Type |
|---|---|
x? |
number |
y? |
number |
Returns: Vector2
+ new Vector2(xy?: number): Vector2
Creates an instance, where 'x' and 'y' are both equal to 'xy'.
| Name | Type |
|---|---|
xy? |
number |
Returns: Vector2
+ new Vector2(): Vector2
Creates an instance, where 'x' and 'y' are both equal to 0.
Returns: Vector2
+ new Vector2(vec?: Vector2): Vector2
Creates an instance, where 'x' and 'y' are equal to 'x' and 'y' of 'vec', respectively.
| Name | Type |
|---|---|
vec? |
Vector2 |
Returns: Vector2
• x: number
• y: number
• 0(): number
Returns: number
alias for 'x'.
• 0(value: number): void
| Name | Type |
|---|---|
value |
number |
Returns: void
alias for 'x'.
• 1(): number
Returns: number
alias for 'y'.
• 1(value: number): void
| Name | Type |
|---|---|
value |
number |
Returns: void
alias for 'y'.
• angle(): number
The signed angle (from -PI to PI radians).
Returns: number
• angle(value: number): void
The signed angle (from -PI to PI radians).
| Name | Type |
|---|---|
value |
number |
Returns: void
• mag(): number
The length.
Note, when setting 'mag' to negative value, both 'x' and 'y' will be set to 0, and the magnitude will be 0.
Returns: number
• mag(value: number): void
The length.
Note, when setting 'mag' to negative value, both 'x' and 'y' will be set to 0, and the magnitude will be 0.
| Name | Type |
|---|---|
value |
number |
Returns: void
• max(): number
Returns: number
the largest of 'x' and 'y'.
• min(): number
Returns: number
the smallest of 'x' and 'y'.
• sqrMag(): number
Returns: number
the length, squared.
• u(): number
Returns: number
alias for 'x'.
• u(value: number): void
| Name | Type |
|---|---|
value |
number |
Returns: void
alias for 'x'.
• v(): number
Returns: number
alias for 'y'.
• v(value: number): void
| Name | Type |
|---|---|
value |
number |
Returns: void
alias for 'y'.
▸ add(deltaX: number): Vector2
Adds 'deltaX' to both 'x' and 'y'.
| Name | Type |
|---|---|
deltaX |
number |
Returns: Vector2
▸ add(deltaX: number, deltaY: number): Vector2
Adds 'deltaX' and 'deltaY' to 'x' and 'y', respectively.
| Name | Type |
|---|---|
deltaX |
number |
deltaY |
number |
Returns: Vector2
Adds 'x' and 'y' of 'vec' to 'x' and 'y', respectively.
| Name | Type |
|---|---|
vec |
Vector2 |
Returns: Vector2
▸ ceil(): Vector2
Does the rounding to the nearest larger integer.
Returns: Vector2
▸ clamp(): Vector2
Restricts 'x' and 'y' from going below 0 or above 1.
Returns: Vector2
▸ clamp(upperBound: number): Vector2
Restricts 'x' and 'y' from going below 0 or above 'upperBound'.
| Name | Type |
|---|---|
upperBound |
number |
Returns: Vector2
▸ clamp(lowerBound: number, upperBound: number): Vector2
Restricts 'x' and 'y' from going below 'lowerBound' or above 'upperBound'.
| Name | Type |
|---|---|
lowerBound |
number |
upperBound |
number |
Returns: Vector2
▸ clamp(lowerBoundX: number, upperBoundX: number, lowerBoundY: number, upperBoundY: number): Vector2
Restricts 'x' from going below 'lowerBoundX' or above 'upperBoundX', and for 'y' from going below 'lowerBoundY' or above 'upperBoundY'.
| Name | Type |
|---|---|
lowerBoundX |
number |
upperBoundX |
number |
lowerBoundY |
number |
upperBoundY |
number |
Returns: Vector2
▸ copy(): Vector2
Returns: Vector2
a copy of this instance.
▸ div(divisor: number): Vector2
Divides both 'x' and 'y' by 'divisor'.
| Name | Type |
|---|---|
divisor |
number |
Returns: Vector2
▸ div(divisorX: number, divisorY: number): Vector2
Divides 'x' and 'y' by 'divisorX' and 'divisorY', respectively.
| Name | Type |
|---|---|
divisorX |
number |
divisorY |
number |
Returns: Vector2
Divides 'x' and 'y' by 'x' and 'y' of vec, respectively.
| Name | Type |
|---|---|
vec |
Vector2 |
Returns: Vector2
▸ floor(): Vector2
Does the rounding to the nearest smaller integer.
Returns: Vector2
▸ isEquals(vec: Vector2): boolean
Checks if 'x' and 'y' are strictly equal to 'x' and 'y', respectively.
| Name | Type |
|---|---|
vec |
Vector2 |
Returns: boolean
▸ lerp(vec1: Vector2, vec2: Vector2, t: number): Vector2
Linearly interpolates between 'vec1' and 'vec2' by 't', setting this instance to result.
| Name | Type |
|---|---|
vec1 |
Vector2 |
vec2 |
Vector2 |
t |
number |
Returns: Vector2
▸ mod(divisor: number): Vector2
Divides both 'x' and 'y' by 'divisor' and sets them to the remainder of that division, respectively.
| Name | Type |
|---|---|
divisor |
number |
Returns: Vector2
▸ mod(divisorX: number, divisorY: number): Vector2
Divides 'x' and 'y' by 'divisorX' and 'divisorY', respectively, and sets them to the remainder of that division, also respectively.
| Name | Type |
|---|---|
divisorX |
number |
divisorY |
number |
Returns: Vector2
▸ mult(multiplier: number): Vector2
Multiplies both 'x' and 'y' by 'multiplier'.
| Name | Type |
|---|---|
multiplier |
number |
Returns: Vector2
▸ mult(multiplierX: number, multiplierY: number): Vector2
Multiplies 'x' and 'y' by 'multiplierX' and 'multiplierY', respectively.
| Name | Type |
|---|---|
multiplierX |
number |
multiplierY |
number |
Returns: Vector2
Multiplies 'x' and 'y' by 'x' and 'y' of 'vec', respectively.
| Name | Type |
|---|---|
vec |
Vector2 |
Returns: Vector2
▸ negate(): Vector2
Inverts both 'x' and 'y' (makes them -'x' and -'y').
Returns: Vector2
▸ normalize(): Vector2
Mangles the length of this instance until it will be 1.
Note, if the length of this instance is 0, than it will not be changed.
Returns: Vector2
▸ rotateBy(angleInRadiansDelta: number): Vector2
Rotates this instance by 'angleInRadiansDelta' radians.
| Name | Type |
|---|---|
angleInRadiansDelta |
number |
Returns: Vector2
▸ rotateBy(vec: Vector2): Vector2
Rotates this instance by the signed angle of 'vec'.
| Name | Type |
|---|---|
vec |
Vector2 |
Returns: Vector2
▸ rotateTo(angleInRadians: number): Vector2
Rotates this instance to 'angleInRadians' radians.
| Name | Type |
|---|---|
angleInRadians |
number |
Returns: Vector2
▸ rotateTo(vec: Vector2): Vector2
Rotates this instance to the angle of 'vec'.
| Name | Type |
|---|---|
vec |
Vector2 |
Returns: Vector2
▸ round(): Vector2
Does the rounding to the nearest integer.
Returns: Vector2
▸ set(x?: number, y?: number): Vector2
Sets 'x' and 'y', respectively.
| Name | Type |
|---|---|
x? |
number |
y? |
number |
Returns: Vector2
▸ set(xy?: number): Vector2
Sets both 'x' and 'y' to 'xy'.
| Name | Type |
|---|---|
xy? |
number |
Returns: Vector2
▸ set(): Vector2
Sets both 'x' and 'y' to 0.
Returns: Vector2
Sets 'x' and 'y' to 'x' and 'y' of 'vec', respectively.
| Name | Type |
|---|---|
vec? |
Vector2 |
Returns: Vector2
▸ setMag(value: number): Vector2
Function alias of 'mag'.
Sets the length.
Note, when setting 'value' is negative, both 'x' and 'y' will be set to 0, and the magnitude will be 0.
| Name | Type |
|---|---|
value |
number |
Returns: Vector2
▸ setMag(vec: Vector2): Vector2
Sets the length to the length of vec.
| Name | Type |
|---|---|
vec |
Vector2 |
Returns: Vector2
▸ sub(delta: number): Vector2
Subtracts 'delta' from both 'x' and 'y'.
| Name | Type |
|---|---|
delta |
number |
Returns: Vector2
▸ sub(deltaX: number, deltaY: number): Vector2
Subtracts 'deltaX' and 'deltaY' from 'x' and 'y', respectively.
| Name | Type |
|---|---|
deltaX |
number |
deltaY |
number |
Returns: Vector2
Subtracts 'x' and 'y' of 'vec' from 'x' and 'y', respectively.
| Name | Type |
|---|---|
vec |
Vector2 |
Returns: Vector2
▸ Staticadd(...vecs: [Vector2, Vector2, ...Vector2[]]): Vector2
Adds 'vecs' together.
| Name | Type |
|---|---|
...vecs |
[Vector2, Vector2, ...Vector2[]] |
Returns: Vector2
▸ StaticangleBetween(vec1: Vector2, vec2: Vector2): number
| Name | Type |
|---|---|
vec1 |
Vector2 |
vec2 |
Vector2 |
Returns: number
an unsigned angle (from 0 to PI radians) between 'vec1' and 'vec2'.
▸ StaticangleBetweenSigned(vec1: Vector2, vec2: Vector2): number
| Name | Type |
|---|---|
vec1 |
Vector2 |
vec2 |
Vector2 |
Returns: number
the smallest signed angle (from -PI to PI radians) between 'vec1' and 'vec2'.
Note, that the resulting angle is the angle that, if applied as a rotation to 'vec1', will result in 'vec2'.
▸ Staticdist(vec1: Vector2, vec2: Vector2): number
| Name | Type |
|---|---|
vec1 |
Vector2 |
vec2 |
Vector2 |
Returns: number
the distance between 'vec1' and 'vec2'.
▸ Staticdiv(...vectors: [Vector2, Vector2, ...Vector2[]]): Vector2
Sequentially divides 'vecs', one by one.
| Name | Type |
|---|---|
...vectors |
[Vector2, Vector2, ...Vector2[]] |
Returns: Vector2
▸ Staticdot(vec1: Vector2, vec2: Vector2): number
| Name | Type |
|---|---|
vec1 |
Vector2 |
vec2 |
Vector2 |
Returns: number
the dot product of 'vec1' and 'vec2'.
▸ StaticfromAngle(angleOfRadians: number, length?: number): Vector2
| Name | Type | Default value |
|---|---|---|
angleOfRadians |
number | - |
length |
number | 1 |
Returns: Vector2
a new instance with angle 'angleOfRadians' radians and length 'length'.
▸ Staticlerp(vecOut: Vector2, vec1: Vector2, vec2: Vector2, t: number): Vector2
Linearly interpolates between 'vec1' and 'vec2' by 't', setting 'vecOut' to result.
| Name | Type |
|---|---|
vecOut |
Vector2 |
vec1 |
Vector2 |
vec2 |
Vector2 |
t |
number |
Returns: Vector2
'vecOut'.
▸ Staticmult(...vecs: [Vector2, Vector2, ...Vector2[]]): Vector2
Multiplies 'vecs' together.
| Name | Type |
|---|---|
...vecs |
[Vector2, Vector2, ...Vector2[]] |
Returns: Vector2
▸ Staticrandom(multiplier?: number): Vector2
| Name | Type | Default value |
|---|---|---|
multiplier |
number | 1 |
Returns: Vector2
a new instance with 'x' and 'y' individually set to some random value from -1 to below 1, multiplied by 'multiplier'.
▸ StaticrandomWithLength(length?: number): Vector2
| Name | Type | Default value |
|---|---|---|
length |
number | 1 |
Returns: Vector2
a new instance with 'x' and 'y' individually set to some random value in such a way that the length of the new instance will be 'length'.
▸ Staticsub(...vecs: [Vector2, Vector2, ...Vector2[]]): Vector2
Sequentially subtracts 'vecs', one by one.
| Name | Type |
|---|---|
...vecs |
[Vector2, Vector2, ...Vector2[]] |
Returns: Vector2
▸ Staticzero(): Vector2
Returns: Vector2
a new instance with both 'x' and 'y' set to 0.
@aliser/vector2 / Exports
• Const DEG2RAD: number
Multiply your degrees by this to convert them to radians.
• Const RAD2DEG: number
Multiply your radians by this to convert them to degrees.