@@ -13,8 +13,7 @@ import { useRecoilValue, useRecoilState } from "recoil";
13
13
import { eraserState , cursorPosition } from "./atoms" ;
14
14
15
15
function App ( ) {
16
-
17
- const [ showMenu , setShowMenu ] = useState ( false ) ;
16
+ const [ showMenu , setShowMenu ] = useState ( false ) ;
18
17
const eraserMode = useRecoilValue ( eraserState ) ;
19
18
const [ position , setPosition ] = useRecoilState ( cursorPosition ) ;
20
19
const [ ctx , setCtx ] = useState ( null ) ;
@@ -23,14 +22,14 @@ function App() {
23
22
const [ isDrawing , setIsDrawing ] = useState ( false ) ;
24
23
const [ penColor , setPenColor ] = useState ( "#000000" ) ;
25
24
26
- function toggleMenu ( ) {
25
+ function toggleMenu ( ) {
27
26
setShowMenu ( ! showMenu ) ;
28
27
}
29
28
30
29
const canvasRef = useRef ( null ) ;
31
30
const sidebarRef = useRef ( null ) ;
32
31
let lineWidth ;
33
-
32
+
34
33
function drawLine ( sx , sy , ex , ey , penColor , lineWidth ) {
35
34
ctx . moveTo ( sx , sy ) ;
36
35
ctx . lineTo ( ex , ey ) ;
@@ -39,10 +38,10 @@ function App() {
39
38
ctx . strokeStyle = penColor ;
40
39
ctx . stroke ( ) ;
41
40
}
42
-
41
+
43
42
useEffect ( ( ) => {
44
43
const canvas = canvasRef . current ;
45
- if ( canvas ) {
44
+ if ( canvas ) {
46
45
const context = canvas . getContext ( "2d" ) ;
47
46
canvas . width = canvas . getBoundingClientRect ( ) . width ;
48
47
canvas . height = canvas . getBoundingClientRect ( ) . height ;
@@ -51,7 +50,7 @@ function App() {
51
50
} , [ canvasRef . current ] ) ;
52
51
53
52
useEffect ( ( ) => {
54
- if ( ! ctx ) return ;
53
+ if ( ! ctx ) return ;
55
54
const canvas = canvasRef . current ;
56
55
function handleMousemove ( e ) {
57
56
// if eraseMode is set the position of the eraser cursor
@@ -66,7 +65,6 @@ function App() {
66
65
socket . emit ( "draw" , { startX, startY, endX, endY, penColor, lineWidth } ) ;
67
66
setStartX ( endX ) ;
68
67
setStartY ( endY ) ;
69
-
70
68
}
71
69
72
70
function handleMousedown ( e ) {
@@ -105,43 +103,42 @@ function App() {
105
103
data . lineWidth
106
104
) ;
107
105
} ) ;
108
-
106
+
109
107
socket . on ( "clear" , ( ) => {
110
108
clearRect ( ) ;
111
109
} ) ;
112
-
113
- return ( ) => {
114
- socket . off ( "draw" ) ;
115
- socket . off ( "clear" ) ;
116
- }
110
+
111
+ return ( ) => {
112
+ socket . off ( "draw" ) ;
113
+ socket . off ( "clear" ) ;
114
+ } ;
117
115
} , [ socket , ctx ] ) ;
118
116
119
117
function clearRect ( ) {
120
- if ( ctx ) {
118
+ if ( ctx ) {
121
119
ctx . clearRect ( 0 , 0 , canvasRef . current . width , canvasRef . current . height ) ;
122
120
}
123
121
}
124
122
125
- function clearOnClick ( ) {
123
+ function clearOnClick ( ) {
126
124
clearRect ( ) ;
127
- socket . emit ( ' clear' )
125
+ socket . emit ( " clear" ) ;
128
126
}
129
127
130
128
function addStroke ( e ) {
131
129
if ( e . target . id === "penColor" ) {
132
130
const newColor = e . target . value ;
133
131
setPenColor ( newColor ) ;
134
- if ( ctx ) {
132
+ if ( ctx ) {
135
133
ctx . strokeStyle = newColor ;
136
134
}
137
-
138
135
}
139
136
}
140
137
141
138
function addLineWidth ( e ) {
142
139
if ( e . target . id === "lineWidth" ) {
143
140
lineWidth = e . target . value ;
144
- if ( ctx ) {
141
+ if ( ctx ) {
145
142
ctx . lineWidth = lineWidth ;
146
143
}
147
144
}
@@ -164,5 +161,4 @@ function App() {
164
161
) ;
165
162
}
166
163
167
- export default App ;
168
-
164
+ export default App ;
0 commit comments