-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.html
81 lines (72 loc) · 2.3 KB
/
page.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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<button type="button" onclick="reset()">Reset Page</button>
<label> RGB Mode
<input type="checkbox" class="rgbBox" onclick="reset()">
</label>
<div class="container"></div>
</body>
<script>
const grid = document.querySelector(".container")
const rgbBox = document.querySelector(".rgbBox")
function blackSketch(e) {
if (e.target.classList.contains('block')) {
e.target.style.backgroundColor = 'black';
let shade = Number(e.target.style.opacity)
if (shade < 1) {
e.target.style.opacity = shade + .1;
}
}
}
function colorSketch(e) {
var r = Math.floor(Math.random()*256);
var g = Math.floor(Math.random()*256);
var b = Math.floor(Math.random()*256);
var rgb = "rgb(" + r + "," + g + "," + b + ")";
if (e.target.classList.contains('block')) {
e.target.style.backgroundColor = rgb;
let shade = Number(e.target.style.opacity)
if (shade < 1) {
e.target.style.opacity = shade + .1;
}
}
}
function generateBoxes (dimension) {
if (rgbBox.checked) {
for (let i = 0; i < (dimension**2); i ++) {
grid.style.gridTemplateRows = "repeat(" + dimension + ", 1fr)";
grid.style.gridTemplateColumns = "repeat(" + dimension + ", 1fr)";
grid.style.gridGap = (dimension/100) + "px";
var div = document.createElement('div');
div.classList.add('block');
div.addEventListener("mouseover", colorSketch);
grid.appendChild(div);
}
} else {
for (let i = 0; i < (dimension**2); i ++) {
grid.style.gridTemplateRows = "repeat(" + dimension + ", 1fr)";
grid.style.gridTemplateColumns = "repeat(" + dimension + ", 1fr)";
grid.style.gridGap = (dimension/100) + "px";
var div = document.createElement('div');
div.classList.add('block');
div.addEventListener("mouseover", blackSketch);
grid.appendChild(div);
}
}
document.body.appendChild(grid);
}
function reset() {
var numBoxes = prompt("Please enter the number of squares per side you would like.")
var divs = document.querySelectorAll('.block')
for (let i = 0; i < divs.length; i++) {
divs[i].remove();
}
generateBoxes(numBoxes);
}
generateBoxes(10);
</script>
</html>