MDN self-hosted library · e01.org

DOMMatrix: fromMatrix() static method

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

js
const matrix = DOMMatrix.fromMatrix({
  a: 1,
  b: 0,
  c: 0,
  d: 1,
  e: 50,
  f: 50,
  is2D: true,
});

console.log(matrix.toString());
// Output: matrix(1, 0, 0, 1, 50, 50)

console.log(matrix.is2D);
// Output: true
js
const matrix1 = new DOMMatrix([1, 0, 0, 1, 100, 100]);
const matrix2 = DOMMatrix.fromMatrix(matrix1);

console.log(matrix2.toString());
// Output: matrix(1, 0, 0, 1, 100, 100)

// Now we can mutate it
matrix2.translateSelf(50, 25);

console.log(matrix2.toString());
// Output: matrix(1, 0, 0, 1, 150, 125)

console.log(matrix1.toString());
// Output: matrix(1, 0, 0, 1, 100, 100)
js
const identityMatrix = DOMMatrix.fromMatrix();

console.log(identityMatrix.toString());
// Output: matrix(1, 0, 0, 1, 0, 0)

console.log(identityMatrix.isIdentity);
// Output: true