For the needs of a small personal game, I have to draw a racing track as an SVG path. For this, I thought that using the D3.js library seems to be perfect for my needs, but I canโt find a way to achieve what I want. My diagram is in the following form and serves for my game engine to calculate the speed and behavior of cars. It's just a sequence of lines and curves
"sectors": [ { "length": 300, "type": "line" }, { "length": 18, "type": "curve", "radius": 11, "angle": 86, "turn" : 'right' }, { "length": 100, "type": "line" }
]
I tried to use naively D3.js, and I managed to draw several paths corresponding to each sector of the scheme: straight lines and arcs. Now I want to find a way to combine all of these elements, so draw my last diagram. I followed the tutorial on the next page ( https://www.dashingd3js.com/svg-paths-and-d3js ), and I can create a path consisting of several lines that represent different lines of my circuit with this code:
var lineFunction = d3.svg.line() .x(function(d) { return d.length; }) .y(function(d) { return 0; }) .interpolate("linear"); var lineGraph = svgContainer.append("path") .attr("d", lineFunction(mySectors)) .attr("stroke", "blue") .attr("stroke-width", 2) .attr("fill", "none");
But how can I switch between drawing lines and curves?
Many thanks in advance to those who read this and who can give me an answer.
raccoon
I found something like this:
path = svg.append("path") .datum(points); path.attr("d", function (d) { var lines = new Array(); for (var i = 0; i < d.length; i++) { lines.push([d[i][0], d[i][1]]); } return line(lines); })
Is this a good track?
I try this, but I do not work ... Any idea?
svg.append("path") .datum(formatSectors) .attr("d", function (d) { var paths = new Array(); for (var i = 0; i < d.length; i++) { if (d[i].type === 'line') { var lineData = [{"x": 0, "y": 0}, {"x": d[i]['distance'], "y": d[i]['distance']}] paths.push(lineFunction(lineData)); } if (d[i].type === 'curve') { var curveData = {"radius": d[i].radius, "angle": d[i].angle}; var arcData = arcFunction(curveData); paths.push(arcData); } } return paths; }) .attr("stroke", "blue") .attr("stroke-width", 2) .attr("fill", "none");