SVGAngle: newValueSpecifiedUnits() 方法
SVGAngle
接口的 newValueSpecifiedUnits()
方法使用具有关联 unitType
的数字设置该值,从而替换对象上所有属性的值。
语法
js
newValueSpecifiedUnits(unitType, valueInSpecifiedUnits)
参数
unitType
-
一个表示角度值应转换为的单位类型的常量。这必须是为
unitType
属性定义的常量值之一,但SVG_ANGLETYPE_UNKNOWN
除外。SVGAngle.SVG_ANGLETYPE_DEG
: 转换为度SVGAngle.SVG_ANGLETYPE_RAD
: 转换为弧度SVGAngle.SVG_ANGLETYPE_GRAD
: 转换为百分度SVGAngle.SVG_ANGLETYPE_UNSPECIFIED
: 转换为无单位数字,解释为度
valueInSpecifiedUnits
-
角度值的数值因子,以指定的单位类型表示。
返回值
无(undefined
)。
异常
此方法可能会抛出以下类型之一的DOMException
:
NotSupportedError
DOMException
-
如果
unitType
是SVG_ANGLETYPE_UNKNOWN
或不是有效的单位类型常量,则抛出此异常。 NoModificationAllowedError
DOMException
-
如果
SVGAngle
对应于只读属性或对象本身是只读的,则抛出此异常。
示例
以度为单位设置角度
js
// Get an SVGAngle object
const svg = document.querySelector("svg");
const angle = svg.createSVGAngle();
// Set the angle's value in degrees using newValueSpecifiedUnits()
angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG, 45);
// Retrieve the angle's value in degrees
console.log(angle.value); // Output: 45
console.log(angle.unitType); // Output: 2 (SVG_ANGLETYPE_DEG)
以弧度为单位设置角度
js
// Get an SVGAngle object
const svg = document.querySelector("svg");
const angle = svg.createSVGAngle();
// Set the angle's value in radians using newValueSpecifiedUnits()
angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_RAD, Math.PI / 2);
// Retrieve the angle's value
console.log(angle.value); // Output: 90
console.log(angle.unitType); // Output: 3 (SVG_ANGLETYPE_RAD)
以百分度为单位设置角度
js
// Get an SVGAngle object
const svg = document.querySelector("svg");
const angle = svg.createSVGAngle();
// Set the angle's value in gradians using newValueSpecifiedUnits()
angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_GRAD, 100);
// Retrieve the angle's value in gradians
console.log(angle.value); // Output: 90
console.log(angle.unitType); // Output: 4 (SVG_ANGLETYPE_GRAD)
规范
规范 |
---|
Scalable Vector Graphics (SVG) 2 # __svg__SVGAngle__newValueSpecifiedUnits |
浏览器兼容性
加载中…