Stanko / offset-polygon

Small, no dependency library for offsetting polygons.
https://muffinman.io/offset-polygon/
MIT License
35 stars 2 forks source link
margin offset padding polygon

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[]

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);