-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
154 lines (130 loc) · 3.89 KB
/
script.js
File metadata and controls
154 lines (130 loc) · 3.89 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
let isDragging = false;
let rub = false;
let pen = false;
let rainbowcolor = false;
let initialRow, initialCol;
let gridItems = [];
let bor = false;
function createGrid(rows, cols) {
const gridContainer = document.getElementById('gridContainer');
gridContainer.innerHTML = '';
gridContainer.style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
gridContainer.style.gridTemplateRows = `repeat(${rows}, 1fr)`;
for (let i = 0; i < rows; i++) {
gridItems[i] = [];
for (let j = 0; j < cols; j++) {
const gridItem = document.createElement('div');
gridItem.classList.add('grid-item');
gridItem.setAttribute('data-row', i);
gridItem.setAttribute('data-col', j);
gridItem.addEventListener('mousedown', handleMouseDown);
gridItem.addEventListener('mouseenter', handleMouseEnter);
gridItem.addEventListener('mouseup', handleMouseUp);
gridContainer.appendChild(gridItem);
gridItems[i][j] = gridItem;
}
}
}
function handleMouseDown(event) {
isDragging = true;
initialRow = parseInt(event.target.getAttribute('data-row'));
initialCol = parseInt(event.target.getAttribute('data-col'));
toggleMark(event.target);
}
function handleMouseEnter(event) {
if (isDragging) {
const currentRow = parseInt(event.target.getAttribute('data-row'));
const currentCol = parseInt(event.target.getAttribute('data-col'));
if (currentRow !== initialRow || currentCol !== initialCol) {
toggleMark(event.target);
initialRow = currentRow;
initialCol = currentCol;
}
}
}
function handleMouseUp() {
isDragging = false;
}
function toggleMark(gridItem) {
gridItem.classList.toggle('marked');
if (rub) {
gridItem.style.backgroundColor = "white";
} else if(pen){
color(gridItem);
}
else if(rainbowcolor){
random_color(gridItem);
}
clear(gridItem);
}
function color(gridItem) {
gridItem.style.backgroundColor = document.getElementById('selectColor').value;
}
function random_color(gridItem) {
let r = Math.random() * 100;
let g = Math.random() * 100;
let b = Math.random() * 100;
gridItem.style.backgroundColor = `rgb(${r},${g},${b})`;
}
function clear(gridItem) {
document.getElementById('cl').addEventListener("click", () => {
gridItem.style.backgroundColor = "white";
});
}
function rubber() {
document.getElementById('rb').addEventListener("click", () => {
rub = true;
pen = false;
rainbowcolor = false;
});
return rub;
}
function pencil() {
document.getElementById('pn').addEventListener("click", () => {
pen = true;
rub = false;
rainbowcolor = false;1
});
return pen;
}
function rainbow() {
document.getElementById('rnd').addEventListener("click", () => {
rainbowcolor = true;
pen = false;
rub = false;
});
return rainbowcolor;
}
function rB() {
gridItems.forEach(row => {
row.forEach(gridItem => {
gridItem.style.border = "none";
});
});
}
function removeBorder(){
document.getElementById('rm').addEventListener("click", rB());
}
function main() {
let rows, cols;
let n = document.getElementById('sz');
document.getElementById('ent').addEventListener("click", () => {
rows = parseInt(n.value);
cols = parseInt(n.value);
if (isNaN(rows) || isNaN(cols) || rows < 1 || cols < 1 || rows > 50 || cols > 50) {
alert("Please enter a valid number between 1 and 50.");
return;
}
createGrid(rows, cols);
pencil();
rubber();
rainbow();
});
document.getElementById('rm').addEventListener("click", () => {
bor = true;
if (rows && cols) {
removeBorder();
}
});
}
main();