A tiny TypeScript library for 2D vector math.

Overview

Logo

Vecti

A tiny TypeScript library for 2D vector math.

Documentation

CI NPM Coverage LGTM Grade MIT npm bundle size

Features

  • 🧮 Addition, subtraction, multiplication and division
  • Dot, cross and Hadamard product
  • 📏 Length and normalization
  • 📐 Rotation by radians and degrees
  • 🪨 Immutable data structure encourages chaining
  • 💾 Tiny and typed

Projects using Vecti

Installation

# yarn
$ yarn add vecti

# npm
$ npm install vecti

Usage

Vectors have two properties, x and y, representing their components. Since vectors are entirely immutable, they are read-only.

To use Vecti, add the following import to your TypeScript file.

import { Vector } from 'vecti'

Instances of the Vector class can be created either by using its constructor or the static method of the class. The latter accepts a number array of length 2, with the first element being the x-axis component and the second element being the y-axis component.

const a = new Vector(42, 7)
console.log(a) // == Vector { x: 42, y: 7 }

const b = Vector.of([42, 7])
console.log(b) // == Vector { x: 42, y: 7 }

Addition

Two vectors can be added using the add method.

const a = new Vector(0, 1)
const b = new Vector(1, 0)

const c = a.add(b)

console.log(c) // == Vector { x: 1, y: 1 }

Subtraction

Two vectors can be subtracted using the subtract method. The parameter is the subtrahend and the instance is the minuend.

const a = new Vector(2, 1)
const b = new Vector(1, 0)

const c = a.subtract(b)

console.log(c) // == Vector { x: 1, y: 0 }

Multiplication

Vectors can be multiplied by scalars using the multiply method.

const a = new Vector(1, 0)

const b = a.multiply(2)

console.log(b) // == Vector { x: 2, y: 0 }

Division

Vectors can be divided by scalars using the divide method. The parameter is the divisor and the instance is the dividend.

const a = new Vector(4, 2)

const b = a.divide(2)

console.log(b) // == Vector { x: 2, y: 1 }

Dot product

The dot product of two vectors can be calculated using the dot method.

const a = new Vector(2, 3)
const b = new Vector(1, 3)

const c = a.dot(b)

console.log(c) // == 11

Cross product

The cross product of two vectors can be calculated using the cross method. The cross product of two vectors a and b is defined as a.x * b.y - a.y * b.x.

const a = new Vector(2, 1)
const b = new Vector(1, 3)

const c = a.cross(b)

console.log(c) // == 5

Hadamard product

The Hadamard product of two vectors can be calculated using the hadamard method.

const a = new Vector(2, 1)
const b = new Vector(1, 3)

const c = a.hadamard(b)

console.log(c) // == Vector { x: 2, y: 3 }

Length

The length of a vector can be calculated using the length method.

Length is defined as the L2 norm.

const a = new Vector(1, 0)

console.log(a.length()) // == 1

const b = new Vector(-3, 4)

console.log(b.length()) // == 5

Normalization

A normalized version of a vector can be calculated using the normalize method. The resulting vector will have a length of 1.

const a = new Vector(2, 0)

console.log(a.length()) // == 2

const b = a.normalize()

console.log(b) // == Vector { x: 1, y: 0 }
console.log(b.length()) // == 1

Rotation

Vectors can be rotated by radians or degrees using the methods rotateByRadians and rotateByDegrees respectively.

Due to the rotation using Math.sin and Math.cos, rounding errors can occur. Notice that in the example below, the resulting x-component is 6.123233995736766e-17 and not 0 as expected.

const a = new Vector(1, 0)

console.log(a.rotateByDegrees(90)) // == Vector { x: 6.123233995736766e-17, y: 1 }


console.log(a.rotateByRadians(Math.PI / 2)) // == Vector { x: 6.123233995736766e-17, y: 1 }

Chaining

Vecti encourages chaining methods to achieve readable and concise calculations.

import { Vector } from 'vecti'

const vector = new Vector(-5, 0)
  .normalize()
  .rotateByDegrees(180)
  .add(Vector.of([0, 1]))
  .multiply(42)
console.log(vector) // == Vector { x: 42, y: 41.99999999999999 }

Development

# install dependencies
$ yarn install

# build for production
$ yarn build

# lint project files
$ yarn lint

# run tests
$ yarn test

License

MIT - Copyright © Jan Müller

Comments
Releases(v2.1.38)
Owner
Jan Müller
Studying Software Engineering & Internet Computing @ TU Wien
Jan Müller
The Swiss Army Knife of Vector Graphics Scripting – Scriptographer ported to JavaScript and the browser, using HTML5 Canvas. Created by @lehni & @puckey

