1
1
import { useEffect , useRef } from "react" ;
2
- import ' ./App.css'
2
+ import " ./App.css" ;
3
3
4
4
function App ( ) {
5
5
const canvasRef = useRef ( null ) ;
@@ -15,13 +15,18 @@ function App() {
15
15
const canvasHeight = canvas . getBoundingClientRect ( ) . height ;
16
16
const canvasWidht = canvas . getBoundingClientRect ( ) . width ;
17
17
18
+ const canvasOffsetX = canvas . getBoundingClientRect ( ) . left ;
19
+ const canvasOffsetY = canvas . getBoundingClientRect ( ) . top ;
20
+
21
+ canvas . width = canvas . getBoundingClientRect ( ) . width ;
22
+ canvas . height = canvas . getBoundingClientRect ( ) . height ;
18
23
function drawLine ( sx , sy , ex , ey ) {
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 ( ) ;
24
+ ctx . moveTo ( sx , sy ) ;
25
+ ctx . lineTo ( ex , ey ) ;
26
+ ctx . strokeStyle = ctx . strokeStyle ;
27
+ ctx . lineWidth = ctx . lineWidth ;
28
+ ctx . lineCap = " round" ;
29
+ ctx . stroke ( ) ;
25
30
}
26
31
27
32
function handleMouseover ( e ) {
@@ -51,18 +56,15 @@ function App() {
51
56
canvas . addEventListener ( "mouseup" , handleMouseup ) ;
52
57
53
58
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
+ sidebar . addEventListener ( "click" , ( e ) => {
60
+ if ( e . target . id === "clear" ) {
61
+ ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
62
+ } else if ( e . target . id === "stroke" ) {
59
63
ctx . strokeStyle = e . target . value ;
60
- }
61
- else if ( e . target . id === 'lineWidth' ) {
64
+ } else if ( e . target . id === "lineWidth" ) {
62
65
ctx . lineWidth = e . target . value ;
63
66
}
64
- } )
65
-
67
+ } ) ;
66
68
67
69
return ( ) => {
68
70
canvas . removeEventListener ( "mousemove" , handleMouseover ) ;
@@ -72,24 +74,28 @@ function App() {
72
74
} , [ ] ) ;
73
75
74
76
return (
75
- < div id = "container" style = { { display : 'flex' } } >
76
- < div id = "sidebar" ref = { sidebarRef } >
77
- < h1 id = "drawRTC" > drawRTC</ h1 >
78
- < div className = "input-container" id = 'colorpicker' >
79
- < label htmlFor = "stroke" > Stroke</ label >
80
- < input id = "stroke" name = "stroke" type = "color" />
81
- </ div >
82
- < div className = "input-container" id = 'linewidth' >
83
- < label htmlFor = "lineWidth" > Line Width</ label >
84
- < input id = "lineWidth" name = "lineWidth" type = "number" defaultValue = "5" />
77
+ < div id = "container" style = { { display : "flex" } } >
78
+ < div id = "sidebar" ref = { sidebarRef } >
79
+ < h1 id = "drawRTC" > drawRTC</ h1 >
80
+ < div className = "input-container" id = "colorpicker" >
81
+ < label htmlFor = "stroke" > Stroke</ label >
82
+ < input id = "stroke" name = "stroke" type = "color" />
83
+ </ div >
84
+ < div className = "input-container" id = "linewidth" >
85
+ < label htmlFor = "lineWidth" > Line Width</ label >
86
+ < input
87
+ id = "lineWidth"
88
+ name = "lineWidth"
89
+ type = "number"
90
+ defaultValue = "5"
91
+ />
92
+ </ div >
93
+ < button id = "clear" > Clear</ button >
94
+ </ div >
95
+ < div className = "canvas-container" >
96
+ < canvas className = "canvas" ref = { canvasRef } > </ canvas >
97
+ </ div >
85
98
</ div >
86
- < button id = "clear" > Clear</ button >
87
- </ div >
88
- < div className = "canvas-container" >
89
- < canvas className = "canvas" ref = { canvasRef } > </ canvas >
90
- </ div >
91
- </ div >
92
-
93
99
) ;
94
100
}
95
101
0 commit comments