Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a circle / arc animation in SVG

Does anyone know how to define an animated arc / circle in SVG, such that the arc starts at 0 degrees and ends at 360 degrees?

like image 829
GarethOwen Avatar asked Jul 15 '10 15:07

GarethOwen


People also ask

How do I create an arc in SVG?

Arcs. The other type of curved line that can be created using SVG is the arc, called with the A command. Arcs are sections of circles or ellipses. For a given x-radius and y-radius, there are two ellipses that can connect any two points (as long as they're within the radius of the circle).

How do you animate a SVG circle stroke?

You can animate strokes by using a stroke-dasharray that has the length of your circle (2 * PI * r) and a dash offset of equal length. Play around with the animation values of your dash length and offset to create different effects. Here's an example of how to do so.

How do I create a circle path in SVG?

To make a circular path, we're going to actually make two arcs, i.e. semicircles that complete the circle in one path. As you've probably noticed in the SVG above, the attributes CX , CY , and R respectively define where the circle is drawn along the X and Y axis, while R defines the radius of the circle.

How do you animate elements in SVG?

To animate an SVG path, you specify the attributeName to be d , and then set the from and to values that specify the start and end shapes, and you can use the values attribute to specify any intermediate values you want the shape to go through in between.


2 Answers

you can paint it "by hand" using path's lineto and calculate the position of the arc:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"
   viewBox="0 0 1200 800"
   preserveAspectRatio="xMidYMid"
   style="width:100%; height:100%; position:absolute; top:0; left:0;"
   onload="drawCircle();"> 
  <script> 
    function drawCircle() {
        var i = 0;
        var circle = document.getElementById("arc");
        var angle = 0;
        var radius = 100;     
        window.timer = window.setInterval(
        function() {
            angle -=5;  
            angle %= 360;
            var radians= (angle/180) * Math.PI;
            var x = 200 + Math.cos(radians) * radius;
            var y = 200 + Math.sin(radians) * radius;
            var e = circle.getAttribute("d");
            if(i==0) {
                var d = e+ " M "+x + " " + y;
            }
            else {
                var d = e+ " L "+x + " " + y;
            }
            if (angle === -5 && i !== 0) {
                window.clearInterval(window.timer);
            }

            circle.setAttribute("d", d);
            i++;
        } 
      ,10)
    }
    </script> 

    <path d="M200,200 " id="arc" fill="none" stroke="blue" stroke-width="2" />
</svg>

http://jsfiddle.net/k99jy/138/

like image 50
räph Avatar answered Sep 19 '22 01:09

räph


You can also draw the SVG by hand using a circle and the following technique:

  1. Give the circle a stroke.
  2. Make the stroke dashed with a dash length equal to the circle's circumference.
  3. Offset the stroke by a length equal to the circle's circumference.
  4. Animate the stroke.

HTML:

<svg width="200" height="200">
  <circle class="path" cx="100" cy="100" r="96" stroke="blue" stroke-width="4" fill="none" />
</svg>

CSS:

circle {
  stroke-dasharray: /* circumference */;
  stroke-dashoffset: /* circumference */;
  animation: dash 5s linear forwards;
}

@keyframes dash {
  to {
    stroke-dashoffset: /* length at which to stop the animation */;
  }
}

jsfiddle

source: https://css-tricks.com/svg-line-animation-works/

like image 39
Raphael Rafatpanah Avatar answered Sep 19 '22 01:09

Raphael Rafatpanah