Paper.js - The Swiss Army Knife of Vector Graphics Scripting If you want to work with Paper.js, simply download the latest "stable" version from http:

Paper.js 13.5k Dec 30, 2022
HTML5 Canvas Gauge. Tiny implementation of highly configurable gauge using pure JavaScript and HTML5 canvas. No dependencies. Suitable for IoT devices because of minimum code base.

HTML Canvas Gauges v2.1 Installation Documentation Add-Ons Special Thanks License This is tiny implementation of highly configurable gauge using pure

Mykhailo Stadnyk 1.5k Dec 30, 2022
Simple tiny dependency graph engine, MobX inspired

?? Quarx Simple tiny dependency graph engine, MobX inspired Introduction In less than 200 lines of code and zero dependencies Quarx supports most of M

Dmitry Maevsky 22 Nov 2, 2022
A TypeScript library designed to help with making Beat Saber modcharts

Welcome to ReMapper! This is a TypeScript library designed to help with making Beat Saber modcharts. Here are some notable features: Wrappers for Note

Swifter 37 Dec 28, 2022
Java library for use with Chart.js javascript library

Chart.java Chart.java enables integration with the excellent Chart.js library from within a Java application. Usage example In Java: BarDataset datase

Marceau Dewilde 102 Dec 16, 2022
A port of Phoenix LiveView to Typescript/Javascript

LiveViewJS Front-end framework for back-end developers Credit ?? This is a backend implementation of Phoenix LiveView in Typescript. What the Phoenix

Donnie Flood 512 Jan 9, 2023
JavaScript 3D library.

three.js JavaScript 3D library The aim of the project is to create an easy to use, lightweight, cross-browser, general purpose 3D library. The current

Mr.doob 87.9k Jan 2, 2023
Apache ECharts is a powerful, interactive charting and data visualization library for browser

Apache ECharts Apache ECharts is a free, powerful charting and visualization library offering an easy way of adding intuitive, interactive, and highly

The Apache Software Foundation 53.8k Jan 9, 2023
Javascript Canvas Library, SVG-to-Canvas (& canvas-to-SVG) Parser

Fabric.js Fabric.js is a framework that makes it easy to work with HTML5 canvas element. It is an interactive object model on top of canvas element. I

Fabric.js 23.6k Jan 3, 2023
Redefined chart library built with React and D3

Recharts Introduction Recharts is a Redefined chart library built with React and D3. The main purpose of this library is to help you to write charts i

recharts 19.4k Jan 2, 2023
The JavaScript library for modern SVG graphics.

Snap.svg · A JavaScript SVG library for the modern web. Learn more at snapsvg.io. Follow us on Twitter. Install Bower - bower install snap.svg npm - n

Adobe Web Platform 13.6k Dec 30, 2022
A JavaScript library dedicated to graph drawing

sigma.js - v1.2.1 Sigma is a JavaScript library dedicated to graph drawing, mainly developed by @jacomyal and @Yomguithereal. Resources The website pr

Alexis Jacomy 10.3k Jan 3, 2023
A reusable charting library written in d3.js

NVD3 - A reusable D3 charting library Inspired by the work of Mike Bostock's Towards Reusable Charts, and supported by a combined effort of Novus and

Novus 7.2k Jan 3, 2023
:bar_chart: A D3-based reusable chart library

c3 c3 is a D3-based reusable chart library that enables deeper integration of charts into web applications. Follow the link for more information: http

C3.js 9.2k Jan 2, 2023
The lightweight library for manipulating and animating SVG

SVG.js A lightweight library for manipulating and animating SVG, without any dependencies. SVG.js is licensed under the terms of the MIT License. Inst

SVG.js 10k Dec 25, 2022
JavaScript diagramming library for interactive flowcharts, org charts, design tools, planning tools, visual languages.

GoJS, a JavaScript Library for HTML Diagrams GoJS is a JavaScript and TypeScript library for creating and manipulating diagrams, charts, and graphs. S

Northwoods Software Corporation 6.6k Dec 30, 2022
mxGraph is a fully client side JavaScript diagramming library

NOTE 09.11.2020 : Development on mxGraph has now stopped, this repo is effectively end of life. Known forks: https://github.com/jsGraph/mxgraph https:

JGraph 6.5k Dec 30, 2022
A library optimized for concise and principled data graphics and layouts.

MetricsGraphics is a library built for visualizing and laying out time-series data. At around 15kB (gzipped), it provides a simple way to produce comm

Metrics Graphics 7.5k Dec 22, 2022
🔥 JavaScript Library for HTML5 canvas based heatmaps

heatmap.js Dynamic Heatmaps for the Web. How to get started The fastest way to get started is to install heatmap.js with bower. Just run the following

Patrick Wied 5.9k Jan 2, 2023