Skip to content

Instantly share code, notes, and snippets.

@codetricity
Created February 27, 2018 05:51
Show Gist options
  • Save codetricity/b0b2b8bfce8b1a99fbb5f76e20e68f0d to your computer and use it in GitHub Desktop.
Save codetricity/b0b2b8bfce8b1a99fbb5f76e20e68f0d to your computer and use it in GitHub Desktop.
blocks for loop and random number lesson
<canvas id='myCanvas' width="400" height="400"></canvas>
<script>
var ctx = document.getElementById('myCanvas').getContext('2d');
var colors = [
'red',
'blue',
'green',
'yellow',
'purple']
function drawBlock(){
var blocks = [];
for (var i=0; i < 100; i++){
colorIndex = Math.floor(Math.random() * colors.length);
blockColor = colors[colorIndex];
blockX = Math.floor(Math.random() * 400);
blockY = Math.floor(Math.random() * 400);
ctx.fillStyle = blockColor;
ctx.fillRect(blockX, blockY, 6, 6);
}
setTimeout(drawBlock, 2000);
}
drawBlock();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment