Offset Polygon
Small, no dependency library for offsetting polygons. Heavily based on this CodePen by Hans Muller.
Interactive demo
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 },
];
const smallerPolygon = offsetPolygon(polygon, -10, 5);
const largerPolygon = offsetPolygon(polygon, 10, 5);