Created
January 19, 2015 22:24
-
-
Save elifitch/8a4b0d84e49a3a9a593a to your computer and use it in GitHub Desktop.
Draw grid of blocks on canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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