Skip to content

packages/core/src/lib/geometry/ellipse.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 ellipse path.
5  *
6  * @see {@link createEllipsePath}
7  */
8 export interface EllipsePathOptions {
9   /** Horizontal coordinate of the center. See {@link EllipsePathOptions}. */
10   cx?: number;
11   /** Vertical coordinate of the center. See {@link EllipsePathOptions}. */
12   cy?: number;
13   /** Horizontal ellipse radius. See {@link EllipsePathOptions}. */
14   radiusX: number;
15   /** Vertical ellipse radius. See {@link EllipsePathOptions}. */
16   radiusY: number;
17   /** Rotation applied to the containing geometry. See {@link EllipsePathOptions}. */
18   rotation?: number;
19 }
20 
21 /**
22  * Creates a Canvas Path2D for ellipse geometry from validated options.
23  *
24  * @param options - Ellipse center, radii, and rotation. See {@link EllipsePathOptions}.
25  * @returns A native Canvas path containing the ellipse.
26  *
27  * @see {@link EllipsePathOptions}
28  */
29 export function createEllipsePath(options: Readonly<EllipsePathOptions>): Path2D {
30   const cx = finiteNumber(options.cx ?? 0, 'cx');
31   const cy = finiteNumber(options.cy ?? 0, 'cy');
32   const radiusX = nonnegativeNumber(options.radiusX, 'radiusX');
33   const radiusY = nonnegativeNumber(options.radiusY, 'radiusY');
34   const rotation = finiteNumber(options.rotation ?? 0, 'rotation');
35   const path = new Path2D();
36   if (radiusX > 0 && radiusY > 0) {
37     path.ellipse(cx, cy, radiusX, radiusY, rotation, 0, Math.PI * 2);
38   }
39   return path;
40 }
41 

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