现在SMIL is dying我想将我的简单动画 SVG 转换为使用 CSS 动画,但我不太确定如何进行映射。在这个特定的 svg 中有两个动画元素。
#embedded {
color: red;
}
<svg id="embedded" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid">
<circle cx="50" cy="50" r="45" stroke="rgba(43,43,43,0.3)" fill="none" stroke-width="10" stroke-linecap="round"/>
<circle cx="50" cy="50" r="45" stroke="currentColor" 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>
虽然我一开始似乎对这些 CSS 规则还不错,但很快就卡住了
stroke-dasharray: 150.6 100.4 1 250 150.6 100.4;
animation-duration: 2s;
animation-iteration-count: infinite;
stroke-dashoffset: ?;
完整的映射是什么样的?有可能吗? Sara Soueidan说并不是所有的动画都可以使用 CSS 而可以使用 SMIL。
最佳答案
下面是如何将 SMIL 动画转换为等效的 CSS 动画:
- 您的
animate
标签都有dur="2s"
,因此 CSS 动画的持续时间 (animation-duration
) 也将是2 秒
。您可以使用长手属性或短手属性指定此值,如以下代码段所示。 - 没有为您的
animate
元素指定calcMode
属性,因此插值模式为linear
。由于插值模式是线性
,animation-timing-function
也将是线性
。 repeatCount
是不确定的
,因此animation-iteration-count
将是无限的
。- 对于
stroke-dashoffset
,动画只有一个从 (0%) 和一个到 (100%) 的值。因此,在 CSS 动画的关键帧中,我们将stroke-dashoffset
指定为0
(from
值)在0%
和502
(到
值)在100%
。 - 对于
stroke-dasharray
,动画使用了values 而不仅仅是from
和到
。在这种情况下,有三个分号分隔的值,因此列表中的第一个值在0%
关键帧中给出,列表中的第二个值在50%< 中给出
关键帧,最后一个在100%
关键帧选择器中给出。
#embedded, #embedded2 {
color: red;
width: 200px;
height: 200px;
}
#embedded circle#animated {
animation: demo 2s infinite linear;
}
@keyframes demo {
0% {
stroke-dashoffset: 0;
stroke-dasharray: 150.6 100.4;
}
50% {
stroke-dasharray: 1 250;
}
100% {
stroke-dashoffset: 502;
stroke-dasharray: 150.6 100.4;
}
}
<svg id="embedded" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid">
<circle cx="50" cy="50" r="45" stroke="rgba(43,43,43,0.3)" fill="none" stroke-width="10" stroke-linecap="round" />
<circle cx="50" cy="50" r="45" stroke="currentColor" fill="none" stroke-width="6" stroke-linecap="round" id="animated">
</circle>
</svg>
<svg id="embedded2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid">
<circle cx="50" cy="50" r="45" stroke="rgba(43,43,43,0.3)" fill="none" stroke-width="10" stroke-linecap="round" />
<circle cx="50" cy="50" r="45" stroke="currentColor" 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>
关于css - SMIL 到 CSS 动画 : multiple animations on single element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42248616/