I was working against a financial library that requires me to provide realtime updates to a line chart in canvas. To optimize the process of updating the chart, I thought of just updated the latest data-point rather than clearing and re-drawing the entire canvas.
When re-rendering only the latest datapoint frequently, I'm noticing that the line is not clear(there's a spot in the image).
Here's how the line looks initially(no redraw)
And after a few updates of calling "partial_rerender", this is how the line looks:
Notice the "joining" of the 2 lines is visible with a darker shade.
Is there a way to achieve partial re-drawing of lines only for the latest data point & not drawing the entire line completely?
Reference code
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.lineWidth = 2;
ctx.lineJoin = "miter";
ctx.moveTo(20, 50);
ctx.lineTo(100, 50);
ctx.save();
ctx.lineTo(150, 60);
ctx.stroke();
/*Call this every second to re-draw only the latest data point*/
function partial_rerender(){
ctx.clearRect(100,50, 400,400);
ctx.restore();
ctx.lineTo(150, 60);
ctx.stroke();
}