大网数据广告活动
Web 标准与前端开发

DOMMatrix: DOMMatrix() constructor

· 52CE 我爱测

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

Geometry Interfaces

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

Syntax

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

Parameters

  • initString
    • : A string representing a 2D or 3D matrix in CSS transform-function/matrix or transform-function/matrix3d format.
  • initArray
    • : 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_static or DOMMatrix.fromFloat64Array_static 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 transform-function/matrix or transform-function/matrix3d format.

Examples

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

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

Browser compatibility

See also

  • DOMMatrix.fromFloat32Array_static
  • DOMMatrix.fromFloat64Array_static
  • DOMMatrix.fromMatrix_static

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。