【问题标题】:How do you add easing to an SVG SMIL animation?如何为 SVG SMIL 动画添加缓动?
【发布时间】:2021-05-21 16:25:06
【问题描述】:

我一直在试验 SVG 动画的 SMIL 方法,但我无法添加缓动。

动画显示了一个绘制多边形的十字准线。

这是没有缓动的工作动画:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<div>
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.2" width="100%"
        height="100%">

        <g id="crosshair_01" transform="translate(-15 -15)">
            <line x1="25" y1="15" x2="5" y2="15"
                style="fill:none;stroke:#005a84;stroke-miterlimit:10;stroke-width:2px" />
            <line x1="15" y1="5" x2="15" y2="25"
                style="fill:none;stroke:#005a84;stroke-miterlimit:10;stroke-width:2px" />
        </g>

        <path stroke-dasharray="1000" stroke="#58595B" pathLength="1000" fill="none" stroke-linejoin="round"
            stroke-dashoffset="1000" stroke-linecap="round" marker-start="url(#bm)" stroke-width="2" id="polygon_01"
            d="M 72.1 13.4 46.3 40.6 4.9 55.3 33 86.6 74.3 72.6 95.1 28.3 72.1 13.4" marker-end="url(#bm)" />


        <animate attributeName="stroke-dashoffset" fill="freeze" begin="0s; hidePolygon_01.end" from="1000"
            id="revealPolygon_01" dur="2s" xlink:href="#polygon_01" to="2000" />

        <animate attributeName="stroke-dashoffset" fill="freeze" begin="revealPolygon_01.end" from="0"
            id="hidePolygon_01" dur="2s" xlink:href="#polygon_01" to="1000" />

        <animateMotion xlink:href="#crosshair_01" begin="0s; hidePolygon_01.end" dur="2s" repeatCount="indefinite"
            additive="replace" calcMode="linear" keyTimes="0 ; 1" keyPoints="1 ; 0">
            <mpath xlink:href="#polygon_01" />
        </animateMotion>



    </svg>
</div>

在这里我尝试使用 keyTimes 和 keySplines 添加缓动:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<div>
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.2" width="100%"
        height="100%">

        <g id="crosshair_01" transform="translate(-15 -15)">
            <line x1="25" y1="15" x2="5" y2="15"
                style="fill:none;stroke:#005a84;stroke-miterlimit:10;stroke-width:2px" />
            <line x1="15" y1="5" x2="15" y2="25"
                style="fill:none;stroke:#005a84;stroke-miterlimit:10;stroke-width:2px" />
        </g>

        <path stroke-dasharray="1000" stroke="#58595B" pathLength="1000" fill="none" stroke-linejoin="round"
            stroke-dashoffset="1000" stroke-linecap="round" marker-start="url(#bm)" stroke-width="2" id="polygon_01"
            d="M 72.1 13.4 46.3 40.6 4.9 55.3 33 86.6 74.3 72.6 95.1 28.3 72.1 13.4" marker-end="url(#bm)" />


        <animate attributeName="stroke-dashoffset" fill="freeze" begin="0s; hidePolygon_01.end" from="1000"
            id="revealPolygon_01" dur="2s" xlink:href="#polygon_01" to="2000" calcMode="spline" values="1000; 1332; 1498; 1664; 1830; 2000;" keyTimes="0; 0.16; 0.33; 0.49; 0.66; 0.83; 1;" 
            keySplines="0.42 0 1 1;
                0.42 0 1 1;
                0.42 0 1 1;
                0.42 0 1 1;
                0.42 0 1 1;
                0.42 0 1 1;" />

        <animate attributeName="stroke-dashoffset" fill="freeze" begin="revealPolygon_01.end" from="0"
            id="hidePolygon_01" dur="2s" xlink:href="#polygon_01" to="1000" />

        <animateMotion xlink:href="#crosshair_01" begin="0s; hidePolygon_01.end" dur="2s" repeatCount="indefinite"
            additive="replace" calcMode="linear" keyTimes="0 ; 1" keyPoints="1 ; 0">
            <mpath xlink:href="#polygon_01" />
        </animateMotion>



    </svg>
</div>

由于某种原因,它使路径消失了。有人有什么建议吗?

到目前为止,我只尝试向第一个动画添加缓动,显然我希望在复制到其他动画之前让它工作。

【问题讨论】:

    标签: css animation svg smil


    【解决方案1】:

    您的 values 属性中的条目数有误。

    对于 n 个样条,您需要:

    • n+1 values 条目
    • n+1 keyTimes 条目
    • n keySplines 条目

    values 属性中只有六个条目。你需要七个。

    此外,您列表中的尾随分号在技术上是非法的。但我认为浏览器对此都很宽容。 更正:某些浏览器是。

    <div>
        <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.2" width="100%"
            height="100%">
    
            <g id="crosshair_01" transform="translate(-15 -15)">
                <line x1="25" y1="15" x2="5" y2="15"
                    style="fill:none;stroke:#005a84;stroke-miterlimit:10;stroke-width:2px" />
                <line x1="15" y1="5" x2="15" y2="25"
                    style="fill:none;stroke:#005a84;stroke-miterlimit:10;stroke-width:2px" />
            </g>
    
            <path stroke-dasharray="1000" stroke="#58595B" pathLength="1000" fill="none" stroke-linejoin="round"
                stroke-dashoffset="1000" stroke-linecap="round" marker-start="url(#bm)" stroke-width="2" id="polygon_01"
                d="M 72.1 13.4 46.3 40.6 4.9 55.3 33 86.6 74.3 72.6 95.1 28.3 72.1 13.4" marker-end="url(#bm)" />
    
    
            <animate attributeName="stroke-dashoffset" fill="freeze" begin="0s; hidePolygon_01.end" 
                id="revealPolygon_01" dur="2s" xlink:href="#polygon_01" calcMode="spline"
                values="1000; 1167; 1333; 1500; 1667; 1833; 2000"
                keyTimes="0; 0.16; 0.33; 0.49; 0.66; 0.83; 1" 
                keySplines="0.42 0 1 1; 0.42 0 1 1; 0.42 0 1 1; 0.42 0 1 1; 0.42 0 1 1; 0.42 0 1 1" />
    />
    
            <animate attributeName="stroke-dashoffset" fill="freeze" begin="revealPolygon_01.end" from="0"
                id="hidePolygon_01" dur="2s" xlink:href="#polygon_01" to="1000" />
    
            <animateMotion xlink:href="#crosshair_01" begin="0s; hidePolygon_01.end" dur="2s" repeatCount="indefinite"
                additive="replace" calcMode="linear" keyTimes="0 ; 1" keyPoints="1 ; 0">
                <mpath xlink:href="#polygon_01" />
            </animateMotion>
    
        </svg>
    </div>

    【讨论】:

    • 我不认为 Safari 喜欢尾随;非常喜欢。
    • 感谢罗伯特。
    • 感谢您的回复,这很有道理。
    猜你喜欢
    • 2016-09-18
    • 2018-03-06
    • 2019-10-07
    • 2015-01-14
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    相关资源
    最近更新 更多