Skip to content

Commit 5603b85

Browse files
committed
fix clear
1 parent fffa665 commit 5603b85

File tree

1 file changed

+49
-38
lines changed

1 file changed

+49
-38
lines changed

client/src/App.jsx

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef } from "react";
1+
import { useEffect, useRef, useState } from "react";
22
import "./App.css";
33
import { io } from "socket.io-client";
44

@@ -7,12 +7,21 @@ const socket = io("http://localhost:8000");
77
function App() {
88
const canvasRef = useRef(null);
99
const sidebarRef = useRef(null);
10-
let color = "#000000";
10+
let color = '#000000'
1111
let ctx;
1212
let canvas;
1313
let lineWidth;
1414

15-
useEffect(() => {
15+
function drawLine(sx, sy, ex, ey, color, lineWidth) {
16+
ctx.moveTo(sx, sy);
17+
ctx.lineTo(ex, ey);
18+
ctx.lineCap = "round";
19+
ctx.lineWidth = lineWidth;
20+
ctx.strokeStyle = color;
21+
ctx.stroke();
22+
}
23+
24+
useEffect(() => {
1625
canvas = canvasRef.current;
1726
let isDrawing = false;
1827
let startX = 0;
@@ -22,40 +31,16 @@ function App() {
2231
canvas.width = canvas.getBoundingClientRect().width;
2332
canvas.height = canvas.getBoundingClientRect().height;
2433

25-
function drawLine(sx, sy, ex, ey, color, lineWidth) {
26-
ctx.moveTo(sx, sy);
27-
ctx.lineTo(ex, ey);
28-
ctx.lineCap = "round";
29-
ctx.stroke();
30-
ctx.lineWidth = lineWidth;
31-
ctx.strokeStyle = color;
32-
}
33-
3434
function handleMousemove(e) {
3535
if (!isDrawing) return;
3636
const endX = e.clientX - canvas.getBoundingClientRect().left;
3737
const endY = e.clientY - canvas.getBoundingClientRect().top;
38-
drawLine(startX, startY, endX, endY);
38+
drawLine(startX, startY, endX, endY, color );
3939
socket.emit("draw", { startX, startY, endX, endY, color, lineWidth });
4040
startX = endX;
4141
startY = endY;
4242
}
4343

44-
socket.on("draw", (data) => {
45-
drawLine(
46-
data.startX,
47-
data.startY,
48-
data.endX,
49-
data.endY,
50-
data.color,
51-
data.lineWidth
52-
);
53-
});
54-
55-
socket.on("clear", () => {
56-
clearRect();
57-
});
58-
5944
function handleMousedown(e) {
6045
isDrawing = true;
6146
startX = e.clientX - canvas.getBoundingClientRect().left;
@@ -65,7 +50,6 @@ function App() {
6550
isDrawing = false;
6651
startX = 0;
6752
startY = 0;
68-
ctx.stroke();
6953
ctx.beginPath();
7054
}
7155

@@ -77,19 +61,46 @@ function App() {
7761
canvas.removeEventListener("mousemove", handleMousemove);
7862
canvas.removeEventListener("mousedown", handleMousedown);
7963
canvas.removeEventListener("mouseup", handleMouseup);
80-
socket.off("draw");
81-
socket.off("clear");
8264
};
65+
}, [color]);
66+
67+
useEffect(() => {
68+
socket.on("draw", (data) => {
69+
drawLine(
70+
data.startX,
71+
data.startY,
72+
data.endX,
73+
data.endY,
74+
data.color,
75+
data.lineWidth
76+
);
77+
});
78+
79+
socket.on("clear", () => {
80+
clearRect();
81+
});
82+
83+
return () => {
84+
socket.off("draw");
85+
socket.off("clear");
86+
}
8387
}, []);
8488

8589
function clearRect() {
8690
ctx.clearRect(0, 0, canvas.width, canvas.height);
8791
}
8892

93+
function clearOnClick(){
94+
clearRect();
95+
socket.emit('clear')
96+
}
97+
8998
function addStroke(e) {
9099
if (e.target.id === "stroke") {
91-
color = e.target.value;
92-
ctx.strokeStyle = color;
100+
const newColor = e.target.value;
101+
color = newColor;
102+
ctx.strokeStyle = newColor;
103+
93104
}
94105
}
95106

@@ -109,9 +120,8 @@ function App() {
109120
<input
110121
id="stroke"
111122
name="stroke"
112-
type="color"
113-
defaultValue={color}
114-
onInput={addStroke}
123+
type="color"
124+
onChange={addStroke}
115125
/>
116126
</div>
117127
<div className="input-container" id="linewidth">
@@ -121,10 +131,10 @@ function App() {
121131
name="lineWidth"
122132
type="number"
123133
defaultValue="3"
124-
onInput={addLineWidth}
134+
onChange={addLineWidth}
125135
/>
126136
</div>
127-
<button id="clear" onClick={() => socket.emit("clear")}>
137+
<button id="clear" onClick={clearOnClick}>
128138
Clear
129139
</button>
130140
</div>
@@ -136,3 +146,4 @@ function App() {
136146
}
137147

138148
export default App;
149+

0 commit comments

Comments
 (0)