## Paths ### Path Utility class extending [Curve](./CURVES.md#curve) for manipulating connected curves. - [new Path(params?)](#path-constructor) - .isPath: `true` - [.curves](#path-curves): `Curve[]` - [.points](#path-points): `Vector[]` - [.autoClose](#path-auto-close): `boolean` - [.add(curve)](#path-add-method): `void` - [.getCurveLengths()](#path-get-curve-lengths-method): `number[]` #### Constructor | Parameter | Type | Default | Description | | ------------------ | --------- | ------- | ----------------------------------------------------------------------- | | [params] | `object` | | | | [params.autoClose] | `boolean` | `false` | Define if a last point should be automatically added to close the path. | #### Properties ##### .`curves` Array of curves composing the path. ```ts Path.curves: Curve[]; ``` ##### .`points` Array of points composing the path. ```ts Path.points: Vector[]; ``` ##### .`autoClose` ```ts Path.autoClose: boolean; ``` #### Methods ##### .`add(curve)` Add a curve to the path. - `curve`: Curve to add. ```ts Path.add(curve: Curve): void; ``` ##### .`getCurveLengths()` Compute the cumulative curve lengths of the path. ```ts Path.getCurveLengths(): number[]; ``` ### PathContext Utility class extending [Path](#path) providing methods similar to the 2D Canvas API. - [new PathContext()](#path-context) - [.setFromPoints(points)](#path-context-set-from-points-method): `this` - [.beginPath()](#path-context-begin-path-method): `this` - [.closePath()](#path-context-close-path-method): `this` - [.moveTo(x, y)](#path-context-move-to-method): `this` - [.lineTo(x, y)](#path-context-line-to-method): `this` - [.polylineTo(points)](#path-context-polyline-to-method): `this` - [.quadraticCurveTo(cpx, cpy, x2, y2)](#path-context-quadratic-curve-to-method): `this` - [.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x2, y2)](#path-context-bezier-curve-to-method): `this` - [.catmullRomCurveTo(cp1x, cp1y, cp2x, cp2y, x2, y2)](#path-context-catmull-rom-to-method): `this` - [.splineTo(points)](#path-context-spline-to-method): `this` - [.ellipse(cx, cy, rx, ry, rotation, startAngle, endAngle, counterclockwise?)](#path-context-ellipse-method): `this` - [.arc(cx, cy, radius, startAngle, endAngle, counterclockwise?)](#path-context-arc-method): `this` - [.arcTo(x1, y1, x2, y2, radius)](#path-context-arc-to-method): `this` - [.rect(x, y, width, height)](#path-context-rect-method): `this` - [.roundRect(x, y, width, height, radius)](#path-context-round-rect-method): `this` #### Methods ##### .`setFromPoints(points)` Create a path from a given list of points. - `points`: Array of points defining the path. ```ts PathContext.setFromPoints(points: Array<[number, number]>): this; ``` ##### .`beginPath()` Begin the path. ```ts PathContext.beginPath(): this; ``` ##### .`closePath()` Draw a line from the ending position to the beginning position of the path. Add an instance of [LineCurve](./CURVES.md#line-curve) to the path. ```ts PathContext.closePath(): this; ``` ##### .`moveTo(x, y)` Move `currentPosition` to the coordinates specified by `x` and `y`. - `x`: X-axis coordinate of the point. - `y`: Y-axis coordinate of the point. ```ts PathContext.moveTo(x: number, y: number): this; ``` ##### .`lineTo(x, y)` Draw a line from the current position to the position specified by `x` and `y`. Add an instance of [LineCurve](./CURVES.md#line-curve) to the path. - `x`: X-axis coordinate of the point. - `y`: Y-axis coordinate of the point. ```ts PathContext.lineTo(x: number, y: number): this; ``` ##### .`polylineTo(points)` Draw a Polyline curve from the current position through given points. Add an instance of [PolylineCurve](./CURVES.md#polyline-curve) to the path. - `points`: Array of points defining the curve. ```ts PathContext.polylineTo(points: Array<[number, number]>): this; ``` ##### .`quadraticCurveTo(cpx, cpy, x2, y2)` Draw a Quadratic Bézier curve from the current position to the end point specified by `x` and `y`, using the control point specified by `cpx` and `cpy`. Add an instance of [QuadraticBezierCurve](./CURVES.md#quadratic-bezier-curve) to the path. - `cpx`: X-axis coordinate of the control point. - `cpy`: Y-axis coordinate of the control point. - `x2`: X-axis coordinate of the end point. - `y2`: Y-axis coordinate of the end point. ```ts PathContext.quadraticCurveTo(cpx: number, cpy: number, x2: number, y2: number): this; ``` ##### .`bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x2, y2)` Draw a Cubic Bézier curve from the current position to the end point specified by `x` and `y`, using the control point specified by (`cp1x`, `cp1y`) and (`cp2x`, `cp2y`). Add an instance of [CubicBezierCurve](./CURVES.md#cubic-bezier-curve) to the path. - `cp1x`: X-axis coordinate of the first control point. - `cp1y`: Y-axis coordinate of the first control point. - `cp2x`: X-axis coordinate of the second control point. - `cp2y`: Y-axis coordinate of the second control point. - `x2`: X-axis coordinate of the end point. - `y2`: Y-axis coordinate of the end point. ```ts PathContext.bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x2: number, y2: number): this; ``` ##### .`catmullRomCurveTo(cp1x, cp1y, cp2x, cp2y, x2, y2)` Draw a Catmull-Rom curve from the current position to the end point specified by `x` and `y`, using the control points specified by (`cp1x`, `cp1y`) and (`cp2x`, `cp2y`). Add an instance of [CatmullRomCurve](./CURVES.md#catmull-rom-curve) to the path. - `cp1x`: X-axis coordinate of the first control point. - `cp1y`: Y-axis coordinate of the first control point. - `cp2x`: X-axis coordinate of the second control point. - `cp2y`: Y-axis coordinate of the second control point. - `x2`: X-axis coordinate of the end point. - `y2`: Y-axis coordinate of the end point. ```ts PathContext.catmullRomCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x2: number, y2: number): this; ``` ##### .`splineTo(points)` Draw a Spline curve from the current position through given points. Add an instance of [SplineCurve](./CURVES.md#spline-curve) to the path. - `points`: Array of points defining the curve. ```ts PathContext.splineTo(points: Array<[number, number]>): this; ``` ##### .`ellipse(cx, cy, rx, ry, rotation, startAngle, endAngle, counterclockwise?)` Draw an Ellispe curve which is centered at (`cx`, `cy`) position. Add an instance of [EllipseCurve](./CURVES.md#ellipse-curve) to the path. - `cx`: X-axis coordinate of the center of the ellipse. - `cy`: Y-axis coordinate of the center of the ellipse. - `rx`: X-radius of the ellipse. - `ry`: Y-radius of the ellipse. - `rotation`: Rotation angle of the ellipse (in radians), counterclockwise from the positive X-axis. - `startAngle`: Start angle of the arc (in radians). - `endAngle`: End angle of the arc (in radians). - `[counterclockwise]`: Flag indicating the direction of the arc. ```ts PathContext.ellipse(cx: number, cy: number, rx: number, ry: number, rotation?: number, startAngle?: number, endAngle?: number, counterclockwise?: boolean): this; ``` ##### .`arc(cx, cy, radius, startAngle, endAngle, counterclockwise?)` Draw an Arc curve which is centered at (`cx`, `cy`) position. Add an instance of [ArcCurve]./CURVES.md#arc-curve) to the path. - `cx`: X-axis coordinate of the center of the circle. - `cy`: Y-axis coordinate of the center of the circle. - `radius`: Radius of the circle. - `startAngle`: Start angle of the arc (in radians). - `endAngle`: End angle of the arc (in radians). - `[counterclockwise]`: Flag indicating the direction of the arc. ```ts PathContext.arc(cx: number, cy: number, radius: number, startAngle?: number, endAngle?: number, counterclockwise?: boolean): this; ``` ##### .`arcTo(x1, y1, x2, y2, radius)` Draw an Arc curve from the current position, tangential to the 2 segments created by both control points Add an instance of [EllipseCurve](./CURVES.md#ellipse-curve) to the path. - `x1`: X-axis coordinate of the first control point. - `y1`: Y-axis coordinate of the first control point. - `x2`: X-axis coordinate of the second control point. - `y2`: Y-axis coordinate of the second control point. - `radius`: Arc radius (Must be non-negative). ```ts PathContext.arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): this; ``` ##### .`rect(x, y, width, height)` Draw a rectangular path from the start position specified by `x` and `y` to the end position using `width` and `height`. Add an instance of [PolylineCurve](./CURVES.md#polyline-curve) to the path. - `x`: X-axis coordinate of the rectangle starting point. - `y`: Y-axis coordinate of the rectangle starting point. - `width`: Rectangle width (Positive values are to the right and negative to the left). - `height`: Rectangle height (Positive values are down, and negative are up). ```ts PathContext.rect(x: number, y: number, width: number, height: number): this; ``` ##### .`roundRect(x, y, width, height, radius)` Draw a rounded rectangular path from the start position specified by `x` and `y` to the end position using `width` and `height`. Add an instance of [Path](#path) to the path. - `x`: X-axis coordinate of the rectangle starting point. - `y`: Y-axis coordinate of the rectangle starting point. - `width`: Rectangle width (Positive values are to the right and negative to the left). - `height`: Rectangle height (Positive values are down, and negative are up). - `radius`: Radius of the circular arc to be used for the corners of the rectangle. ```ts PathContext.roundRect(x: number, y: number, width: number, height: number, radius: number | number[]): this; ``` ### PathSVG Utility class extending [PathContext](#path-context) for manipulating connected curves and generating SVG path. It works by serializing 2D Canvas API to SVG path data. - new PathSVG() - [.toString(params?)](#path-svg-to-string-method): `string` - `static` [.approximate(curve, resolution?)](#path-svg-static-serialize-method): `string` - `static` [.serialize(curve, params?)](#path-svg-static-serialize-method): `string` - `static` [.serializeLineCurve(curve)](#path-svg-static-serialize-line-curve-method): `string` - `static` [.serializePolylineCurve(curve)](#path-svg-static-serialize-polyline-curve-method): `string` - `static` [.serializeQuadraticBezierCurve(curve)](#path-svg-static-serialize-quadratic-bezier-curve-method): `string` - `static` [.serializeCubicBezierCurve(curve)](#path-svg-static-serialize-cubic-bezier-curve-method): `string` - `static` [.serializeCatmullRomCurve(curve, resolution?)](#path-svg-static-serialize-catmull-rom-curve-method): `string` - `static` [.serializeSplineCurve(curve, resolution?)](#path-svg-static-serialize-spline-curve-method): `string` - `static` [.serializeEllipseCurve(curve)](#path-svg-static-serialize-ellipse-curve-method): `string` - `static` [.serializeArcCurve(curve)](#path-svg-static-serialize-arc-curve-method): `string` - `static` [.serializePath(path, params?)](#path-svg-static-serialize-path-method): `string` #### Types PathSVG serialization parameters: ```ts /** * Parameters used for SVG serialization of a path */ type PathSVGSerializationParameters = { /** * Flag indicating if given curve should be approximated into straight lines */ approximate?: boolean; /** * Resolution used for approximations */ resolution?: number; }; ``` #### Methods ##### .`toString(params?)` Serialize the path into a SVG path string. - `[params]`: [Serialization parameters](#path-svg-serialization-parameters). ```ts PathSVG.toString(params?: PathSVGSerializationParameters): string; ``` ##### `static` PathSVG.`approximate(curve, resolution?)` Convert a [Curve](./CURVES.md#curve) into straight segments. - `curve`: Curve to approximate. - `[resolution=5]`: Approximation resolution. ```ts static PathSVG.approximate(curve: Curve, resolution?: number): Vector2[]; ``` ##### `static` PathSVG.`serialize(curve, params?)` Serialize a [Curve](./CURVES.md#curve). - `curve`: Curve to serialize. - `[params]`: [Serialization parameters](#path-svg-serialization-parameters). ```ts static PathSVG.serialize(curve: Curve, params?: PathSVGSerializationParameters): string; ``` ##### `static` PathSVG.`serializeLineCurve(curve)` Serialize a [LineCurve](./CURVES.md#line-curve). - `curve`: Curve to serialize. ```ts static PathSVG.serializeLineCurve(curve: LineCurve): string; ``` ##### `static` PathSVG.`serializePolylineCurve(curve)` Serialize a [PolylineCurve](./CURVES.md#polyline-curve). - `curve`: PolylineCurve to serialize. ```ts static PathSVG.serializePolylineCurve(curve: PolylineCurve): string; ``` ##### `static` PathSVG.`serializeQuadraticBezierCurve(curve)` Serialize a [QuadraticBezierCurve](./CURVES.md#quadratic-bezier-curve). - `curve`: QuadraticBezierCurve to serialize. ```ts static PathSVG.serializeQuadraticBezierCurve(curve: QuadraticBezierCurve): string; ``` ##### `static` PathSVG.`serializeCubicBezierCurve(curve)` Serialize a [CubicBezierCurve](./CURVES.md#cubic-bezier-curve). - `curve`: CubicBezierCurve to serialize. ```ts static PathSVG.serializeCubicBezierCurve(curve: CubicBezierCurve): string; ``` ##### `static` PathSVG.`serializeCatmullRomCurve(curve, resolution?)` Serialize a [CatmullRomCurve](./CURVES.md#catmull-rom-curve) by approximating it into straight lines. - `curve`: CatmullRomCurve to serialize. - `[resolution]`: Approximation resolution. ```ts static PathSVG.serializeCatmullRomCurve(curve: CatmullRomCurve, resolution?: number): string; ``` ##### `static` PathSVG.`serializeSplineCurve(curve, resolution?)` Serialize a [SplineCurve](./CURVES.md#spline-curve) by approximating it into straight lines. - `curve`: SplineCurve to serialize. - `[resolution]`: Approximation resolution. ```ts static PathSVG.serializeSplineCurve(curve: SplineCurve, resolution?: number): string; ``` ##### `static` PathSVG.`serializeEllipseCurve(curve)` Serialize a [EllipseCurve](./CURVES.md#ellipse-curve). - `curve`: EllipseCurve to serialize. ```ts static PathSVG.serializeEllipseCurve(curve: EllipseCurve): string; ``` ##### `static` PathSVG.`serializeArcCurve(curve)` Serialize a [ArcCurve](./CURVES.md#arc-curve). - `curve`: ArcCurve to serialize. ```ts static PathSVG.serializeArcCurve(curve: ArcCurve): string; ``` ##### `static` PathSVG.`serializePath(path, params?)` Serialize a [Path](#path). - `path`: Path to serialize. - `[params]`: [Serialization parameters](#path-svg-serialization-parameters). ```ts static PathSVG.serializePath(path: Path, params?: PathSVGSerializationParameters): string; ```