-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (111 loc) · 3.56 KB
/
script.js
File metadata and controls
129 lines (111 loc) · 3.56 KB
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
127
128
129
// --------- UI
const gridContainer = document.querySelector('.grid-container');
const rangeSelector = document.querySelector('#range-selector');
const clearBtn = document.querySelector('#clear');
const customColor = document.querySelector('#custom-color');
const randomColorBtn = document.querySelector('#random-color');
let R = 0; let G = 0; let B = 0;
let randomToggle = false;
randomColorBtn.addEventListener('click', () => {
if (randomToggle) {
randomToggle = false
randomColorBtn.style.cssText = 'background-color: default; border: none';
} else {
randomToggle = true
randomColorBtn.style.cssText = 'background-color: rgba(161, 186, 207, 0.555); border: 1px solid white';
}
})
let mouseDown = false;
gridContainer.addEventListener('mousedown', () => {
mouseDown = true;
event.preventDefault(); // to prevent drag and drop of cells
});
gridContainer.addEventListener('mouseup', () => {
mouseDown = false;
});
let inputRange; // updates by the user (event istener)
let gridSize; // total amount of grid elements
// register input and return gridSize
rangeSelector.addEventListener('mousemove', updateUI);
rangeSelector.addEventListener('mouseup', runGrid);
// runs funciton at the start
runGrid();
function runGrid(){
buildGrid();
updateUI();
createHover();
draw();
}
function buildGrid() {
// check if there was previously created grit-items and deletes them
const gridToRemove = document.querySelectorAll('.grid-items');
for (let i = 0; i < gridToRemove.length; i++) {
gridToRemove[i].remove();
};
//user input register
inputRange = rangeSelector.value;
gridSize = inputRange ** 2;
//updates CSS grid-template to properly display (1:1)
gridContainer.style.cssText = `grid-template: repeat(${inputRange}, 1fr) / repeat(${inputRange}, 1fr)`;
// build grid of requested size
for (let i = 1; i <= gridSize; i++) {
const gridItemsCreate = document.createElement('div');
gridItemsCreate.classList.add('grid-items');
gridContainer.appendChild(gridItemsCreate);
};
}
function updateUI() {
//user input register
inputRange = rangeSelector.value;
gridSize = inputRange ** 2;
// update info on the top of input range
const selectorP = document.querySelector('.selector-p');
selectorP.textContent = `${inputRange} x ${inputRange}`;
}
clearBtn.addEventListener('click', () => {
runGrid()
})
function createHover() {
const gridItems = document.querySelectorAll('.grid-items');
gridItems.forEach((item) => {
item.addEventListener('mouseenter', () => {
item.classList.add('hover');
});
item.addEventListener('mouseleave', () => {
setTimeout(() => {
item.classList.remove('hover');
}, 100);
});
});
}
function randomRGB() {
R = Math.round(Math.random() * 255);
G = Math.round(Math.random() * 255);
B = Math.round(Math.random() * 255);
}
function draw() {
const gridItems = document.querySelectorAll('.grid-items');
gridItems.forEach((item) => {
item.addEventListener('mousemove', () => {
if (mouseDown) {
if (randomToggle) {
// random RGB
randomRGB();
item.style.cssText = `background-color: rgb(${R}, ${G}, ${B})`;
} else {
// custom color
item.style.cssText = `background-color: ${customColor.value}`;
}};
});
item.addEventListener('click', () => {
if (randomToggle) {
// random RGB
randomRGB();
item.style.cssText = `background-color: rgb(${R}, ${G}, ${B})`;
} else {
// custom color
item.style.cssText = `background-color: ${customColor.value}`;
}
});
});
};