-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
111 lines (77 loc) · 2.36 KB
/
scripts.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
// GLOBALS
let GRID_COLOR = "#228D2F"; // default pen color
const slider = document.querySelector('#myRange');
function start()
{
makeGrid(slider.value);
colorChangeListener();
sliderUpdate();
resetButtonEvent();
}
function makeGrid(numberOfDivs)
{
const wholePage = document.querySelector('.pageWrapper');
const gridWrap = document.createElement('div');
gridWrap.setAttribute('class', 'gridWrapper');
wholePage.appendChild(gridWrap);
gridWrap.style.setProperty('--column-number', slider.value);
for(let i = 0; i < numberOfDivs*numberOfDivs; i++)
{
let div = document.createElement('div');
div.setAttribute("class","gridBox");
gridWrap.appendChild(div);
}
startDrawing();
}
function startDrawing()
{
const boxNodes = document.querySelectorAll('.gridBox'); // Need to convert to array which is what Array.from() does
const boxArray = Array.from(boxNodes);
boxArray.forEach(box => {
box.addEventListener("mouseover", function(e){
box.style.backgroundColor = GRID_COLOR;
}
);
});
}
function resetGrid()
{
const boxNodes = document.querySelectorAll('.gridBox'); // Need to convert to array which is what Array.from() does
const boxArray = Array.from(boxNodes);
boxArray.forEach(box => {
box.style.backgroundColor = 'var(--main-bg-c)';
});
}
function deleteGrid()
{
const pageWrap = document.querySelector('.pageWrapper');
const gridWrap = document.querySelector('.gridWrapper'); // created from makeGrid() function
pageWrap.removeChild(gridWrap);
}
function colorChangeListener()
{
const colorPicker = document.querySelector('#colorPicker');
colorPicker.addEventListener('change', function(e){
GRID_COLOR = colorPicker.value;
console.log(colorPicker.value);
});
}
// Event Listeners
function resetButtonEvent() {
const resetButton = document.querySelector('#resetButton');
resetButton.addEventListener('click', function (e) {
resetGrid();
});
}
function sliderUpdate()
{
slider.onchange = function(e) {
deleteGrid();
makeGrid(slider.value);
}
slider.oninput = function(e) {
document.querySelector('#valueOfSlider').textContent = slider.value + "x" + slider.value;
}
}
// Function Calls
start();