Skip to content

Commit dd9aa08

Browse files
Store TOP files in TOP folder
1 parent 5a187c0 commit dd9aa08

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

TOP version/index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>Etch a sketch</title>
9+
<link href="https://fonts.googleapis.com/css2?family=Roboto&amp;display=swap" rel="stylesheet">
10+
<link rel="stylesheet" href="style.css">
11+
</head>
12+
13+
<body>
14+
<h1>Etch a sketch</h1>
15+
<div id="boxgrid"></div>
16+
<button id="resolution">Change resolution</button><br>
17+
<label for="colorChooser">Choose Colors</label>
18+
<select name="colorColorDropdown" id="colorChooser">
19+
<option value="bw">Black and White</option>
20+
<option value="cl">Random Colors</option>
21+
</select>
22+
23+
24+
<!--
25+
Add:
26+
- "Resoultion: 16x16", to change grid size, with dropdown menu-->
27+
</body>
28+
29+
<script src="main.js"></script>
30+
31+
</html>

TOP version/main.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const boxGrid = document.getElementById("boxgrid");
2+
const button = document.getElementById("resolution");
3+
const colorChooser = document.getElementById("colorChooser");
4+
5+
const gridWidth = 960;
6+
7+
var random = function(number) {
8+
// random number from 0 to number
9+
return Math.floor(Math.random() * (number + 1));
10+
}
11+
12+
13+
var drawCanvas = function(boxesPerSide) {
14+
for (let i = 1; i <= boxesPerSide; i++) {
15+
for (let j = 1; j <= boxesPerSide; j++) {
16+
const box = document.createElement("div");
17+
box.classList.add("col" + j.toString());
18+
box.classList.add("row" + i.toString());
19+
box.classList.add("l100");
20+
21+
boxGrid.appendChild(box);
22+
23+
box.addEventListener("mouseenter", () => {
24+
if (colorChooser.value === "cl") {
25+
box.style.backgroundColor = `hsl(${random(360)}, ${random(100)}%, ${random(100)}%)`;
26+
} else {
27+
// pick lXX for lightness value
28+
let className = String(box.classList);
29+
let lightness = +className.slice(className.lastIndexOf("l") + 1, className.length);
30+
if (lightness > 0) {
31+
box.classList.remove(`l${lightness}`);
32+
lightness -= 10;
33+
box.classList.add(`l${lightness}`);
34+
}
35+
box.style.backgroundColor = `hsl(0, 0%, ${lightness}%)`;
36+
}
37+
});
38+
}
39+
}
40+
const boxWidth = gridWidth / boxesPerSide;
41+
42+
boxGrid.style.display = "grid";
43+
boxGrid.style.gridTemplate = `repeat(${boxesPerSide}, ${boxWidth}px) / repeat(${boxesPerSide}, ${boxWidth}px)`;
44+
}
45+
46+
var clearCanvas = function() {
47+
boxGrid.textContent = "";
48+
boxGrid.removeAttribute("style");
49+
}
50+
51+
var askResolution = () => {
52+
const res = prompt("Enter resolution size", "10");
53+
if (res === null) return;
54+
if (res > 100) {
55+
alert("Number too big, max: 100");
56+
askResolution();
57+
return;
58+
}
59+
clearCanvas();
60+
drawCanvas(res);
61+
}
62+
63+
64+
button.addEventListener("click", askResolution);
65+
66+
67+
drawCanvas(16);

TOP version/style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
body {
2+
margin: 0;
3+
font-family: 'Roboto', 'Open Sans', sans-serif;
4+
}
5+
6+
#boxgrid div {
7+
background-color: rgb(240, 248, 255);
8+
}

0 commit comments

Comments
 (0)