Shaku

Shaku JS

Back To Table of Content

Vector2

Vector2

A simple Vector object for 2d positions.

Kind: global class

new Vector2(x, y)

Create the Vector object.

Param Type Default Description
x number 0 Vector X.
y number 0 Vector Y.

vector2.clone() ⇒ Vector2

Clone the vector.

Kind: instance method of Vector2
Returns: Vector2 - cloned vector.

vector2.set(x, y) ⇒ Vector2

Set vector value.

Kind: instance method of Vector2
Returns: Vector2 - this.

Param Type Description
x Number X component.
y Number Y component.

vector2.copy() ⇒ Vector2

Copy values from other vector into self.

Kind: instance method of Vector2
Returns: Vector2 - this.

vector2.add(Other) ⇒ Vector2

Return a new vector of this + other.

Kind: instance method of Vector2
Returns: Vector2 - result vector.

Param Type Description
Other Number | Vector2 Vector3 or number to add to all components.

vector2.sub(Other) ⇒ Vector2

Return a new vector of this - other.

Kind: instance method of Vector2
Returns: Vector2 - result vector.

Param Type Description
Other Number | Vector2 Vector3 or number to sub from all components.

vector2.div(Other) ⇒ Vector2

Return a new vector of this / other.

Kind: instance method of Vector2
Returns: Vector2 - result vector.

Param Type Description
Other Number | Vector2 Vector3 or number to divide by all components.

vector2.mul(Other) ⇒ Vector2

Return a new vector of this * other.

Kind: instance method of Vector2
Returns: Vector2 - result vector.

Param Type Description
Other Number | Vector2 Vector2 or number to multiply with all components.

vector2.round() ⇒ Vector2

Return a round copy of this vector.

Kind: instance method of Vector2
Returns: Vector2 - result vector.

vector2.floor() ⇒ Vector2

Return a floored copy of this vector.

Kind: instance method of Vector2
Returns: Vector2 - result vector.

vector2.ceil() ⇒ Vector2

Return a ceiled copy of this vector.

Kind: instance method of Vector2
Returns: Vector2 - result vector.

vector2.minSelf(v) ⇒ Vector2

Set self values to be min values between self and a given vector.

Kind: instance method of Vector2
Returns: Vector2 - Self.

Param Type Description
v Vector2 Vector to min with.

vector2.maxSelf(v) ⇒ Vector2

Set self values to be max values between self and a given vector.

Kind: instance method of Vector2
Returns: Vector2 - Self.

Param Type Description
v Vector2 Vector to max with.

vector2.min(v) ⇒ Vector2

Create a clone vector that is the min result between self and a given vector.

Kind: instance method of Vector2
Returns: Vector2 - Result vector.

Param Type Description
v Vector2 Vector to min with.

vector2.max(v) ⇒ Vector2

Create a clone vector that is the max result between self and a given vector.

Kind: instance method of Vector2
Returns: Vector2 - Result vector.

Param Type Description
v Vector2 Vector to max with.

vector2.normalized() ⇒ Vector2

Return a normalized copy of this vector.

Kind: instance method of Vector2
Returns: Vector2 - result vector.

vector2.rotatedByRadians(radians) ⇒ Vector2

Get a copy of this vector rotated by radians.

Kind: instance method of Vector2
Returns: Vector2 - New vector with the length of this vector and direction rotated by given radians.

Param Type Description
radians Number Radians to rotate by.

vector2.rotatedByDegrees(degrees) ⇒ Vector2

Get a copy of this vector rotated by degrees.

Kind: instance method of Vector2
Returns: Vector2 - New vector with the length of this vector and direction rotated by given degrees.

Param Type Description
degrees Number Degrees to rotate by.

vector2.addSelf(Other) ⇒ Vector2

Add other vector values to self.

Kind: instance method of Vector2
Returns: Vector2 - this.

Param Type Description
Other Number | Vector2 Vector or number to add.

vector2.subSelf(Other) ⇒ Vector2

Sub other vector values from self.

Kind: instance method of Vector2
Returns: Vector2 - this.

Param Type Description
Other Number | Vector2 Vector or number to substract.

vector2.divSelf(Other) ⇒ Vector2

Divide this vector by other vector values.

Kind: instance method of Vector2
Returns: Vector2 - this.

Param Type Description
Other Number | Vector2 Vector or number to divide by.

vector2.mulSelf(Other) ⇒ Vector2

Multiply this vector by other vector values.

Kind: instance method of Vector2
Returns: Vector2 - this.

Param Type Description
Other Number | Vector2 Vector or number to multiply by.

vector2.roundSelf() ⇒ Vector2

Round self.

Kind: instance method of Vector2
Returns: Vector2 - this.

vector2.floorSelf() ⇒ Vector2

Floor self.

