SMIL SVG动画已被弃用,可使用CSS或Web动画效果(悬停、点击)替代。

39

根据这个话题:

Firefox 38-40 SMIL问题 - 速度非常慢(在FF版本41中已解决,从2015年9月22日起)

以及这个话题:

计划不支持使用SMIL

SVG标签“animateTransform”效果不佳。将SMIL(animate标签)替换为CSS或CSS转换会更好。

CONSOLE WARNING: Please use CSS animations or Web animations instead),
which would work fast on the latest versions of Firefox and Chrome.
下一个谷歌Chrome浏览器的警告:
CONSOLE WARNING: SVG's SMIL animations ('animate', 'set', etc.) are deprecated 
and will be removed. Please use CSS animations or Web animations instead.

修订版196823:添加SMIL弃用警告


首先,我需要实现三件事:

1)鼠标悬停时的悬停效果(最简单的)

它是这样的:

<rect x="-0.5" y="-0.5" width="1" height="1" fill="white">
    <!--it makes half-visible selecting effect -->
    <set attributeName="stroke-opacity" begin="mouseover" end="mouseout" to="0.5"/>
    <!-- explicitly reverse the opacity animation on mouseout -->
    <set attributeName="stroke-opacity" begin="mouseout" end="mouseover" to="1"/>
</rect>

我去掉了set标签,给rect标签添加了类,并将其添加到CSS的悬停伪类中:

.element_tpl:hover {
    stroke-opacity: 0.5;
}

2) 更改提交到此元素后(页面加载),它会扩展几次

原来的样子:

<!--animation-->
<!--it scales a few times after change committed to this element -->
<animateTransform attributeType="XML" attributeName="transform" type="scale" dur="0.5s" keyTimes="0;0.5;0.5;1" values="1;1.12;1.12;1" repeatCount="6" fill="freeze"/>

如何在没有 animate 标签的情况下进行组织:

???


3) 点击后它会动态缩放(scale up 和 scale down)

以前的做法:

<!--it animates scale up and scale down onclick -->
    <animateTransform attributeName="transform" attributeType="XML" type="scale" from="1" to="1.15" repeatCount="1" begin="mousedown+0.2s" dur = "0.2s" fill="freeze"/>
    <animateTransform attributeName="transform" attributeType="XML" type="scale" from="1.15" to="1" repeatCount="1" begin="mouseup+0.4s" dur = "0.2s" fill="freeze"/>

如何在没有 animate 标签的情况下进行组织?尝试使用 :active,但其行为存在差异:

.element_tpl:active {
    transform: scale(1.1); 
}

这是我的模板元素的完整代码:

<g id="switcher" cursor="pointer" stroke-width="0.15">
    <g transform="scale(1,1.375)">
        <g>
            <rect x="-0.5" y="-0.5" width="1" height="1" stroke="white" pointer-events="none"/>
            <rect x="-0.5" y="-0.5" width="1" height="1" fill="white">
                <!--it makes half-visible selecting effect -->
                <set attributeName="stroke-opacity" begin="mouseover" end="mouseout" to="0.5"/>
                <!-- explicitly reverse the opacity animation on mouseout -->
                <set attributeName="stroke-opacity" begin="mouseout" end="mouseover" to="1"/>
            </rect>
            <line x1="0" y1="-0.25" x2="0" y2="0.25" stroke-width="0.17" stroke-linecap="round" pointer-events="none"/><!-- vertical on -->
            <!--animation-->
            <!--it scales a few times after change committed to this element -->
            <animateTransform attributeType="XML" attributeName="transform" type="scale" dur="0.5s" keyTimes="0;0.5;0.5;1" values="1;1.12;1.12;1" repeatCount="6" fill="freeze"/>
            <!--it animates scale up and scale down onclick -->
            <animateTransform attributeName="transform" attributeType="XML"
            type="scale" from="1" to="1.15" repeatCount="1" begin="mousedown+0.2s" dur = "0.2s"
            fill="freeze"/>
            <animateTransform attributeName="transform" attributeType="XML"
            type="scale" from="1.15" to="1" repeatCount="1" begin="mouseup+0.4s" dur = "0.2s"
            fill="freeze"/>
        </g>
    </g>
</g>

我当前工作项目中的可工作版本如下:

http://jsfiddle.net/7e2jeet0(之前仅由Firefox浏览器使用 - 因为(请注意)此处的鼠标悬停效果通过两个图形实现 - 原因是[Chrome支持SMIL和'use'一起使用,而Firefox当前不支持SMIL和'use'一起使用]/根据Robert Longson的说法)

我尝试制作等效的CSS样式,如下:

http://jsfiddle.net/7e2jeet0/1/(在Firefox中)

http://jsfiddle.net/7e2jeet0/2/(在Chrome中)


或其他元素也可做相同处理。可工作版本如下:

http://jsfiddle.net/f7o03rsr/

http://jsfiddle.net/f7o03rsr/1/

http://jsfiddle.net/f7o03rsr/2/

谢谢!


编辑1

我发现这个组合变体在Firefox中可以很好地实现悬停和鼠标按下效果,但在Chrome中仅能实现悬停效果。


我还想知道如何通过转换到CSS / Web动画来保存这些动画:

http://jsfiddle.net/e4dxx2wg/

http://jsfiddle.net/e4dxx2wg/1/


16
从Chrome浏览器中移除SMIL支持是一场灾难。目前CSS没有替代这些高级动画和交互功能的方法。SMIL非常先进且强大。我怀疑SMIL polyfill可能是唯一的解决方案。请看FakeSmile polyfill:https://launchpad.net/smil - Emanuele Sabetta
@EmanueleSabetta - 除了缺乏功能外,CSS动画的质量也比SMIL差。CSS会对渲染后的光栅元素进行动画处理,而SMIL始终在原始矢量数据上工作(至少在Chrome中是这样)。 - tomekwi
1
@EmanueleSabetta 你仍然可以使用JavaScript来应用CSS在SVG元素上以实现动画效果。看一下GreenSock GSAP(GreenSock动画平台)http://greensock.com/ - Jonathan Marzullo
1个回答

10

Chrome并没有移除SMIL支持,而是使用了Polyfill进行替代。Eric Willigers创建了一个完全基于Web Animations API实现的SMIL polyfill。您可以在此处找到它: https://github.com/ericwilligers/svg-animation


这里有一个SVG卡通图,使用了一点JS。链接为http://www.tbyrne.org/flash2svg-v3-13(来自于这个讨论https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/5o0yiO440LM/59rZqirUQNwJ)。 - Artem.Borysov
2
是的,看起来Polyfill是最好的解决方案,因为Web动画API的跨浏览器支持不足。http://caniuse.com/#search=web%20animations - Jason Lydon
请翻译以下与编��相关的内容,从英文到中文。仅返回翻译后的文本:https://github.com/webframes/smil2css(来自此讨论https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/5o0yiO440LM/59rZqirUQNwJ)。这里有一个动画:https://webframes.github.io/smil2css/。谁知道flash2svg或smil2css的相关信息? - Artem.Borysov
3
最近谷歌宣布,由于众多用户的要求,他们暂停了SMIL的弃用计划。 SMIL现在已经重新出现在Chrome浏览器中。 - Emanuele Sabetta
SVG动画已经死了吗?代码已经一年没有更新,也没有自述文件。 - Richard
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接