Javascript library for 2d geometry
flatten-js is a javascript library for manipulating abstract geometrical shapes like point, vector, line, segment,
circle, arc and polygon. Shapes may be organized into Planar Set - searchable container which support spatial queries.
flatten-js provides a lot of useful methods and algorithms like finding intersections, checking inclusion, calculating distance, apply
transformations and more.
Library consists of several packages, published under scope @flatten-js/ (will be more):
NOTE: Package flatten-js is not supported and will be deprecated soon.
Read documentation for each package to understand which dependencies should be installed with the package
Library provides different entry points suitable for various targets.
TypeScript users may take advantage of static type checking with typescript definition file index.d.ts included into the package.
flatten-js does not concern too much about visualization.
Anyway, all objects have svg() methods, that returns a string which may be inserted into SVG container.
This works pretty well together with d3js library. But it is definitely possible to create bridges to other graphic libraries.
The best way to start working with FlattenJS is to use awesome Observable javascript interactive notebooks.
There are several flatten-js tutorials published in Observable Notebooks, see below.
Full documentation may be found here: https://alexbol99.github.io/flatten-js/index.html
Installation
npm install --save @flatten-js/core
Usage
import {Point, Vector, Circle, Line, Segment, Arc, Box, Polygon, Matrix, PlanarSet} from '@flatten-js/core';
It is possible to import Flatten namespace as default import, and then destruct all classes from it.
import Flatten from '@flatten-js/core'
const {Point, Vector, Circle, Line, Segment, Arc, Box, Polygon, Matrix, PlanarSet} = Flatten;
Some classes have shortcuts to avoid annoying new constructor:
import {point, vector, circle, line, segment, arc, ray, matrix} from '@flatten-js/core';
Example
After module imported, it is possible to create some construction:
import {point, circle, segment} from '@flatten-js/core';
let s1 = segment(10,10,200,200);
let s2 = segment(10,160,200,30);
let c = circle(point(200, 110), 50);
let ip = s1.intersect(s2);
You may test the code above also in NPM RunKit
Tutorials
Contacts
Follow me on Twitter @alex_bol_