-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
158 lines (124 loc) · 4.43 KB
/
script.js
File metadata and controls
158 lines (124 loc) · 4.43 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
let val = 0;
let idName = 1;
let opacity = 1;
let darken = true;
//CREATING HTML ELEMENTS
const btnCreate = document.createElement("button");
btnCreate.setAttribute("id","btnCreate");
btnCreate.textContent="CHANGE SIZE OF GRID";
const btnErase = document.createElement("button");
btnErase.setAttribute("id","btnErase");
btnErase.textContent="ERASE GRID";
const blackColor = document.createElement("button");
blackColor.setAttribute("id","btnBlackColor");
blackColor.textContent ="BLACK COLOR";
const randomColor = document.createElement("button");
randomColor.setAttribute("id","btnRandomColor");
randomColor.textContent ="RANDOM COLOR";
const cont = document.getElementById("container");
const option = document.getElementById("option");
option.appendChild(btnCreate);
option.appendChild(btnErase);
option.appendChild(blackColor);
option.appendChild(randomColor);
//FUNCTION THAT CREATES THE GRID
function addDiv(numDivs){
let divSize = `${720/ numDivs}px`;
console.log(divSize);
for(let i=0; i<numDivs; i++){
const row = document.createElement("div");
row.setAttribute("class", "row");
cont.appendChild(row);
for (let j=0; j<numDivs; j++){
const appendDiv = document.createElement("div");
appendDiv.setAttribute("class", "newDiv");
appendDiv.setAttribute("id", "div" + idName);
appendDiv.style.width = divSize;
appendDiv.style.height = divSize;
row.appendChild(appendDiv);
idName++;
}
}
}
addDiv(16);
const newDivs = document.querySelectorAll('.newDiv');
[...newDivs].forEach(newDiv => {
newDiv.addEventListener('mouseover', () => {
newDiv.classList.add('permahover');
// let color = '#'+Math.floor(Math.random()*16777215).toString(16);
// newDiv.style['background-color'] = color;
newDiv.style['opacity'] = opacity;
if(darken){
opacity = opacity - .1;
if(opacity<=0){
darken = false;
}
} else {
opacity = opacity + .1;
if(opacity>=1){
darken = true;
}
}
});
})
btnCreate.addEventListener("click", getNumberDivs);
function getNumberDivs(){
let userChoice;
userChoice = prompt("Enter the number of cells per side, maximum is 100.");
if (userChoice === null || userChoice === "" || userChoice === undefined || userChoice > 100){
alert("You must enter a valid option into the prompt box!\nCannot be empty and must be maximum 100");
return getNumberDivs();
} else {
cont.replaceChildren();
idName = 1;
addDiv(userChoice);
const newDivs = document.querySelectorAll('.newDiv');
[...newDivs].forEach(newDiv => {
newDiv.addEventListener('mouseover', () => {
newDiv.classList.add('permahover');
newDiv.style['background-color'] = '#000000';
newDiv.style['opacity'] = opacity;
if(darken){
opacity = opacity - .1;
if(opacity<=0){
darken = false;
}
} else {
opacity = opacity + .1;
if(opacity>=1){
darken = true;
}
}
});
})
// addDiv(userChoice);
}
}
btnErase.addEventListener("click", eraseGrid);
function eraseGrid(){
const newDivs = document.querySelectorAll('.newDiv');
[...newDivs].forEach(newDiv => {
newDiv.style['background-color'] = '#faca70';
newDiv.style['opacity'] = 1;
});
}
blackColor.addEventListener("click", getBlackColor);
function getBlackColor(){
const newDivs = document.querySelectorAll('.newDiv');
[...newDivs].forEach(newDiv => {
newDiv.addEventListener('mouseover', () => {
newDiv.style['background-color'] = '#000000';
});
})
}
randomColor.addEventListener("click", getRandomColor);
function getRandomColor(){
const newDivs = document.querySelectorAll('.newDiv');
[...newDivs].forEach(newDiv => {
newDiv.addEventListener('mouseover', () => {
newDiv.classList.remove('permahover');
let color = '#'+Math.floor(Math.random()*16777215).toString(16);
newDiv.style['background-color'] = color;
});
})
}