fix: resize canvas on window resize

This commit is contained in:
trafficlunar 2024-12-07 16:03:27 +00:00
parent 3bc0032d4b
commit 2c960ebd84

View file

@ -54,7 +54,7 @@ function App() {
}));
}
onMouseDown(e);
onMouseDown();
}
const stageRect = stageContainerRef.current?.getBoundingClientRect();
@ -65,15 +65,15 @@ function App() {
setMousePosition({
x: mouseX,
y: mouseY
y: mouseY,
});
setLocalMousePosition({
x: (mouseX - coords.x) / scale,
y: (mouseY - coords.y) / scale
y: (mouseY - coords.y) / scale,
});
};
const onMouseDown = (e: React.MouseEvent) => {
const onMouseDown = () => {
setDragging(true);
const blockX = Math.floor(localMousePosition.x / 16);
@ -120,12 +120,18 @@ function App() {
};
useEffect(() => {
const resizeCanvas = () => {
if (stageContainerRef.current) {
setStageSize({
width: stageContainerRef.current.offsetWidth,
height: stageContainerRef.current.offsetHeight,
});
}
};
resizeCanvas();
window.addEventListener("resize", resizeCanvas);
}
}, []);
return (