-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
126 lines (98 loc) · 3.47 KB
/
app.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
let mainContainer = document.querySelector(".container");
let blackButton = document.querySelector("#blackButton");
let colorfulButton = document.querySelector("#colorfulButton");
// create div for box
let boxGroup = document.createElement("div");
boxGroup.classList.add('box-group');
// create singleDiv as a box
function createDiv(size) {
let newDiv = document.createElement("div");
newDiv.style.width = `${size}px`;
newDiv.style.height = `${size}px`;
newDiv.style.boxSizing = `border-box`;
newDiv.classList.add('box');
return newDiv
}
// create multiple box for nxn
// if n = 2, the div will divide total width by 4
// if n = 3, the div will divide total width by 9 and so on
function createMultipleDiv(size) {
let boxContainerSize = 400;
let boxSize = boxContainerSize / (size);
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
boxGroup.appendChild(createDiv(boxSize));
mainContainer.appendChild(boxGroup);
}
}
}
function setContainerSize() {
let slider = document.querySelector("#rangeSlider");
let sliderValue = 32;
createMultipleDiv(sliderValue);
slider.addEventListener('change', () => {
// reset before create new one
while (boxGroup.firstChild) {
boxGroup.removeChild(boxGroup.firstChild);
}
while (mainContainer.firstChild) {
mainContainer.removeChild(mainContainer.firstChild);
}
// reset button active
blackButton.classList.remove("button-active");
colorfulButton.classList.remove("colorful-button-active");
// create new one
createMultipleDiv(slider.value);
});
}
// add background color when hovering the box
function addBackgroundWhenHover(color = null) {
let boxes = document.querySelectorAll('.box');
boxes.forEach(element => {
element.addEventListener('mouseover', () => {
if (color == null) {
const randomColor = getRandomColor();
element.style.backgroundColor = randomColor;
} else {
element.style.backgroundColor = "black"
}
})
});
}
// get random color
function getRandomColor() {
const colors = ['#fd8800', '#fd008f', '#9700fd', '#003dfd', '#05c7e6', '#4bd58d'];
const randomIndex = Math.floor(Math.random() * colors.length);
return colors[randomIndex];
}
// clear canvas
function clearcanvas() {
let clearButton = document.querySelector('#clearButton');
clearButton.addEventListener('click', () => {
let boxes = document.querySelectorAll('.box');
boxes.forEach(element => {
element.style.backgroundColor = "white";
});
})
}
// choose button color
function chooseColor() {
blackButton.addEventListener('click', () => {
blackButton.classList.add("button-active");
colorfulButton.classList.remove("colorful-button-active");
return addBackgroundWhenHover("black");
})
colorfulButton.addEventListener('click', () => {
blackButton.classList.remove("button-active");
colorfulButton.classList.add("colorful-button-active")
const randomengdomColor = getRandomColor();
// element.style.backgroundColor = randomColor;
return addBackgroundWhenHover();
})
blackButton.classList.add("button-active");
return addBackgroundWhenHover("black");
}
// call all function needed
setContainerSize();
chooseColor();
clearcanvas();