Skip to content

Commit 2d2fece

Browse files
committed
Fix color selection
1 parent 71b6810 commit 2d2fece

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

client/src/App.jsx

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ import "./App.css";
44
function App() {
55
const canvasRef = useRef(null);
66
const sidebarRef = useRef(null);
7+
let color = '#000000'
8+
let ctx;
9+
let canvas;
710

811
useEffect(() => {
9-
const canvas = canvasRef.current;
12+
canvas = canvasRef.current;
1013
let isDrawing = false;
1114
let startX = 0;
1215
let startY = 0;
13-
const ctx = canvas.getContext("2d");
16+
ctx = canvas.getContext("2d");
17+
1418

1519
canvas.width = canvas.getBoundingClientRect().width;
1620
canvas.height = canvas.getBoundingClientRect().height;
1721
function drawLine(sx, sy, ex, ey) {
1822
ctx.moveTo(sx, sy);
1923
ctx.lineTo(ex, ey);
20-
ctx.strokeStyle = ctx.strokeStyle;
21-
ctx.lineWidth = ctx.lineWidth;
2224
ctx.lineCap = "round";
2325
ctx.stroke();
2426
}
@@ -49,42 +51,51 @@ function App() {
4951
canvas.addEventListener("mousedown", handleMousedown);
5052
canvas.addEventListener("mouseup", handleMouseup);
5153

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-
6354
return () => {
6455
canvas.removeEventListener("mousemove", handleMouseover);
6556
canvas.removeEventListener("mousedown", handleMousedown);
6657
canvas.removeEventListener("mouseup", handleMouseup);
6758
};
6859
}, []);
6960

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+
7080
return (
7181
<div id="container" style={{ display: "flex" }}>
7282
<div id="sidebar" ref={sidebarRef}>
7383
<h1 id="drawRTC">drawRTC</h1>
7484
<div className="input-container" id="colorpicker">
7585
<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}/>
7787
</div>
7888
<div className="input-container" id="linewidth">
7989
<label htmlFor="lineWidth">Line Width</label>
8090
<input
8191
id="lineWidth"
8292
name="lineWidth"
8393
type="number"
84-
defaultValue="5"
94+
defaultValue="3"
95+
onInput={addLineWidth}
8596
/>
8697
</div>
87-
<button id="clear">Clear</button>
98+
<button id="clear" onClick={clearRect}>Clear</button>
8899
</div>
89100
<div className="canvas-container">
90101
<canvas className="canvas" ref={canvasRef}></canvas>

0 commit comments

Comments
 (0)