众所周知,我们可以使用方法 SVGElement.pauseAnimations() 暂停 SVG 动画,并且我们也可以使用方法 SVGElement.setCurrentTime() 设置动画的当前时间-第一个参数是以秒为单位的时间。一切都很顺利,但我的问题是,您能否将暂停的帧导出为光栅图像-JPG、PNG。
示例(在此,我们创建了一个带有动画的 SVG,并在时间-0.958s 处暂停了动画)。
现在,如果我们使用XMLSerializer将svg设置为图像的src。
图片已设置,但动画正常播放,现在已暂停。那么有没有一种方法可以暂停图像标签中的动画?或者使用已暂停的SVG元素并从中导出光栅图像。
示例(在此,我们创建了一个带有动画的 SVG,并在时间-0.958s 处暂停了动画)。
let svg = document.getElementById('testSvg');
svg.pauseAnimations();
svg.setCurrentTime(0.958); // keyframes times: 0s, 0.458s, 0.958s
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<svg id="testSvg" image-rendering="auto" baseProfile="basic" version="1.1" x="0px" y="0px" width="550" height="400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Scene-1" overflow="visible" transform="translate(-56 -145.5)">
<g display="none" id="Layer3_0_FILL">
<path fill="#F00" stroke="none" d="M116.3 159.95L116.3 220.95 232.8 220.95 232.8 159.95 116.3 159.95Z" test="Scene 1"/>
<animate attributeName="display" repeatCount="indefinite" dur="1s" keyTimes="0;.958;1" values="none;inline;inline"/>
</g>
<g display="none" id="Layer2_0_FILL">
<path fill="#0F0" stroke="none" d="M116.3 159.95L116.3 220.95 232.8 220.95 232.8 159.95 116.3 159.95Z" test="Scene 1"/>
<animate attributeName="display" repeatCount="indefinite" dur="1s" keyTimes="0;.458;.958;1" values="none;inline;none;none"/>
</g>
<g id="Layer1_0_FILL">
<path fill="#0F0" stroke="none" d="M78.05 139.95L78.05 240.95 271 240.95 271 139.95 78.05 139.95Z" test="Scene 1"/>
<animate attributeName="display" repeatCount="indefinite" dur="1s" keyTimes="0;.458;1" values="inline;none;none"/>
</g>
</g>
</svg>
</body>
</html>
let image = document.createElement( "image" );
let xml = new XMLSerializer().serializeToString(svg);
let data = "data:image/svg+xml;base64," + btoa(xml);
image.src = data;
document.body.appendChild();
图片已设置,但动画正常播放,现在已暂停。那么有没有一种方法可以暂停图像标签中的动画?或者使用已暂停的SVG元素并从中导出光栅图像。