-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
109 lines (92 loc) · 3.11 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
const customSquaresButton = document.querySelector(".btn");
const hoverEffectButton = document.querySelector(".hover-effect-btn");
const clearButton = document.querySelector(".clear-btn");
const gridContainer = document.querySelector(".container");
let numOfSquares = 16; // Default number of squares for larger screens
let hoverEffect = "multicolor"; // Default hover effect
const generateRandomRGBColors = () => {
const red = Math.floor(Math.random() * 256);
const green = Math.floor(Math.random() * 256);
const blue = Math.floor(Math.random() * 256);
return `rgb(${red}, ${green}, ${blue})`;
};
const generateSingleColor = () => {
return "#616161"; // Darker grey color
};
const adjustSquaresForScreenSize = () => {
const screenWidth = window.innerWidth;
if (screenWidth < 960) {
// Adjust the number of squares to maintain an even grid for smaller screens
numOfSquares = 8; // Set this to an even number of squares for mobile devices
} else {
numOfSquares = 16;
}
createGameGrid();
};
const createGameGrid = () => {
gridContainer.innerHTML = ""; // Clear the existing grid
const containerWidth = gridContainer.clientWidth;
const squareSize = containerWidth / numOfSquares;
for (let i = 0; i < numOfSquares * numOfSquares; i++) {
const squareDiv = document.createElement("div");
squareDiv.className = "square";
squareDiv.style.width = `${squareSize}px`;
squareDiv.style.height = `${squareSize}px`;
// Add the hover effect for desktops/laptops
squareDiv.addEventListener("mouseover", () => {
if (hoverEffect === "multicolor") {
squareDiv.style.backgroundColor = generateRandomRGBColors();
} else {
squareDiv.style.backgroundColor = generateSingleColor();
}
});
// Add the hover effect for mobile devices
squareDiv.addEventListener(
"touchstart",
() => {
if (hoverEffect === "multicolor") {
squareDiv.style.backgroundColor = generateRandomRGBColors();
} else {
squareDiv.style.backgroundColor = generateSingleColor();
}
},
{ passive: true }
);
gridContainer.appendChild(squareDiv);
}
};
const clearGrid = () => {
const gridSquares = document.querySelectorAll(".square");
gridSquares.forEach((square) => {
square.style.backgroundColor = "";
});
};
hoverEffectButton.addEventListener("click", () => {
let choice = prompt(
"Type either multicolor or single as an option.",
""
).toLowerCase();
if (choice === "multicolor" || choice === "single") {
hoverEffect = choice;
} else {
alert(
"You didn't pick a valid choice. Please type multicolor or single."
);
}
});
customSquaresButton.addEventListener("click", () => {
let userInput = parseInt(
prompt("How many squares per side do you want for the new grid?", "")
);
if (userInput >= 16 && userInput <= 100) {
numOfSquares = userInput;
createGameGrid();
} else {
alert("Please enter a number between 16 and 100.");
}
});
// Call adjustSquaresForScreenSize() when the page loads
window.addEventListener("load", adjustSquaresForScreenSize);
// Recalculate the grid upon window resize
window.addEventListener("resize", adjustSquaresForScreenSize);
clearButton.addEventListener("click", clearGrid);