Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG Path Overlay and Animate Out Another Path

I have an SVG of a dashed gray line. What I want to do is overlay that on top of a green SVG dashed line, and animate out the gray to reveal the green. Sorta like a meter moving from right to left.

I saw this example of how to make a dash line:

http://jsfiddle.net/ehan4/2/

and was able to do it but my line is already dashed.

I ended up doing this:

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
         viewBox="0 0 666.9 123.8" enable-background="new 0 0 666.9 123.8" xml:space="preserve">
    <path  opacity="0.4" stroke-width="3" fill="none" stroke="#66CD00" stroke-linecap="round" stroke-miterlimit="5" stroke-dasharray="1,6" d="
        M656.2,118.5c0,0-320.4-251-645.9-0.7" />
        <path id="top"  opacity="0.4" fill="none" stroke="#AEAEAE" stroke-linecap="round" stroke-miterlimit="5" stroke-dasharray="1,6" d="
    M656.2,118.5c0,0-320.4-251-645.9-0.7"/>

</svg>

var path = document.querySelector('#top');
var length = path.getTotalLength();
// Clear any previous transition
path.style.transition = path.style.WebkitTransition =
  'none';
// Set up the starting positions
path.style.strokeDasharray = 1  + ' ' + 6;
path.style.strokeDashoffset = length;

// Trigger a layout so styles are calculated & the browser
// picks up the starting position before animating
path.getBoundingClientRect();
// Define our transition
path.style.transition = path.style.WebkitTransition =
  'stroke-dashoffset 20s linear';
// Go!
path.style.strokeDashoffset = '0';

https://jsfiddle.net/ps5yLyab/

How can I overlay the two dash lines and animate out the gray?

like image 497
Kyle Calica-St Avatar asked Apr 21 '15 02:04

Kyle Calica-St


People also ask

How do I animate a path in SVG?

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 path CSS?

SVG is a very neat format to display any illustration, icon or logo on a website. Furthermore, they can be animated in CSS or JavaScript to make them more attractive.

What is SVG path?

The <path> element is the most powerful element in the SVG library of basic shapes. It can be used to create lines, curves, arcs, and more. Paths create complex shapes by combining multiple straight lines or curved lines. Complex shapes composed only of straight lines can be created as <polyline> s.


1 Answers

You can do it with a clip path.

First we add a clipPath to the SVG.

<defs>
    <clipPath id="myclip">
        <rect id="cliprect" x="100%" y="0%" width="100%" height="100%"/>
    </clipPath>
</defs>

This clip path is sized the same size as the SVG (width and height 100%) and starts with its x postion at the far right of the SVG (100%). So at the start it is not revealing anything.

Then every 10mS we reduce it's x coord by 1% (ie 100% -> 99% -> 98% etc). until it reached zero.

var cliprect = document.getElementById("cliprect");
var offsetX = 100;
var speed = 10;

function clipAdjust()
{
    cliprect.setAttribute("x", offsetX+"%");
    offsetX -= 1;
    if (offsetX >= 0) {
        window.setTimeout(clipAdjust, speed);
    }
}

window.setTimeout(clipAdjust, speed);

Working demo below:

var path = document.querySelector('#top');
		var length = path.getTotalLength();
		// Clear any previous transition
		path.style.transition = path.style.WebkitTransition =
		  'none';
		// Set up the starting positions
		path.style.strokeDasharray = 1  + ' ' + 6;
		path.style.strokeDashoffset = length;

		// Trigger a layout so styles are calculated & the browser
		// picks up the starting position before animating
		path.getBoundingClientRect();
		// Define our transition
		path.style.transition = path.style.WebkitTransition =
		  'stroke-dashoffset 20s linear';
		// Go!
		path.style.strokeDashoffset = '0';

var cliprect = document.getElementById("cliprect");
var offsetX = 100;
var speed = 10;

function clipAdjust()
{
    cliprect.setAttribute("x", offsetX+"%");
    offsetX -= 1;
    if (offsetX >= 0) {
        window.setTimeout(clipAdjust, speed);
    }
}

window.setTimeout(clipAdjust, speed);
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
		 viewBox="0 0 666.9 123.8" enable-background="new 0 0 666.9 123.8" xml:space="preserve">

    <defs>
        <clipPath id="myclip">
            <rect id="cliprect" x="100%" y="0%" width="100%" height="100%"/>
        </clipPath>
    </defs>

    <path opacity="0.4" fill="none" stroke="#AEAEAE" stroke-linecap="round"
          stroke-miterlimit="5" stroke-dasharray="1,6" stroke-width="2"
          d="M656.2,118.5c0,0-320.4-251-645.9-0.7"/>

    <g clip-path="url(#myclip)">
        <path stroke-width="3" fill="none" stroke="white"
              stroke-linecap="round" stroke-miterlimit="5"
              d="M656.2,118.5c0,0-320.4-251-645.9-0.7" />
        <path id="top"  opacity="0.4" stroke-width="3" fill="none" stroke="#66CD00"
              stroke-linecap="round" stroke-miterlimit="5" stroke-dasharray="6,6"
              d="M656.2,118.5c0,0-320.4-251-645.9-0.7" />
    </g>
  
</svg>
like image 99
Paul LeBeau Avatar answered Sep 26 '22 06:09

Paul LeBeau