1

I just generated myself an SVG-based loading indicator using an online service, but every time a page loads that uses it I get a warning from Chrome which informs me that SMIL animations are getting deprecated, which is rather obnoxious. In an attempt to get rid of it I decided to look into replacing the <animate> tags with Web Animations. Below is the original SVG:

<svg width='200px' height='200px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" class="uil-ring-alt">
  <circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/>
  <circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round">
    <animate attributeName="stroke-dashoffset" dur="2s" repeatCount="indefinite" from="0" to="502"/>
    <animate attributeName="stroke-dasharray" dur="2s" repeatCount="indefinite" values="150.6 100.4;1 250;150.6 100.4"/>
  </circle>
</svg>

And here is what I ended up with:

<svg width='200px' height='200px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" xmlns:xlink="http://www.w3.org/1999/xlink">
  <circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/>
  <circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round" id="line"/>
  <script type="text/javascript" xlink:href="https://cdn.rawgit.com/web-animations/web-animations-js/45d8e40300e82ff02ccfbbc78c89500de0f5616f/web-animations.min.js"></script>
  <script type="text/javascript"><![CDATA[
    var line = document.getElementById("line");
    line.animate(
      [
        {strokeDashoffset:0},
        {strokeDashoffset:502}
      ],
      { duration: 2000, iterations: Infinity }
    );
    line.animate(
      [
        {strokeDasharray:"150.6 100.4"},
        {strokeDasharray:"1 250"},
        {strokeDasharray:"150.6 100.4"}
      ],
      { duration: 2000, iterations: Infinity }
    );
  ]]></script>
</svg>

I was about to be really excited that I managed to make it work, then my smile froze on my face as soon as I noticed that the exact same SVG, when used as a background-image in CSS, refuses to animate at all (demo below; note that I inlined the SVG using a data URI here, but the same happens when the SVG is loaded using a regular URL).

body:before {
  content: '';
  display: block;
  width: 200px;
  height: 200px;
  background: url('data:image/svg+xml;utf8,<svg width="200px" height="200px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" xmlns:xlink="http://www.w3.org/1999/xlink"><circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/><circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round" id="line"/><script type="text/javascript" xlink:href="https://cdn.rawgit.com/web-animations/web-animations-js/45d8e40300e82ff02ccfbbc78c89500de0f5616f/web-animations.min.js"></script><script type="text/javascript"><![CDATA[var line = document.getElementById("line");line.animate([{strokeDashoffset:0},{strokeDashoffset:502}],{ duration: 2000, iterations: Infinity });line.animate([{strokeDasharray:"150.6 100.4"},{strokeDasharray:"1 250"},{strokeDasharray:"150.6 100.4"}],{ duration: 2000, iterations: Infinity });]]></script></svg>') no-repeat center center;
  background-size: contain;
}

Should I just ignore the warning and stay with SMIL or is there a way to make Web Animations work inside SVGs?

SeinopSys
  • 8,787
  • 10
  • 62
  • 110

1 Answers1

2

Unfortunately there is not support yet for animation with backgrounImage property.

While CSS Backgrounds and Borders Module Level 3 Editor’s Draft says “Animatable: no” for background-image at the time of writing, support for crossfading images in CSS appeared in Chrome 19 Canary. Until widespread support arrives this can be faked via image sprites and background-position or opacity. To animate gradients they must be the same type

You can read more in the following articles:

http://oli.jp/2010/css-animatable-properties/

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties

GibboK
  • 71,848
  • 143
  • 435
  • 658