I have a set of coordinates (x,y,t). x and y represent the position on a 2D space, and t is at which time ( in ms ) they appear
For each point, I draw a rectangle, and draw a line to the next one.
The rectangles do appear one by one, respecting the time at which they appear. The lines do not.
I've tried using setTimeout, setInterval and requestAnimationFrame. It doesn't seem to matter which one I use I always get the problem
Here's the code that plays the animation :
let i = 0; // i represents the time
const iterate = function () {
if (i > max_t) { // when i has reached the last point in time, the function stops
return;
}
data.forEach(function (e, j) { // data is an array of set of (x,y,t) coordinates
if (e.interval[indexes[j]] == i) { // e.interval[indexes[j]] is t mentioned earlier
indexes[j]++; // increment the pointer so we can draw the next point
ctx.fillStyle = "#000000"; // this part is where we plot the dots and draw the lines
let plot_x = middle_width + (e.x[indexes[j]]);
let plot_y = middle_height + (e.y[indexes[j]]);
ctx.strokeStyle = colors[j];
if (indexes[j] > 0) { // this doesn't seem to wait for the timeout to execute
ctx.moveTo(plot_x, plot_y);
ctx.lineTo(middle_width + e.x[indexes[j] - 1], middle_height + e.y[indexes[j] - 1]);
ctx.stroke();
}
ctx.fillRect(plot_x - 3, plot_y - 3, 6, 6); // but this does seem to wait for the timeout to execute
ctx.fillStyle = colors[j];
ctx.fillRect(plot_x - 2, plot_y - 2, 4, 4);
}
});
i++;
window.setTimeout(iterate, 1);
};
window.setTimeout(iterate, 1);
Here's a dummy data :
data = [
{
"x": [1,2,3],
"y": [4,1,0],
"z": [1,1,3],
"interval": [23,67,100]
},
{
"x": [1],
"y": [3],
"z": [10],
"interval": [1]
}
]
The result is as such : the points are plotted along with the time. But the lines do not, here's a gif : https://i.stack.imgur.com/i2qsN.jpg
Thank you