Hello! I know the question is stupid, but still: how can I give each canvas pixel a random color? It seems to be all simple, but there is one BUT.

var canvas = document.getElementById("canvas"); canvas.width = 300; canvas.height = 300; var ctx = canvas.getContext("2d"); for (var y = 0; y < canvas.height; y++) { for (var x = 0; x < canvas.width; x++) { ctx.fillStyle = getRandRGB(); ctx.fillRect(x, y, 1, 1); } } 

Instead of creating small squares of random colors on all the canvas, he does it enter image description here

(Only the top of the canvas is painted)

  • What is the problem? Your algorithm does exactly what you want - it gives pixels a random color. Or the key question is why not the entire canvas is painted over? - P. Ilyin
  • I did not study the canvas, but logically - you need to paint not x and y, but x [i] and y [i] - user190134
  • @ P.Ilyin that's it: why not all. - Reaget
  • @ user190134 So it is not the x and y that are colored, the squares that are in position with the x and y coordinates are colored. - Reaget
  • How do you create ctx? Give a piece of code in which this occurs. - Xander

0