-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (73 loc) · 2.64 KB
/
index.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
const container = document.querySelector('#container');
const resetButton = document.querySelector('.resetButton');
resetButton.addEventListener('click', resetGrid);
let slider = document.getElementById("gridRange");
let sliderValue = Number(slider.value);
//shows grid size text
let gridRangeDisplay = document.getElementById("gridRangeDisplay");
gridRangeDisplay.innerText = `${slider.value} x ${slider.value}`;
//updates grid size text and slider value based on user slider input
slider.oninput = function() {
sliderValue = this.value;
gridRangeDisplay.innerHTML= `${slider.value} x ${slider.value}`;
makeGrid(this.value, this.value);
};
//creates cells based on grid sliderValue
function makeCells(num) {
for (i = 0; i < num; i++) {
let div = document.createElement('div');
div.classList.add('box');
div.addEventListener('mouseover', function(event) { //changes cell color on mouseover
event.currentTarget.style.backgroundColor = 'black';
});
container.appendChild(div);
}
};
//creates grid matrix from grid sliderValue by accessing CSS and calling makeCells
function makeGrid(numCols, numRows) {
container.style.setProperty('--numCols', numCols);
container.style.setProperty('--numRows', numRows);
for (let i = 0; i < numCols; i++) {
makeCells(numCols);
}
};
//adds rainbow button functionality
const rgb = document.querySelector('.rgbButton');
rgb.addEventListener('click', function() {
let cell = container.children;
for (let i = 0; i < sliderValue * sliderValue; i++) {
cell [i].addEventListener('mouseover', function(event) {
event.currentTarget.style.backgroundColor = getRainbow();
})
}
});
//returns a random rainbow color
function getRainbow() {
let rgbArray = [
'#FF6663',
'#FEB144',
'#FDFD97',
'#9EE09E',
'#9EC1CF',
'#CC99C9',
];
let color = rgbArray[Math.floor(Math.random()*rgbArray.length)];
return color;
};
//adds black pen button functionality
const black = document.querySelector('.blackButton');
black.addEventListener('click', function() {
let cell = container.children;
for (let i = 0; i < sliderValue * sliderValue; i++) {
cell [i].addEventListener('mouseover', function(event) {
event.currentTarget.style.backgroundColor = 'black';
})
}
});
function resetGrid() {
while (container.firstChild) {
container.removeChild(container.lastChild);
}
makeGrid(sliderValue, sliderValue);
};
makeGrid(sliderValue, sliderValue);