offset-polygon
TypeScript icon, indicating that this package has built-in type declarations

0.9.2 • Public • Published

Offset Polygon

Small, no dependency library for offsetting polygons. Heavily based on this CodePen by Hans Muller.

Interactive demo

Example polygons

Params

function offsetPolygon(vertices: Vector[], offset: number, arcSegments?: number): Vector[]
  • vertices - array of vector objects { x: number, y: number }
  • offset - number, how much should the polygon be offset. Positive values will create add margin, and negative padding.
  • arcSegments - number, default 0. When set, corners of the generated polygon will be rounded by an arc formed of straight line segments.

Returns newly generated polygon vertices as an array of vector objects { x: number, y: number }.

Usage

Get it from npm:

npm install offset-polygon
import offsetPolygon from "offset-polygon";

const polygon = [
  { "x": 413, "y": 123 },
  { "x": 510, "y": 299 },
  { "x": 395, "y": 487 },
  { "x": 292, "y": 341 },
  { "x": 92,  "y": 327 },
  { "x": 146, "y": 158 },
];

// Padding
const smallerPolygon = offsetPolygon(polygon, -10, 5);

// Margin
const largerPolygon = offsetPolygon(polygon, 10, 5);

/offset-polygon/

    Package Sidebar

    Install

    npm i offset-polygon

    Weekly Downloads

    616

    Version

    0.9.2

    License

    MIT

    Unpacked Size

    614 kB

    Total Files

    42

    Last publish

    Collaborators

    • stanko.tadic