Skip to content

Commit cd88832

Browse files
committed
fix the bug that delete the drawing on eraserMode
1 parent 12355fa commit cd88832

File tree

1 file changed

+47
-33
lines changed

1 file changed

+47
-33
lines changed

client/src/App.jsx

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable react-hooks/exhaustive-deps */
12
import { useEffect, useRef, useState } from "react";
23
import "./App.css";
34
import { io } from "socket.io-client";
@@ -16,39 +17,43 @@ function App() {
1617
const [showMenu, setShowMenu ] = useState(false);
1718
const eraserMode = useRecoilValue(eraserState);
1819
const [position, setPosition] = useRecoilState(cursorPosition);
20+
const [ctx, setCtx] = useState(null);
21+
const [startX, setStartX] = useState(0);
22+
const [startY, setStartY] = useState(0);
23+
const [isDrawing, setIsDrawing] = useState(false);
24+
const [penColor, setPenColor] = useState("#000000");
1925

2026
function toggleMenu(){
2127
setShowMenu(!showMenu);
2228
}
2329

2430
const canvasRef = useRef(null);
2531
const sidebarRef = useRef(null);
26-
let color = '#000000'
27-
let ctx;
28-
let canvas;
2932
let lineWidth;
30-
31-
function drawLine(sx, sy, ex, ey, color, lineWidth) {
33+
34+
function drawLine(sx, sy, ex, ey, penColor, lineWidth) {
3235
ctx.moveTo(sx, sy);
3336
ctx.lineTo(ex, ey);
3437
ctx.lineCap = "round";
3538
ctx.lineWidth = lineWidth;
36-
ctx.strokeStyle = color;
39+
ctx.strokeStyle = penColor;
3740
ctx.stroke();
3841
}
39-
42+
4043
useEffect(() => {
41-
canvas = canvasRef.current;
42-
let isDrawing = false;
43-
let startX = 0;
44-
let startY = 0;
45-
46-
ctx = canvas.getContext("2d");
47-
canvas.width = canvas.getBoundingClientRect().width;
48-
canvas.height = canvas.getBoundingClientRect().height;
44+
const canvas = canvasRef.current;
45+
if(canvas){
46+
const context = canvas.getContext("2d");
47+
canvas.width = canvas.getBoundingClientRect().width;
48+
canvas.height = canvas.getBoundingClientRect().height;
49+
setCtx(context);
50+
}
51+
}, [canvasRef.current]);
4952

53+
useEffect(() => {
54+
if(!ctx) return;
55+
const canvas = canvasRef.current;
5056
function handleMousemove(e) {
51-
5257
// if eraseMode is set the position of the eraser cursor
5358
if (eraserMode) {
5459
setPosition(() => ({ x: e.clientX, y: e.clientY }));
@@ -57,21 +62,24 @@ function App() {
5762
if (!isDrawing) return;
5863
const endX = e.clientX - canvas.getBoundingClientRect().left;
5964
const endY = e.clientY - canvas.getBoundingClientRect().top;
60-
drawLine(startX, startY, endX, endY, color);
61-
socket.emit("draw", { startX, startY, endX, endY, color, lineWidth });
62-
startX = endX;
63-
startY = endY;
65+
drawLine(startX, startY, endX, endY, penColor);
66+
socket.emit("draw", { startX, startY, endX, endY, penColor, lineWidth });
67+
setStartX(endX);
68+
setStartY(endY);
69+
6470
}
6571

6672
function handleMousedown(e) {
67-
isDrawing = true;
68-
startX = e.clientX - canvas.getBoundingClientRect().left;
69-
startY = e.clientY - canvas.getBoundingClientRect().top;
73+
setIsDrawing(true);
74+
let X = e.clientX - canvas.getBoundingClientRect().left;
75+
let Y = e.clientY - canvas.getBoundingClientRect().top;
76+
setStartX(X);
77+
setStartY(Y);
7078
}
7179
function handleMouseup() {
72-
isDrawing = false;
73-
startX = 0;
74-
startY = 0;
80+
setIsDrawing(false);
81+
setStartX(0);
82+
setStartY(0);
7583
ctx.beginPath();
7684
}
7785

@@ -84,7 +92,7 @@ function App() {
8492
canvas.removeEventListener("mousedown", handleMousedown);
8593
canvas.removeEventListener("mouseup", handleMouseup);
8694
};
87-
}, [color, eraserMode, position]);
95+
}, [penColor, eraserMode, position, ctx, isDrawing, startX, startY]);
8896

8997
useEffect(() => {
9098
socket.on("draw", (data) => {
@@ -93,7 +101,7 @@ function App() {
93101
data.startY,
94102
data.endX,
95103
data.endY,
96-
data.color,
104+
data.penColor,
97105
data.lineWidth
98106
);
99107
});
@@ -106,10 +114,12 @@ function App() {
106114
socket.off("draw");
107115
socket.off("clear");
108116
}
109-
}, []);
117+
}, [socket, ctx]);
110118

111119
function clearRect() {
112-
ctx.clearRect(0, 0, canvas.width, canvas.height);
120+
if(ctx){
121+
ctx.clearRect(0, 0, canvasRef.current.width, canvasRef.current.height);
122+
}
113123
}
114124

115125
function clearOnClick(){
@@ -120,16 +130,20 @@ function App() {
120130
function addStroke(e) {
121131
if (e.target.id === "penColor") {
122132
const newColor = e.target.value;
123-
color = newColor;
124-
ctx.strokeStyle = newColor;
133+
setPenColor(newColor);
134+
if(ctx){
135+
ctx.strokeStyle = newColor;
136+
}
125137

126138
}
127139
}
128140

129141
function addLineWidth(e) {
130142
if (e.target.id === "lineWidth") {
131143
lineWidth = e.target.value;
132-
ctx.lineWidth = lineWidth;
144+
if(ctx){
145+
ctx.lineWidth = lineWidth;
146+
}
133147
}
134148
}
135149

0 commit comments

Comments
 (0)