Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG Fade in Animation

Hi I have an SVG which is 5 arrow shapes stacked. I am wanting to fade in each arrow from the bottom up. When the top arrow has faded in, I want the first one to fade out, then the second etc. And then start the animation again by fading in each arrow.

Here is a code pen of the SVG code: https://codepen.io/anon/pen/gyJZEy

<svg xmlns="http://www.w3.org/2000/svg" width="80" height="80" viewBox="0 0 122.91 110.38">
    <defs>
        <style>.hg{fill:#ee2330;opacity:0}</style>
    </defs>
    <g id="Layer_2" data-name="Layer 2">
        <g id="Layer_1-2" data-name="Layer 1">
            <rect>
                <animate id="hg0" begin="0;hg0.end" dur="8s"
                attributeName="visibility" from="hide" to="hide"/>
            </rect>
            <path class="hg" d="M61.65 86.78l-.16-.06L0 109.38v.99l61.49-22.65 61.43 22.66v-.99L61.65 86.78z">
                <animate id="hg1" attributeName="opacity" values="0;1;0" dur="4s" begin="hg0.begin;hg0.end" repeatCount="indefinite"/>
            </path>
            <path class="hg" d="M0 87.69v1.49l61.49-22.66 61.43 22.67v-1.48L61.49 65.04 0 87.69z">
                <animate id="hg2" attributeName="opacity" values="0;1;0" dur="4s" begin="hg0.begin+1s;hg0.end+1s" repeatCount="indefinite"/>
            </path>
            <path class="hg" d="M0 66.05v1.97l61.49-22.66 61.43 22.67v-1.97L61.49 43.39 0 66.05z">
                <animate id="hg3" attributeName="opacity" values="0;1;0" dur="4s" begin="hg0.begin+2s;hg0.end+2s" repeatCount="indefinite"/>
            </path>
            <path class="hg" d="M61.49 21.65L0 44.31v2.64l61.49-22.66 61.43 22.67v-2.64l-61-22.51-.43-.16z">
                <animate id="hg4" attributeName="opacity" values="0;1;0" dur="4s" begin="hg0.begin+3s;hg0.end+3s" repeatCount="indefinite"/>
            </path>
            <path class="hg" d="M0 22.66v3.13L61.49 3.13l61.43 22.67v-3.13L61.49 0 0 22.66z">
                <animate id="hg5" attributeName="opacity" values="0;1;0" dur="4s" begin="hg0.begin+4s;hg0.end+4s" repeatCount="indefinite"/>
            </path>
        </g>
    </g>
</svg>
like image 262
Pedro Avatar asked Apr 30 '19 23:04

Pedro


People also ask

Can SVGs be animated?

SVG supports the ability to change vector graphics over time, to create animated effects. SVG content can be animated in the following ways: Using SVG's animation elements [svg-animation]. SVG document fragments can describe time-based modifications to the document's elements.

Can SVG animate CSS?

While we can animate SVGs with CSS, there are other tools we can use to handle and create animations. SVG animations can get a little complicated, but the following tools make it extremely simple for us to animate SVGs.

How do I make an SVG animation?

How it create SVG Animations: Select the Frame you want to animate and click on Enable SVG Export. Select a node within that Frame to set up animations such as X Position, Y Position, Scale, Rotation and Opacity. Use the built-in live-preview to tweak your animations until you're happy with the result.


1 Answers

The simplest approach is to use a keyTimes attribute to control the fade in and fade out timing.

We have five arrows. The first one takes one second to fade in, then waits for the other four to fade in. Then takes one second to fade out again, and waits for the other four to do the same.

This means that the animation takes 10s in total for each arrow. And there are five key times in that animation:

  • at 0s, opacity value is 0
  • at 1s, opacity value is 1
  • at 5s, opacity value is 1
  • at 6s, opacity value is 0
  • at 10s, opacity value is 0

The keyTimes attribute works in conjunction with the values attribute. It specifies at which time in the animation the opacity has to be at each value. So it needs to have the same number of values as there are in the values attribute. The other thing you need to know about keyTimes values is that its time values have to be in fractions of the duration. So for the second time above (1s), we need to use 0.1 (1s of 10s).

So our values attribute should be "0; 1; 1; 0; 0", and our keyTimes attribute will be "0; 0.1; 0.5; 0.6; 1".

Then to offset the start of each arrow's animation, we just use staggered begin times.

<svg xmlns="http://www.w3.org/2000/svg" width="80" height="80" viewBox="0 0 122.91 110.38">
    <defs>
        <style>.hg{fill:#ee2330;opacity:0}</style>
    </defs>
    <g id="Layer_2" data-name="Layer 2">
        <g id="Layer_1-2" data-name="Layer 1">
            <path class="hg" d="M61.65 86.78l-.16-.06L0 109.38v.99l61.49-22.65 61.43 22.66v-.99L61.65 86.78z">
                <animate attributeName="opacity" dur="10s" keyTimes="0;0.1;0.5;0.6;1" values="0;1;1;0;0" repeatCount="indefinite"/>
            </path>
            <path class="hg" d="M0 87.69v1.49l61.49-22.66 61.43 22.67v-1.48L61.49 65.04 0 87.69z">
                <animate attributeName="opacity" dur="10s" keyTimes="0;0.1;0.5;0.6;1" values="0;1;1;0;0" repeatCount="indefinite" begin="1s"/>
            </path>
            <path class="hg" d="M0 66.05v1.97l61.49-22.66 61.43 22.67v-1.97L61.49 43.39 0 66.05z">
                <animate attributeName="opacity" dur="10s" keyTimes="0;0.1;0.5;0.6;1" values="0;1;1;0;0" repeatCount="indefinite" begin="2s"/>
            </path>
            <path class="hg" d="M61.49 21.65L0 44.31v2.64l61.49-22.66 61.43 22.67v-2.64l-61-22.51-.43-.16z">
                <animate attributeName="opacity" dur="10s" keyTimes="0;0.1;0.5;0.6;1" values="0;1;1;0;0" repeatCount="indefinite" begin="3s"/>
            </path>
            <path class="hg" d="M0 22.66v3.13L61.49 3.13l61.43 22.67v-3.13L61.49 0 0 22.66z">
                <animate attributeName="opacity" dur="10s" keyTimes="0;0.1;0.5;0.6;1" values="0;1;1;0;0" repeatCount="indefinite" begin="4s"/>
            </path>
        </g>
    </g>
</svg>
like image 125
Paul LeBeau Avatar answered Sep 22 '22 07:09

Paul LeBeau