Created
July 27, 2021 18:48
-
-
Save abuseofnotation/bcd2423520cb08f27c33dfc5dc05c16d to your computer and use it in GitHub Desktop.
Creates a grid, filled with squares of random color
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>random pattern generator</title> | |
<!--<link rel="stylesheet" href="style.css" /> --> | |
</head> | |
<body id="home"> | |
<canvas id="canvas" width="1000px" height="1000px"></canvas> | |
</body> | |
<script> | |
const canvas = document.getElementById('canvas') | |
.getContext('2d'); | |
canvas.fillStyle = 'green'; | |
canvas.fillRect(0, 0, 100, 100); | |
const size = 50; | |
const fillGrid = (rows) => { | |
rows.forEach((row, verticalIndex) => { | |
row.forEach((blockColor, horizontalIndex) => { | |
canvas.fillStyle = blockColor; | |
canvas.fillRect(horizontalIndex * size, verticalIndex * size , size, size); | |
}); | |
}); | |
}; | |
/* | |
fillGrid([ | |
['green', 'blue', 'white'], | |
['blue', 'white', 'green'], | |
['white', 'blue', 'green'], | |
]) | |
*/ | |
const getRandomInt = (max) => { | |
return Math.floor(Math.random() * Math.floor(max)); | |
} | |
const pick = (options) => options[getRandomInt(options.length)]; | |
const generateGrid = (colors, width, height) => | |
Array(width).fill().map(() => Array(height).fill().map(() => pick(colors))) | |
fillGrid(generateGrid(['green', 'blue', 'white', 'yellow'], 1000/size, 1000/size)); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment