I'm trying to create dynamic line that randomly moves on the page, I also want to move the tip of the line with mouse movement so I tried adding another data in "linedata" variable but it only renders 1 line.
the code below only has 1 data in "linedata" array, I tried something like
var random = { a: Math.floor(Math.random()*randNum), b: Math.floor(Math.random()*randNum), }; linedata = [ "M 0 0 L 200 " + (100+random.a), "M 100 100 L 200 " + (100+random.b) ];
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>d3 test</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script> </head> <body> <script> //Width and Height var w = 500; var h = 50; //Create SVG Element var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); var line = svg.append("path") .attr("stroke","orange") .attr("stroke-width", 7) .attr("fill","none"); //Global array var linedata = []; //random multiplier var randNum = 50; setInterval(function() { var random = { a: Math.floor(Math.random()*randNum), }; linedata = [ "M 0 0 L 200 " + (100+random.a) ]; line.data(linedata); line.attr("d", function(d) { return d; }) }, 10);