allow dragging of points while drawing polygon

This commit is contained in:
Josh Hawkins 2024-04-18 11:00:16 -05:00
parent 98085a028b
commit bd1fb901a7
2 changed files with 61 additions and 3 deletions

View File

@ -60,7 +60,7 @@ export function PolygonCanvas({
return [stage.getPointerPosition()!.x, stage.getPointerPosition()!.y]; return [stage.getPointerPosition()!.x, stage.getPointerPosition()!.y];
}; };
const isMouseOverPoint = (polygon: Polygon, mousePos: number[]) => { const isMouseOverFirstPoint = (polygon: Polygon, mousePos: number[]) => {
if (!polygon || !polygon.points) { if (!polygon || !polygon.points) {
return false; return false;
} }
@ -72,6 +72,25 @@ export function PolygonCanvas({
return distance < 15; return distance < 15;
}; };
const isMouseOverAnyPoint = (polygon: Polygon, mousePos: number[]) => {
if (!polygon || !polygon.points || polygon.points.length === 0) {
return false;
}
for (let i = 1; i < polygon.points.length; i++) {
const point = polygon.points[i];
const distance = Math.hypot(
mousePos[0] - point[0],
mousePos[1] - point[1],
);
if (distance < 15) {
return true;
}
}
return false;
};
const handleMouseDown = (e: KonvaEventObject<MouseEvent | TouchEvent>) => { const handleMouseDown = (e: KonvaEventObject<MouseEvent | TouchEvent>) => {
if (activePolygonIndex === undefined || !polygons) { if (activePolygonIndex === undefined || !polygons) {
return; return;
@ -84,7 +103,7 @@ export function PolygonCanvas({
if ( if (
activePolygon.points.length >= 3 && activePolygon.points.length >= 3 &&
isMouseOverPoint(activePolygon, mousePos) isMouseOverFirstPoint(activePolygon, mousePos)
) { ) {
// Close the polygon // Close the polygon
updatedPolygons[activePolygonIndex] = { updatedPolygons[activePolygonIndex] = {
@ -93,7 +112,10 @@ export function PolygonCanvas({
}; };
setPolygons(updatedPolygons); setPolygons(updatedPolygons);
} else { } else {
if (!activePolygon.isFinished) { if (
!activePolygon.isFinished &&
!isMouseOverAnyPoint(activePolygon, mousePos)
) {
// Add a new point to the active polygon // Add a new point to the active polygon
updatedPolygons[activePolygonIndex] = { updatedPolygons[activePolygonIndex] = {
...activePolygon, ...activePolygon,
@ -136,6 +158,24 @@ export function PolygonCanvas({
} }
}; };
const handleMouseOverAnyPoint = (
e: KonvaEventObject<MouseEvent | TouchEvent>,
) => {
if (activePolygonIndex === undefined || !polygons) {
return;
}
e.target.getStage()!.container().style.cursor = "move";
};
const handleMouseOutAnyPoint = (
e: KonvaEventObject<MouseEvent | TouchEvent>,
) => {
if (activePolygonIndex === undefined || !polygons) {
return;
}
e.target.getStage()!.container().style.cursor = "default";
};
const handlePointDragMove = ( const handlePointDragMove = (
e: KonvaEventObject<MouseEvent | TouchEvent>, e: KonvaEventObject<MouseEvent | TouchEvent>,
) => { ) => {
@ -215,6 +255,8 @@ export function PolygonCanvas({
handleGroupDragEnd={handleGroupDragEnd} handleGroupDragEnd={handleGroupDragEnd}
handleMouseOverStartPoint={handleMouseOverStartPoint} handleMouseOverStartPoint={handleMouseOverStartPoint}
handleMouseOutStartPoint={handleMouseOutStartPoint} handleMouseOutStartPoint={handleMouseOutStartPoint}
handleMouseOverAnyPoint={handleMouseOverAnyPoint}
handleMouseOutAnyPoint={handleMouseOutAnyPoint}
/> />
), ),
)} )}

View File

@ -20,6 +20,12 @@ type PolygonDrawerProps = {
handleMouseOutStartPoint: ( handleMouseOutStartPoint: (
e: KonvaEventObject<MouseEvent | TouchEvent>, e: KonvaEventObject<MouseEvent | TouchEvent>,
) => void; ) => void;
handleMouseOverAnyPoint: (
e: KonvaEventObject<MouseEvent | TouchEvent>,
) => void;
handleMouseOutAnyPoint: (
e: KonvaEventObject<MouseEvent | TouchEvent>,
) => void;
}; };
export default function PolygonDrawer({ export default function PolygonDrawer({
@ -33,6 +39,8 @@ export default function PolygonDrawer({
handleGroupDragEnd, handleGroupDragEnd,
handleMouseOverStartPoint, handleMouseOverStartPoint,
handleMouseOutStartPoint, handleMouseOutStartPoint,
handleMouseOverAnyPoint,
handleMouseOutAnyPoint,
}: PolygonDrawerProps) { }: PolygonDrawerProps) {
const vertexRadius = 6; const vertexRadius = 6;
const [stage, setStage] = useState<Konva.Stage>(); const [stage, setStage] = useState<Konva.Stage>();
@ -121,6 +129,13 @@ export default function PolygonDrawer({
onMouseOut: handleMouseOutStartPoint, onMouseOut: handleMouseOutStartPoint,
} }
: null; : null;
const otherPointsAttr =
index !== 0
? {
onMouseOver: handleMouseOverAnyPoint,
onMouseOut: handleMouseOutAnyPoint,
}
: null;
return ( return (
<Circle <Circle
@ -146,6 +161,7 @@ export default function PolygonDrawer({
} }
}} }}
{...startPointAttr} {...startPointAttr}
{...otherPointsAttr}
/> />
); );
})} })}