SVGTransformList: appendItem() 方法
SVGTransformList
接口的 appendItem()
方法会在列表的末尾插入一个新项。
插入的项是该项本身,而不是副本。
- 如果
newItem
已经存在于某个列表中,在将其插入到此列表之前,会先将其从之前的列表中移除。
语法
js
appendItem(newItem)
参数
newItem
-
要添加到列表的
SVGTransform
项。
返回值
一个 SVGTransform
对象;列表中的已添加项。
异常
NoModificationAllowedError
DOMException
-
如果
SVGTransformList
对应于只读属性,或者对象本身是只读的,则会抛出此异常。
示例
添加新变换
html
<svg width="200" height="200">
<rect width="100" height="100" fill="red" />
</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;
// Create a new translation transformation
const svgTransform = svgElement.createSVGTransform();
svgTransform.setTranslate(50, 50);
// Append the new transformation to the list
const appendedTransform = transformList.appendItem(svgTransform);
console.dir(appendedTransform); // Output: SVGTransform { type: 2, matrix: SVGMatrix, angle: 0 }
console.log(`Total number of transformations: ${transformList.numberOfItems}`); // Output: 1
规范
规范 |
---|
Scalable Vector Graphics (SVG) 2 # __svg__SVGNameList__appendItem |
浏览器兼容性
加载中…