-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
107 lines (86 loc) · 2.96 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="reset.css">
<div class = "top">
<h1>
ETCH-A-SKETCH
</h1>
</div>
</head>
<body>
<div class="grid_flex">
</div>
<div class = "buttons">
<button id = "reset" type="button">
Reset
</button>
<button id = "clear">
Clear
</button>
</div>
</body>
<script>
const grid = document.querySelector(".grid_flex");
var total = 0;
function renderGrid()
{
const max_boxes = prompt("How many boxes per row of the etch-a-sketch?");
const max_columns = prompt("How many columns in total?");
total = max_boxes * max_columns
var grid_template_columns = "1fr";
for(var tmp_col_fr = 1; tmp_col_fr < max_boxes; tmp_col_fr++) grid_template_columns += " 1fr";
grid.style.gridTemplateColumns = grid_template_columns;
for(var i = 0; i < total; i++)
{
const gridbox = document.createElement("div");
var isDrawing = false;
gridbox.style.userDrag = "none";
gridbox.addEventListener("mousedown", function(e)
{
console.log(e);
if(e.srcElement.style.backgroundColor !== "grey")
{
e.srcElement.style.backgroundColor = "grey";
}
isDrawing = true;
});
gridbox.addEventListener("mousemove", function(e)
{
if(isDrawing && e.srcElement.style.backgroundColor !== "grey")
{
e.srcElement.style.backgroundColor = "grey";
}
});
gridbox.addEventListener("mouseup", function(e)
{
isDrawing = false;
});
grid.appendChild(gridbox);
}
}
renderGrid();
const clear = document.querySelector("#clear");
const reset = document.querySelector("#reset");
var allboxes = grid.children;
clear.onclick = function()
{
var allboxes = grid.children;
for(var child_itr = 0; child_itr < total; child_itr++)
{
allboxes[child_itr].style.backgroundColor = "white";
}
};
reset.onclick = function()
{
var child = grid.lastElementChild;
while(child)
{
grid.removeChild(grid.lastChild);
child = grid.lastElementChild;
}
renderGrid();
};
</script>
</html>