D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
reinson
Full window
Github gist
Path animation 2
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <svg> <linearGradient id="gradient-rainbow-main" gradientUnits="userSpaceOnUse" x1="0%" y1="0%" x2="60%" y2="0%"><stop offset="0" stop-color="#EFB605"></stop><stop offset="0.06666666666666667" stop-color="#E9A501"></stop><stop offset="0.13333333333333333" stop-color="#E48405"></stop><stop offset="0.2" stop-color="#E34914"></stop><stop offset="0.26666666666666666" stop-color="#DE0D2B"></stop><stop offset="0.3333333333333333" stop-color="#CF003E"></stop><stop offset="0.4" stop-color="#B90050"></stop><stop offset="0.4666666666666667" stop-color="#A30F65"></stop><stop offset="0.5333333333333333" stop-color="#8E297E"></stop><stop offset="0.6" stop-color="#724097"></stop><stop offset="0.6666666666666666" stop-color="#4F54A8"></stop><stop offset="0.7333333333333333" stop-color="#296DA4"></stop><stop offset="0.8" stop-color="#0C8B8C"></stop><stop offset="0.8666666666666667" stop-color="#0DA471"></stop><stop offset="0.9333333333333333" stop-color="#39B15E"></stop><stop offset="1" stop-color="#7EB852"></stop></linearGradient> </svg> <script> var points = [ [480, 200], [580, 400], [680, 100], [780, 300], [180, 300], [280, 100], [380, 400] ]; var svg = d3.select("svg") .attr("fill","transparent") .attr("stroke","url(#gradient-rainbow-main)") .attr("width", 960) .attr("height", 500); var middlePoint = [0,1].map(function(i){ return d3.mean(points,function(d){return d[i]}) }); var data2 = d3.range(0,0.16,0.01).map(function(x){ return points.map(function(p){ return [ p[0]-(p[0]-middlePoint[0])*x, p[1]-(p[1]-middlePoint[1])*x, ] }) }) var data = [points] var path = svg.selectAll("path").data(data2).enter().append("path") .attr("d", function(d){return d3.line().curve(d3.curveCardinalClosed)(d)}) .attr("stroke-width",5) .each(function(d){ d.totalLength = this.getTotalLength(); }); var totalLength = path.node().getTotalLength(); var visibleLength = 300; animatePath(); function animatePath(){ path.attr("stroke-dasharray",function(d){ return visibleLength + " " + d.totalLength }) .attr("stroke-dashoffset", visibleLength) .transition() .ease(d3.easeQuadInOut) .duration(3000) .attr("stroke-dashoffset",function(d){ return -d.totalLength }) .on("end", animatePath); } </script> </body>
https://d3js.org/d3.v4.min.js