npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aliser/vector2

v2.0.0

Published

Some basic functionality to represent and work with 2D vectors.

Downloads

8

Readme

@aliser/vector2 / Exports

Build Status Coverage Status

Usage

Install with npm.

npm install @aliser/vector2

Import 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.

TODO

  • [ ] 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.

Classes

@aliser/vector2 / Exports / Vector2

Class: Vector2

Hierarchy

  • Vector2

Implements

  • IVector2

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

+ new Vector2(x?: number, y?: number): Vector2

Creates an instance.

Parameters:

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'.

Parameters:

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.

Parameters:

Name | Type | ------ | ------ | vec? | Vector2 |

Returns: Vector2

Properties

x

x: number


y

y: number

Accessors

0

0(): number

Returns: number

alias for 'x'.

0(value: number): void

Parameters:

Name | Type | ------ | ------ | value | number |

Returns: void

alias for 'x'.


1

1(): number

Returns: number

alias for 'y'.

1(value: number): void

Parameters:

Name | Type | ------ | ------ | value | number |

Returns: void

alias for 'y'.


angle

angle(): number

The signed angle (from -PI to PI radians).

Returns: number

angle(value: number): void

The signed angle (from -PI to PI radians).

Parameters:

Name | Type | ------ | ------ | value | number |

Returns: void


mag

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.

Parameters:

Name | Type | ------ | ------ | value | number |

Returns: void


max

max(): number

Returns: number

the largest of 'x' and 'y'.


min

min(): number

Returns: number

the smallest of 'x' and 'y'.


sqrMag

sqrMag(): number

Returns: number

the length, squared.


u

u(): number

Returns: number

alias for 'x'.

u(value: number): void

Parameters:

Name | Type | ------ | ------ | value | number |

Returns: void

alias for 'x'.


v

v(): number

Returns: number

alias for 'y'.

v(value: number): void

Parameters:

Name | Type | ------ | ------ | value | number |

Returns: void

alias for 'y'.

Methods

add

add(deltaX: number): Vector2

Adds 'deltaX' to both 'x' and 'y'.

Parameters:

Name | Type | ------ | ------ | deltaX | number |

Returns: Vector2

add(deltaX: number, deltaY: number): Vector2

Adds 'deltaX' and 'deltaY' to 'x' and 'y', respectively.

Parameters:

Name | Type | ------ | ------ | deltaX | number | deltaY | number |

Returns: Vector2

add(vec: Vector2): Vector2

Adds 'x' and 'y' of 'vec' to 'x' and 'y', respectively.

Parameters:

Name | Type | ------ | ------ | vec | Vector2 |

Returns: Vector2


ceil

ceil(): Vector2

Does the rounding to the nearest larger integer.

Returns: Vector2


clamp

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'.

Parameters:

Name | Type | ------ | ------ | upperBound | number |

Returns: Vector2

clamp(lowerBound: number, upperBound: number): Vector2

Restricts 'x' and 'y' from going below 'lowerBound' or above 'upperBound'.

Parameters:

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'.

Parameters:

Name | Type | ------ | ------ | lowerBoundX | number | upperBoundX | number | lowerBoundY | number | upperBoundY | number |

Returns: Vector2


copy

copy(): Vector2

Returns: Vector2

a copy of this instance.


div

div(divisor: number): Vector2

Divides both 'x' and 'y' by 'divisor'.

Parameters:

Name | Type | ------ | ------ | divisor | number |

Returns: Vector2

div(divisorX: number, divisorY: number): Vector2

Divides 'x' and 'y' by 'divisorX' and 'divisorY', respectively.

Parameters:

Name | Type | ------ | ------ | divisorX | number | divisorY | number |

Returns: Vector2

div(vec: Vector2): Vector2

Divides 'x' and 'y' by 'x' and 'y' of vec, respectively.

Parameters:

Name | Type | ------ | ------ | vec | Vector2 |

Returns: Vector2


floor

floor(): Vector2

Does the rounding to the nearest smaller integer.

Returns: Vector2


isEquals

isEquals(vec: Vector2): boolean

Checks if 'x' and 'y' are strictly equal to 'x' and 'y', respectively.

Parameters:

Name | Type | ------ | ------ | vec | Vector2 |

Returns: boolean


lerp

lerp(vec1: Vector2, vec2: Vector2, t: number): Vector2

Linearly interpolates between 'vec1' and 'vec2' by 't', setting this instance to result.

Parameters:

Name | Type | ------ | ------ | vec1 | Vector2 | vec2 | Vector2 | t | number |

Returns: Vector2


mod

mod(divisor: number): Vector2

Divides both 'x' and 'y' by 'divisor' and sets them to the remainder of that division, respectively.

Parameters:

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.

Parameters:

Name | Type | ------ | ------ | divisorX | number | divisorY | number |

Returns: Vector2


mult

mult(multiplier: number): Vector2

Multiplies both 'x' and 'y' by 'multiplier'.

Parameters:

Name | Type | ------ | ------ | multiplier | number |

Returns: Vector2

mult(multiplierX: number, multiplierY: number): Vector2

Multiplies 'x' and 'y' by 'multiplierX' and 'multiplierY', respectively.

Parameters:

Name | Type | ------ | ------ | multiplierX | number | multiplierY | number |

Returns: Vector2

mult(vec: Vector2): Vector2

Multiplies 'x' and 'y' by 'x' and 'y' of 'vec', respectively.

Parameters:

Name | Type | ------ | ------ | vec | Vector2 |

Returns: Vector2


negate

negate(): Vector2

Inverts both 'x' and 'y' (makes them -'x' and -'y').

Returns: Vector2


normalize

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

rotateBy(angleInRadiansDelta: number): Vector2

Rotates this instance by 'angleInRadiansDelta' radians.

