1
1
import { useEffect , useRef } from "react" ;
2
2
import "./App.css" ;
3
- import { io } from ' socket.io-client' ;
3
+ import { io } from " socket.io-client" ;
4
4
5
- const socket = io ( ' http://localhost:8000' ) ;
5
+ const socket = io ( " http://localhost:8000" ) ;
6
6
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 ;
@@ -18,7 +18,6 @@ function App() {
18
18
let startX = 0 ;
19
19
let startY = 0 ;
20
20
ctx = canvas . getContext ( "2d" ) ;
21
-
22
21
23
22
canvas . width = canvas . getBoundingClientRect ( ) . width ;
24
23
canvas . height = canvas . getBoundingClientRect ( ) . height ;
@@ -37,16 +36,25 @@ function App() {
37
36
const endX = e . clientX - canvas . getBoundingClientRect ( ) . left ;
38
37
const endY = e . clientY - canvas . getBoundingClientRect ( ) . top ;
39
38
drawLine ( startX , startY , endX , endY ) ;
40
- socket . emit ( ' draw' , { startX, startY, endX, endY, color, lineWidth} )
39
+ socket . emit ( " draw" , { startX, startY, endX, endY, color, lineWidth } ) ;
41
40
startX = endX ;
42
41
startY = endY ;
43
42
}
44
43
45
- socket . on ( 'draw' , ( data ) => {
46
- drawLine ( data . startX , data . startY , data . endX , data . endY , data . color , data . lineWidth ) ;
47
- } )
48
-
49
- socket . on ( 'clear' , ( ) => { clearRect ( ) } )
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
+ } ) ;
50
58
51
59
function handleMousedown ( e ) {
52
60
isDrawing = true ;
@@ -69,24 +77,23 @@ function App() {
69
77
canvas . removeEventListener ( "mousemove" , handleMousemove ) ;
70
78
canvas . removeEventListener ( "mousedown" , handleMousedown ) ;
71
79
canvas . removeEventListener ( "mouseup" , handleMouseup ) ;
72
- socket . off ( 'draw' ) ;
73
- socket . off ( 'clear' ) ;
74
-
80
+ socket . off ( "draw" ) ;
81
+ socket . off ( "clear" ) ;
75
82
} ;
76
83
} , [ ] ) ;
77
84
78
- function clearRect ( ) {
79
- ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
85
+ function clearRect ( ) {
86
+ ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
80
87
}
81
88
82
- function addStroke ( e ) {
83
- if ( e . target . id === "stroke" ) {
89
+ function addStroke ( e ) {
90
+ if ( e . target . id === "stroke" ) {
84
91
color = e . target . value ;
85
92
ctx . strokeStyle = color ;
86
93
}
87
94
}
88
95
89
- function addLineWidth ( e ) {
96
+ function addLineWidth ( e ) {
90
97
if ( e . target . id === "lineWidth" ) {
91
98
lineWidth = e . target . value ;
92
99
ctx . lineWidth = lineWidth ;
@@ -99,7 +106,13 @@ function App() {
99
106
< h1 id = "drawRTC" > drawRTC</ h1 >
100
107
< div className = "input-container" id = "colorpicker" >
101
108
< label htmlFor = "stroke" > Stroke</ label >
102
- < input id = "stroke" name = "stroke" type = "color" defaultValue = { color } onInput = { addStroke } />
109
+ < input
110
+ id = "stroke"
111
+ name = "stroke"
112
+ type = "color"
113
+ defaultValue = { color }
114
+ onInput = { addStroke }
115
+ />
103
116
</ div >
104
117
< div className = "input-container" id = "linewidth" >
105
118
< label htmlFor = "lineWidth" > Line Width</ label >
@@ -108,10 +121,12 @@ function App() {
108
121
name = "lineWidth"
109
122
type = "number"
110
123
defaultValue = "3"
111
- onInput = { addLineWidth }
124
+ onInput = { addLineWidth }
112
125
/>
113
126
</ div >
114
- < button id = "clear" onClick = { ( ) => socket . emit ( 'clear' ) } > Clear</ button >
127
+ < button id = "clear" onClick = { ( ) => socket . emit ( "clear" ) } >
128
+ Clear
129
+ </ button >
115
130
</ div >
116
131
< div className = "canvas-container" >
117
132
< canvas className = "canvas" ref = { canvasRef } > </ canvas >
0 commit comments