-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatedScript.js
108 lines (84 loc) · 2.6 KB
/
updatedScript.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
const gridContainer = document.querySelector('#grid-container');
const eraserButton = document.querySelector('#eraser');
const blackButton = document.querySelector("#blackColor");
const clearButton = document.querySelector('#clear');
const rainbowButton = document.querySelector('#rainbow');
const slider = document.querySelector('#myRange');
const sliderDisplay = document.querySelector('#sliderDisplay');
const sliderRefresh = document.querySelector("#sliderRefresh");
let currentMode = 'color';
let mainColor = 'black';
createDiv(gridContainer, 16);
let tiles = document.querySelectorAll('.grid-tile');
function createDiv (container, n) {
reset(container);
container.style.gridTemplateColumns = `repeat(${n}, 1fr)`
container.style.gridTemplateRows = `repeat(${n}, 1fr)`
for (let i = 0; i < n * n; i++){
let div = document.createElement('div');
div.classList.add('grid-tile');
container.appendChild(div);
}
}
function reset (grid) {
while (grid.firstChild) {
grid.removeChild(grid.lastChild);
}
}
function backgroundColorChange(color) {
gridContainer.addEventListener('mouseover', e => {
if (e.target.id != 'grid-container'){
e.target.style.backgroundColor = color;
}
})
}
function eraseTiles(){
gridContainer.addEventListener('mouseover', e => {
if(e.target.id != "grid-container"){
e.target.style.backgroundColor = 'white';
}
})
}
function changeModes (){
switch (currentMode){
case 'color':
backgroundColorChange(mainColor);
break;
case 'eraser':
eraseTiles();
break;
}
}
function clearGrid() {
tiles.forEach(tile => {tile.style.backgroundColor = 'white';})
}
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function rainbow(){
gridContainer.addEventListener('mouseover', e => {
if (e.target.id != 'grid-container'){
e.target.style.backgroundColor = getRandomColor();
}
})
}
slider.oninput = function() {
sliderDisplay.textContent = slider.value;
}
//Active Events
changeModes();
blackButton.addEventListener('click', e => {backgroundColorChange('black')});
eraserButton.addEventListener('click', e => {
currentMode = 'eraser';
changeModes();
});
clearButton.addEventListener('click', clearGrid);
rainbowButton.addEventListener('click', rainbow);
sliderRefresh.addEventListener('click', e => {
createDiv(gridContainer, slider.value);
});