SVGTransform:setRotate() 方法

Baseline 已广泛支持

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

SVGTransform 接口的 setRotate() 方法将变换类型设置为 SVG_TRANSFORM_ROTATE,其中参数 angle 定义了旋转角度,参数 cxcy 定义了可选的旋转中心。

语法

js
setRotate(angle, cx, cy)

参数

angle

一个浮点数,定义了以度为单位的旋转角度。

cx 可选

一个浮点数,定义了旋转中心的 X 坐标。默认为 0

cy 可选

一个浮点数,定义了旋转中心的 Y 坐标。默认为 0

返回值

无(undefined)。

异常

NoModificationAllowedError DOMException

如果属性或 SVGTransform 对象是只读的,则抛出此异常。

示例

旋转 SVG 元素

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

// Set a rotation of 45 degrees
transform.setRotate(45, 0, 0);

// Output the rotation angle
console.log(`Rotation Angle: ${transform.angle}`); // Output: 45

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGTransform__setRotate

浏览器兼容性

另见