Created
February 27, 2018 05:51
-
-
Save codetricity/b0b2b8bfce8b1a99fbb5f76e20e68f0d to your computer and use it in GitHub Desktop.
blocks for loop and random number lesson
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
<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