thewhodidthis / vector

Helps with vector math

Home Page:https://thewhodidthis.com/modules/vector/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

about

Meant for extending, a simple 2d vector abstraction to aid working with the equations of motion in canvas animations.

setup

Load via script tag:

<!-- Just an IIFE namespaced `vector` -->
<script src="https://thewhodidthis.github.io/vector/vector.js"></script>

Source from an import map:

{
  "imports": {
    "@thewhodidthis/vector": "https://thewhodidthis.github.io/vector/main.js"
  }
}

Download from GitHub directly if using a package manager:

# Add to package.json
npm install thewhodidthis/vector

usage

Includes a class based definition with x, y properties and a basic set of helper methods attached such as add, subtract, multiply, divide, copy, clone, etc. A corresponding factory function is also available. For example,

import { vector, Vector } from "@thewhodidthis/vector"

// Show built-in methods.
console.log(Object.getOwnPropertyNames(Vector.prototype))

// Extend.
class Hector extends Vector {
  get w() {
    return this.x
  },
  get h() {
    return this.y
  },
  min() {
    return Math.min(this.x, this.y)
  },
  max() {
    return Math.max(this.x, this.y)
  },
}

// Expand.
const fromAngle = a => vector(Math.cos(a), Math.sin(a))
const rand = () => fromAngle(Math.random() * Math.PI * 2)
const lerp = (a, b, fraction) => b.minus(a).times(fraction).plus(a)

see also

About

Helps with vector math

https://thewhodidthis.com/modules/vector/

License:ISC License


Languages

Language:JavaScript 100.0%