【问题标题】:SMIL to CSS Animation: multiple animations on single elementSMIL 到 CSS 动画:单个元素上的多个动画
【发布时间】:2017-07-04 01:39:09
【问题描述】:

现在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 可以实现。

【问题讨论】:

    标签: css animation css-animations svg-animate smil


    【解决方案1】:

    以下是将 SMIL 动画转换为等效 CSS 动画的方法:

    • 您的两个animate 标签都有dur="2s",因此CSS 动画的持续时间(animation-duration) 也将是2s。您可以使用 long-hand 属性或使用 short-hand 属性指定此值,如下面的 sn-p 所示。
    • 没有为animate 元素指定calcMode 属性,因此插值模式为linear。由于插值模式是linear,所以animation-timing-function也将是linear
    • repeatCountindefinite,因此 animation-iteration-count 将是 infinite
    • 对于stroke-dashoffset,动画只有一个从(0%)和一个到(100%)的值。因此,在 CSS 动画的关键帧中,我们将 stroke-dashoffset 指定为 0from 值)0%502to 值)100%
    • 对于stroke-dasharray,动画使用 而不仅仅是fromto。在这种情况下,有三个分号分隔的值,因此列表中的第一个值在 0% 关键帧中给出,列表中的第二个值在 50% 关键帧中给出,最后一个值在 @ 中给出987654348@ 关键帧选择器。

    #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>

    【讨论】:

    • 不客气@oligofren。乐意效劳。顺便说一句,我最近读到,甚至 Chrome 也在重新考虑他们关于弃用 SMIL 动画的决定。所以也许一切还没有丢失:)
    猜你喜欢
    • 2019-11-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多