Skip to content

Instantly share code, notes, and snippets.

@elifitch
Created January 19, 2015 22:24
Show Gist options
  • Save elifitch/8a4b0d84e49a3a9a593a to your computer and use it in GitHub Desktop.
Save elifitch/8a4b0d84e49a3a9a593a to your computer and use it in GitHub Desktop.
Draw grid of blocks on canvas
var c = document.createElement('canvas');
c.height = window.innerHeight;
c.width = window.innerWidth;
var ctx = c.getContext('2d');
function blocks(size) {
//draw row
for(var j=0, x=c.height/size; j<x; j++) {
//draw block
for(var i = 0, l = c.width/size; i<l; i++) {
var red = Math.floor(Math.random() * 256);
var green = Math.floor(Math.random() * 256);
var blue = Math.floor(Math.random() * 256);
var alpha = 255;
ctx.beginPath();
ctx.fillStyle = "rgba( "+red+", "+green+", "+blue+", "+alpha+")";
ctx.rect(i*size,j*size,size,size);
ctx.fill();
}
}
}
blocks(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment