大网数据广告活动
Web 标准与前端开发

SVGLength: newValueSpecifiedUnits() method

· 52CE 我爱测

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

SVG

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

Syntax

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 SVGLength.unitType property, with the exception of SVG_LENGTHTYPE_UNKNOWN.
      • SVGLength.SVG_LENGTHTYPE_NUMBER: convert to unitless number
      • SVGLength.SVG_LENGTHTYPE_PERCENTAGE: convert to percentage
      • SVGLength.SVG_LENGTHTYPE_EMS: convert to em units
      • SVGLength.SVG_LENGTHTYPE_EXS: convert to ex units
      • SVGLength.SVG_LENGTHTYPE_PX: convert to pixels
      • SVGLength.SVG_LENGTHTYPE_CM: convert to centimeters
      • SVGLength.SVG_LENGTHTYPE_MM: convert to millimeters
      • SVGLength.SVG_LENGTHTYPE_IN: convert to inches
      • SVGLength.SVG_LENGTHTYPE_PT: convert to points
      • SVGLength.SVG_LENGTHTYPE_PC: convert to picas
  • 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

Setting a length value with specific units

// 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

Browser compatibility

See also

  • SVGAnimatedLength

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。