Vector(​[x][, y])

A simple 2d vector object. Takes either separate x and y coordinates or a Vector-like object.

let { Vector } = kontra;

let vector = Vector(100, 200);
let vector2 = Vector({x: 100, y: 200});
import { Vector } from 'path/to/kontra.mjs';

let vector = Vector(100, 200);
let vector2 = Vector({x: 100, y: 200});
import { Vector } from 'kontra';

let vector = Vector(100, 200);
let vector2 = Vector({x: 100, y: 200});

Vector Parameters

x Optional

Number or Object. X coordinate of the vector or a Vector-like object. If passing an object, the y param is ignored. Defaults to 0.

y Optional

Number. Y coordinate of the vector. Defaults to 0.

Table of Contents

Vector​.add(​vector)

Calculate the addition of the current vector with the given vector.

add Parameters

vector

Vector or Object. Vector to add to the current Vector.

add Return value

Vector. A new Vector instance whose value is the addition of the two vectors.

Vector​.angle(​vector)

Calculate the angle (in radians) between the current vector and the given vector. Requires the Vector dot and length functions.

angle Parameters

vector

Vector. Vector to calculate the angle between.

angle Return value

Number. The angle (in radians) between the two vectors.

Vector​.clamp(​xMin, yMin, xMax, yMax)

Clamp the Vector between two points, preventing x and y from going below or above the minimum and maximum values. Perfect for keeping a sprite from going outside the game boundaries.

let { Vector } = kontra;

let vector = Vector(100, 200);
vector.clamp(0, 0, 200, 300);

vector.x += 200;
console.log(vector.x);  //=> 200

vector.y -= 300;
console.log(vector.y);  //=> 0

vector.add({x: -500, y: 500});
console.log(vector);    //=> {x: 0, y: 300}
import { Vector } from 'path/to/kontra.mjs';

let vector = Vector(100, 200);
vector.clamp(0, 0, 200, 300);

vector.x += 200;
console.log(vector.x);  //=> 200

vector.y -= 300;
console.log(vector.y);  //=> 0

vector.add({x: -500, y: 500});
console.log(vector);    //=> {x: 0, y: 300}
import { Vector } from 'kontra';

let vector = Vector(100, 200);
vector.clamp(0, 0, 200, 300);

vector.x += 200;
console.log(vector.x);  //=> 200

vector.y -= 300;
console.log(vector.y);  //=> 0

vector.add({x: -500, y: 500});
console.log(vector);    //=> {x: 0, y: 300}

clamp Parameters

xMin

Number. Minimum x value.

yMin

Number. Minimum y value.

xMax

Number. Maximum x value.

yMax

Number. Maximum y value.

Vector​.direction(​)

Calculate the angle (in radians) of the current vector.

direction Return value

Number. The angle (in radians) of the vector.

Vector​.distance(​vector)

Calculate the distance between the current vector and the given vector.

distance Parameters

vector

Vector or Object. Vector to calculate the distance between.

distance Return value

Number. The distance between the two vectors.

Vector​.dot(​vector)

Calculate the dot product of the current vector with the given vector.

dot Parameters

vector

Vector or Object. Vector to dot product against.

dot Return value

Number. The dot product of the vectors.

Vector​.length​(​)

Calculate the length (magnitude) of the Vector.

length​ Return value

Number. The length of the vector.

Vector​.normalize(​)

Calculate the normalized value of the current vector. Requires the Vector length function.

normalize Return value

Vector. A new Vector instance whose value is the normalized vector.

Vector​.scale(​value)

Calculate the multiple of the current vector by a value.

scale Parameters

value

Number. Value to scale the current Vector.

scale Return value

Vector. A new Vector instance whose value is multiplied by the scalar.

Vector​.set(​vector)

Set the x and y coordinate of the vector.

set Parameters

vector

Vector or Object. Vector to set coordinates from.

Vector​.subtract(​vector)

Calculate the subtraction of the current vector with the given vector.

subtract Parameters

vector

Vector or Object. Vector to subtract from the current Vector.

subtract Return value

Vector. A new Vector instance whose value is the subtraction of the two vectors.

Vector​.x

Number. coordinate of the vector.

Vector​.y

Number. coordinate of the vector.