-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
115 lines (87 loc) · 2.97 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//Caching the DOM
const container = document.getElementById("container");
const gridItem = document.getElementsByClassName("grid-item");
const buttonGrid = document.getElementById("grid-size");
const buttonRandom = document.getElementById("random-colors");
const buttonColorPicker = document.getElementById("color-picker");
const buttonGridLines = document.getElementById("grid-lines");
const buttonErase = document.getElementById("erase");
const colorValue = document.getElementById("color-value");
// Create Grid
function makeGrid(rows, cols) {
container.style.setProperty("--grid-rows", rows);
container.style.setProperty("--grid-cols", cols);
for (i = 0; i < (rows * cols); i++) {
let cell = document.createElement("div");
container.appendChild(cell).className = "grid-item";
};
colorHoverEffect();
};
// Call Function Make Grid
makeGrid(16, 16);
// Reset Grid Every Time User Give New Size
function resetGrid() {
container.textContent = "";
while (container.firstChild) {
container.removeChild(container.lastChild);
}
}
// Change Grid Size Event
buttonGrid.addEventListener("click", function () {
resetGrid();
var size = prompt("Choose grid size(must enter a value between 16 and 128)");
var num1 = parseInt(size);
var num2 = parseInt(size);
if(size < 16 || size > 128){
alert("Sorry you must insert a valid value!");
makeGrid(16,16);
}else if(size != num1 && size != num2){
alert("Sorry you must insert a valid value!");
makeGrid(16,16);
}else{
makeGrid(num1, num2)
}
})
// Generate Random Color
function getRandomColor() {
var letters = "0123456789ABCDEF";
var color = "#";
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// Random Color Hover Effect
function randomHoverEffect() {
for (let i = 0; i < gridItem.length; i++) {
gridItem[i].addEventListener("mouseover", function () {
gridItem[i].style.backgroundColor = getRandomColor();
})
}
}
// Color Picker Hover Effect
function colorHoverEffect() {
for (let i = 0; i < gridItem.length; i++) {
gridItem[i].addEventListener("mouseover", function () {
gridItem[i].style.backgroundColor = buttonColorPicker.value;
})
}
colorValue.style.color = buttonColorPicker.value;
colorValue.textContent = buttonColorPicker.value;
}
// Grid Lines On-Off
function gridLines() {
for (let i = 0; i < gridItem.length; i++) {
gridItem[i].classList.toggle("grid-lines");
}
}
// Clear Pixels Brush
function clearPixels() {
for (let i = 0; i < gridItem.length; i++) {
gridItem[i].style.backgroundColor = "white";
}
}
buttonRandom.addEventListener("click", randomHoverEffect);
buttonColorPicker.addEventListener("input", colorHoverEffect);
buttonGridLines.addEventListener("click", gridLines);
buttonErase.addEventListener("click", clearPixels);