当用作背景图像时,SVG 中的 Web 动画会中断
Web Animation inside SVG breaks when used as a background image
我刚刚使用在线服务为自己生成了一个基于 SVG 的加载指示器,但每次加载使用它的页面时,我都会收到来自 Chrome 的警告,通知我 SMIL 动画已被弃用,这比较讨厌。为了摆脱它,我决定研究用网络动画替换 <animate>
标签。以下是原始 SVG:
<svg width='200px' height='200px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" class="uil-ring-alt">
<circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/>
<circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round">
<animate attributeName="stroke-dashoffset" dur="2s" repeatCount="indefinite" from="0" to="502"/>
<animate attributeName="stroke-dasharray" dur="2s" repeatCount="indefinite" values="150.6 100.4;1 250;150.6 100.4"/>
</circle>
</svg>
这是我最终得到的结果:
<svg width='200px' height='200px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" xmlns:xlink="http://www.w3.org/1999/xlink">
<circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/>
<circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round" id="line"/>
<script type="text/javascript" xlink:href="https://cdn.rawgit.com/web-animations/web-animations-js/45d8e40300e82ff02ccfbbc78c89500de0f5616f/web-animations.min.js"></script>
<script type="text/javascript"><![CDATA[
var line = document.getElementById("line");
line.animate(
[
{strokeDashoffset:0},
{strokeDashoffset:502}
],
{ duration: 2000, iterations: Infinity }
);
line.animate(
[
{strokeDasharray:"150.6 100.4"},
{strokeDasharray:"1 250"},
{strokeDasharray:"150.6 100.4"}
],
{ duration: 2000, iterations: Infinity }
);
]]></script>
</svg>
我正要为我设法使它工作而感到非常兴奋,然后当我注意到完全相同的 SVG,当用作 background-image
在 CSS,完全拒绝动画(下面的演示;请注意,我在这里使用数据 URI 内联了 SVG,但是当使用常规 URL 加载 SVG 时也会发生同样的情况)。
body:before {
content: '';
display: block;
width: 200px;
height: 200px;
background: url('data:image/svg+xml;utf8,<svg width="200px" height="200px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" xmlns:xlink="http://www.w3.org/1999/xlink"><circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/><circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round" id="line"/><script type="text/javascript" xlink:href="https://cdn.rawgit.com/web-animations/web-animations-js/45d8e40300e82ff02ccfbbc78c89500de0f5616f/web-animations.min.js"></script><script type="text/javascript"><![CDATA[var line = document.getElementById("line");line.animate([{strokeDashoffset:0},{strokeDashoffset:502}],{ duration: 2000, iterations: Infinity });line.animate([{strokeDasharray:"150.6 100.4"},{strokeDasharray:"1 250"},{strokeDasharray:"150.6 100.4"}],{ duration: 2000, iterations: Infinity });]]></script></svg>') no-repeat center center;
background-size: contain;
}
我应该忽略警告并继续使用 SMIL 还是有办法让 Web 动画在 SVG 中工作?
遗憾的是,backgrounImage
属性 的动画尚不支持。
While CSS Backgrounds and Borders Module Level 3 Editor’s Draft says
“Animatable: no” for background-image at the time of writing, support
for crossfading images in CSS appeared in Chrome 19 Canary. Until
widespread support arrives this can be faked via image sprites and
background-position or opacity. To animate gradients they must be the
same type
您可以在以下文章中阅读更多内容:
http://oli.jp/2010/css-animatable-properties/
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties
我刚刚使用在线服务为自己生成了一个基于 SVG 的加载指示器,但每次加载使用它的页面时,我都会收到来自 Chrome 的警告,通知我 SMIL 动画已被弃用,这比较讨厌。为了摆脱它,我决定研究用网络动画替换 <animate>
标签。以下是原始 SVG:
<svg width='200px' height='200px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" class="uil-ring-alt">
<circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/>
<circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round">
<animate attributeName="stroke-dashoffset" dur="2s" repeatCount="indefinite" from="0" to="502"/>
<animate attributeName="stroke-dasharray" dur="2s" repeatCount="indefinite" values="150.6 100.4;1 250;150.6 100.4"/>
</circle>
</svg>
这是我最终得到的结果:
<svg width='200px' height='200px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" xmlns:xlink="http://www.w3.org/1999/xlink">
<circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/>
<circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round" id="line"/>
<script type="text/javascript" xlink:href="https://cdn.rawgit.com/web-animations/web-animations-js/45d8e40300e82ff02ccfbbc78c89500de0f5616f/web-animations.min.js"></script>
<script type="text/javascript"><![CDATA[
var line = document.getElementById("line");
line.animate(
[
{strokeDashoffset:0},
{strokeDashoffset:502}
],
{ duration: 2000, iterations: Infinity }
);
line.animate(
[
{strokeDasharray:"150.6 100.4"},
{strokeDasharray:"1 250"},
{strokeDasharray:"150.6 100.4"}
],
{ duration: 2000, iterations: Infinity }
);
]]></script>
</svg>
我正要为我设法使它工作而感到非常兴奋,然后当我注意到完全相同的 SVG,当用作 background-image
在 CSS,完全拒绝动画(下面的演示;请注意,我在这里使用数据 URI 内联了 SVG,但是当使用常规 URL 加载 SVG 时也会发生同样的情况)。
body:before {
content: '';
display: block;
width: 200px;
height: 200px;
background: url('data:image/svg+xml;utf8,<svg width="200px" height="200px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" xmlns:xlink="http://www.w3.org/1999/xlink"><circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/><circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round" id="line"/><script type="text/javascript" xlink:href="https://cdn.rawgit.com/web-animations/web-animations-js/45d8e40300e82ff02ccfbbc78c89500de0f5616f/web-animations.min.js"></script><script type="text/javascript"><![CDATA[var line = document.getElementById("line");line.animate([{strokeDashoffset:0},{strokeDashoffset:502}],{ duration: 2000, iterations: Infinity });line.animate([{strokeDasharray:"150.6 100.4"},{strokeDasharray:"1 250"},{strokeDasharray:"150.6 100.4"}],{ duration: 2000, iterations: Infinity });]]></script></svg>') no-repeat center center;
background-size: contain;
}
我应该忽略警告并继续使用 SMIL 还是有办法让 Web 动画在 SVG 中工作?
遗憾的是,backgrounImage
属性 的动画尚不支持。
While CSS Backgrounds and Borders Module Level 3 Editor’s Draft says “Animatable: no” for background-image at the time of writing, support for crossfading images in CSS appeared in Chrome 19 Canary. Until widespread support arrives this can be faked via image sprites and background-position or opacity. To animate gradients they must be the same type
您可以在以下文章中阅读更多内容:
http://oli.jp/2010/css-animatable-properties/
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties