|
| 1 | +--- |
| 2 | +title: "SVGAngle: newValueSpecifiedUnits() method" |
| 3 | +short-title: newValueSpecifiedUnits() |
| 4 | +slug: Web/API/SVGAngle/newValueSpecifiedUnits |
| 5 | +page-type: web-api-instance-method |
| 6 | +browser-compat: api.SVGAngle.newValueSpecifiedUnits |
| 7 | +--- |
| 8 | + |
| 9 | +{{APIRef("SVG")}} |
| 10 | + |
| 11 | +The `newValueSpecifiedUnits()` method of the {{domxref("SVGAngle")}} interface sets the value to a number with an associated {{domxref("SVGAngle.unitType", "unitType")}}, thereby replacing the values for all of the attributes on the object. |
| 12 | + |
| 13 | +## Syntax |
| 14 | + |
| 15 | +```js-nolint |
| 16 | +svgAngle.newValueSpecifiedUnits(unitType, valueInSpecifiedUnits) |
| 17 | +``` |
| 18 | + |
| 19 | +### Parameters |
| 20 | + |
| 21 | +- `unitType` |
| 22 | + |
| 23 | + - : A constant representing the unit type to which the angle's value should be converted. This must be one of the constant values defined for the {{domxref("SVGAngle.unitType", "unitType")}} property, with the exception of `SVG_ANGLETYPE_UNKNOWN`. |
| 24 | + - `SVGAngle.SVG_ANGLETYPE_DEG`: convert to degrees |
| 25 | + - `SVGAngle.SVG_ANGLETYPE_RAD`: convert to radians |
| 26 | + - `SVGAngle.SVG_ANGLETYPE_GRAD`: convert to gradians |
| 27 | + - `SVGAngle.SVG_ANGLETYPE_UNSPECIFIED`: convert to a unitless number, interpreted as degrees |
| 28 | + |
| 29 | +- `valueInSpecifiedUnits` |
| 30 | + - : The numeric factor for the angle value, expressed in the specified unit type. |
| 31 | + |
| 32 | +### Return value |
| 33 | + |
| 34 | +None ({{jsxref('undefined')}}). |
| 35 | + |
| 36 | +### Exceptions |
| 37 | + |
| 38 | +This method may raise a {{domxref("DOMException")}} of one of the following types: |
| 39 | + |
| 40 | +- `NotSupportedError` {{domxref("DOMException")}} |
| 41 | + |
| 42 | + - : Thrown if `unitType` is `SVG_ANGLETYPE_UNKNOWN` or not a valid unit type constant. |
| 43 | + |
| 44 | +- `NoModificationAllowedError` {{domxref("DOMException")}} |
| 45 | + - : Thrown if {{domxref("SVGAngle")}} corresponds to a read-only attribute or when the object itself is read-only. |
| 46 | + |
| 47 | +## Examples |
| 48 | + |
| 49 | +### Setting an angle in degrees |
| 50 | + |
| 51 | +```js |
| 52 | +// Get an SVGAngle object |
| 53 | +const svg = document.querySelector("svg"); |
| 54 | +const angle = svg.createSVGAngle(); |
| 55 | + |
| 56 | +// Set the angle's value in degrees using newValueSpecifiedUnits() |
| 57 | +angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG, 45); |
| 58 | + |
| 59 | +// Retrieve the angle's value in degrees |
| 60 | +console.log(angle.value); // Output: 45 |
| 61 | +console.log(angle.unitType); // Output: 2 (SVG_ANGLETYPE_DEG) |
| 62 | +``` |
| 63 | + |
| 64 | +### Setting an angle in radians |
| 65 | + |
| 66 | +```js |
| 67 | +// Get an SVGAngle object |
| 68 | +const svg = document.querySelector("svg"); |
| 69 | +const angle = svg.createSVGAngle(); |
| 70 | + |
| 71 | +// Set the angle's value in radians using newValueSpecifiedUnits() |
| 72 | +angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_RAD, Math.PI / 2); |
| 73 | + |
| 74 | +// Retrieve the angle's value |
| 75 | +console.log(angle.value); // Output: 90 |
| 76 | +console.log(angle.unitType); // Output: 3 (SVG_ANGLETYPE_RAD) |
| 77 | +``` |
| 78 | + |
| 79 | +### Setting an angle in gradians |
| 80 | + |
| 81 | +```js |
| 82 | +// Get an SVGAngle object |
| 83 | +const svg = document.querySelector("svg"); |
| 84 | +const angle = svg.createSVGAngle(); |
| 85 | + |
| 86 | +// Set the angle's value in gradians using newValueSpecifiedUnits() |
| 87 | +angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_GRAD, 100); |
| 88 | + |
| 89 | +// Retrieve the angle's value in gradians |
| 90 | +console.log(angle.value); // Output: 90 |
| 91 | +console.log(angle.unitType); // Output: 4 (SVG_ANGLETYPE_GRAD) |
| 92 | +``` |
| 93 | + |
| 94 | +## Specifications |
| 95 | + |
| 96 | +{{Specifications}} |
| 97 | + |
| 98 | +## Browser compatibility |
| 99 | + |
| 100 | +{{Compat}} |
| 101 | + |
| 102 | +## See also |
| 103 | + |
| 104 | +- {{domxref("SVGAnimatedAngle")}} |
0 commit comments