npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

flatten-js

v0.6.9

Published

Javascript library for 2d geometry

Downloads

1,622

Readme

npm version Build Status Coverage Status

Javascript library for 2d geometry

FlattenJS is a javascript library (about 50 Kb minified) 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.

FlattenJS provides a lot of useful methods and algorithms like finding intersections, checking inclusion, calculating distance, apply transformations and more. Polygon model is rather comprehensive and supports multi polygons with many islands and holes. Edges of polygon may be circular arcs or segments. Some algorithms like Boolean Operations and Offset, implemented in separate packages.

This library designed to work in any modern browser as well as under nodejs. It is written in plain javascript with es6 syntax elements. You can use es5 precompiled bundled package (added in v0.6.2) if you need to support old browsers.

TypeScript users may take advantage of static type checking with typescript definition file index.d.ts included into the package.

FlattenJS 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 FlattenJS tutorials published in Observable Notebooks, see below.

Full documentation may be found here

Important note

This package is not supported and will be deprecated soon. Consider moving to the scoped version @flatten-js/core.

Contacts

Follow me on Twitter @alex_bol_

Installation

npm install --save flatten-js

Usage

Package may be required in different ways:

Require as es6 module:
    import Flatten from 'flatten-js';
Require as CommonJS package (nodejs)
    const Flatten = require('flatten-js');
Require minified package precompiled into UMD format.

Observable notebooks requires this format.

    const Flatten = require('flatten-js.umd.min.js');
Require precompiled to es5 package in Commonjs2 format.
    import Flatten from "flatten-js/dist/flatten.commonjs2"

This package is not minified.

This is the way you have to consume the package for React library, at least when you use create-react-library starter kit:

""
    Some third-party packages don't compile their code to ES5 before publishing to npm.
    This often causes problems in the ecosystem because neither browsers (except for most modern versions)
    nor some tools currently support all ES6 features.
    We recommend to publish code on npm as ES5 at least for a few more years.
""    

You can see example of FlattenJS + React usage in flatten-react-demo project. It is live here. Just clone it from the GitHub, install dependencies and start working using npm start or compile it to production using npm run build.

Example

After module required, you can create some construction:

    // extract object creators
    let {point, circle, segment} = Flatten;

    // make some construction
    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

  1. Getting Started
  2. Messing Around
  3. Planar Set
  4. polygons