Kind: instance method of Vector2
Returns: Vector2 - this.

vector2.ceilSelf() ⇒ Vector2

Ceil self.

Kind: instance method of Vector2
Returns: Vector2 - this.

vector2.normalizeSelf() ⇒ Vector2

Return a normalized copy of this vector.

Kind: instance method of Vector2
Returns: Vector2 - this.

vector2.equals(other) ⇒ Boolean

Return if vector equals another vector.

Kind: instance method of Vector2
Returns: Boolean - if vectors are equal.

Param Type Description
other Vector2 Other vector to compare to.

vector2.approximate(other, threshold) ⇒ Boolean

Return if vector approximately equals another vector.

Kind: instance method of Vector2
Returns: Boolean - if vectors are equal.

Param Type Description
other Vector2 Other vector to compare to.
threshold Number Distance threshold to consider as equal. Defaults to 1.

vector2.length() ⇒ Number

Return vector length (aka magnitude).

Kind: instance method of Vector2
Returns: Number - Vector length.

vector2.scaled() ⇒ Vector2

Return a copy of this vector multiplied by a factor.

Kind: instance method of Vector2
Returns: Vector2 - result vector.

vector2.degreesTo(other) ⇒ Number

Get degrees between this vector and another vector.

Kind: instance method of Vector2
Returns: Number - Angle between vectors in degrees.

Param Type Description
other Vector2 Other vector.

vector2.radiansTo(other) ⇒ Number

Get radians between this vector and another vector.

Kind: instance method of Vector2
Returns: Number - Angle between vectors in radians.

Param Type Description
other Vector2 Other vector.

vector2.wrappedDegreesTo(other) ⇒ Number

Get degrees between this vector and another vector. Return values between 0 to 360.

Kind: instance method of Vector2
Returns: Number - Angle between vectors in degrees.

Param Type Description
other Vector2 Other vector.

vector2.wrappedRadiansTo(other) ⇒ Number

Get radians between this vector and another vector. Return values between 0 to PI2.

Kind: instance method of Vector2
Returns: Number - Angle between vectors in radians.

Param Type Description
other Vector2 Other vector.

vector2.distanceTo(other) ⇒ Number

Calculate distance between this vector and another vectors.

Kind: instance method of Vector2
Returns: Number - Distance between vectors.

Param Type Description
other Vector2 Other vector.

vector2.distanceToSquared(other) ⇒ Number

Calculate squared distance between this vector and another vector.

Kind: instance method of Vector2
Returns: Number - Distance between vectors.

Param Type Description
other Vector2 Other vector.

vector2.clamp(min, max) ⇒ Vector2

Return a clone and clamp its values to be between min and max.

Kind: instance method of Vector2
Returns: Vector2 - Clamped vector.

Param Type Description
min Vector2 Min vector.
max Vector2 Max vector.

vector2.clampSelf(min, max) ⇒ Vector2

Clamp this vector values to be between min and max.

Kind: instance method of Vector2
Returns: Vector2 - Self.

Param Type Description
min Vector2 Min vector.
max Vector2 Max vector.

vector2.dot(other) ⇒ Number

Calculate the dot product with another vector.

Kind: instance method of Vector2
Returns: Number - Dot product value.

Param Type Description
other Vector2 Vector to calculate dot with.

vector2.getDegrees() ⇒ Number

Get vector’s angle in degrees.

Kind: instance method of Vector2
Returns: Number - Vector angle in degrees.

vector2.getRadians() ⇒ Number

Get vector’s angle in radians.

Kind: instance method of Vector2
Returns: Number - Vector angle in degrees.

vector2.string()

Convert to string.

Kind: instance method of Vector2

vector2.toArray() ⇒ Array.<Number>

Convert to array of numbers.

Kind: instance method of Vector2
Returns: Array.<Number> - Vector components as array.

vector2.toDict(minimized) ⇒ \*

Convert to dictionary.

Kind: instance method of Vector2
Returns: \* - Dictionary with {x,y}

Param Type Description
minimized Boolean If true, will not include keys that their values are 0. You can use fromDict on minimized dicts.

Vector2.zeroReadonly

Vector with 0,0 values as a frozen shared object. Be careful not to try and change it.

Kind: static property of Vector2

Vector2.oneReadonly

Vector with 1,1 values as a frozen shared object. Be careful not to try and change it.

Kind: static property of Vector2

Vector2.halfReadonly

Vector with 0.5,0.5 values as a frozen shared object. Be careful not to try and change it.

Kind: static property of Vector2

Vector2.leftReadonly

Vector with -1,0 values as a frozen shared object. Be careful not to try and change it.

Kind: static property of Vector2

Vector2.rightReadonly

Vector with 1,0 values as a frozen shared object. Be careful not to try and change it.

Kind: static property of Vector2

