@@ -4,21 +4,23 @@ import "./App.css";
4
4
function App ( ) {
5
5
const canvasRef = useRef ( null ) ;
6
6
const sidebarRef = useRef ( null ) ;
7
+ let color = '#000000'
8
+ let ctx ;
9
+ let canvas ;
7
10
8
11
useEffect ( ( ) => {
9
- const canvas = canvasRef . current ;
12
+ canvas = canvasRef . current ;
10
13
let isDrawing = false ;
11
14
let startX = 0 ;
12
15
let startY = 0 ;
13
- const ctx = canvas . getContext ( "2d" ) ;
16
+ ctx = canvas . getContext ( "2d" ) ;
17
+
14
18
15
19
canvas . width = canvas . getBoundingClientRect ( ) . width ;
16
20
canvas . height = canvas . getBoundingClientRect ( ) . height ;
17
21
function drawLine ( sx , sy , ex , ey ) {
18
22
ctx . moveTo ( sx , sy ) ;
19
23
ctx . lineTo ( ex , ey ) ;
20
- ctx . strokeStyle = ctx . strokeStyle ;
21
- ctx . lineWidth = ctx . lineWidth ;
22
24
ctx . lineCap = "round" ;
23
25
ctx . stroke ( ) ;
24
26
}
@@ -49,42 +51,51 @@ function App() {
49
51
canvas . addEventListener ( "mousedown" , handleMousedown ) ;
50
52
canvas . addEventListener ( "mouseup" , handleMouseup ) ;
51
53
52
- const sidebar = sidebarRef . current ;
53
- sidebar . addEventListener ( "click" , ( e ) => {
54
- if ( e . target . id === "clear" ) {
55
- ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
56
- } else if ( e . target . id === "stroke" ) {
57
- ctx . strokeStyle = e . target . value ;
58
- } else if ( e . target . id === "lineWidth" ) {
59
- ctx . lineWidth = e . target . value ;
60
- }
61
- } ) ;
62
-
63
54
return ( ) => {
64
55
canvas . removeEventListener ( "mousemove" , handleMouseover ) ;
65
56
canvas . removeEventListener ( "mousedown" , handleMousedown ) ;
66
57
canvas . removeEventListener ( "mouseup" , handleMouseup ) ;
67
58
} ;
68
59
} , [ ] ) ;
69
60
61
+ function clearRect ( e ) {
62
+ if ( e . target . id === "clear" ) {
63
+ ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
64
+ }
65
+ }
66
+
67
+ function addStroke ( e ) {
68
+ if ( e . target . id === "stroke" ) {
69
+ color = e . target . value ;
70
+ ctx . strokeStyle = color ;
71
+ }
72
+ }
73
+
74
+ function addLineWidth ( e ) {
75
+ if ( e . target . id === "lineWidth" ) {
76
+ ctx . lineWidth = e . target . value ;
77
+ }
78
+ }
79
+
70
80
return (
71
81
< div id = "container" style = { { display : "flex" } } >
72
82
< div id = "sidebar" ref = { sidebarRef } >
73
83
< h1 id = "drawRTC" > drawRTC</ h1 >
74
84
< div className = "input-container" id = "colorpicker" >
75
85
< label htmlFor = "stroke" > Stroke</ label >
76
- < input id = "stroke" name = "stroke" type = "color" />
86
+ < input id = "stroke" name = "stroke" type = "color" defaultValue = { color } onInput = { addStroke } />
77
87
</ div >
78
88
< div className = "input-container" id = "linewidth" >
79
89
< label htmlFor = "lineWidth" > Line Width</ label >
80
90
< input
81
91
id = "lineWidth"
82
92
name = "lineWidth"
83
93
type = "number"
84
- defaultValue = "5"
94
+ defaultValue = "3"
95
+ onInput = { addLineWidth }
85
96
/>
86
97
</ div >
87
- < button id = "clear" > Clear</ button >
98
+ < button id = "clear" onClick = { clearRect } > Clear</ button >
88
99
</ div >
89
100
< div className = "canvas-container" >
90
101
< canvas className = "canvas" ref = { canvasRef } > </ canvas >
0 commit comments