forked from Charuhas10/Etch-a-Sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
140 lines (118 loc) · 4.07 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
const divContainer = document.getElementById("grid-container");
const resizeBtn = document.getElementById("resize");
const clearBtn = document.getElementById("clear");
const eraseBtn = document.getElementById("erase");
const blackBtn = document.getElementById("black");
const colorBtn = document.getElementById("color");
const rainbowBtn = document.getElementById("rainbow");
const containerSize = divContainer.clientWidth;
const num = 16;
const itemSize = Math.floor(containerSize / num);
let isDrawing = false;
function makeDivs(num, itemSize) {
divContainer.style.gridTemplateColumns = `repeat(${num}, ${itemSize}px)`;
divContainer.style.gridTemplateRows = `repeat(${num}, ${itemSize}px)`;
for (let i = 0; i < num * num; i++) {
const div = document.createElement("div");
divContainer.appendChild(div).className = "grid-item";
}
colorDivs(document.querySelectorAll(".grid-item"), "black", false);
}
resizeBtn.onclick = () => {
const gridSize = parseInt(prompt("Enter the desired grid size:"));
if (!isNaN(gridSize) && gridSize > 0 && gridSize <= 100) {
const itemSize = Math.floor(containerSize / gridSize);
clearGrid();
makeDivs(gridSize, itemSize);
} else if (!isNaN(gridSize)) {
alert("Invalid grid size! Please enter a positive number.");
}
};
function clearGrid() {
while (divContainer.firstChild) {
divContainer.removeChild(divContainer.firstChild);
}
}
clearBtn.onclick = () => {
let divs = document.querySelectorAll(".grid-item");
divs.forEach((div) => {
div.style.backgroundColor = "white";
});
colorDivs(document.querySelectorAll(".grid-item"), "black", false);
};
eraseBtn.onclick = () => {
let divs = document.querySelectorAll(".grid-item");
colorDivs(divs, "white", false);
};
blackBtn.onclick = () => {
let divs = document.querySelectorAll(".grid-item");
colorDivs(divs, "black", false);
};
colorBtn.addEventListener("click", () => {
const colorPicker = document.createElement("input");
colorPicker.type = "color";
colorPicker.addEventListener("change", () => {
const color = colorPicker.value;
let divs = document.querySelectorAll(".grid-item");
colorDivs(divs, color, false);
});
colorPicker.click();
});
rainbowBtn.onclick = () => {
let divs = document.querySelectorAll(".grid-item");
colorDivs(divs, "rainbow", true);
};
makeDivs(num, itemSize);
function colorDivs(divs, color, IsRgb) {
divs.forEach((div) => {
div.addEventListener("mousedown", () => {
isDrawing = true;
div.style.backgroundColor =
IsRgb == false
? color
: `rgb(${Math.floor(Math.random() * 256)}, ${Math.floor(
Math.random() * 256
)}, ${Math.floor(Math.random() * 256)})`;
});
div.addEventListener("mousemove", () => {
if (isDrawing) {
div.style.backgroundColor =
IsRgb == false
? color
: `rgb(${Math.floor(Math.random() * 256)}, ${Math.floor(
Math.random() * 256
)}, ${Math.floor(Math.random() * 256)})`;
}
});
div.addEventListener("mouseup", () => {
isDrawing = false;
});
div.addEventListener("touchstart", () => {
isDrawing = true;
div.style.backgroundColor =
IsRgb == false
? color
: `rgb(${Math.floor(Math.random() * 256)}, ${Math.floor(
Math.random() * 256
)}, ${Math.floor(Math.random() * 256)})`;
});
div.addEventListener("touchmove", (event) => {
event.preventDefault(); // Prevent scrolling while drawing
if (isDrawing) {
const touch = event.touches[0];
const target = document.elementFromPoint(touch.clientX, touch.clientY);
if (target && target.classList.contains("grid-item")) {
target.style.backgroundColor =
IsRgb == false
? color
: `rgb(${Math.floor(Math.random() * 256)}, ${Math.floor(
Math.random() * 256
)}, ${Math.floor(Math.random() * 256)})`;
}
}
});
div.addEventListener("touchend", () => {
isDrawing = false;
});
});
}