SVGTransformList: initialize() 方法

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

SVGTransformList 介面的 initialize() 方法會清除列表中的所有現有專案,並用引數指定的單個專案重新初始化列表。

如果插入的專案已存在於某個列表中,它會在插入到此列表之前從其先前列表中移除。插入的是專案本身,而不是副本。

語法

js
initialize(newItem)

引數

newItem

要插入到列表中的一個 SVGTransform 專案。

返回值

一個 SVGTransform 物件;要插入到列表中的專案。

異常

NoModificationAllowedError DOMException

如果 SVGTransformList 對應於只讀屬性,或者物件本身是隻讀的,則會丟擲此異常。

示例

使用新的變換重新初始化變換列表

html
<svg width="200" height="200" id="mySvg">
  <rect width="100" height="100" fill="blue" />
</svg>
js
const svgElement = document.querySelector("svg");
const rectElement = svgElement.querySelector("rect");

// Access the transform list of the <rect> element
const transformList = rectElement.transform.baseVal;

// Apply an initial translate transformation to the <rect> element
const translateTransform = svgElement.createSVGTransform();
translateTransform.setTranslate(50, 50);
transformList.appendItem(translateTransform);

// Number of transformations before initialization
console.log(
  `Number of transformations before initialization: ${transformList.length}`,
); // Output: 1

// Create a new scale transformation
const scaleTransform = svgElement.createSVGTransform();
scaleTransform.setScale(2, 2);

// Initialize the list with the new scale transform
transformList.initialize(scaleTransform);

// Number of transformations after initialization
console.log(
  `Number of transformations after initialization: ${transformList.length}`,
); // Output: 1

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGNameList__initialize

瀏覽器相容性

另見