-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoto.js
47 lines (37 loc) · 1.27 KB
/
photo.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
const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
const clearButton = document.getElementById("clear-button");
const colorButtons = document.querySelectorAll(".color-button");
let isDrawing = false;
let currentColor = "black";
canvas.addEventListener("mousedown", startDrawing);
canvas.addEventListener("mousemove", draw);
canvas.addEventListener("mouseup", stopDrawing);
clearButton.addEventListener("click", clearCanvas);
colorButtons.forEach(button => {
button.addEventListener("click", changeColor);
});
function startDrawing(e) {
isDrawing = true;
context.beginPath();
context.moveTo(e.clientX - canvas.getBoundingClientRect().left, e.clientY - canvas.getBoundingClientRect().top);
}
function draw(e) {
if (!isDrawing) return;
context.lineWidth = 2;
context.lineCap = "round";
context.strokeStyle = currentColor;
context.lineTo(e.clientX - canvas.getBoundingClientRect().left, e.clientY - canvas.getBoundingClientRect().top);
context.stroke();
}
function stopDrawing() {
isDrawing = false;
context.beginPath();
}
function clearCanvas() {
context.clearRect(0, 0, canvas.width, canvas.height);
}
function changeColor(e) {
currentColor = e.target.id;
context.strokeStyle = currentColor;
}