-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
145 lines (125 loc) · 3.78 KB
/
main.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
const canvas = document.getElementById("canvas");
const sizeElement = document.getElementById("size");
const colorElement = document.getElementById("color");
const clearElement = document.getElementById("clear");
const increaseButton = document.getElementById("increase");
const decreaseButton = document.getElementById("decrease");
const redoButton = document.getElementById("redo");
const undoButton = document.getElementById("undo");
const ctx = canvas.getContext("2d");
// Error handling for DOM elements to ensure they exist before adding event listeners
if (!canvas || !sizeElement || !colorElement || !clearElement || !increaseButton || !decreaseButton || !redoButton || !undoButton) {
console.error("One or more required DOM elements are missing.");
}
// Resize the canvas
let resizeTimeout;
window.addEventListener("resize", resizeCanvas);
resizeCanvas();
// Update the Color of the brush
let color = "black";
colorElement.addEventListener("change", (e) => {
color = e.target.value;
});
// Update the size of the brush on the screen using input Field
let size = 1;
increaseButton.addEventListener("click", () => updateBrushSize(1));
decreaseButton.addEventListener("click", () => updateBrushSize(-1));
// Drawing Constraints
let isDrawing = false;
let points = [];
let lines = [];
let undoStack = [];
var mouse = { x: 0, y: 0 };
var previous = { x: 0, y: 0 };
canvas.addEventListener("mousedown", (e) => {
isDrawing = true;
previous = { x: mouse.x, y: mouse.y };
mouse = mousePos(canvas, e);
points = [{ x: mouse.x, y: mouse.y }];
});
canvas.addEventListener("mousemove", (e) => {
if (isDrawing) {
previous = { x: mouse.x, y: mouse.y };
mouse = mousePos(canvas, e);
points.push({ x: mouse.x, y: mouse.y });
drawLine(previous, mouse);
}
});
canvas.addEventListener("mouseup", () => {
if (isDrawing) {
isDrawing = false;
lines.push([...points]);
}
});
clearElement.addEventListener("click", clear);
undoButton.addEventListener("click", Undo);
redoButton.addEventListener("click", redo);
function resizeCanvas() {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
canvas.width = window.innerWidth - 170;
canvas.height = window.innerHeight - 40;
}, 100);
}
function updateValue(e) {
const newSize = parseInt(e.target.value, 10);
if (!isNaN(newSize)) {
size = newSize;
sizeElement.value = size;
} else {
console.log("Invalid input. Please enter a numeric value.");
}
}
// Increase and decrease the size of the brush using buttons
function updateBrushSize(increment) {
size = Math.min(50, Math.max(1, size + increment));
sizeElement.value = size;
updateSizeOnScreen();
}
function drawLine(prev, curr) {
requestAnimationFrame(() => {
ctx.beginPath();
ctx.moveTo(prev.x, prev.y);
ctx.lineTo(curr.x, curr.y);
ctx.strokeStyle = color;
ctx.lineWidth = size;
ctx.lineCap = "round";
ctx.stroke();
});
}
// Function to find the mouse position
function mousePos(canvas, e) {
var ClientRect = canvas.getBoundingClientRect();
return {
x: Math.round(e.clientX - ClientRect.left),
y: Math.round(e.clientY - ClientRect.top),
};
}
// Function to draw the paths again on the canvas
function drawPaths() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
lines.forEach(path => {
ctx.beginPath();
ctx.moveTo(path[0].x, path[0].y);
for (let i = 1; i < path.length; i++) {
ctx.lineTo(path[i].x, path[i].y);
}
ctx.strokeStyle = color;
ctx.lineWidth = size;
ctx.lineCap = "round";
ctx.stroke();
});
}
function clear() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function Undo() {
if (lines.length === 0) return;
undoStack.push(lines.pop());
drawPaths();
}
function redo() {
if (undoStack.length === 0) return;
lines.push(undoStack.pop());
drawPaths();
}