SVGTransform:setMatrix() 方法

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

SVGTransform 介面的 setMatrix() 方法將變換型別設定為 SVG_TRANSFORM_MATRIX,並使用 matrix 引數定義新的變換。

請注意,matrix 引數中的值會被複制,這意味著呼叫此方法後對 matrix 物件所做的更改不會影響變換。

語法

js
setMatrix(matrix)

引數

matrix

一個活動的 DOMMatrix 物件,定義了要應用的新的變換矩陣。

返回值

無(undefined)。

異常

NoModificationAllowedError DOMException

如果屬性或 SVGTransform 物件是隻讀的,則丟擲此異常。

示例

設定變換矩陣

js
// Get an SVG element and create a transform object
const svgElement = document.querySelector("svg");
const transform = svgElement.createSVGTransform();

// Create a DOMMatrix with specific values
const matrix = new DOMMatrix();
matrix.a = 1; // Scale X
matrix.d = 1; // Scale Y
matrix.e = 50; // Translate X
matrix.f = 50; // Translate Y

// Set the transform to the new matrix
transform.setMatrix(matrix);

console.dir(transform.matrix); // Output: SVGMatrix { a: 1, b: 0, c: 0, d: 1, e: 50, f: 50 }

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGTransform__setMatrix

瀏覽器相容性

參見