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

simple-vector

v0.0.14

Published

A 2D Vector library

Readme

A 2D vector math library built as a modern module without dependencies.

Check out the documentation and the interactive examples.

The code is largely inspired from Victor.js adding TypeScript typing, updating documentation, fixing some long running issues and adding some features.

The published package on npm contains CommonJS, ECMAScript and UMD modules. So you should be able to use this package anywhere from nodeJS or deno backends to frontent applications using vanilla JavaScript or your favorite frontend framework like Svelte or React.

Installation

Npm

Install with

npm install --save simple-vector

Use:

// If in an ESM project
import { Vector } from 'simple-vector';
// Else if in a commonJS project
const { Vector } = require('simple-vector');

// Create a new vector
const v = new Vector(1, 0);

// Create a second vector from the first one and transform it
const w = v
    .clone()
    .multiplyScalar(10)
    .rotateBy(Math.PI / 2);

// Compute their dot product
const dot = v.dot(w);

CDN

Using a CDN to include the package in a vanilla JS page

<html>
    <head>
        <title>SimpleVector Example</title>
    </head>
    <body>
        <script src="https://unpkg.com/simple-vector/dist/simple-vector.umd.js"></script>
        <script>
            const { Vector } = SimpleVector;

            const v1 = new Vector(0, 0);
            const v2 = new Vector(5, 5);

            const sum = v1.clone().add(v2);

            const span = document.createElement('span');
            span.innerText = `${v1.toString()} + ${v2.toString()} = ${sum.toString()}`;
            document.body.appendChild(span);
        </script>
    </body>
</html>

Differences with Victor.js

If you are a user of Victor.js you should feel mostly at home with simple-vector. Be warned that some features have been reworked:

  • Removed .rotateBy and .rotateByDeg which seemed to be broken or not useful (Related issue)
  • Renamed .rotate and .rotateDeg to .rotateBy and .rotateByDeg to make the name more explicit.
  • Renamed .length and .lengthSq to .mag and .magSq for personal preferences, because it's more consistent with method names like clampMag and the vocabulary used in several places in the doc.
  • Added explicit errors when trying to divide by zero (Related issue)
  • Added explicit errors when required parameters are missing or have invalid values.
  • .toFixed was renamed to .fixPrecision. Also it converted components to string so we fixed the method to keep them number (Related issue)

Note that we also added a few features we felt were missing on Victor.js or were requested in the project's issues:

  • Completed documentation (In particular for the remaining rotation functions)
  • Added methods .angleWith, .angleWithDeg, .orientedAngleWith and .orientedAngleDegWith to compute angle between two vectors.
  • .mix now validates the percentage value and throws an error if the percentage is <0 or >1.
  • Added .resize (Related issue, Victor MR #39 but with a ~10x faster implementation than the proposed code)
  • Added .rotateTowards/.rotateTowardsDeg to steer a vector toward another one.
  • Updated .fromArray and .fromObject to explicitly fail on invalid input.
  • Added .clampMag to clamp the magnitude, also .clampX, .clampY and .clampAxes to clamp the axes.
  • Added .fromPolar and .toPolar methods. (Related issue)
  • Added .limitX and .limitY to go with .limit
  • Added .isParallelTo and .isPerpendicularTo. (Victor #42 but using already existing .dot and .cross methods)
  • Added .slope to get the slope of the line passing by the vector
  • Added .reflect to reflect the vector against a surface's normal vector
  • Added .randomUnitVector to create a random vector of magnitude 1
  • Added .isEqualTo and .isCloseTo to compare vectors axes
  • Added .manhattanDistance(other: Vector) and distanceChebyshev(vec: Vector) to compute different distances between vectors.

We also ported the original tests and added new ones.

Development

DEV.md has some notes about how to setup the repo for development.

I don't expect anyone to contribute to this repo anytime soon, if I'm wrong don't hesitate to create an issue on GitHub.