SVGTransformList: initialize() 方法
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 |
浏览器兼容性
加载中…