JSPM

visibility-polygon

1.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 50
  • Score
    100M100P100Q87424F
  • License MIT

A library used to construct a visibility polygon for a set of line segments

Package Exports

  • visibility-polygon

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (visibility-polygon) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

visibility-polygon npm bundle size

A library used to construct a visibility polygon for a set of line segments.

Demo

Original demo by Byron Knoll: http://www.byronknoll.com/visibility.html

Performance

The time complexity of this implementation is O(n log n) (where n is the total number of line segments). This is the optimal time complexity for this problem.

Quick Start

Installing

# npm
npm install --save visibility-polygon

# yarn
yarn add visibility-polygon

Example Usage

import {
  breakIntersections,
  convertToSegments,
  compute,
  computeViewport,
  inPolygon,
  Position,
  Polygon,
} from 'visibility-polygon';

const polygons: Polygon[] = [];
// this is the 'world' polygon, which bounds all the polygons you want to compute againts
polygons.push([
  [-1, -1],
  [501, -1],
  [501, 501],
  [-1, 501],
]);
// define vertexes of your polygons
polygons.push([
  [250, 100],
  [260, 140],
  [240, 140],
]);

const segments = breakIntersections(convertToSegments(polygons));

// define your position in which the visibility should be calculated from
const position: Position = [60, 60];

// check if the position is inside the world polygon
if (inPolygon(position, polygons[0])) {
  // compute the visibility polygon, this can be used to draw a polygon with Canvas or WebGL
  const visibility = compute(position, segments);
}
const viewportVisibility = computeViewport(
  position,
  segments,
  [50, 50],
  [450, 450]
);

Detailed API information can be found in the usage of the methods in your code-editor (JSDoc).

Credits

Original source code by Byron Knoll (@byronknoll) on https://github.com/byronknoll/visibility-polygon-js

This version of the library adds TypeScript support and re-implements it in an ESM module, while supporting CommonJS as well.