From 092056b3ad22e66e83b5a7528a4097e6a324dbf8 Mon Sep 17 00:00:00 2001 From: brausjonas Date: Fri, 7 Jul 2023 00:20:55 +0200 Subject: [PATCH] Update App.js --- src/App.js | 114 +++++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 55 deletions(-) diff --git a/src/App.js b/src/App.js index ff697ac..dd3a065 100644 --- a/src/App.js +++ b/src/App.js @@ -82,70 +82,74 @@ export default function App() { } function handleMouseDownCanvas(e) { - mouseDownCanvas = true - hintContext.clearRect(0, 0, 5000, 3000) - setShowTextInput(false) - context.fillStyle = selectedTool !== "eraser" ? colorValue : eraserColor - if (selectedTool === "pencil" || selectedTool === "eraser") { - context.beginPath() - context.arc(e.pageX, e.pageY, strokeWidthSliderValue, 0, 2 * Math.PI, false) - context.fill() + if(e.button === 0) { + mouseDownCanvas = true + hintContext.clearRect(0, 0, 5000, 3000) + setShowTextInput(false) + context.fillStyle = selectedTool !== "eraser" ? colorValue : eraserColor + if (selectedTool === "pencil" || selectedTool === "eraser") { + 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)) - } else { + lastMouse = Date.now() + lastPositions.push(new Position(e.pageX, e.pageY)) + } else { - formAnchor = new Position(e.pageX, e.pageY) - } - if(selectedTool === "text") { - mouseDownCanvas = false - setTextInputX(e.pageX + 10) - setTextInputY(e.pageY + 10) - setShowTextInput(true) - hintContext.beginPath() - hintContext.fillStyle = "black" - hintContext.arc(e.pageX, e.pageY, 5, 0, 2*Math.PI, false) - hintContext.fill() + formAnchor = new Position(e.pageX, e.pageY) + } + if(selectedTool === "text") { + mouseDownCanvas = false + setTextInputX(e.pageX + 10) + setTextInputY(e.pageY + 10) + setShowTextInput(true) + hintContext.beginPath() + hintContext.fillStyle = "black" + hintContext.arc(e.pageX, e.pageY, 5, 0, 2*Math.PI, false) + hintContext.fill() + } } } function handleMouseUpCanvas(e) { - mouseDownCanvas = false + if(e.button === 0) { + mouseDownCanvas = false - if (selectedTool === "rect") { - context.strokeStyle = colorValue - context.beginPath() - context.rect(formAnchor.x, formAnchor.y, e.pageX - formAnchor.x, e.pageY - formAnchor.y) - context.lineWidth = strokeWidthSliderValue - context.stroke() + if (selectedTool === "rect") { + context.strokeStyle = colorValue + context.beginPath() + context.rect(formAnchor.x, formAnchor.y, e.pageX - formAnchor.x, e.pageY - formAnchor.y) + context.lineWidth = strokeWidthSliderValue + context.stroke() - } else if (selectedTool === "circle") { - context.strokeStyle = colorValue - context.beginPath() - let radius = (e.pageX - formAnchor.x) / 2 - let yRad = (e.pageY - formAnchor.y) / 2 - context.arc(formAnchor.x + radius, formAnchor.y + yRad, Math.abs(radius), 0, 2 * Math.PI, false) - context.lineWidth = strokeWidthSliderValue - context.stroke() - } else if(selectedTool === "selector") { - context.fillStyle = eraserColor - context.fillRect(formAnchor.x, formAnchor.y, e.pageX - formAnchor.x, e.pageY - formAnchor.y) - } - else if(selectedTool === "line") { - context.strokeStyle = colorValue - context.beginPath() - context.lineWidth = strokeWidthSliderValue - context.moveTo(formAnchor.x, formAnchor.y) - context.lineTo(e.pageX, e.pageY) - context.stroke() - context.moveTo(0, 0) - } + } else if (selectedTool === "circle") { + context.strokeStyle = colorValue + context.beginPath() + let radius = (e.pageX - formAnchor.x) / 2 + let yRad = (e.pageY - formAnchor.y) / 2 + context.arc(formAnchor.x + radius, formAnchor.y + yRad, Math.abs(radius), 0, 2 * Math.PI, false) + context.lineWidth = strokeWidthSliderValue + context.stroke() + } else if(selectedTool === "selector") { + context.fillStyle = eraserColor + context.fillRect(formAnchor.x, formAnchor.y, e.pageX - formAnchor.x, e.pageY - formAnchor.y) + } + else if(selectedTool === "line") { + context.strokeStyle = colorValue + context.beginPath() + context.lineWidth = strokeWidthSliderValue + context.moveTo(formAnchor.x, formAnchor.y) + context.lineTo(e.pageX, e.pageY) + context.stroke() + context.moveTo(0, 0) + } - if(selectedTool !== "text") { - hintContext.clearRect(0, 0, 5000, 3000) - } - else { - textInput.focus() + if(selectedTool !== "text") { + hintContext.clearRect(0, 0, 5000, 3000) + } + else { + textInput.focus() + } } }