Skip to content

transformPath

Transforms SVG path data by a matrix. The result is written with absolute M, L, C, Q and Z only: relative commands are made absolute, H/V/S/T are expanded, and arcs become cubic curves (so any matrix works, skews included). Moving a path is the common case.

Available since v3.1.10

import { transformPath } from '@helpers4/geometry';
// or, from the all-in-one package (same code, one install):
import { transformPath } from 'helpers4/geometry';
transformPath(d: string, m: Matrix, precision: number): string
ParameterTypeDescription
dstringThe path data
mMatrixThe matrix, in the CSS/SVG layout
precisionnumberDecimals kept per number. Defaults to `6`

string — The transformed path data

Relative commands are made absolute, then every point is transformed.

transformPath('M0 0 h10 v10 z', translate(5, 5)) // => 'M5 5 L15 5 L15 15 Z'

A 2D affine transformation, in the layout of CSS and SVG matrix(a, b, c, d, e, f):

x' = a·x + c·y + e
y' = b·x + d·y + f
interface Matrix {
  a: number;
  b: number;
  c: number;
  d: number;
  e: number;
  f: number;
}

View source on GitHub