Skip to content

packages/core/src/lib/geometry/line.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 { validateCoordinates } from './validation.js';
2 
3 /**
4  * Geometry and construction options for a line path.
5  *
6  * @see {@link createLinePath}
7  */
8 export interface LinePathOptions {
9   /** Ordered coordinate pairs describing the path vertices. See {@link LinePathOptions}. */
10   coords: readonly (readonly [number, number])[];
11   /** Whether to connect the final vertex back to the first. See {@link LinePathOptions}. */
12   closePath?: boolean;
13 }
14 
15 /**
16  * Creates a Canvas Path2D for line geometry from validated options.
17  *
18  * @param options - Ordered vertices and whether to close the path. See {@link LinePathOptions}.
19  * @returns A native Canvas path containing the line.
20  *
21  * @see {@link LinePathOptions}
22  */
23 export function createLinePath(options: Readonly<LinePathOptions>): Path2D {
24   const coords = validateCoordinates(options.coords);
25   const path = new Path2D();
26   coords.forEach(([x, y], index) => {
27     if (index === 0) path.moveTo(x, y);
28     else path.lineTo(x, y);
29   });
30   if (options.closePath) path.closePath();
31   return path;
32 }
33 

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