-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
154 lines (138 loc) · 4.21 KB
/
script.js
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
let gridContainerElement = document.querySelector('.grid-container');
let inputElement = document.querySelector('input');
let gridArray = [];
const buttonElement = document.querySelector('.create-grid');
let eraserbtn = document.querySelector('.eraser-btn');
let rainbowbtn = document.querySelector('.rainbow-btn');
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min); // Include max by adding 1 to the range
}
rainbowbtn.addEventListener('click', () => {
checkMouseOnGrid(true);
if(rainbowbtn.style.backgroundColor !== 'rgb(40, 40, 40)'){
rainbowbtn.style.backgroundColor = 'rgb(40, 40, 40)';
eraserbtn.style.backgroundColor = null;
eraserbtn.style.fontSize = null;
}else{
rainbowbtn.style.backgroundColor = null;
eraserbtn.style.backgroundColor = null;
eraserbtn.style.fontSize = null;
checkMouseOnGrid(false);
}
});
eraserbtn.addEventListener('click', () => {
if(eraserbtn.style.backgroundColor !== 'rgb(40, 40, 40)'){
eraserbtn.style.backgroundColor = 'rgb(40, 40, 40)';
eraserbtn.style.fontSize = '1.5rem';
rainbowbtn.style.backgroundColor = null;
isEraserOn();
}else{
eraserbtn.style.backgroundColor = null;
eraserbtn.style.fontSize = null;
checkMouseOnGrid();
}
});
buttonElement.addEventListener('click', () => {
drawGrid();
toggleButton();
});
inputElement.addEventListener('keydown', (event) => {
if(event.key === 'Enter'){
drawGrid();
toggleButton();
}
});
function getGridSizeFromUser() {
let size = (inputElement.value % 2 == 0)? inputElement.value : (+inputElement.value + +1);
console.log('Input value: '+inputElement.value);
return size;
}
function setGridContainerStyles(size) {
gridContainerElement.style.cssText = `
display:grid;
width: 400px;
height: 400px;
grid-template-columns: repeat(${size}, 1fr);
grid-template-rows: repeat(${size}, 1fr);
`;
}
function createGrid(rows) {
if(gridArray.length === 0) {
for(let i = 0; i < rows; i++){
for(let j = 0; j < rows; j++){
const box = document.createElement('div');
gridArray.push(box);
}
}
return gridArray;
}else {
gridArray.splice(0, gridArray.length);
gridContainerElement.innerHTML = '';
return gridArray;
}
}
function drawGrid() {
const grid = createGrid(getGridSizeFromUser()) || null;
setGridContainerStyles(getGridSizeFromUser());
if(grid !== null){
grid.forEach(gridBox => {
gridContainerElement.appendChild(gridBox);
gridBox.style.cssText = `
border: 0.001rem solid rgba(0,0,0,0.2);
`;
gridBox.classList.add('box');
});
checkMouseOnGrid(false);
}
}
function checkMouseOnGrid(rainbowMode = false) {
let gridArray = document.querySelectorAll('.box');
let red, blue, green;
gridArray.forEach((box) => {
// console.log(red + ' ' + blue + ' ' + green);
box.addEventListener('mouseenter', () => {
if(rainbowMode === true){
//
red = getRandomIntInclusive(0, 255);
blue = getRandomIntInclusive(0, 255);
green = getRandomIntInclusive(0, 255);
box.style.backgroundColor = `rgb(${red}, ${blue}, ${green})`;
console.log(typeof box.style.backgroundColor);
}else {
box.style.backgroundColor = `black`;
}
});
});
}
// function isMouseLeftClickDown() {
// let gridArray = document.querySelectorAll('.box');
// gridArray.forEach((box) => {
// box.addEventListener('mousedown', () => {
// box.classList.remove('box');
// });
// })
// return false;
// }
function toggleButton() {
if(buttonElement.innerText === 'Create Grid') {
buttonElement.innerText = 'Delete Grid';
eraserbtn.style.backgroundColor = null;
rainbowbtn.style.backgroundColor = null;
}else {
buttonElement.innerText = 'Create Grid';
inputElement.value = '';
eraserbtn.style.backgroundColor = null;
eraserbtn.style.fontSize = null;
rainbowbtn.style.backgroundColor = null;
}
}
function isEraserOn() {
let gridArray = document.querySelectorAll('.box');
gridArray.forEach((box) => {
box.addEventListener('mouseenter', () => {
box.style.backgroundColor = null;
});
});
}