DOMMatrix: DOMMatrix() constructor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

Note: This feature is available in Web Workers.

The DOMMatrix() constructor creates a new DOMMatrix object which represents a 4x4 matrix, suitable for 2D and 3D operations.

Syntax

js
new DOMMatrix()
new DOMMatrix(initString)
new DOMMatrix(initArray)

Parameters

initString Optional

A string representing a 2D or 3D matrix in CSS matrix() or matrix3d() format.

initArray Optional

An array containing either 6 or 16 numbers in column-major order. Other array lengths throw a TypeError.

  • A 6-element array is interpreted as the matrix components [m11, m12, m21, m22, m41, m42], creating a 2D matrix.
  • A 16-element array is interpreted as the matrix components [m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44], creating a 3D matrix.

If this argument is omitted, an identity matrix is created, i.e., equivalent to [1, 0, 0, 1, 0, 0].

If this argument is provided as a Float32Array or Float64Array, consider using the more performant static methods DOMMatrix.fromFloat32Array() or DOMMatrix.fromFloat64Array() instead.

Return value

A new DOMMatrix object.

Exceptions

TypeError

Thrown if the argument is not a string or an array with a length other than 6 or 16.

SyntaxError

Thrown if the string argument is not in a valid CSS matrix() or matrix3d() format.

Examples

This example creates a DOMMatrix to use as an argument for calling DOMPointReadOnly.matrixTransform().

js
const point = new DOMPoint(5, 4);
const scaleX = 2;
const scaleY = 3;
const translateX = 12;
const translateY = 8;
const angle = Math.PI / 2;
const matrix = new DOMMatrix([
  Math.cos(angle) * scaleX,
  Math.sin(angle) * scaleX,
  -Math.sin(angle) * scaleY,
  Math.cos(angle) * scaleY,
  translateX,
  translateY,
]);
const transformedPoint = point.matrixTransform(matrix);

Specifications

Specification
Geometry Interfaces Module Level 1
# dom-dommatrix-dommatrix

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
DOMMatrix() constructor
Chrome – Full support
Chrome 2 (Release date: 2009-05-21)
altname
altname Alternate name: WebKitCSSMatrix
Chrome – Full support
Chrome 5 (Release date: 2010-05-25)
altname
altname Alternate name: SVGMatrix
Chrome – Full support
Chrome 61 (Release date: 2017-09-05)
footnote Full support
Edge – No support
Edge 12 – 18 (Release date: 2015-07-29)
altname
altname Alternate name: MSCSSMatrix
Edge – Full support
Edge 12 (Release date: 2015-07-29)
altname
altname Alternate name: SVGMatrix
Edge – Full support
Edge 12 (Release date: 2015-07-29)
altname
altname Alternate name: WebKitCSSMatrix
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 1.5 (Release date: 2005-11-29)
altname
altname Alternate name: SVGMatrix
Firefox – Full support
Firefox 49 (Release date: 2016-09-20)
altname
altname Alternate name: WebKitCSSMatrix
Firefox – Full support
Firefox 33 (Release date: 2014-10-14)
footnote Full support
Opera – Full support
Opera 12.1 (Release date: 2012-11-20)
altname
altname Alternate name: SVGMatrix
Opera – Full support
Opera 15 (Release date: 2013-07-02)
altname
altname Alternate name: WebKitCSSMatrix
Opera – Full support
Opera 48 (Release date: 2017-09-27)
footnote Full support
Safari – Full support
Safari 4 (Release date: 2009-06-08)
altname
altname Alternate name: WebKitCSSMatrix
Safari – Full support
Safari 5 (Release date: 2010-06-07)
altname
altname Alternate name: SVGMatrix
Safari – Full support
Safari 11 (Release date: 2017-09-19)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
altname
altname Alternate name: WebKitCSSMatrix
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
altname
altname Alternate name: SVGMatrix
Chrome Android – Full support
Chrome Android 61 (Release date: 2017-09-05)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
altname
altname Alternate name: SVGMatrix
Firefox for Android – Full support
Firefox for Android 49 (Release date: 2016-09-20)
altname
altname Alternate name: WebKitCSSMatrix
Firefox for Android – Full support
Firefox for Android 33 (Release date: 2014-10-14)
footnote Full support
Opera Android – Full support
Opera Android 12.1 (Release date: 2012-10-09)
altname
altname Alternate name: SVGMatrix
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
altname
altname Alternate name: WebKitCSSMatrix
Opera Android – Full support
Opera Android 48 (Release date: 2018-11-08)
footnote Full support
Safari on iOS – Full support
Safari on iOS 3 (Release date: 2009-06-17)
altname
altname Alternate name: WebKitCSSMatrix
Safari on iOS – Full support
Safari on iOS 4 (Release date: 2010-06-21)
altname
altname Alternate name: SVGMatrix
Safari on iOS – Full support
Safari on iOS 11 (Release date: 2017-09-19)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
altname
altname Alternate name: WebKitCSSMatrix
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
altname
altname Alternate name: SVGMatrix
Samsung Internet – Full support
Samsung Internet 8 (Release date: 2018-07-18)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
altname
altname Alternate name: WebKitCSSMatrix
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
altname
altname Alternate name: SVGMatrix
WebView Android – Full support
WebView Android 61 (Release date: 2017-09-05)
footnote Full support
WebView on iOS – Full support
WebView on iOS 3 (Release date: 2009-06-17)
altname
altname Alternate name: WebKitCSSMatrix
WebView on iOS – Full support
WebView on iOS 4 (Release date: 2010-06-21)
altname
altname Alternate name: SVGMatrix
WebView on iOS – Full support
WebView on iOS 11 (Release date: 2017-09-19)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Uses a non-standard name
Has more compatibility info.

See also