Vector2.upReadonly

Vector with 0,1 values as a frozen shared object. Be careful not to try and change it.

Kind: static property of Vector2

Vector2.downReadonly

Vector with 0,-1 values as a frozen shared object. Be careful not to try and change it.

Kind: static property of Vector2

Vector2.zero() ⇒ Vector2

Get vector (0,0).

Kind: static method of Vector2
Returns: Vector2 - result vector.

Vector2.one() ⇒ Vector2

Get vector with 1,1 values.

Kind: static method of Vector2
Returns: Vector2 - result vector.

Vector2.half() ⇒ Vector2

Get vector with 0.5,0.5 values.

Kind: static method of Vector2
Returns: Vector2 - result vector.

Vector2.left() ⇒ Vector2

Get vector with -1,0 values.

Kind: static method of Vector2
Returns: Vector2 - result vector.

Vector2.right() ⇒ Vector2

Get vector with 1,0 values.

Kind: static method of Vector2
Returns: Vector2 - result vector.

Vector2.up() ⇒ Vector2

Get vector with 0,-1 values.

Kind: static method of Vector2
Returns: Vector2 - result vector.

Vector2.down() ⇒ Vector2

Get vector with 0,1 values.

Kind: static method of Vector2
Returns: Vector2 - result vector.

Vector2.random() ⇒ Vector2

Get a random vector with length of 1.

Kind: static method of Vector2
Returns: Vector2 - result vector.

Vector2.fromDegrees(degrees) ⇒ Vector2

Get vector from degrees.

Kind: static method of Vector2
Returns: Vector2 - result vector.

Param Type Description
degrees Number Angle to create vector from (0 = vector pointing right).

Vector2.fromRadians(radians) ⇒ Vector2

Get vector from radians.

Kind: static method of Vector2
Returns: Vector2 - result vector.

Param Type Description
radians Number Angle to create vector from (0 = vector pointing right).

Vector2.lerp(p1, p2, a) ⇒ Vector2

Lerp between two vectors.

Kind: static method of Vector2
Returns: Vector2 - result vector.

Param Type Description
p1 Vector2 First vector.
p2 Vector2 Second vector.
a Number Lerp factor (0.0 - 1.0).

Vector2.degreesBetween(p1, p2) ⇒ Number

Get degrees between two vectors. Return values between -180 to 180.

Kind: static method of Vector2
Returns: Number - Angle between vectors in degrees.

Param Type Description
p1 Vector2 First vector.
p2 Vector2 Second vector.

Vector2.radiansBetween(p1, p2) ⇒ Number

Get radians between two vectors. Return values between -PI to PI.

Kind: static method of Vector2
Returns: Number - Angle between vectors in radians.

Param Type Description
p1 Vector2 First vector.
p2 Vector2 Second vector.

Vector2.wrappedDegreesBetween(p1, p2) ⇒ Number

Get degrees between two vectors. Return values between 0 to 360.

Kind: static method of Vector2
Returns: Number - Angle between vectors in degrees.

Param Type Description
p1 Vector2 First vector.
p2 Vector2 Second vector.

Vector2.wrappedRadiansBetween(p1, p2) ⇒ Number

Get radians between two vectors. Return values between 0 to PI2.

Kind: static method of Vector2
Returns: Number - Angle between vectors in radians.

Param Type Description
p1 Vector2 First vector.
p2 Vector2 Second vector.

Vector2.distance(p1, p2) ⇒ Number

Calculate distance between two vectors.

Kind: static method of Vector2
Returns: Number - Distance between vectors.

Param Type Description
p1 Vector2 First vector.
p2 Vector2 Second vector.

Vector2.cross(p1, p2) ⇒ Number

Return cross product between two vectors.

Kind: static method of Vector2
Returns: Number - Cross between vectors.

Param Type Description
p1 Vector2 First vector.
p2 Vector2 Second vector.

Vector2.dot(p1, p2) ⇒ Number

Return dot product between two vectors.

Kind: static method of Vector2
Returns: Number - Dot between vectors.

Param Type Description
p1 Vector2 First vector.
p2 Vector2 Second vector.

Vector2.parse(str) ⇒ Vector2

Parse and return a vector object from string in the form of “x,y”.

Kind: static method of Vector2
Returns: Vector2 - Parsed vector.

Param Type Description
str String String to parse.

Vector2.fromArray(arr) ⇒ Vector2

Create vector from array of numbers.

Kind: static method of Vector2
Returns: Vector2 - Vector instance.

Param Type Description
arr Array.<Number> Array of numbers to create vector from.

Vector2.fromDict(data) ⇒ Vector2

Create vector from a dictionary.

Kind: static method of Vector2
Returns: Vector2 - Newly created vector.

Param Type Description
data \* Dictionary with {x,y}.