Tengo el siguiente script, que dibuja rectángulos rojos en el lienzo y el problema es que los rectángulos se oscurecen con el tiempo. ¿Por qué?
HTML:
<canvas id="myCanvas" width="500" height="150" style="border:1px solid black;"> </canvas>JavaScript:
canvas = document.getElementById("myCanvas"); ctx = canvas.getContext("2d"); const inter = setInterval(start, 1000); i = 0; myarr = []; function drawarray(arr){ for ( var k = 0; k<arr.length; k++){ // I tried to use Math random, but it also don't work // I tried rgba, but also don't work ctx.globalAlpha = 0.2; ctx.fillStyle = "red"; ctx.fillRect(canvas.width-90-i*50,arr[k][0]*5,10,2); } } function start() { ctx.clearRect(0, 0, canvas.width, canvas.height); for ( var e=0; e<10;e++){ myarr.push([e,Math.random().toFixed(1)]); } drawarray(myarr); i++; } function stop() { clearInterval(inter); }Vacíe "myarr" después de hacer el dibujo. Lo que sucede es que estás dibujando 10 rectángulos, luego 20, luego 30 y así sucesivamente.