DOMMatrix: setMatrixValue() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流浏览器均已支持。

注意:此功能在 Web Workers 中可用。

DOMMatrix 接口的 setMatrixValue() 方法用指定的变换或变换列表描述的矩阵替换矩阵内容,并返回自身。

语法

js
setMatrixValue(transformList)

参数

transformList

一个字符串。其值遵循与 CSS transform 属性值相同的语法。

返回值

返回自身;即更新了值的 DOMMatrix

示例

在此示例中,我们创建一个矩阵,使用 DOMMatrix.translateSelf() 方法应用 3D 变换,使用 setMatrixValue() 方法将其恢复为 2D 变换,然后再次调用 setMatrixValue() 方法将其恢复为 3D 变换。

js
const matrix = new DOMMatrix();

console.log(matrix.toString()); // matrix(1, 0, 0, 1, 0, 0)
console.log(matrix.is2D); // true

matrix.translateSelf(30, 40, 50);
console.log(matrix.toString()); // matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 30, 40, 50, 1)
console.log(matrix.is2D); // false

matrix.setMatrixValue("matrix(1, 0, 0, 1, 15, 45)");
console.log(matrix.toString()); // output: matrix(1, 0, 0, 1, 15, 45)
console.log(matrix.is2D); // true

matrix.setMatrixValue(
  "matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 30, 40, 50, 1)",
);
console.log(matrix.toString()); // matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 30, 40, 50, 1)
console.log(matrix.is2D); // false

规范

规范
Geometry Interfaces Module Level 1
# dom-dommatrix-setmatrixvalue

浏览器兼容性

另见