【发布时间】:2019-01-21 04:29:10
【问题描述】:
众所周知,我们可以使用 SVGElement.pauseAnimations() 方法暂停 svg 动画,我们还可以使用 SVGElement.setCurrentTime() 方法设置动画当前时间 - 以秒为单位的第一个参数。一切正常,但我的问题是,您可以将暂停的帧导出到光栅图像 -jpg, png 吗?
示例(这里我们创建一个带有动画的 svg 并在时间 - 0.958 秒处暂停动画)
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>
现在,如果我们使用 XMLSerializer 将 svg 设置为图像的 src。
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 元素并以某种方式从中导出光栅图像。
【问题讨论】:
标签: javascript image svg export