Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/seanchas116/paintvec
JavaScript 2D vector math library
https://github.com/seanchas116/paintvec
Last synced: 4 days ago
JSON representation
JavaScript 2D vector math library
- Host: GitHub
- URL: https://github.com/seanchas116/paintvec
- Owner: seanchas116
- License: mit
- Created: 2016-09-30T06:53:33.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-09-08T09:00:50.000Z (2 months ago)
- Last Synced: 2024-10-08T13:41:11.772Z (about 1 month ago)
- Language: TypeScript
- Homepage: https://seanchas116.github.io/paintvec/
- Size: 929 KB
- Stars: 19
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# paintvec
[![npm version](https://badge.fury.io/js/paintvec.svg)](https://badge.fury.io/js/paintvec)
[![CircleCI](https://circleci.com/gh/seanchas116/paintvec.svg?style=svg)](https://circleci.com/gh/seanchas116/paintvec)paintvec is a simple JavaScript 2D vector math library.
```
npm install paintvec
```## Features
- 2D point / vector (`Vec2`)
- 2D rectangle (`Rect`)
- 2D affine transform (`Transform`)[Documentation](https://seanchas116.github.io/paintvec/)
```js
import { Rect, Vec2, Transform } from "paintvec";/// Vec2
// point at (100, 200)
const pos = new Vec2(100, 200);// arithmetics
const pos2 = pos.add(new Vec2(50, 30)).mul(new Vec2(10, 20));// math functions
pos.ceil().abs();/// Rect
// 100*200 rectangle at (0, 0)
const rect = new Rect(new Vec2(0), new Vec2(100, 200));
// 100*200 rectangle at (50, 50)
const rect2 = new Rect(new Vec2(50, 50), new Vec2(100, 200));// get intersection
const intersect = Rect.intersection(rect, rect2); //=> Rect(Vec2(50, 50), Vec2(50, 150))// get union
const union = Rect.union(rect, rect2); //=> Rect(Vec2(0, 0), Vec2(150, 250))/// Transform
// translate by (100, 200)
const translate = Transform.translate(new Vec2(100, 200));// 2x scale
const scale = Transform.scale(new Vec2(2));// rotate 45 degrees
const rotate = Transform.rotate(Math.PI / 4);// translate then scale then rotate
const transform = translate.merge(scale).merge(rotate);// apply transform to Vec2/Rect
pos.transform(transform);
rect.transform(transform);
```