Skip to content

formatPath

Writes path commands back as SVG path data: letters and numbers separated by single spaces, numbers rounded to precision decimals with the trailing zeros dropped (-0 is written 0).

Available since v3.1.10

import { formatPath } from '@helpers4/geometry';
// or, from the all-in-one package (same code, one install):
import { formatPath } from 'helpers4/geometry';
formatPath(commands: readonly PathCommand[], precision: number): string
ParameterTypeDescription
commandsreadonly PathCommand[]The commands, as parsePath returns them
precisionnumberDecimals kept per number. Defaults to `6`

string — The path data

Numbers are rounded and written with single spaces.

formatPath([{ type: 'M', values: [0, 0.5] }, { type: 'L', values: [10, 1 / 3] }]) // => 'M0 0.5 L10 0.333333'

One command of SVG path data: its letter (uppercase is absolute, lowercase relative to the current point) and its numbers, as written in the d attribute.

LetterCommandNumbers
M L Tmove, line, smooth quadraticx y
H Vhorizontal, vertical linex or y
Ccubic Bézierx1 y1 x2 y2 x y
S Qsmooth cubic, quadraticx2 y2 x y or x1 y1 x y
Aelliptical arcrx ry rotation large-arc sweep x y
Zclose the subpathnone
interface PathCommand {
  type: string;
  values: number[];
}

View source on GitHub