fix: resize canvas on window resize
This commit is contained in:
parent
3bc0032d4b
commit
2c960ebd84
1 changed files with 15 additions and 9 deletions
14
src/App.tsx
14
src/App.tsx
|
|
@ -54,7 +54,7 @@ function App() {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
onMouseDown(e);
|
onMouseDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
const stageRect = stageContainerRef.current?.getBoundingClientRect();
|
const stageRect = stageContainerRef.current?.getBoundingClientRect();
|
||||||
|
|
@ -65,15 +65,15 @@ function App() {
|
||||||
|
|
||||||
setMousePosition({
|
setMousePosition({
|
||||||
x: mouseX,
|
x: mouseX,
|
||||||
y: mouseY
|
y: mouseY,
|
||||||
});
|
});
|
||||||
setLocalMousePosition({
|
setLocalMousePosition({
|
||||||
x: (mouseX - coords.x) / scale,
|
x: (mouseX - coords.x) / scale,
|
||||||
y: (mouseY - coords.y) / scale
|
y: (mouseY - coords.y) / scale,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const onMouseDown = (e: React.MouseEvent) => {
|
const onMouseDown = () => {
|
||||||
setDragging(true);
|
setDragging(true);
|
||||||
|
|
||||||
const blockX = Math.floor(localMousePosition.x / 16);
|
const blockX = Math.floor(localMousePosition.x / 16);
|
||||||
|
|
@ -120,12 +120,18 @@ function App() {
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const resizeCanvas = () => {
|
||||||
if (stageContainerRef.current) {
|
if (stageContainerRef.current) {
|
||||||
setStageSize({
|
setStageSize({
|
||||||
width: stageContainerRef.current.offsetWidth,
|
width: stageContainerRef.current.offsetWidth,
|
||||||
height: stageContainerRef.current.offsetHeight,
|
height: stageContainerRef.current.offsetHeight,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
resizeCanvas();
|
||||||
|
window.addEventListener("resize", resizeCanvas);
|
||||||
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue