feat: add cursor onto tiles
This commit is contained in:
parent
98b901ca75
commit
e1b3f3a091
2 changed files with 39 additions and 7 deletions
24
src/App.tsx
24
src/App.tsx
|
|
@ -15,6 +15,7 @@ import {
|
||||||
|
|
||||||
import ThemeChanger from "./components/menubar/theme-changer";
|
import ThemeChanger from "./components/menubar/theme-changer";
|
||||||
import Blocks from "./components/blocks";
|
import Blocks from "./components/blocks";
|
||||||
|
import Cursor from "./components/cursor";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const stageContainerRef = useRef<HTMLDivElement>(null);
|
const stageContainerRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
@ -27,23 +28,30 @@ function App() {
|
||||||
|
|
||||||
const [stageScale, setStageScale] = useState(1);
|
const [stageScale, setStageScale] = useState(1);
|
||||||
const [stageCoords, setStageCoords] = useState<Position>({ x: 0, y: 0 });
|
const [stageCoords, setStageCoords] = useState<Position>({ x: 0, y: 0 });
|
||||||
|
const [mousePosition, setMousePosition] = useState<Position>({ x: 0, y: 0 });
|
||||||
|
|
||||||
|
const onMouseMove = (e) => {
|
||||||
|
const stage = e.target.getStage();
|
||||||
|
const oldScale = stage.scaleX();
|
||||||
|
const pointer = stage.getPointerPosition();
|
||||||
|
|
||||||
|
setMousePosition({
|
||||||
|
x: (pointer.x - stage.x()) / oldScale,
|
||||||
|
y: (pointer.y - stage.y()) / oldScale,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const onWheel = (e) => {
|
const onWheel = (e) => {
|
||||||
const stage = e.target.getStage();
|
const stage = e.target.getStage();
|
||||||
const oldScale = stage.scaleX();
|
const oldScale = stage.scaleX();
|
||||||
const pointer = stage.getPointerPosition();
|
const pointer = stage.getPointerPosition();
|
||||||
|
|
||||||
const mousePoint = {
|
|
||||||
x: (pointer.x - stage.x()) / oldScale,
|
|
||||||
y: (pointer.y - stage.y()) / oldScale,
|
|
||||||
};
|
|
||||||
|
|
||||||
const newScale = e.evt.deltaY < 0 ? oldScale * 1.05 : oldScale / 1.05;
|
const newScale = e.evt.deltaY < 0 ? oldScale * 1.05 : oldScale / 1.05;
|
||||||
|
|
||||||
setStageScale(newScale);
|
setStageScale(newScale);
|
||||||
setStageCoords({
|
setStageCoords({
|
||||||
x: pointer.x - mousePoint.x * newScale,
|
x: pointer.x - mousePosition.x * newScale,
|
||||||
y: pointer.y - mousePoint.y * newScale,
|
y: pointer.y - mousePosition.y * newScale,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -96,10 +104,12 @@ function App() {
|
||||||
y={stageCoords.y}
|
y={stageCoords.y}
|
||||||
scaleX={stageScale}
|
scaleX={stageScale}
|
||||||
scaleY={stageScale}
|
scaleY={stageScale}
|
||||||
|
onMouseMove={onMouseMove}
|
||||||
onWheel={onWheel}
|
onWheel={onWheel}
|
||||||
>
|
>
|
||||||
<Layer imageSmoothingEnabled={false}>
|
<Layer imageSmoothingEnabled={false}>
|
||||||
<Blocks />
|
<Blocks />
|
||||||
|
<Cursor mousePosition={mousePosition} />
|
||||||
</Layer>
|
</Layer>
|
||||||
</Stage>
|
</Stage>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
22
src/components/cursor.tsx
Normal file
22
src/components/cursor.tsx
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { Rect } from "react-konva";
|
||||||
|
|
||||||
|
function Cursor({ mousePosition }: { mousePosition: Position }) {
|
||||||
|
const [position, setPosition] = useState({ x: 0, y: 0 });
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (mousePosition) {
|
||||||
|
const snappedX = Math.floor(mousePosition.x / 16) * 16;
|
||||||
|
const snappedY = Math.floor(mousePosition.y / 16) * 16;
|
||||||
|
|
||||||
|
setPosition({
|
||||||
|
x: snappedX,
|
||||||
|
y: snappedY,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [mousePosition]);
|
||||||
|
|
||||||
|
return <Rect x={position.x} y={position.y} width={16} height={16} stroke={"white"} strokeWidth={0.5} dash={[2.5]} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Cursor;
|
||||||
Loading…
Reference in a new issue