-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
150 lines (114 loc) · 3.86 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* INITIAL PAGE */
const container = document.querySelector(".container")
const grid = document.createElement("div");
grid.setAttribute("class", "grid");
const DEFAULT_COLOR = "#778DA9";
const DEFAULT_SIZE = 16;
let squareColor = DEFAULT_COLOR;
let currentSize = DEFAULT_SIZE;
function createGrid(n) {
currentSize = n;
for (let i = 0; i < n; i++) {
const row = document.createElement("div");
row.setAttribute("class", "row");
for (let j = 0; j < n; j++) {
const square = document.createElement("div");
square.setAttribute("class", "square");
row.appendChild(square);
}
grid.appendChild(row);
} container.appendChild(grid);
}
function colorGrid() {
const squares = document.querySelectorAll(".square");
squares.forEach((square) => {
let squareOpacity = 0;
square.addEventListener("mouseenter", (e) => {
if (e.buttons === 1) {
if (darkenState) {
if (squareOpacity < 1) {
squareOpacity += 0.1;
}
square.style.opacity = squareOpacity;
} else {
square.style.opacity = 1;
}
if (rainbowState) {
square.style.backgroundColor = `rgb(${getRandomInteger(255)}, ${getRandomInteger(255)}, ${getRandomInteger(255)})`;
} else {
square.style.backgroundColor = squareColor;
}
}
})
})
}
document.addEventListener("mousedown", (e) => {
if (e.buttons === 1) {
e.preventDefault();
}
});
function deleteGrid() {
grid.textContent = "";
}
createGrid(currentSize);
colorGrid();
/* NEW GRID BUTTON */
const buttonSection = document.querySelector(".buttonSection");
const newGrid = document.createElement("button");
newGrid.textContent = "New";
newGrid.addEventListener("click", () => {
let squareNumber = parseInt(prompt("Write the number of squares per side for the new grid (1 - 100): "));
if (squareNumber > 100 || squareNumber < 1) {
alert("That number is not in the range!");
} else if (squareNumber) {
deleteGrid();
createGrid(squareNumber);
colorGrid();
}
})
/* CLEAR GRID BUTTON */
const clearGrid = document.createElement("button");
clearGrid.textContent = "Clear";
const squares = document.querySelectorAll(".square")
clearGrid.addEventListener("click", () => {
deleteGrid();
createGrid(currentSize);
colorGrid();
})
/* COLOR SELECT BUTTON */
const colorSelect = document.createElement("input");
colorSelect.setAttribute("type", "color");
colorSelect.setAttribute("value", `${squareColor}`);
colorSelect.addEventListener("change", (e) => {
squareColor = e.target.value;
rainbowState = false;
rainbowMode.classList.remove("active");
darkenState = false;
darkenMode.classList.remove("active");
})
/* RAINBOW MODE BUTTON */
const rainbowMode = document.createElement("button");
rainbowMode.textContent = "Rainbow";
function getRandomInteger(max) {
return Math.floor(Math.random() * (max + 1));
}
let rainbowState = false;
rainbowMode.addEventListener("click", () => {
rainbowState = !(rainbowState);
rainbowMode.classList.toggle("active");
})
/* DARKEN MODE BUTTON */
const darkenMode = document.createElement("button");
darkenMode.textContent = "Darken";
let darkenState = false;
darkenMode.addEventListener("click", () => {
darkenState = !(darkenState);
darkenMode.classList.toggle("active");
})
buttonSection.appendChild(colorSelect);
buttonSection.appendChild(newGrid);
buttonSection.appendChild(clearGrid);
buttonSection.appendChild(rainbowMode);
buttonSection.appendChild(darkenMode);
// TODO: COLOR MODE button (store the current color from COLOR SELECTOR)
// TODO: color grid ONLY when mouse is held down