@@ -3,6 +3,7 @@ import './App.css'
3
3
4
4
function App ( ) {
5
5
const canvasRef = useRef ( null ) ;
6
+ const sidebarRef = useRef ( null ) ;
6
7
7
8
useEffect ( ( ) => {
8
9
const canvas = canvasRef . current ;
@@ -11,13 +12,16 @@ function App() {
11
12
let startY = 0 ;
12
13
const ctx = canvas . getContext ( "2d" ) ;
13
14
15
+ const canvasHeight = canvas . getBoundingClientRect ( ) . height ;
16
+ const canvasWidht = canvas . getBoundingClientRect ( ) . width ;
17
+
14
18
function drawLine ( sx , sy , ex , ey ) {
15
- ctx . beginPath ( ) ;
16
- ctx . moveTo ( sx , sy ) ;
17
- ctx . lineTo ( ex , ey ) ;
18
- ctx . strokeStyle = "blue" ;
19
- ctx . lineWidth = 2 ;
20
- ctx . stroke ( ) ;
19
+ ctx . moveTo ( sx , sy ) ;
20
+ ctx . lineTo ( ex , ey ) ;
21
+ ctx . strokeStyle = ctx . strokeStyle ;
22
+ ctx . lineWidth = ctx . lineWidth ;
23
+ ctx . lineCap = 'round' ;
24
+ ctx . stroke ( ) ;
21
25
}
22
26
23
27
function handleMouseover ( e ) {
@@ -36,12 +40,30 @@ function App() {
36
40
}
37
41
function handleMouseup ( e ) {
38
42
isDrawing = false ;
43
+ startX = 0 ;
44
+ startY = 0 ;
45
+ ctx . stroke ( ) ;
46
+ ctx . beginPath ( ) ;
39
47
}
40
48
41
49
canvas . addEventListener ( "mousemove" , handleMouseover ) ;
42
50
canvas . addEventListener ( "mousedown" , handleMousedown ) ;
43
51
canvas . addEventListener ( "mouseup" , handleMouseup ) ;
44
52
53
+ const sidebar = sidebarRef . current ;
54
+ sidebar . addEventListener ( 'click' , ( e ) => {
55
+ if ( e . target . id === 'clear' ) {
56
+ ctx . clearRect ( 0 , 0 , canvasWidht , canvasHeight )
57
+ }
58
+ else if ( e . target . id === 'stroke' ) {
59
+ ctx . strokeStyle = e . target . value ;
60
+ }
61
+ else if ( e . target . id === 'lineWidth' ) {
62
+ ctx . lineWidth = e . target . value ;
63
+ }
64
+ } )
65
+
66
+
45
67
return ( ) => {
46
68
canvas . removeEventListener ( "mousemove" , handleMouseover ) ;
47
69
canvas . removeEventListener ( "mousedown" , handleMousedown ) ;
@@ -51,15 +73,15 @@ function App() {
51
73
52
74
return (
53
75
< div id = "container" style = { { display : 'flex' } } >
54
- < div id = "sidebar" >
76
+ < div id = "sidebar" ref = { sidebarRef } >
55
77
< h1 id = "drawRTC" > drawRTC</ h1 >
56
78
< div className = "input-container" id = 'colorpicker' >
57
79
< label htmlFor = "stroke" > Stroke</ label >
58
80
< input id = "stroke" name = "stroke" type = "color" />
59
81
</ div >
60
82
< div className = "input-container" id = 'linewidth' >
61
83
< label htmlFor = "lineWidth" > Line Width</ label >
62
- < input id = "lineWidth" name = "lineWidth" type = "number" value = "5" />
84
+ < input id = "lineWidth" name = "lineWidth" type = "number" defaultValue = "5" />
63
85
</ div >
64
86
< button id = "clear" > Clear</ button >
65
87
</ div >
0 commit comments