Skip to content

Commit 5d04f49

Browse files
committed
implement sidebar
1 parent c4eee11 commit 5d04f49

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

client/src/App.css

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@
7676
box-shadow: 0 0 4px #000;
7777
}
7878

79-
/* #colorpicker{
80-
margin-top: '40px';
81-
} */
79+
8280

8381

client/src/App.jsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import './App.css'
33

44
function App() {
55
const canvasRef = useRef(null);
6+
const sidebarRef = useRef(null);
67

78
useEffect(() => {
89
const canvas = canvasRef.current;
@@ -11,13 +12,16 @@ function App() {
1112
let startY = 0;
1213
const ctx = canvas.getContext("2d");
1314

15+
const canvasHeight = canvas.getBoundingClientRect().height;
16+
const canvasWidht = canvas.getBoundingClientRect().width;
17+
1418
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();
2125
}
2226

2327
function handleMouseover(e) {
@@ -36,12 +40,30 @@ function App() {
3640
}
3741
function handleMouseup(e) {
3842
isDrawing = false;
43+
startX = 0;
44+
startY = 0;
45+
ctx.stroke();
46+
ctx.beginPath();
3947
}
4048

4149
canvas.addEventListener("mousemove", handleMouseover);
4250
canvas.addEventListener("mousedown", handleMousedown);
4351
canvas.addEventListener("mouseup", handleMouseup);
4452

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+
4567
return () => {
4668
canvas.removeEventListener("mousemove", handleMouseover);
4769
canvas.removeEventListener("mousedown", handleMousedown);
@@ -51,15 +73,15 @@ function App() {
5173

5274
return (
5375
<div id="container" style={{ display: 'flex' }}>
54-
<div id="sidebar">
76+
<div id="sidebar" ref={sidebarRef}>
5577
<h1 id="drawRTC">drawRTC</h1>
5678
<div className="input-container" id='colorpicker'>
5779
<label htmlFor="stroke">Stroke</label>
5880
<input id="stroke" name="stroke" type="color" />
5981
</div>
6082
<div className="input-container" id='linewidth'>
6183
<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" />
6385
</div>
6486
<button id="clear">Clear</button>
6587
</div>

0 commit comments

Comments
 (0)