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 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-angusj-clipper

v1.3.1

Published

Polygon and line clipping and offsetting library for Javascript / Typescript - a port of Angus Johnson's clipper to WebAssembly / Asm.JS

Downloads

19,139

Readme

js-angusj-clipper

Polygon and line clipping and offsetting library for Javascript/Typescript

a port of Angus Johnson's clipper to WebAssembly/Asm.js

npm version Build Status


Install it with npm install --save js-angusj-clipper

To support this project star it on github!


What is this?

A library to make polygon clipping (boolean operations) and offsetting fast on Javascript thanks to WebAssembly with a fallback to Asm.js, based on the excellent Polygon Clipping (also known as Clipper) library by Angus Johnson.


Why?

Because sometimes performance does matter and I could not find a javascript library as fast or as rock solid as the C++ version of Clipper.

As an example, the results of the benchmarks included on the test suite when running on my machine (node 17.9.0) are:

Note, pureJs is jsclipper, a pure JS port of the same library

    500 boolean operations over two circles of 5000 points each
      clipType: intersection, subjectFillType: evenOdd
        ✓ wasm (212 ms)
        ✓ asmJs (598 ms)
        ✓ pureJs (573 ms)
      clipType: union, subjectFillType: evenOdd
        ✓ wasm (267 ms)
        ✓ asmJs (666 ms)
        ✓ pureJs (663 ms)
      clipType: difference, subjectFillType: evenOdd
        ✓ wasm (232 ms)
        ✓ asmJs (575 ms)
        ✓ pureJs (573 ms)
      clipType: xor, subjectFillType: evenOdd
        ✓ wasm (296 ms)
        ✓ asmJs (681 ms)
        ✓ pureJs (779 ms)
    10000 boolean operations over two circles of 100 points each
      clipType: intersection, subjectFillType: evenOdd
        ✓ wasm (143 ms)
        ✓ asmJs (347 ms)
        ✓ pureJs (255 ms)
      clipType: union, subjectFillType: evenOdd
        ✓ wasm (181 ms)
        ✓ asmJs (417 ms)
        ✓ pureJs (265 ms)
      clipType: difference, subjectFillType: evenOdd
        ✓ wasm (159 ms)
        ✓ asmJs (339 ms)
        ✓ pureJs (239 ms)
      clipType: xor, subjectFillType: evenOdd
        ✓ wasm (186 ms)
        ✓ asmJs (404 ms)
        ✓ pureJs (262 ms)
    100 offset operations over a circle of 5000 points
      joinType: miter, endType: closedPolygon, delta: 5
        ✓ wasm (129 ms)
        ✓ asmJs (390 ms)
        ✓ pureJs (702 ms)
      joinType: miter, endType: closedPolygon, delta: 0
        ✓ wasm (34 ms)
        ✓ asmJs (140 ms)
        ✓ pureJs (108 ms)
      joinType: miter, endType: closedPolygon, delta: -5
        ✓ wasm (146 ms)
        ✓ asmJs (386 ms)
        ✓ pureJs (770 ms)
    5000 offset operations over a circle of 100 points
      joinType: miter, endType: closedPolygon, delta: 5
        ✓ wasm (74 ms)
        ✓ asmJs (161 ms)
        ✓ pureJs (278 ms)
      joinType: miter, endType: closedPolygon, delta: 0
        ✓ wasm (61 ms)
        ✓ asmJs (138 ms)
        ✓ pureJs (162 ms)
      joinType: miter, endType: closedPolygon, delta: -5
        ✓ wasm (109 ms)
        ✓ asmJs (271 ms)
        ✓ pureJs (659 ms)

More or less, the results for boolean operations over moderately big polygons are:

  • Pure JS port of the Clipper library: ~1.0s, baseline
  • This library (WebAssembly): ~0.4s
  • This library (Asm.js): ~1.0s (mostly due to the emulation of 64-bit integer operations)

and for small polygons are:

  • Pure JS port of the Clipper library: ~1.0s, baseline
  • This library (WebAssembly): ~0.7s (due to the overhead of copying structures to/from JS/C++)
  • This library (Asm.js): ~1.4s (mostly due to the emulation of 64-bit integer operations + the overhead of copying structures to/from JS/C++)

As for offsetting, the results for a moderately big polygon are:

  • Pure JS port of the Clipper library: ~1s, baseline
  • This library (WebAssembly): ~0.2s
  • This library (Asm.js): ~0.5s

and for small polygons are:

  • Pure JS port of the Clipper library: ~1s, baseline
  • This library (WebAssembly): ~0.2s
  • This library (Asm.js): ~0.5s

Getting started

// universal version
// import it with
import * as clipperLib from "js-angusj-clipper"; // es6 / typescript
// or
const clipperLib = require("js-angusj-clipper"); // nodejs style require

// web-only version (for example for angular 6+)
// import it with
import * as clipperLib from "js-angusj-clipper/web"; // es6 / typescript
// or
const clipperLib = require("js-angusj-clipper/web"); // nodejs style require

async function mainAsync() {
  // create an instance of the library (usually only do this once in your app)
  const clipper = await clipperLib.loadNativeClipperLibInstanceAsync(
    // let it autodetect which one to use, but also available WasmOnly and AsmJsOnly
    clipperLib.NativeClipperLibRequestedFormat.WasmWithAsmJsFallback
  );

  // create some polygons (note that they MUST be integer coordinates)
  const poly1 = [{ x: 0, y: 0 }, { x: 10, y: 0 }, { x: 10, y: 10 }, { x: 0, y: 10 }];

  const poly2 = [{ x: 10, y: 0 }, { x: 20, y: 0 }, { x: 20, y: 10 }, { x: 10, y: 10 }];

  // get their union
  const polyResult = clipper.clipToPaths({
    clipType: clipperLib.ClipType.Union,

    subjectInputs: [{ data: poly1, closed: true }],

    clipInputs: [{ data: poly2 }],

    subjectFillType: clipperLib.PolyFillType.EvenOdd
  });

  /* polyResult will be:
  [
    [
      { x: 0, y: 0 },
      { x: 20, y: 0 },
      { x: 20, y: 10 },
      { x: 0, y: 10 }
    ]
  ]
 */
}

mainAsync();

For an in-depth description of the library see: