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

瀏覽器相容性

另見