From d55a5f2c54040dec10c269ddda0ef6900b558f42 Mon Sep 17 00:00:00 2001 From: brausjonas Date: Thu, 6 Jul 2023 21:13:23 +0200 Subject: [PATCH] Added Eraser --- src/App.js | 94 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 23 deletions(-) diff --git a/src/App.js b/src/App.js index ba11653..8891a25 100644 --- a/src/App.js +++ b/src/App.js @@ -10,14 +10,15 @@ let downLink let lastPositions = [] let strokeWidth = 5 let lastMouse = 0 +let eraserColor = "rgb(255, 255, 255)" +let lastStrokeWidthPencil = 5, lastStrokeWidthEraser = 5 export default function App() { useLayoutEffect(() => { canvas = document.getElementById("canvas") context = canvas.getContext("2d") - context.fillStyle = "rgb(255, 255, 255)" - context.fillRect(0, 0, window.innerWidth, window.innerHeight) + clearScreen("rgb(255, 255, 255)") downLink = document.createElement("a") downLink.className = "no-display" @@ -49,6 +50,7 @@ export default function App() { const [strokeWidthSliderValue, setStrokeWidthSliderValue] = useState(strokeWidth) const [colorValue, setColorValue] = useState("#000000") const [menuBarShown, setMenuBarShown] = useState(true) + const [selectedTool, setSelectedTool] = useState("pencil") class Position { constructor(x, y) { @@ -57,6 +59,24 @@ export default function App() { } } + function clearScreen(color) { + context.fillStyle = color + context.fillRect(0, 0, window.innerWidth, window.innerHeight) + eraserColor = color + } + + function handleMouseDownCanvas(e) + { + mouseDownCanvas = true + context.fillStyle = selectedTool === "pencil" ? colorValue : eraserColor + context.beginPath() + context.arc(e.pageX, e.pageY, strokeWidthSliderValue, 0, 2 * Math.PI, false) + context.fill() + + lastMouse = Date.now() + lastPositions.push(new Position(e.pageX, e.pageY)) + } + function handleMouseMove(event) { let mouseX = event.pageX let mouseY = event.pageY @@ -66,7 +86,7 @@ export default function App() { setMenuBarLeft(mouseX - mouseMenuBarOffsetX) setMenuBarTop(mouseY - mouseMenuBarOffsetY) } else if (mouseDownCanvas) { - context.fillStyle = colorValue + context.fillStyle = selectedTool === "pencil" ? colorValue : eraserColor lastPositions.push(new Position(mouseX, mouseY)) @@ -83,6 +103,19 @@ export default function App() { downLink.click() } + function handleToolChange(e) + { + setSelectedTool(e.target.value) + if(e.target.value === "pencil") { + setStrokeWidthSliderValue(lastStrokeWidthPencil) + strokeWidth = lastStrokeWidthPencil + } + else if(e.target.value === "eraser") { + setStrokeWidthSliderValue(lastStrokeWidthEraser) + strokeWidth = lastStrokeWidthEraser + } + } + return (
handleMouseMove(e)} style={{overflow: "unset"}}> @@ -90,14 +123,7 @@ export default function App() { width={window.innerWidth} height={window.innerHeight} onMouseDown={e => { - mouseDownCanvas = true - context.fillStyle = colorValue - context.beginPath() - context.arc(e.pageX, e.pageY, strokeWidthSliderValue, 0, 2 * Math.PI, false) - context.fill() - - lastMouse = Date.now() - lastPositions.push(new Position(e.pageX, e.pageY)) + handleMouseDownCanvas(e) }} onMouseUp={e => { mouseDownCanvas = false @@ -157,30 +183,53 @@ export default function App() {
+
- - - -

{strokeWidthSliderValue}

+ +
+ + + + { + handleToolChange(e) + }}/> +
+
+ + + + { + handleToolChange(e) + }}/> +
+ { setStrokeWidthSliderValue(e.currentTarget.value) strokeWidth = e.currentTarget.value + if(selectedTool === "pencil") { + lastStrokeWidthPencil = strokeWidth + } + else if(selectedTool === "eraser") { + lastStrokeWidthEraser = strokeWidth; + } }} style={{pointerEvents: "auto"}}/> +

{strokeWidthSliderValue}

{/*Color Selector*/} @@ -206,8 +255,7 @@ export default function App() { borderRadius: 15, pointerEvents: "auto" }} onClick={() => { - context.fillStyle = colorValue - context.fillRect(0, 0, window.innerWidth, window.innerHeight) + clearScreen(colorValue) }}> Clear