Skip to content

Commit defa375

Browse files
committed
fix drawing
1 parent 5d04f49 commit defa375

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

client/src/App.jsx

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useRef } from "react";
2-
import './App.css'
2+
import "./App.css";
33

44
function App() {
55
const canvasRef = useRef(null);
@@ -15,13 +15,18 @@ function App() {
1515
const canvasHeight = canvas.getBoundingClientRect().height;
1616
const canvasWidht = canvas.getBoundingClientRect().width;
1717

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;
1823
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();
2530
}
2631

2732
function handleMouseover(e) {
@@ -51,18 +56,15 @@ function App() {
5156
canvas.addEventListener("mouseup", handleMouseup);
5257

5358
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") {
5963
ctx.strokeStyle = e.target.value;
60-
}
61-
else if(e.target.id === 'lineWidth'){
64+
} else if (e.target.id === "lineWidth") {
6265
ctx.lineWidth = e.target.value;
6366
}
64-
})
65-
67+
});
6668

6769
return () => {
6870
canvas.removeEventListener("mousemove", handleMouseover);
@@ -72,24 +74,28 @@ function App() {
7274
}, []);
7375

7476
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>
8598
</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-
9399
);
94100
}
95101

0 commit comments

Comments
 (0)