Skip to content

Commit 0e7cc23

Browse files
committed
add clear functionality
1 parent 05fe9d7 commit 0e7cc23

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

api/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ io.on('connection', (socket)=>{
2222
socket.on('draw', (data)=>{
2323
socket.broadcast.emit('draw', data);
2424
})
25+
socket.on('clear', ()=>{
26+
io.emit('clear');
27+
})
2528
})
2629

2730
server.listen(PORT, ()=>{

client/src/App.jsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import {io} from 'socket.io-client';
44

55
const socket = io('http://localhost:8000');
66

7-
socket.on('connect', (socket)=>{
8-
console.log('socket connected on client side');
9-
})
107
function App() {
118
const canvasRef = useRef(null);
129
const sidebarRef = useRef(null);
@@ -49,6 +46,8 @@ function App() {
4946
drawLine(data.startX, data.startY, data.endX, data.endY, data.color, data.lineWidth);
5047
})
5148

49+
socket.on('clear', ()=>{clearRect()})
50+
5251
function handleMousedown(e) {
5352
isDrawing = true;
5453
startX = e.clientX - canvas.getBoundingClientRect().left;
@@ -70,13 +69,14 @@ function App() {
7069
canvas.removeEventListener("mousemove", handleMousemove);
7170
canvas.removeEventListener("mousedown", handleMousedown);
7271
canvas.removeEventListener("mouseup", handleMouseup);
72+
socket.off('draw');
73+
socket.off('clear');
74+
7375
};
7476
}, []);
7577

76-
function clearRect(e){
77-
if (e.target.id === "clear") {
78+
function clearRect(){
7879
ctx.clearRect(0, 0, canvas.width, canvas.height);
79-
}
8080
}
8181

8282
function addStroke(e){
@@ -111,7 +111,7 @@ function App() {
111111
onInput={addLineWidth}
112112
/>
113113
</div>
114-
<button id="clear" onClick={clearRect}>Clear</button>
114+
<button id="clear" onClick={()=>socket.emit('clear')}>Clear</button>
115115
</div>
116116
<div className="canvas-container">
117117
<canvas className="canvas" ref={canvasRef}></canvas>

0 commit comments

Comments
 (0)