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

@ishoa/vector2

v1.0.2

Published

A Vector representing X and Y values in 2D space

Downloads

17

Readme

Vector2

A simple Vector2 object

A Vector representing X and Y values in 2D space

new Vector2(x, y)

Constructor

| Param | Type | Default | Description | | --- | --- | --- | --- | | x | Number | 0 | x value for vector | | y | Number | 0 | y value for vector |

vector2.add(vector) ⇒ Vector2

Add another vector2 to this vector2

Returns: Vector2 - - the modified vector (this)

| Param | Type | Description | | --- | --- | --- | | vector | Vector2 | Vector2 to add to this one |

Example

- vector1.add(vector2);

vector2.addScalar(scalar) ⇒ Vector2

Add a scalar value to our vector

Returns: Vector2 - - the modified vector (this)

| Param | Type | Description | | --- | --- | --- | | scalar | Number | number to add to both x and y |

Example

- vector.addScalar(5);

vector2.addVectors(vector1, vector2) ⇒ Vector2

Set this x and y vector to be the addition of two Vector2(s)

Returns: Vector2 - - the modified vector (this)

| Param | Type | Description | | --- | --- | --- | | vector1 | Vector2 | first vector to add to this | | vector2 | Vector2 | second vector to add to this |

Example

- let vector = new Vector2().addVectors(vector1, vector2);

vector2.angle() ⇒ Number

Calculate the angle to the coordinates 0, 0

Returns: Number - - the angle from (0, 0) of this vector

vector2.angleBetween(vector) ⇒ Number

Calculate the angle between this vector and another vector

Returns: Number - - the angle between this vector and another vector

| Param | Type | Description | | --- | --- | --- | | vector | Vector2 | a vector to get the angle of |

Example

- let angle = vector1.angleBetween(vector2);

vector2.angleTo(vector) ⇒ Number

Calculate the angle in radians from this vector to another

Returns: Number - - the angle in radians from this vector to another

| Param | Type | Description | | --- | --- | --- | | vector | Vector2 | Vector2 to get the angle too |

Example

- let angle = vector1.angleTo(vector2);

vector2.cartesianToPolar(x, y) ⇒ Object

Calculate polar coordinates from our current x and y properties OR from a passed in x and y value

Returns: Object - - The vector representation in polar coordinates (length and angle)

| Param | Type | Description | | --- | --- | --- | | x | Number | x cartesian value - defaults to this Vector2 | | y | Number | y cartesian value - defaults to this Vector2 |

Example

- let polar = vector.cartesianToPolar();

vector2.clone() ⇒ Vector2

Create a new vector of this vector

Returns: Vector2 - - the copy of this vector

vector2.cross() ⇒ Number

Calculate the cross product between this Vector2 and another Vector2

Returns: Number - - the cross product

Example

- let crossProduct = vector1.cross(vector2);

vector2.distanceTo(vector2) ⇒ Number

Calculate the distance between the 2 Vector2 Objects

Returns: Number - - the distance between this vector and another

| Param | Type | Description | | --- | --- | --- | | vector2 | Vector2 | vector 2 to measure distance to this vector |

Example

- let distance = vector1.distanceTo(vector2);

vector2.divide(vector) ⇒ Vector2

Divide a Vector2 from this

Returns: Vector2 - - the modified vector (this)

| Param | Type | Description | | --- | --- | --- | | vector | Vector2 | Vector2 to divide from this |

Example

- vector1.divide(vector2);

vector2.divideScalar(scalar) ⇒ Vector2

Divide a scalar value to our vector

Returns: Vector2 - - the modified vector (this)

| Param | Type | Description | | --- | --- | --- | | scalar | Number | scalar number to divide to both x and y |

Example

- vector.divide(5);

vector2.dot(vector) ⇒ Number

The dot product gives a number as a "scalar"

Returns: Number - - A number or scalar value which is the dot product

