Skip to content

PathData

IPathString

String path data.

Supports SVG + Canvas drawing commands.

ts
const data = 'M 0 0 L 100 100 L 0 100 Z'

IPathCommandData

Numeric path data, using a high-performance, pure numeric one-dimensional array.

Only supports Canvas drawing commands. Can be quickly created via PathCreator.

ts
const data = [1, 0, 0, 2, 100, 100, 2, 0, 100, 11] // M 0 0 L 100 100 L 0 100 Z

IPathCommandObject

Object form of M | L | C | Q | Z drawing commands, highly readable and convenient for modifying data, with average performance.

ts
export type IPathCommandObject =
  | MoveToCommandObject
  | LineToCommandObject
  | BezierCurveToCommandObject
  | QuadraticCurveToCommandObject
  | ClosePathCommandObject // M | L | C | Q | Z   commands drawable by canvas

// Path command objects
export interface MoveToCommandObject {
  name: 'M' //  moveTo
  x: number
  y: number
}
export interface LineToCommandObject {
  name: 'L' // lineTo
  x: number
  y: number
}

export interface BezierCurveToCommandObject {
  name: 'C' // bezierCurveTo
  x1: number
  y1: number
  x2: number
  y2: number
  x: number
  y: number
}

export interface QuadraticCurveToCommandObject {
  name: 'Q' // quadraticCurveTo
  x1: number
  y1: number
  x: number
  y: number
}

export interface ClosePathCommandObject {
  name: 'Z' // closePath
}

Drawing Commands

SVG Commands

CommandCodeParametersData Lengthcomment
M1x, y3moveTo
m10x, y3moveTo relative coordinates
L2x, y3lineTo
l20x, y3lineTo relative coordinates
H3x2lineToX
h30x2lineToX relative coordinates
V4y2lineToY
v40y2lineToY relative coordinates
C5x1, y1, x2, y2, x, y7bezierCurveTo cubic Bézier curve
c50x1, y1, x2, y2, x, y7bezierCurveTo relative coordinates
S6x2, y2, x, y5smoothBezierCurveTo smooth cubic Bézier
s60x2, y2, x, y5smoothBezierCurveTo relative coordinates
Q7x1, y1, x, y5quadraticCurveTo quadratic Bézier curve
q70x1, y1, x, y5quadraticCurveTo relative coordinates
T8x, y3smoothQuadraticCurveTo smooth quadratic
t80x, y3smoothQuadraticCurveTo relative coordinates
A9rx, ry, x-axis-rotation, large-arc-flag, sweep-flag, x, y8ellipticalArc arc
a90rx, ry, x-axis-rotation, large-arc-flag, sweep-flag, x, y8ellipticalArc relative coordinates
Z111closePath
z111closePath
R12x, y3catmullRom smooth curve (not supported yet)

canvas Commands

CommandCodeParametersData Lengthcomment
M1x, y3moveTo
L2x, y3lineTo
C5x1, y1, x2, y2, x, y7bezierCurveTo cubic Bézier
Q7x1, y1, x, y5quadraticCurveTo quadratic Bézier
Z111closePath
N21x, y, width, height5rect
D22x, y, width, height, radius1, radius2,radius3,radius49roundRect
X23x, y, width, height, radius6roundRect simple
G24cx, cy, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise9ellipse
F25cx, cy, radiusX, radiusY5ellipse simple
O26cx, cy, radius, startAngle, endAngle, anticlockwise7arc
P27cx, cy, radius4arc simple
U28x1, y1, x2, y2, radius6arcTo

IPointsCurve

Defines a custom points smoothing type that converts a set of points into a smooth curve.

ts
import { IPointData, IPathCommandData } from '@leafer-ui/interface'
import { registerPointsCurve } from '@leafer-ui/draw'

// Define how to smooth points
function drawPoints(
  data: IPathCommandData,
  originPoints: number[] | IPointData[],
  curve?: boolean | number,
  close?: boolean,
  options?: IObject, // Access all data properties of the element
): void {
  // Smooth the points, convert them into path commands,
  // and append the commands to the data path array...
}

// Register the custom type
registerPointsCurve('C', drawPoints)

// Use the custom type
line.curve = {
  type: 'C',
  value: 0.3,
}

Released under the MIT License.