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:
NotSupportedErrorDOMException-
如果
unitType是SVG_ANGLETYPE_UNKNOWN或不是有效的單位型別常量,則丟擲此異常。 NoModificationAllowedErrorDOMException-
如果
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 |
瀏覽器相容性
載入中…