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

浏览器兼容性

另见