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

浏览器兼容性

参见