-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
101 lines (85 loc) · 3.18 KB
/
script.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
console.log("Javascript file loaded")
// TODO change event listeners to event delegation
// BUG sometimes the hover effect lingers after the timeout (check again after delegation)
const sketch_container = document.querySelector(".sketch-container")
const bt_clear = document.querySelector("#clear")
const range_selector = document.querySelector("#myRange")
const current_size = document.querySelector("#current-size")
const radioButtons = document.querySelectorAll('input[name="colorize"]');
// generate random color for randomized option
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// callback for hovering
function activateGridElement(event) {
this.classList.add("grid-hover")
switch (currentColorState) {
case "colorize":
this.style["background-color"] = getRandomColor();
this.style["opacity"] = 1;
break;
case "darken":
// reverese effect to work with background color of the container and get the effect of circular dots
this.style["background-color"] = "white";
const currentOpacity = Number(this.style["opacity"])
this.style["opacity"] = currentOpacity <= 0 ? 0 : Number(this.style["opacity"]) - 0.1;
break;
case "normal":
this.style["background-color"] = "white";
this.style["opacity"] = 0.3;
break;
}
}
// callback for transition end
function deactivateGridElement(event) {
if (event.propertyName == "transform") {
this.classList.remove("grid-hover");
}
}
// reset grid, then add new grid rows, then add boxes in each row
function buildGridRows(grid_size) {
sketch_container.innerHTML = '';
for (i = 0; i < grid_size; i++) {
const grid_row = document.createElement("div")
grid_row.classList.add("grid-row")
for (j = 0; j < grid_size; j++) {
grid_row.appendChild(buildSingleBox())
}
sketch_container.appendChild(grid_row)
}
}
// build single box in grid, then add listeners
function buildSingleBox() {
const grid_element = document.createElement("div")
grid_element.classList.add("grid-element")
grid_element.addEventListener('mouseover', activateGridElement)
grid_element.addEventListener('transitionend', deactivateGridElement)
grid_element.style["opacity"] = 1;
return grid_element
}
// get grid size from slider
function getGridSize(event) {
grid_size = range_selector.value;
current_size.textContent = `${grid_size} x ${grid_size}`;
return grid_size
}
// build grid after getting selected grid size
function buildGridReactivaly() {
grid_size = getGridSize();
buildGridRows(grid_size)
}
bt_clear.addEventListener('click', buildGridReactivaly);
range_selector.addEventListener('input', buildGridReactivaly);
let currentColorState = 'normal';
for (const radioButton of radioButtons) {
radioButton.addEventListener('change', (e) => {currentColorState = e.target.value});
if (radioButton.value == "normal") {
radioButton.checked = true;
}
}
buildGridReactivaly()