-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (67 loc) · 1.88 KB
/
script.js
File metadata and controls
80 lines (67 loc) · 1.88 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
const grid = document.getElementById('grid');
const text = document.getElementById('textBox');
const submit = document.getElementById('submitBtn');
const black = document.getElementById('black');
const color = document.getElementById('color');
let colorOn = false;
function createGrid(num){
for(r = 0; r < num; r++){
let row = document.createElement('div');
row.className = 'row';
for(c = 0; c < num; c++){
let square = document.createElement('div');
square.className = 'square';
square.style.width = squareSize(num);
square.style.height = squareSize(num);
row.appendChild(square);
}
grid.appendChild(row);
}
}
function removeGrid(){
while(grid.firstChild){
grid.removeChild(grid.firstChild);
console.log("removed");
}
}
function defaultGrid(){
createGrid(16);
}
function squareSize(num){
const size = 500 / num;
return size;
}
function changeGridSize(size){
removeGrid();
createGrid(size);
}
submit.addEventListener('click', function(){
if(!isNaN(text.value) && (text.value <= 100 && text.value >= 1)){
changeGridSize(text.value);
}
console.log(text.value);
})
grid.addEventListener('mouseover', function(){
const hoverList = document.querySelectorAll(":hover");
const hoverSquare = hoverList.item(hoverList.length-1);
if(colorOn){
colorGrid(hoverSquare);
}else{
blackGrid(hoverSquare);
}
})
function blackGrid(square){
square.style.backgroundColor = 'black';
}
black.addEventListener('click', function(){
colorOn = false;
})
function colorGrid(square){
const randomColor = Math.floor(Math.random()*16777215).toString(16);
square.style.backgroundColor = '#' + randomColor;
}
color.addEventListener('click', function(){
console.log('color on');
colorOn = true;
})
defaultGrid();