Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG Animate in reverse once complete

I've created a basic svg animation which loops every 750ms, however once it's finished a single animation loop it jumps back to the beginning giving it an ugly hard cut look.

<animate attributeName="stop-opacity" from="0.99" to="0.20" dur="750ms" repeatCount="indefinite" />

Is it possible that once the opacity goes from 0.99 down to 0.20 it will work its way back up to 0.99 instead of instantly jumping back to the start variable?

Example fiddle

like image 265
Sam Avatar asked Jan 23 '17 20:01

Sam


People also ask

Is it possible to animate SVG?

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].

How to animate SVG image in html?

a) Using an <img> tag For example <img src="sample. svg" /> . Please note that interactivity for <img> tags are disallowed in most browsers, so in case you've selected 'Animate on mouse over' for your SVG when you've exported it, this will not work with an <img> tag. You can use an <object> tag instead.


2 Answers

You can specify multiple values by using the values attribute on animate. Using this, you could specify to go back to the original opacity or even add more opacity values:

<animate attributeName="stop-opacity" values="0.99;0.20;0.99" dur="750ms" repeatCount="indefinite" />
like image 99
tredzko Avatar answered Sep 20 '22 12:09

tredzko


Here is a working example. There is no out of the box way to have an animation go back and forth using SMIL, but you can use a workaround that starts a secondary animation after the first that is a backwards animation. This will loop and give the effect of your SVG fading in and out.

<animate id="anim1"attributeName="stop-opacity" from="0.99" to="0.20" dur="750ms" begin="0s; anim2.end" fill="freeze"/>
<animate id="anim2"attributeName="stop-opacity" from="0.20" to="0.99" dur="750ms" begin="anim1.end" fill="freeze"/>

Those are the two lines that I changed. Notice I have the second animation start once the first ends and the first one start after the second one ends (looping between the two).

like image 37
Pat Avatar answered Sep 19 '22 12:09

Pat