SVGLength: newValueSpecifiedUnits() method

The newValueSpecifiedUnits() method of the SVGLength interface resets the value as a number with an associated unitType, thereby replacing the values for all of the attributes on the object.

Syntax

js
newValueSpecifiedUnits(unitType, valueInSpecifiedUnits)

Parameters

unitType

A constant representing the unit type to which the length's value should be converted. This must be one of the constant values defined for the unitType property, with the exception of SVG_LENGTHTYPE_UNKNOWN.

valueInSpecifiedUnits

The numeric factor for the length value, expressed in the specified unit type.

Return value

None (undefined).

Exceptions

This method may raise a DOMException of one of the following types:

NotSupportedError DOMException

Thrown if unitType is SVG_LENGTHTYPE_UNKNOWN or not a valid unit type constant.

NoModificationAllowedError DOMException

Thrown if SVGLength corresponds to a read-only attribute or when the object itself is read-only.

Examples

undefined

Setting a length value with specific units

js
// Get an SVGLength object
const svg = document.querySelector("svg");
const length = svg.createSVGLength();

// Set a new value with specific units
length.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER, 45);
console.log(length.valueInSpecifiedUnits); // Output: 45
console.log(length.unitType); // Output: 1 (SVG_LENGTHTYPE_NUMBER)
console.log(length.value); // The value converted to the user coordinate system

// Change the value to pixels
length.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX, 100);
console.log(length.valueInSpecifiedUnits); // Output: 100
console.log(length.unitType); // Output: 5 (SVG_LENGTHTYPE_PX)

Specifications

See also