Skip to content

matrixToCss

Writes a matrix as a CSS transform or SVG transform value.

Entries are written as JavaScript prints them, without rounding; non-finite entries give a value CSS rejects.

Available since v3.1.5

import { matrixToCss } from '@helpers4/geometry';
// or, from the all-in-one package (same code, one install):
import { matrixToCss } from 'helpers4/geometry';
matrixToCss(m: Matrix): string
ParameterTypeDescription
mMatrixThe matrix

string — matrix(a, b, c, d, e, f)

The six entries in CSS and SVG order.

matrixToCss({ a: 2, b: 0, c: 0, d: 2, e: 10, f: -5 }) // => 'matrix(2, 0, 0, 2, 10, -5)'

Combine with fitContain to scale an element with CSS.

element.style.transform = matrixToCss(fitContain(frame, viewport));

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