SVGTransformList: createSVGTransformFromMatrix() 方法
SVGTransformList
接口的 createSVGTransformFromMatrix()
方法创建一个 SVGTransform
对象,该对象被初始化为 SVG_TRANSFORM_MATRIX
类型的变换,其值由给定的矩阵决定。
参数矩阵的值会被复制;矩阵参数不会被采纳为 SVGTransform::matrix
。
语法
js
createSVGTransformFromMatrix(matrix)
参数
返回值
一个 SVGTransform
对象。
示例
从矩阵创建变换
html
<svg width="200" height="200">
<rect width="100" height="100" fill="blue" />
</svg>
js
const svgElement = document.querySelector("svg");
const rectElement = svgElement.querySelector("rect");
// Access the transform list of the <rect> element
const transformList = rectElement.transform.baseVal;
// Create a DOMMatrix object for a rotation transformation
const rotationMatrix = new DOMMatrix();
rotationMatrix.a = Math.cos(Math.PI / 4); // 45-degree rotation
rotationMatrix.b = Math.sin(Math.PI / 4);
rotationMatrix.c = -Math.sin(Math.PI / 4);
rotationMatrix.d = Math.cos(Math.PI / 4);
// Create an SVGTransform object from the matrix
const svgTransform = transformList.createSVGTransformFromMatrix(rotationMatrix);
// Append the new transformation to the transform list
transformList.appendItem(svgTransform);
console.dir(svgTransform); // Output: SVGTransform { type: 1, matrix: SVGMatrix, angle: 0 }
规范
规范 |
---|
Scalable Vector Graphics (SVG) 2 # __svg__SVGTransformList__createSVGTransformFromMatrix |
浏览器兼容性
加载中…