SVGTransformList: consolidate() 方法

Baseline 已廣泛支援

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

SVGTransformList 介面的 consolidate() 方法透過將等效的變換矩陣相乘來合併單獨的 SVGTransform 物件列表,從而生成一個由型別為 SVG_TRANSFORM_MATRIX 的單個 SVGTransform 物件組成的列表。

合併操作會建立一個新的 SVGTransform 物件,作為列表中的第一個也是唯一一個項。

返回的專案是專案本身,而不是副本。對該專案的任何更改都會立即反映在列表中。

語法

js
consolidate()

引數

無。

返回值

一個即時的 SVGTransform 物件;合併後的變換。

異常

NoModificationAllowedError DOMException

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

示例

合併變換

html
<svg width="200" height="200">
  <rect
    width="100"
    height="100"
    fill="red"
    transform="translate(50,50) rotate(45)" />
</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;

// Consolidate the transformations
const consolidatedTransform = transformList.consolidate();

console.dir(consolidatedTransform); // Output: SVGTransform { type: 1, matrix: SVGMatrix, angle: 0 }
console.log(transformList.numberOfItems); // Output: 1

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGTransformList__consolidate

瀏覽器相容性

另見