Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate svg path fill using css

Tags:

css

animation

svg

It is possible to change svg fill using css. But I didn't manage to create an animation. Here is my svg object:

<svg version="1.1" id="tick" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="100%" height="100%" viewBox="0 0 60 60" enable-background="new 0 0 60 60" xml:space="preserve"> 
<rect id="fill" x="21" y="26" width="60" height="60"/>
</svg>

And, using SASS:

@include keyframes(loading)
  0%
    fill: black
  50%
    fill: white
  100%
    fill: black
#fill
  fill: white
  @include animation(loading 3s infinite)

What am I doing wrong?

like image 667
vadirn Avatar asked Feb 07 '13 16:02

vadirn


People also ask

Can you animate fill SVG?

Animating fill requires SVG elements instead of CSS: linearGradient allows us to define multiple colors to fill a shape. Yes, a gradient. stop defines how and where to place colors inside the linearGradient.

How do I animate a path in SVG?

External JavaScript To animate this path as if it's being drawn gradually and smoothly on the screen, you have to set the dash (and gap) lengths, using the stroke-dasharray attribute, equal to the path length. This is so that the length of each dash and gap in the dashed curve is equal to the length of the entire path.

Can you animate SVG with CSS?

Adding classes to the SVG allows CSS to select the individual shapes within the image. This means you can animate different shapes of the image at different times, creating a more complex effect.

Can you animate clip path CSS?

When you animate an element, you use clip-path() to create a clipping region during the stages of the animation, creating the illusion that the element is indeed changing its shape. You can clip the element both before and when you animate it.


1 Answers

EDIT

Okay it looks like something like this should work.

    <svg version="1.1" id="tick" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="100%" height="100%" viewBox="0 0 60 60" enable-background="new 0 0 60 60" xml:space="preserve"> 
  <style type="text/css" >
        <![CDATA[

          @keyframes fill {
              0% {
                  fill: black;
              }
              50% {
                  fill: white;
              }
              100% {
                  fill: black;
              }
          }

          #fill {
              fill: black;
              animation-name: fill;
              animation-duration: 3s;
              animation-iteration-count: infinite;
          }

        ]]>
    </style>
    <rect id="fill" x="21" y="26" width="60" height="60"/>
</svg>

Here is the example: http://jsbin.com/ayiheb/2/edit

like image 52
gotohales Avatar answered Sep 27 '22 20:09

gotohales