Parameters:

Name | Type | ------ | ------ | angleInRadiansDelta | number |

Returns: Vector2

rotateBy(vec: Vector2): Vector2

Rotates this instance by the signed angle of 'vec'.

Parameters:

Name | Type | ------ | ------ | vec | Vector2 |

Returns: Vector2


rotateTo

rotateTo(angleInRadians: number): Vector2

Rotates this instance to 'angleInRadians' radians.

Parameters:

Name | Type | ------ | ------ | angleInRadians | number |

Returns: Vector2

rotateTo(vec: Vector2): Vector2

Rotates this instance to the angle of 'vec'.

Parameters:

Name | Type | ------ | ------ | vec | Vector2 |

Returns: Vector2


round

round(): Vector2

Does the rounding to the nearest integer.

Returns: Vector2


set

set(x?: number, y?: number): Vector2

Sets 'x' and 'y', respectively.

Parameters:

Name | Type | ------ | ------ | x? | number | y? | number |

Returns: Vector2

set(xy?: number): Vector2

Sets both 'x' and 'y' to 'xy'.

Parameters:

Name | Type | ------ | ------ | xy? | number |

Returns: Vector2

set(): Vector2

Sets both 'x' and 'y' to 0.

Returns: Vector2

set(vec?: Vector2): Vector2

Sets 'x' and 'y' to 'x' and 'y' of 'vec', respectively.

Parameters:

Name | Type | ------ | ------ | vec? | Vector2 |

Returns: Vector2


setMag

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.

Parameters:

Name | Type | ------ | ------ | value | number |

Returns: Vector2

setMag(vec: Vector2): Vector2

Sets the length to the length of vec.

Parameters:

Name | Type | ------ | ------ | vec | Vector2 |

Returns: Vector2


sub

sub(delta: number): Vector2

Subtracts 'delta' from both 'x' and 'y'.

Parameters:

Name | Type | ------ | ------ | delta | number |

Returns: Vector2

sub(deltaX: number, deltaY: number): Vector2

Subtracts 'deltaX' and 'deltaY' from 'x' and 'y', respectively.

Parameters:

Name | Type | ------ | ------ | deltaX | number | deltaY | number |

Returns: Vector2

sub(vec: Vector2): Vector2

Subtracts 'x' and 'y' of 'vec' from 'x' and 'y', respectively.

Parameters:

Name | Type | ------ | ------ | vec | Vector2 |

Returns: Vector2


add

Staticadd(...vecs: [Vector2, Vector2, ...Vector2[]]): Vector2

Adds 'vecs' together.

Parameters:

Name | Type | ------ | ------ | ...vecs | [Vector2, Vector2, ...Vector2[]] |

Returns: Vector2


angleBetween

StaticangleBetween(vec1: Vector2, vec2: Vector2): number

Parameters:

Name | Type | ------ | ------ | vec1 | Vector2 | vec2 | Vector2 |

Returns: number

an unsigned angle (from 0 to PI radians) between 'vec1' and 'vec2'.


angleBetweenSigned

StaticangleBetweenSigned(vec1: Vector2, vec2: Vector2): number

Parameters:

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'.


dist

Staticdist(vec1: Vector2, vec2: Vector2): number

Parameters:

Name | Type | ------ | ------ | vec1 | Vector2 | vec2 | Vector2 |

Returns: number

the distance between 'vec1' and 'vec2'.


div

Staticdiv(...vectors: [Vector2, Vector2, ...Vector2[]]): Vector2

Sequentially divides 'vecs', one by one.

Parameters:

Name | Type | ------ | ------ | ...vectors | [Vector2, Vector2, ...Vector2[]] |

Returns: Vector2


dot

Staticdot(vec1: Vector2, vec2: Vector2): number

Parameters:

Name | Type | ------ | ------ | vec1 | Vector2 | vec2 | Vector2 |

Returns: number

the dot product of 'vec1' and 'vec2'.


fromAngle

StaticfromAngle(angleOfRadians: number, length?: number): Vector2

Parameters:

Name | Type | Default value | ------ | ------ | ------ | angleOfRadians | number | - | length | number | 1 |

Returns: Vector2

a new instance with angle 'angleOfRadians' radians and length 'length'.


lerp

Staticlerp(vecOut: Vector2, vec1: Vector2, vec2: Vector2, t: number): Vector2

Linearly interpolates between 'vec1' and 'vec2' by 't', setting 'vecOut' to result.

Parameters:

Name | Type | ------ | ------ | vecOut | Vector2 | vec1 | Vector2 | vec2 | Vector2 | t | number |

Returns: Vector2

'vecOut'.


mult

Staticmult(...vecs: [Vector2, Vector2, ...Vector2[]]): Vector2

Multiplies 'vecs' together.

Parameters:

Name | Type | ------ | ------ | ...vecs | [Vector2, Vector2, ...Vector2[]] |

Returns: Vector2


random

Staticrandom(multiplier?: number): Vector2

Parameters:

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'.


randomWithLength

StaticrandomWithLength(length?: number): Vector2

Parameters:

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'.


sub

Staticsub(...vecs: [Vector2, Vector2, ...Vector2[]]): Vector2

Sequentially subtracts 'vecs', one by one.

Parameters:

Name | Type | ------ | ------ | ...vecs | [Vector2, Vector2, ...Vector2[]] |

Returns: Vector2


zero

Staticzero(): Vector2

Returns: Vector2

a new instance with both 'x' and 'y' set to 0.

@aliser/vector2 / Exports

@aliser/vector2

Table of contents

Classes

Variables

Variables

DEG2RAD

Const DEG2RAD: number

Multiply your degrees by this to convert them to radians.


RAD2DEG

Const RAD2DEG: number

Multiply your radians by this to convert them to degrees.