-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
90 lines (78 loc) · 3.36 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Etch-a-Sketch</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="header">
<button class="new-button">Generate new board</button>
<span class="hero">Etch-a-Sketch!</span>
<button class="reset-button">Reset current board</button>
</div>
<div class="container">
<div class="board"></div>
</div>
<script>
let currentNumRows = 16;
let currentNumCols = 16;
const rgbRE = /rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/;
const mainBoard = document.querySelector('.board');
const boardCSS = window.getComputedStyle(mainBoard, null);
let boardHeight = parseInt(boardCSS.getPropertyValue("height"));
let boardWidth = parseInt(boardCSS.getPropertyValue("width"));
function darkenCell(e) {
const rgbArray = rgbRE.exec(this.style.backgroundColor);
this.style.backgroundColor = `rgb(${rgbArray[1] - 25}, ${rgbArray[2] - 25}, ${rgbArray[3] - 25})`
}
function promptForNewBoard(e) {
const newBoardSize = prompt("How many squares per side (maximum 100)?", '');
if (!newBoardSize || isNaN(newBoardSize)) {
alert("Please enter a number between 1-100");
return;
}
if (newBoardSize < 1 || newBoardSize > 100) {
alert("Please enter a number between 1-100");
return;
}
generateBoard(newBoardSize, newBoardSize);
}
function generateBoard(numRows, numCols) {
currentNumRows = numRows;
currentNumCols = numCols;
const cellHeight = (boardHeight / numRows) - 2; //Border width * 2
const cellWidth = (boardWidth / numCols) - 2;
const oldBoardCells = document.querySelectorAll('.cell');
if (oldBoardCells) {
oldBoardCells.forEach(cell => cell.parentNode.removeChild(cell));
}
for (let i = 1; i <= numCols; i++) {
const col = document.createElement('div');
col.classList.add('col');
mainBoard.appendChild(col);
for (let j = 1; j <= numRows; j++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.style.backgroundColor = 'rgb(255, 255, 255)';
cell.style.height = `${cellHeight}px`;
cell.style.width = `${cellWidth}px`;
col.appendChild(cell);
}
}
const cells = document.querySelectorAll('.cell');
cells.forEach(cell => cell.addEventListener('mouseover', darkenCell));
}
function resetBoard (e) {
generateBoard(currentNumRows, currentNumCols);
}
generateBoard(currentNumRows, currentNumCols);
const newBoardButton = document.querySelector('.new-button');
newBoardButton.addEventListener("click", promptForNewBoard);
const resetBoardButton = document.querySelector('.reset-button');
resetBoardButton.addEventListener("click", resetBoard);
</script>
</body>
</html>