Skip to content

packages/core/src/lib/geometry/circle.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 { finiteNumber, nonnegativeNumber } from './validation.js';
2 
3 /**
4  * Geometry and construction options for a circle path.
5  *
6  * @see {@link createCirclePath}
7  */
8 export interface CirclePathOptions {
9   /** Horizontal coordinate of the center. See {@link CirclePathOptions}. */
10   cx?: number;
11   /** Vertical coordinate of the center. See {@link CirclePathOptions}. */
12   cy?: number;
13   /** Radius in the coordinate system of this geometry or effect. See {@link CirclePathOptions}. */
14   radius: number;
15 }
16 
17 /**
18  * Creates a Canvas Path2D for circle geometry from validated options.
19  *
20  * @param options - Circle center and radius. See {@link CirclePathOptions}.
21  * @returns A native Canvas path containing the circle.
22  *
23  * @see {@link CirclePathOptions}
24  */
25 export function createCirclePath(options: Readonly<CirclePathOptions>): Path2D {
26   const cx = finiteNumber(options.cx ?? 0, 'cx');
27   const cy = finiteNumber(options.cy ?? 0, 'cy');
28   const radius = nonnegativeNumber(options.radius, 'radius');
29   const path = new Path2D();
30   if (radius > 0) path.arc(cx, cy, radius, 0, Math.PI * 2);
31   return path;
32 }
33 

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