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

shapetypes

v1.2.2

Published

2d geometric objects and operations - including intersections, booleans, and transformations

Downloads

6

Readme

npm Build Status codecov

Shapetypes

A 2d geometry library written in Typescript.

Features

Installation

You can install shapetypes with npm:

npm install shapetypes

A quick example

import { Point, Polyline, Polygon, Rectangle, Vector } from 'shapetypes';

// Create a triangle
const triangle = new Polyline([new Point(0, 0), new Point(1, 1), new Point(2, 0)], true);
console.log(triangle.area);
// => 1

// Test to see if a point is inside
const testPoint = new Point(1, 0.5);
console.log(triangle.contains(testPoint));
// => True

// Move the triangle and see if the point is inside
const shifted = triangle.translate(new Vector(3, 4));
console.log(shifted.contains(testPoint));
// => False

// Create a rectangular polygon
const rectangle = new Rectangle(Plane.worldXY(), 10, 20);
const polygon = new Polygon(rectangle.toPolyline());
console.log(polygon.area);
// => 200

// Cut the triangle from the polygon
const result = polygon.difference(triangle);
console.log(result[0].area);
// => 199

Try on Runkit (note: Runkit is in Javascript rather than Typescript).

If you're coming from Rhino

If you've used Rhino, the Shapetypes syntax should feel familiar. There are a couple of important differences though:

  1. There are no 3d functions – this library is just for 2d geometry.
  2. All geometric objects are immutable, meaning they can't be changed after they're created. Rather than modifying an object, you can create a copy with different parameters. This can often be done through helper methods that have 'with' as a prefix, such as Point.withX (see example below).
import {Point} from 'shapetypes';

// Create a new point at 3,4
const p = new Point(3,4);

// Access the x parameter
console.log(p.x);
// => 3

// Try to change the x parameter 
p.x = 5;
// => This will throw an error since objects can't be modified after they're created.

// Instead, create a copy with a different x value.
const newPoint = p.withX(5);
console.log(newPoint.x);
// => 5

Alternatives

  • Rhino3dm: A web assembly port of openNURBS. This is a much more extensive library that supports 3d geometry.
  • VerbNurbs: A cross-platform Nurbs library that compiles to javascript.
  • Javascript Topology Suite: A javascript library of geometric operations primarily for GIS data.
  • Turf JS: Another javascript library of geometric operations primarily for GIS data.

Credits

Package based on Typescript Starter. Booleans via the Polygon-clipping library. This project is not affiliated with Rhino3d or McNeel in any way.

License

MIT License