Skip to content

packages/core/src/lib/geometry/polygon.ts

Read as Markdown

This is the source snapshot used to build these API details. View this revision on GitHub.

Back to reference

1 import { createRoundedClosedPolygonPath, resolveNumericCornerRadii } from './rounded-corners.js';
2 import type { RoundedCornerPathOptions } from './rounded-corners.js';
3 import { validateCoordinates } from './validation.js';
4 
5 /**
6  * Geometry and construction options for a polygon path.
7  *
8  * @see {@link RoundedCornerPathOptions}
9  * @see {@link createPolygonPath}
10  */
11 export interface PolygonPathOptions extends RoundedCornerPathOptions {
12   /** Ordered coordinate pairs describing the path vertices. See {@link PolygonPathOptions}. */
13   coords: readonly (readonly [number, number])[];
14 }
15 
16 /**
17  * Creates a Canvas Path2D for polygon geometry from validated options.
18  *
19  * @param options - Ordered polygon vertices and corner rounding settings. See
20  * {@link PolygonPathOptions} .
21  * @returns A native Canvas path containing the polygon.
22  *
23  * @see {@link PolygonPathOptions}
24  */
25 export function createPolygonPath(
26   options: Readonly<PolygonPathOptions>,
27 ): Path2D {
28   const coords = validateCoordinates(options.coords);
29   const radii = resolveNumericCornerRadii(coords.length, options);
30   if (coords.length < 3) return new Path2D();
31   return createRoundedClosedPolygonPath(coords, radii);
32 }
33 

Documentation built with @pibbl/core 0.0.2, revision 272a94a. ALPHA — NOT FOR PRODUCTION USE.