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 (
{strokeWidthSliderValue}
+ +{strokeWidthSliderValue}