-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
338 lines (283 loc) · 10.6 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const canvas = document.querySelector("canvas"),
toolBtns = document.querySelectorAll(".tool"),
sizeSlider = document.querySelector("#size-slider"),
colorBtns = document.querySelectorAll(".colors .option"),
colorPicker = document.querySelector("#color-picker"),
clearCanvas = document.querySelector(".clear"),
saveImg = document.querySelector(".save"),
ctx = canvas.getContext("2d"),
hamburgerMenu = document.getElementById("hamburger"),
hamburgerIcon1 = document.getElementById("hamburger-icon-1"),
hamburgerIcon2 = document.getElementById("hamburger-icon-2"),
toolsBoard = document.getElementById("tools-board"),
drawingBoard = document.getElementById("drawing-board");
let prevMouseX, prevMouseY, isDrawing = false, brushWidth = 5, selectedTool = "brush", selectedColor = "#000";
const setCanvasBackground = () => {
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = selectedColor;
}
// Store information about drawn circles, rectangles, and triangles
let circles = [];
let rectangles = [];
let triangles = [];
function windowResize() {
canvas.width = drawingBoard.clientWidth;
canvas.height = drawingBoard.clientHeight;
}
window.addEventListener("load", () => {
windowResize();
setCanvasBackground();
});
window.onresize = windowResize;
const startDraw = (e) => {
isDrawing = true;
ctx.beginPath();
ctx.imageSmoothingEnabled = true;
ctx.lineWidth = brushWidth;
ctx.strokeStyle = selectedColor;
prevMouseX = e.offsetX;
prevMouseY = e.offsetY;
};
let isDrawingCircle = false;
let isDrawingRectangle = false;
let isDrawingTriangle = false;
let currentCircle = null;
let currentRectangle = null;
let currentTriangle = null;
const drawCircle = (e) => {
if (isDrawingCircle) {
const radius = Math.sqrt((e.offsetX - prevMouseX) ** 2 + (e.offsetY - prevMouseY) ** 2);
const centerX = (e.offsetX + prevMouseX) / 2;
const centerY = (e.offsetY + prevMouseY) / 2;
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
// Redraw previously drawn shapes
redrawCircles();
redrawRectangles();
redrawTriangles();
ctx.beginPath();
ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
ctx.stroke();
// Store information about the current circle
currentCircle = { centerX, centerY, radius };
}
};
const drawRectangle = (e) => {
if (isDrawingRectangle) {
const width = e.offsetX - prevMouseX;
const height = e.offsetY - prevMouseY;
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
// Redraw previously drawn shapes
redrawCircles();
redrawRectangles();
redrawTriangles();
ctx.beginPath();
ctx.strokeRect(prevMouseX, prevMouseY, width, height);
// Store information about the current rectangle
currentRectangle = { x: prevMouseX, y: prevMouseY, width, height };
}
};
const drawTriangle = (e) => {
if (isDrawingTriangle) {
const x2 = e.offsetX;
const y2 = e.offsetY;
const x3 = prevMouseX + (prevMouseX - x2);
const y3 = y2;
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
// Redraw previously drawn shapes
redrawCircles();
redrawRectangles();
redrawTriangles();
ctx.beginPath();
ctx.moveTo(prevMouseX, prevMouseY);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.closePath();
ctx.stroke();
// Store information about the current triangle
currentTriangle = { x1: prevMouseX, y1: prevMouseY, x2, y2, x3, y3 };
}
};
canvas.addEventListener("mousedown", (e) => {
startDraw(e);
if (selectedTool === "circle") {
isDrawingCircle = true;
currentCircle = null; // Reset the current circle
} else if (selectedTool === "rectangle") {
isDrawingRectangle = true;
currentRectangle = null; // Reset the current rectangle
} else if (selectedTool === "triangle") {
isDrawingTriangle = true;
currentTriangle = null; // Reset the current triangle
}
});
canvas.addEventListener("mouseup", () => {
isDrawingCircle = false;
isDrawingRectangle = false;
isDrawingTriangle = false;
if (currentCircle) {
// Store information about the completed circle
circles.push(currentCircle);
currentCircle = null; // Reset the current circle
} else if (currentRectangle) {
// Store information about the completed rectangle
rectangles.push(currentRectangle);
currentRectangle = null; // Reset the current rectangle
} else if (currentTriangle) {
// Store information about the completed triangle
triangles.push(currentTriangle);
currentTriangle = null; // Reset the current triangle
}
});
canvas.addEventListener("mousemove", (e) => {
if (selectedTool === "circle") {
if (isDrawingCircle) {
drawCircle(e);
}
} else if (selectedTool === "rectangle") {
if (isDrawingRectangle) {
drawRectangle(e);
}
} else if (selectedTool === "triangle") {
if (isDrawingTriangle) {
drawTriangle(e);
}
} else {
drawing(e);
}
});
clearCanvas.addEventListener("click", () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
circles = [];
rectangles = [];
triangles = [];
setCanvasBackground();
});
// Additional functions to redraw existing shapes
function redrawCircles() {
circles.forEach((circle) => {
ctx.beginPath();
ctx.arc(circle.centerX, circle.centerY, circle.radius, 0, 2 * Math.PI);
ctx.stroke();
});
}
function redrawRectangles() {
rectangles.forEach((rectangle) => {
ctx.beginPath();
ctx.strokeRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
});
}
function redrawTriangles() {
triangles.forEach((triangle) => {
ctx.beginPath();
ctx.moveTo(triangle.x1, triangle.y1);
ctx.lineTo(triangle.x2, triangle.y2);
ctx.lineTo(triangle.x3, triangle.y3);
ctx.closePath();
ctx.stroke();
});
}
const drawRect = (e) => {
ctx.beginPath();
const width = e.offsetX - prevMouseX;
const height = e.offsetY - prevMouseY;
ctx.strokeRect(prevMouseX, prevMouseY, width, height);
};
const drawing = (e) => {
e.preventDefault();
if (!isDrawing) return;
if (selectedTool === "brush" || selectedTool === "eraser") {
ctx.strokeStyle = selectedTool === "eraser" ? "#fff" : selectedColor;
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
prevMouseX = e.offsetX;
prevMouseY = e.offsetY;
}
};
toolBtns.forEach(btn => {
if (btn.id != "size") {
btn.addEventListener("click", () => {
document.querySelector(".options .active").classList.remove("active");
btn.classList.add("active");
selectedTool = btn.id;
// Remove previous drawing event listeners
canvas.removeEventListener("mousemove", drawing);
canvas.removeEventListener("mousemove", drawCircle);
canvas.removeEventListener("mousemove", drawRectangle);
canvas.removeEventListener("mousemove", drawTriangle);
// Add the appropriate event listener based on the selected tool
if (selectedTool === "brush" || selectedTool === "eraser") {
canvas.addEventListener("mousemove", drawing);
} else if (selectedTool === "circle") {
canvas.addEventListener("mousemove", drawCircle);
} else if (selectedTool === "rectangle") {
canvas.addEventListener("mousemove", drawRectangle);
} else if (selectedTool === "triangle") {
canvas.addEventListener("mousemove", drawTriangle);
}
});
}
});
sizeSlider.addEventListener("change", () => {
brushWidth = sizeSlider.value;
});
colorBtns.forEach(btn => {
btn.addEventListener("click", () => {
document.querySelector(".options .selected").classList.remove("selected");
btn.classList.add("selected");
selectedColor = window.getComputedStyle(btn).getPropertyValue("background-color");
});
});
colorPicker.addEventListener("change", () => {
colorPicker.parentElement.style.background = colorPicker.value;
});
saveImg.addEventListener("click", () => {
const link = document.createElement("a");
link.download = `${Date.now()}.png`;
link.href = canvas.toDataURL();
link.click();
});
canvas.addEventListener("mousedown", startDraw);
canvas.addEventListener("mouseup", () => isDrawing = false);
// Touch Support
const startDrawTouch = (e) => {
e.preventDefault();
isDrawing = true;
ctx.beginPath();
ctx.imageSmoothingEnabled = true;
ctx.lineWidth = brushWidth;
ctx.strokeStyle = selectedColor;
const touch = e.touches[0];
prevMouseX = touch.clientX - canvas.getBoundingClientRect().left;
prevMouseY = touch.clientY - canvas.getBoundingClientRect().top;
};
const drawTouch = (e) => {
e.preventDefault();
if (!isDrawing) return;
const touch = e.touches[0];
const offsetX = touch.clientX - canvas.getBoundingClientRect().left;
const offsetY = touch.clientY - canvas.getBoundingClientRect().top;
if (selectedTool === "brush" || selectedTool === "eraser") {
ctx.strokeStyle = selectedTool === "eraser" ? "#fff" : selectedColor;
ctx.lineTo(offsetX, offsetY);
ctx.stroke();
prevMouseX = offsetX;
prevMouseY = offsetY;
}
};
const endDrawTouch = () => {
isDrawing = false;
};
canvas.removeEventListener("touchstart", startDrawTouch);
canvas.removeEventListener("touchmove", drawTouch);
canvas.removeEventListener("touchend", endDrawTouch);
canvas.addEventListener("touchstart", startDrawTouch);
canvas.addEventListener("touchmove", drawTouch);
canvas.addEventListener("touchend", endDrawTouch);
canvas.removeEventListener("mousedown", startDraw);
canvas.removeEventListener("mouseup", () => isDrawing = false);
hamburgerMenu.addEventListener("click", () => {
toolsBoard.classList.toggle("tools-board-closed");
hamburgerIcon1.classList.toggle("hamburger-icon-1-closed");
hamburgerIcon2.classList.toggle("hamburger-icon-2-closed");
});