A Vector 2D class for simulations and games. This class is written in TypeScript and compiled out to a UMD module wrapped for easy consumption. Inspired by Unity and Processings Vector and PVector classes respectively.
Clone this repo and run npm i
. A postinstall script will build the files in the src and dist folders.
This package is wrapped in a UMD wrapper ready for consumption by most common module types. See below for implementations:
// ES2015
import Vector2 from 'vector2';
// CommonJS
const Vector2 = require('vector2');
Run gulp dev
to run the file watchers. You can edit and change Vector2 and have the watchers execute the typescript task. When you are done editing you will need to package this for distribution by running the gulp dist
task.
When using the Vector2 class, you will have the following methods and properties exposed for use:
Vector2 |
---|
x: number |
y: number |
constructor(x: number, y: number) |
set(x: number, y: number): void |
limit(maxX: number, maxY: number): void |
add(v: Vector2): void |
sub(v: Vector2): void |
mult(scalar: number): void |
div(divisor: number): void |
cross(v: Vector2): number |
dot(v: Vector2): number |
dist(v: Vector2): number |
getAngle(): number |
setAngle(angle: number): number |
mag(): number |
magSq(): number |
normalize(): void |
random(): void |
copy(): Vector2 |
toArray(): number[] |
toString(): string |
isEqual(v: Vector2): boolean |
zero(): void |
abs(): void |
- .vscode: Visual Studio Code configs here
- dist: public distribution and consumption folder
- lib: Vector2 lib UMD module files here
- spec: unit tests here
- src: source code here
- lib: Vector2 lib source files here
As per usual you can find the dependencies in the package.json. They are listed below for ease of use:
- gulp: build system magic
- gulp-shell: execute command line statements in gulp
- typescript: TypeScript compiler
- typings: TypeScript type definition system
- jasmine-es6: Jasmine for unit tests in ES6
- @types/jasmine: type definition for Jasmine
- babel: for compiling JavaScript
The build system makes of use of gulp on node. You can find all the build tasks in the gulpfile. Below find a listing of the tasks available from the command line:
gulp default
: runs thegulp dev
taskgulp html
: moves HTML files from the src to te dist foldergulp spec
: executes Jasmine and runs the unit testsgulp typescript
: compiles TypeScript into JavaScriptgulp dev
: runsgulp html
,gulp typescript
,gulp spec
andgulp watch
gulp build
: moves HTML files, builds TypeScript and creates dist libgulp dist
: moves transpiled file to the dist foldergulp watch
: runs watches for HTML, TypeScript and Specs
I have included my settings.json file in the .vscode folder. Feel free to add, remove and change the settings to best suit your coding environment.
Copyright (c) 2017 Brendon Conradie
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.