Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to draw random pixels with random colors in browser with JavaScript

3 Answers

0 votes
var canvas = document.createElement("canvas");
canvas.setAttribute("width", window.innerWidth);
canvas.setAttribute("height", window.innerHeight);
document.body.appendChild(canvas);

var ctx = canvas.getContext("2d");
var i;

for (i = 0; i < 500; i++)
     ctx.fillRect(Math.random() * 600, Math.random() * 600, 1, 1);

            
/*
run:
  
draw 500 random pixels
  
*/

 



answered Jul 16, 2015 by avibootz
0 votes
var canvas = document.createElement("canvas");
canvas.setAttribute("width", window.innerWidth);
canvas.setAttribute("height", window.innerHeight);
document.body.appendChild(canvas);

var ctx = canvas.getContext("2d");
var i;

ctx.fillStyle = "rgb(0, 0, 255)";  // blue

for (i = 0; i < 500; i++)
     ctx.fillRect(Math.random() * 600, Math.random() * 600, 1, 1);

            
/*
run:
  
draw 500 random blue pixels
  
*/

 



answered Jul 16, 2015 by avibootz
edited Jul 17, 2015 by avibootz
0 votes
var canvas = document.createElement("canvas");
canvas.setAttribute("width", window.innerWidth);
canvas.setAttribute("height", window.innerHeight);
document.body.appendChild(canvas);

var ctx = canvas.getContext("2d");
var i;

var max = 255
var min = 1;

for (i = 0; i < 700; i++)
{
    var r = Math.floor(Math.random() * (max - min + 1)) + min;
    var g = Math.floor(Math.random() * (max - min + 1)) + min;
    var b = Math.floor(Math.random() * (max - min + 1)) + min;
    
    ctx.fillStyle = "rgb(" + r + ',' + g + ',' + b + ")";  
    ctx.fillRect(Math.random() * 600, Math.random() * 600, 1, 1);
} 

            
/*
run:

draw 700 random pixels with random colors
  
*/

 



answered Jul 16, 2015 by avibootz
...