项目作者: bartolomej

项目描述 :
Mathematical animation engine for the web ♾✖️
高级语言: TypeScript
项目地址: git://github.com/bartolomej/vect-js.git
创建时间: 2020-01-24T16:19:26Z
项目社区:https://github.com/bartolomej/vect-js

开源协议:

下载


vect-js




Edit empty-glade-v6r37

Easily create interactive mathematical simulation or animations for the web.


⚠️ WARNING! ⚠️


This library is still in early development and is not only for experimental and testing usage. Documentation is still of poor quality, because API is still forming.


First stable and better documented release will be v1.1.0

Examples

Installing

  1. npm i vect-js

Usage

Minimal example rendering two vectors and their sum.

  1. import Vect, { Context, Shape, Vector } from "vect-js";
  2. // tell vect where to render ui
  3. const vectContainer = document.body;
  4. const vect = Vect(Context.CANVAS_2D, {
  5. // renders canvas inside container
  6. container: vectContainer,
  7. // sets canvas backgkround color
  8. backgroundColor: "#000000",
  9. // draws coordinate numbers
  10. displayNumbers: false,
  11. // draws emphasized basis coordinates (x=0, y=0)
  12. displayBasis: false,
  13. // draws coordinate grid
  14. displayGrid: false,
  15. // space between grid coordinate lines
  16. coordinatesDelta: 100,
  17. // render canvas in high pixel density
  18. highPixelDensity: true,
  19. // enable drag around with mouse
  20. enableMouseMove: true
  21. });
  22. // starting position of vector arrow (default 0,0)
  23. const p0 = new Shape.Arrow([0,0]);
  24. const v1 = new Shape.Arrow(p0, new Vector([100,50]), '#FFFFFF');
  25. const v2 = new Shape.Arrow(p0, new Vector([50,100]), '#FFFFFF');
  26. const sum1 = new Shape.Arrow(p0, v1.vector.add(v2.vector), '#db002f');
  27. // add shapes to render
  28. vect.addShapes([v1,v2,sum1]);

Updating shapes

  1. import { Shape, Vector } from 'vect-js';
  2. // initialize with position and radius
  3. let circle = new Shape.Circle(new Vector([0, 0]), 10);
  4. // state update callback called by renderer (60 times per sec)
  5. circle.onUpdate = function () {
  6. // update position with velocity vector
  7. // NOTE: math object (Vector, Matrix) are immutable
  8. this.position = this.position.add(new Vector([10,10]));
  9. }

Mouse events on shapes

  1. const c = new Shape.Circle(new Vector([0, 0]), 10);
  2. // called when shape intersects with mouse position
  3. c.onHover = function (m: MouseState) {
  4. // update state variables
  5. this.position = new Vector([0,0]);
  6. // you can also return ShapeStyles object that updates shape
  7. return {
  8. fillColor: '#FFFFFF',
  9. strokeColor: '#FFFFFF',
  10. size: 2
  11. }
  12. };
  13. // called when pressed mouse intersects with shape
  14. c.onDrag = function (m: MouseState) {
  15. // update state variables or/and return styles
  16. };
  17. // called when mouse clicks on shape
  18. c.onClick = function (m: MouseState) {
  19. // update state variables or/and return styles
  20. };

Updating coordinate system

  1. vect.onUpdate = function () {
  2. // translate coordinates
  3. this.translate(new Vector([10, 100]));
  4. // transform coordinates (zoom, skew)
  5. this.transform(new Matrix([[0.9999, 0], [0, 0.9999]]));
  6. }

Development

  1. clone repo git clone https://github.com/bartolomej/vect && cd vect
  2. install dependencies npm i
  3. run tests npm test
  4. run example apps npm position

Building

There are 4 build configurations:

  • empty - main library
  • test - builds live library tests in the browser
  • docs - builds library documentation with examples
  • examples - builds library usage examples

Scripts

  1. Run live hot-reloading npm run start:<build-config>
  2. Builds for production npm run build:<build-config>