SVGSVGElement: createSVGTransformFromMatrix() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

SVGSVGElement 接口的 createSVGTransformFromMatrix() 方法创建一个独立于任何文档树的 SVGTransform 对象,该对象基于给定的 DOMMatrix 对象。

语法

js
createSVGTransformFromMatrix(matrix)

参数

matrix

一个表示变换初始矩阵的 DOMMatrix 对象。

返回值

一个 SVGTransform 对象,它被初始化为给定的矩阵变换。如果 matrix2D 的,则为 matrix() 变换;否则为 matrix3d() 变换。

示例

从矩阵创建变换

html
<svg id="exampleSVG" width="200" height="200">
  <rect id="exampleRect" x="50" y="50" width="100" height="50" fill="blue" />
</svg>
js
const svgElement = document.getElementById("exampleSVG");
const rectElement = document.getElementById("exampleRect");

// Create a new matrix
const matrix = svgElement.createSVGMatrix();
matrix.a = 1; // Scale x
matrix.d = 1; // Scale y
matrix.e = 50; // Translate x
matrix.f = 50; // Translate y

// Create a new SVGTransform from the matrix
const transform = svgElement.createSVGTransformFromMatrix(matrix);

// Apply the transform to the rectangle
rectElement.transform.baseVal.appendItem(transform);

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGSVGElement__createSVGTransformFromMatrix

浏览器兼容性

另见