1
- import { useEffect , useRef } from "react" ;
1
+ import { useEffect , useRef , useState } from "react" ;
2
2
import "./App.css" ;
3
3
import { io } from "socket.io-client" ;
4
4
@@ -7,12 +7,21 @@ const socket = io("http://localhost:8000");
7
7
function App ( ) {
8
8
const canvasRef = useRef ( null ) ;
9
9
const sidebarRef = useRef ( null ) ;
10
- let color = " #000000" ;
10
+ let color = ' #000000'
11
11
let ctx ;
12
12
let canvas ;
13
13
let lineWidth ;
14
14
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 ( ( ) => {
16
25
canvas = canvasRef . current ;
17
26
let isDrawing = false ;
18
27
let startX = 0 ;
@@ -22,40 +31,16 @@ function App() {
22
31
canvas . width = canvas . getBoundingClientRect ( ) . width ;
23
32
canvas . height = canvas . getBoundingClientRect ( ) . height ;
24
33
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
-
34
34
function handleMousemove ( e ) {
35
35
if ( ! isDrawing ) return ;
36
36
const endX = e . clientX - canvas . getBoundingClientRect ( ) . left ;
37
37
const endY = e . clientY - canvas . getBoundingClientRect ( ) . top ;
38
- drawLine ( startX , startY , endX , endY ) ;
38
+ drawLine ( startX , startY , endX , endY , color ) ;
39
39
socket . emit ( "draw" , { startX, startY, endX, endY, color, lineWidth } ) ;
40
40
startX = endX ;
41
41
startY = endY ;
42
42
}
43
43
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
-
59
44
function handleMousedown ( e ) {
60
45
isDrawing = true ;
61
46
startX = e . clientX - canvas . getBoundingClientRect ( ) . left ;
@@ -65,7 +50,6 @@ function App() {
65
50
isDrawing = false ;
66
51
startX = 0 ;
67
52
startY = 0 ;
68
- ctx . stroke ( ) ;
69
53
ctx . beginPath ( ) ;
70
54
}
71
55
@@ -77,19 +61,46 @@ function App() {
77
61
canvas . removeEventListener ( "mousemove" , handleMousemove ) ;
78
62
canvas . removeEventListener ( "mousedown" , handleMousedown ) ;
79
63
canvas . removeEventListener ( "mouseup" , handleMouseup ) ;
80
- socket . off ( "draw" ) ;
81
- socket . off ( "clear" ) ;
82
64
} ;
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
+ }
83
87
} , [ ] ) ;
84
88
85
89
function clearRect ( ) {
86
90
ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
87
91
}
88
92
93
+ function clearOnClick ( ) {
94
+ clearRect ( ) ;
95
+ socket . emit ( 'clear' )
96
+ }
97
+
89
98
function addStroke ( e ) {
90
99
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
+
93
104
}
94
105
}
95
106
@@ -109,9 +120,8 @@ function App() {
109
120
< input
110
121
id = "stroke"
111
122
name = "stroke"
112
- type = "color"
113
- defaultValue = { color }
114
- onInput = { addStroke }
123
+ type = "color"
124
+ onChange = { addStroke }
115
125
/>
116
126
</ div >
117
127
< div className = "input-container" id = "linewidth" >
@@ -121,10 +131,10 @@ function App() {
121
131
name = "lineWidth"
122
132
type = "number"
123
133
defaultValue = "3"
124
- onInput = { addLineWidth }
134
+ onChange = { addLineWidth }
125
135
/>
126
136
</ div >
127
- < button id = "clear" onClick = { ( ) => socket . emit ( "clear" ) } >
137
+ < button id = "clear" onClick = { clearOnClick } >
128
138
Clear
129
139
</ button >
130
140
</ div >
@@ -136,3 +146,4 @@ function App() {
136
146
}
137
147
138
148
export default App ;
149
+
0 commit comments