@@ -13,6 +13,7 @@ function App() {
13
13
let color = '#000000'
14
14
let ctx ;
15
15
let canvas ;
16
+ let lineWidth ;
16
17
17
18
useEffect ( ( ) => {
18
19
canvas = canvasRef . current ;
@@ -24,22 +25,30 @@ function App() {
24
25
25
26
canvas . width = canvas . getBoundingClientRect ( ) . width ;
26
27
canvas . height = canvas . getBoundingClientRect ( ) . height ;
27
- function drawLine ( sx , sy , ex , ey ) {
28
+
29
+ function drawLine ( sx , sy , ex , ey , color , lineWidth ) {
28
30
ctx . moveTo ( sx , sy ) ;
29
31
ctx . lineTo ( ex , ey ) ;
30
32
ctx . lineCap = "round" ;
31
33
ctx . stroke ( ) ;
34
+ ctx . lineWidth = lineWidth ;
35
+ ctx . strokeStyle = color ;
32
36
}
33
37
34
- function handleMouseover ( e ) {
38
+ function handleMousemove ( e ) {
35
39
if ( ! isDrawing ) return ;
36
40
const endX = e . clientX - canvas . getBoundingClientRect ( ) . left ;
37
41
const endY = e . clientY - canvas . getBoundingClientRect ( ) . top ;
38
42
drawLine ( startX , startY , endX , endY ) ;
43
+ socket . emit ( 'draw' , { startX, startY, endX, endY, color, lineWidth} )
39
44
startX = endX ;
40
45
startY = endY ;
41
46
}
42
47
48
+ socket . on ( 'draw' , ( data ) => {
49
+ drawLine ( data . startX , data . startY , data . endX , data . endY , data . color , data . lineWidth ) ;
50
+ } )
51
+
43
52
function handleMousedown ( e ) {
44
53
isDrawing = true ;
45
54
startX = e . clientX - canvas . getBoundingClientRect ( ) . left ;
@@ -53,12 +62,12 @@ function App() {
53
62
ctx . beginPath ( ) ;
54
63
}
55
64
56
- canvas . addEventListener ( "mousemove" , handleMouseover ) ;
65
+ canvas . addEventListener ( "mousemove" , handleMousemove ) ;
57
66
canvas . addEventListener ( "mousedown" , handleMousedown ) ;
58
67
canvas . addEventListener ( "mouseup" , handleMouseup ) ;
59
68
60
69
return ( ) => {
61
- canvas . removeEventListener ( "mousemove" , handleMouseover ) ;
70
+ canvas . removeEventListener ( "mousemove" , handleMousemove ) ;
62
71
canvas . removeEventListener ( "mousedown" , handleMousedown ) ;
63
72
canvas . removeEventListener ( "mouseup" , handleMouseup ) ;
64
73
} ;
@@ -79,7 +88,8 @@ function App() {
79
88
80
89
function addLineWidth ( e ) {
81
90
if ( e . target . id === "lineWidth" ) {
82
- ctx . lineWidth = e . target . value ;
91
+ lineWidth = e . target . value ;
92
+ ctx . lineWidth = lineWidth ;
83
93
}
84
94
}
85
95
0 commit comments