Im new to javascript. I just want to generate some random points every frame and draw a graph using them. But the program doesnt clear the canvas before drawing. So the program just keeps adding new pixels. In the end i want to draw graphs acording to the value of currencies (EUR,USD...). Here is a picture with the graph generated ony once: https://ibb.co/fn1qXd Here is a picture when i generate the graph every frame: https://ibb.co/dmQCdJ
<script>
let canvas = document.getElementById("myCanvas");
let context = canvas.getContext("2d");
function loop()
{
context.clearRect(0, 0, canvas.width, canvas.height);
let growth=20;
points[0]=0;
for(i=1;i<points.length;i++)
points[i]=points[i-1]+(Math.random()*growth-growth/2);
context.strokeStyle="#42BBDB";
for(i=0;i<points.length-1;i++)
{
context.lineWidth=1;
context.moveTo(i, points[i]+160);
context.lineTo(i+1, points[i+1]+160);
}
context.stroke();
requestAnimationFrame(loop);
}
for(i=0;i<values.length;i++)
for(j=0;j<values.length;j++)
if(i!=j)
{
let name=values[i]+" "+values[j];
points=new Array(400);
points[0]=0;
for(k=1;k<points.length;k++)
{
let growth=20;
points[k]=points[k-1]+(Math.random()*growth-growth/2);
}
}
loop();
</script>