【问题标题】:How to achieve this svg animation?如何实现这个 svg 动画?
【发布时间】:2019-10-05 08:55:37
【问题描述】:

世界。我是一个菜鸟编码员,希望第一次在这里回答我的问题!

我正在尝试为我自己的网站获取类似these(横幅下方的三个动画)svg 动画。

/* CSS */

svg {
      max-width: 95%;
      max-height: 95%;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      margin: auto;
    }
    /* HTML */ 

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="212px"
    	 height="154px" viewBox="0 0 212 154" style="enable-background:new 0 0 212 154;" xml:space="preserve">
    <style type="text/css">
    	.st0{fill:#999999;}
    	.st1{fill:none;stroke:#999999;stroke-miterlimit:10;}
    </style>
    <defs>
    </defs>
    <g>
    	<path class="st0" d="M211,1v152H1V1H211 M212,0H0v154h212V0L212,0z"/>
    </g>
    <line class="st1" x1="62.5" y1="1" x2="62.5" y2="153"/>
    <polyline class="st1" points="1,97.65 14.31,97.65 62,97.65 "/>
    <line class="st1" x1="14.39" y1="153" x2="14.39" y2="98"/>
    <line class="st1" x1="62.1" y1="125.82" x2="14.9" y2="125.82"/>
    </svg>

我已经通过 illustrator 制作了 svg,然后将代码导出到我选择的编码程序中,但是我不确定如何实际为线条设置动画,以便它从一个点开始并以不同的颜色跟踪到另一个点,各个方向。

希望我在这里有意义,任何帮助都会很棒。如果您有任何解决方案或资源可以帮助我实现这一目标,请告诉我。

【问题讨论】:

    标签: javascript html css animation svg


    【解决方案1】:

    您可能想查看这篇文章,了解有关如何为 SVG 线条设置动画的更多信息:https://css-tricks.com/svg-line-animation-works/

    这是一个使用 SVG 形状的示例:

    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="212px"
            height="154px" viewBox="0 0 212 154" style="enable-background:new 0 0 212 154;" xml:space="preserve">
        <style type="text/css">
            .st0{fill:#999999;}
            .st1{fill:none;stroke:#999999;stroke-miterlimit:10;}
        </style>
        <defs>
        </defs>
        <g>
            <path class="st0" d="M211,1v152H1V1H211 M212,0H0v154h212V0L212,0z"/>
        </g>
        <line  class="st1" x1="62.5" y1="1" x2="62.5" y2="153"/>
        <polyline  class="st1" points="1,97.65 14.31,97.65 62,97.65 "/>
        <line class="st1" x1="14.39" y1="153" x2="14.39" y2="98"/>
        <line class="st1" x1="62.1" y1="125.82" x2="14.9" y2="125.82"/>
    </svg>
    
    svg {
        max-width: 95%;
        max-height: 95%;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        margin: auto;
    }
    svg .st0{
    stroke:#4444ff;
    stroke-width:2;
    stroke-dasharray: 1000;
    stroke-dashoffset: 1000;
    animation: dash 5s linear forwards;
    }
    @keyframes dash {
    to {
    stroke-dashoffset: 0;
    }
    }
    

    Codepen:https://codepen.io/kostasx/pen/yLLLeXJ?editors=1100

    【讨论】:

    • 我想我已经取得了一些进展,我所做的只是创建另一个 svg 作为我的动画,我现在只需要弄清楚如何将它直接放在我的第一个svg,并让它从头到尾动画codepen.io/DavidPicarazzi/project/editor/XJEVag
    • 您可以通过将第二个形状中的部分放入第一个形状中来将两个形状合并在一起。你可以在这里查看我更新的 codepen:codepen.io/kostasx/pen/yLLLeXJ?editors=1100
    • 是从原始动画中出现的蓝线只是自己绘制的 svgs 还是它们是在 html 中创建并在 css 中设置样式以某种方式移动的实际线?
    • 一切都是 SVG。 @DavidPicarazzi。
    【解决方案2】:

    我有一个类似的项目,其中动画提出了挑战。我最初尝试使用视频,然后是 GIF,但最终使用 SVG。

    虽然由于每个动画都是独一无二的,因此没有像调用方法这样的直接解决方案,但这里有一些选项可以帮助您入门。

    1. SVG 动画库,例如 GreenSock 或 SnapSVG
    2. 使用 Airbnb Lottie - 您可以使用 Adob​​e Illustrator 进行设计,Lottie 将输出为 JSON

    我最终使用了 Lotties,因为 JSON 结构是一种方便的方法。

    【讨论】:

    • Lotties 看起来超级有用。我以为您只能将 After Effects 与它一起使用?是否有理由放置动画的实际代码,而不是简单地将其添加为视频?这个过程会是这样吗:我在 after effects 中创建动画,然后使用 bodymovin 导出它?
    • 视频很混乱,尤其是在播放宽高比的情况下。洛蒂可能是要走的路
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 2015-06-26
    • 2021-05-26
    • 2018-03-09
    • 2016-02-03
    • 2020-06-16
    相关资源
    最近更新 更多