Git Product home page Git Product logo

vector2d's Introduction

Vector2D - 2D Vectors for TypeScript & JavaScript

Travis CI Coverage Status TypeScript

Documentation

Detailed documentation is available here.

Installation and Usage

Install via npm:

npm install vector2d

Usage with JavaScript:

var Vec2D = require('vector2d');

const v = new Vec2D.Vector(2, 3)

Usage with TypeScript/ES6:

import * as Vec2D from 'vector2d'

const v = new Vec2D.Vector(2, 3)

It's also possible to use this module directly in the browser through window.Vec2D. Simply clone the code locally and run the following:

npm install
npm run browserify
npm run uglify

This will produce vec2d.js and vec2d.min.js files in dist/ the folder of the repository that you can include via <script> tags.

About

An easy to use 2D Vector library with 3 methods of Vector representation to optimise for your particular use case.

Vector2D provides 3 main modes of Vector representations (classes):

  • Vec2D.ArrayVector
  • Vec2D.Float32Vector
  • Vec2D.Vector

The different representations is really more of an experiment than a necessity since for most users Vector or ArrayVector types will be fine.

Regardless of the class used all methods can be used in the same manner and you don't need to worry about the underlying representation.

API

Vector Instance Methods

All instance methods modify the underlying vector where possible. For example calling multiplyByScalar will multiply the vector x and y components by the provided number and return the updated vector itself (a reference to this) rather than a new instance. The benefit if this is that less objects are created which reduces garbage collection and makes chaining possible.

If you don't want to modfy the vector itself you can call v.clone() and modify the newly returned vector instead. Here's an example.

const av0 = new Vec2D.ArrayVector(10, 0)
const av1 = new Vec2D.ArrayVector(0, 5)

// Methods are chainable where you'd expect
const newVec = av0.clone().add(av1);
newVec.toString(); // (10, 5)

// Original vector hasn't changed!
av0.toString(); // (10, 0)

setAxes(x, y)

Set the x and y values of this vector.

toString()

Convert vector to a String with format "(0, 0)"

toArray()

Convert vector to standard JavaScript Array [2.823, 1.541]

toObject()

Covert vector to a JSON object with format { x: 1.4, y: 8 }.

add(vector)

Modify this vector by adding the provided vector to it.

subtract(vector)

Modify this vector by subtracting the provided vector from it.

equals(vector)

Check does this vector equal the provided vector.

mulV(vector)

Multiply this vector by provided vector.

divV(vector)

Divide this vector by provided vector.

mulS(number)

Multiply this vector by a number.

divS(number)

Divide this vector by a number.

dot(vector)

Returns dot product from this vector and the provided vector.

cross(vector)

Returns cross product from this vector and the provided vector.

reverse()

Reverse the values in this vector.

abs()

Convert stored values to absolute values.

distance(vec)

Find the distance between this vector and the provided vec;

zero()

Set vector values to 0.

rotate(radians)

Rotate vector by provided radians.

clone()

Returns a clone of this vector.

vector2d's People

Contributors

evanshortiss avatar michelypma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vector2d's Issues

Sourcemap warnings on latest create react app

I recently updated to the latest version of Create React App and I now get warnings when running my code.

WARNING in ./node_modules/vector2d/src/AbstractVector.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from '/<redacted>/node_modules/vector2d/src/AbstractVector.ts' file: Error: ENOENT: no such file or directory, open '/<redacted>/node_modules/vector2d/src/AbstractVector.ts'

It sounds like there might be a problem with how the source maps are being generated. I imagine some newer version of something or other has got stricter which is what is causing the warnings now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.