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 🙏

© 2025 – Pkg Stats / Ryan Hefner

vectorjs

v0.1.11

Published

A 2D vector class.

Downloads

14

Readme

VectorJs

Join the chat at https://gitter.im/oeb25/VectorJs Travis testing

A 2D Vector class for JavaScript. That's it!

Installation

$ npm install vectorjs

Building

$ npm run build

$ npm run test

Usage

var Vector = require('vectorjs');

var a = new Vector(2, 2);
var b = new Vector(5, 3);

var ab = a.add(b);

console.log(ab);
// => { x: 7, y: 5 }

Usage without new is also supported

var a = new Vector(2, 2);
// works the same as
var a = Vector(2, 2);

Methods

None of the methods are mutable, which means that they wont modify the caller, but rather return a new Vector


.add(amount): Vector

Returns the vector sum of the two vectors

amount can be either a Number or a Vector


.sub(amount): Vector

Returns caller subtracted by the amount

amount can be either a Number or a Vector


.mul(amount): Vector

Returns the result of caller multiplied by the amount

amount being either a Number or a Vector


.div(amount): Vector

Returns the result of caller divided by the amount

amount being either a Number or a Vector


.len(): Number

Returns the length of the Vector. Alternatively use .lenSq()


.lenSq(): Number

Returns the squared length of the Vector. Useful for comparing two vectors


.normalize(): Vector

Returns the normal of the Vector. This is a unit sized Vector


.angle(other): Number

Returns the angle between calle and other in degrees. Alias for .angleDeg()

other being the other vector


.angleDeg(other): Number

Returns the angle between calle and other in degrees. Returns the same as .angle()

other being the other vector


.angleRad(other): Number

Returns the angle between calle and other in radian.

other being the other vector


.clone(): Vector

Returns the a copy or clone of the vector


.equals(other): Boolean

Returns whether the x and y of the vectors are the same.

This is here since JavaScript is weird and doesn't do.

{ x: 2, y: 3 } === { x: 2, y: 3 }
// => false

Vector.equals({ x: 2, y: 3 }, { x: 2, y: 3 })
// => true

They have their reasons.

other being the other vector


All of these functions can also be called from the class it self, as shown below.

a.add(b) <=> Vector.add(a, b)
a.len() <=> Vector.len(a)
// etc...

Motivation

In the past weeks, I've wrote multiple vector classes for every single one of my games, each with a different implementation. I needed something that was as simple as v(2, 1).add(3) and was easy to install. If you're looking for a super high performance vector library for rendering thousands of particles, this one might not be the one. Although if you do, send it to me please!

Misconceptions

The source it self is written in ES6, but is transpiled to pure ES5 and is ready to be required and used without a precompiler nor transpiler.

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Credits

Thanks to timoxley for supporting me with a wrapper for class to factory.

Thanks to Tribuadore on reddit for pointing out this wasn't a "propper" "libary", but rather just a class.

License

The MIT License (MIT)