A jsfiddle has been created here.
Looking to move a circle along a sine wave graph, triggered by a click event. The circle should stop at specific x and y value pairs on the graph before moving to the last point and looping back to the first (ideally until a stop button is pressed).
The issue currently is that the circle only moves horizontally and the delay effect is only noticeable at the start.
This is the relevant code snippet (full example in the provided link):
Circle creation:
// circle to be moved along the graph
var circle = svg.append("circle")
.attr("id", "concindi")
.attr("cx", x_scale(xval[0]))
.attr("cy", y_scale(yval[0]))
.attr("transform", "translate(" + (0) + "," + (-1 * padding + 15) + ")")
.attr("r", 6)
.style("fill", 'red');
Movement process:
var coordinates = d3.zip(xval, yval);
svg.select("#concindi").on("click", function() {
coordinates.forEach(function(ci, indi){
if (indi < (coordinates.length - 1)){
console.log(coordinates[indi + 1][0]);
console.log(coordinates[indi + 1][1]);
d3.select("#concindi")
.transition()
.delay(2000)
.duration(5000)
.ease("linear")
.attr("cx", x_scale(coordinates[indi + 1][0]))
.attr("cy", y_scale(coordinates[indi + 1][1]));
}
});
The loop might not be utilized correctly here. The goal is to move from one x/y pair to the next every 5 seconds after a 2-second pause. Currently, the delay is only visible initially and the movement is horizontal.
Any suggestions on how to correct this?