| Param | Type | Description | | --- | --- | --- | | vector | Vector2 | {x: 0, y: 0} |

Example

- let dot = vector1.dot(vector2);

vector2.length(x, y) ⇒ Number

Return the length of the x and y value

Returns: Number - - the length or magnitude of this vector

| Param | Type | Description | | --- | --- | --- | | x | Number | the x value - defaults to this.x | | y | Number | the y value - defaults to this.y |

Example

- let length = vector.length();

vector2.limit(value) ⇒ Vector2

Set or limit the magnitude to a value

Returns: Vector2 - - the modified vector (this)

| Param | Type | Description | | --- | --- | --- | | value | Number | Value to limit the magnitude too |

Example

- vector.limit(5);

vector2.mag() ⇒ Number

Calculate the magnitude or length of the vector (shorthand function)

Returns: Number - - the length or magnitude of this vector

Example

- let mag = vector.mag();

vector2.magnitude() ⇒ Number

Calculate the magnitude or length of the vector

Returns: Number - - the length or magnitude of this vector

Example

- let mag = vector.magnitude();

vector2.magSq() ⇒

Calculate the magnitude squared

Returns: - Vector2 magnitude squared

Example

= let magSq = vector.magSq();

vector2.midpoint(vector) ⇒ Vector2

Get the midpoint between this Vector2 to another Vector2

Returns: Vector2 - - {x: x, y: y} midpoint values

| Param | Type | Description | | --- | --- | --- | | vector | Vector2 | {x: 0, y: 0} |

Example

- let midpoint = vector1.midpoint(vector2);

vector2.multiply(vector) ⇒ Vector2

Multiply a Vector2 from this

Returns: Vector2 - - The modified Vector (this)

| Param | Type | Description | | --- | --- | --- | | vector | Vector2 | Vector2 to multiply from this |

Example

- vector1.multiply(vector2);

vector2.multiplyScalar(scalar) ⇒ Vector2

Multiply a scalar value to our vector

Returns: Vector2 - - The modified Vector (this)

| Param | Type | Description | | --- | --- | --- | | scalar | Number | scalar number to multiply to both x and y |

Example

- vector.multiplyScalar(5);

vector2.normalize() ⇒

The normalized vector or keep the direction of the Vector2 the same but the length is 1

Returns: normalized value

Example

- vector.noramlize();

vector2.set(props)

Set the properties on this to be the properties of a passed in Object

| Param | Type | Description | | --- | --- | --- | | props | Object | object with properties to set on this |

Example

- {x: 50, y: 100}

vector2.setFromPolar(length, angle) ⇒ Vector2

Set our X and Y Cartesian Coordiantes based off the provided Polar Coordinates

Returns: Vector2 - - The modified Vector (this)

| Param | Type | Description | | --- | --- | --- | | length | Number | the length or radius of the Polar Coordinates | | angle | Number | the angle or theta of the Polar Coordinates |

Example

- vector.setFromPolar(5, Math.PI() / 2);

vector2.sub(vector) ⇒ Vector2

Subtract a Vector2 from this

Returns: Vector2 - - The modified Vector (this)

| Param | Type | Description | | --- | --- | --- | | vector | Vector2 | Vector2 to subtract from this |

Example

- vector1.sub(vector2);

vector2.subScalar(scalar) ⇒ Vector2

Subtract a scalar value to our vector

Returns: Vector2 - - The modified Vector (this)

| Param | Type | Description | | --- | --- | --- | | scalar | Number | number to subtract to both x and y |

Example

- vector.subScalar(5);

vector2.subVectors(vector1, vector2) ⇒ Vector2

Set this x and y vector to be the subtraction of two Vector2(s)

Returns: Vector2 - - The modified Vector (this)

| Param | Type | Description | | --- | --- | --- | | vector1 | Vector2 | first vector to subtract to this | | vector2 | Vector2 | second vector to subtract to this |

Example

- let vector = new Vector2().subVectors(vector1, vector2);