You can do this with a clip.
First we add clipPath to SVG.
<defs> <clipPath id="myclip"> <rect id="cliprect" x="100%" y="0%" width="100%" height="100%"/> </clipPath> </defs>
This clip path has the same size as the SVG (width and height 100%), and starts at position x in the right corner of the SVG (100%). Therefore, in the beginning it does not show anything.
Then every 10mS we reduce its x by 1% (i.e. 100% → 99% → 98%, etc.). until it reaches 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();
<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>
source share