Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE][Feat] #204 : 캔버스 내 마커 추가 및 삭제 기능 구현 #207

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added frontend/src/assets/marker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions frontend/src/component/markerdrawer/MarkerDrawer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, { useRef, useState } from 'react';
import { usePanning } from '@/hooks/usePanning';
import { useZoom } from '@/hooks/useZoom';
import { useMarker } from '@/hooks/useMarker';

interface IPoint {
x: number;
y: number;
}

const NAVER_STEP_SCALES = [
100, 50, 30, 20, 10, 5, 3, 1, 0.5, 0.3, 0.1, 0.05, 0.03, 0.02, 0.01, 0.005,
];
// 선의 굵기 상수
const LINE_WIDTH = 2;
// 선의 색 상수
const STROKE_STYLE = 'black';
// 지도의 처음 확대/축소 비율 단계 index
const INITIAL_ZOOM_INDEX = 7;

export const MarkerDrawer = () => {
const canvasRef = useRef<HTMLCanvasElement>(null);
const [point, setPoint] = useState<IPoint>({ x: 0, y: 0 });

const { mark, scaleRef, viewPosRef } = useMarker({
canvasRef,
point,
lineWidth: LINE_WIDTH,
strokeStyle: STROKE_STYLE,
});
const { handleMouseMove, handleMouseDown, handleMouseUp } = usePanning({
viewPosRef,
draw: mark,
});
const { handleWheel } = useZoom({
scaleRef,
viewPosRef,
draw: mark,
stepScales: NAVER_STEP_SCALES,
initialZoomIndex: INITIAL_ZOOM_INDEX,
});

const handleCanvasClick = (e: React.MouseEvent<HTMLCanvasElement>) => {
const rect = canvasRef.current?.getBoundingClientRect();
if (!rect) return;

const x = (e.clientX - rect.left - viewPosRef.current.x) / scaleRef.current;
const y = (e.clientY - rect.top - viewPosRef.current.y) / scaleRef.current;
setPoint({ x, y });
};

return (
<canvas
ref={canvasRef}
width="800"
height="600"
style={{ border: '1px solid', cursor: 'crosshair' }}
onClick={handleCanvasClick}
onMouseMove={handleMouseMove}
onMouseDown={handleMouseDown}
onMouseUp={handleMouseUp}
onWheel={handleWheel}
/>
);
};
112 changes: 112 additions & 0 deletions frontend/src/hooks/useMarker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { useRef, useEffect, useState } from 'react';
import markerImage from '@/assets/marker.png'; // 이미지 import

interface IPoint {
x: number;
y: number;
}

interface IUseDrawingProps {
canvasRef: React.RefObject<HTMLCanvasElement>;
point: IPoint;
lineWidth: number;
strokeStyle: string;
}

const INITIAL_POSITION = { x: 0, y: 0 };
const STEP_SCALES = [100, 50, 30, 20, 10, 5, 3, 1, 0.5, 0.3, 0.1, 0.05, 0.03, 0.02, 0.01, 0.005];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이건 공통상수로 정의해서 같이 사용해도 될듯. 다른 파일에도 보여서.


export const useMarker = (props: IUseDrawingProps) => {
const scaleRef = useRef(STEP_SCALES[7]);
const viewPosRef = useRef(INITIAL_POSITION);
const [iconImage, setIconImage] = useState<HTMLImageElement | null>(null);
const [markerPoint, setMarkerPoint] = useState<IPoint | null>(props.point);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

point가 props에도 있고 여기에도 있고 차이점이 좀 헷갈릴듯.

const iconSize = 20;

const getCanvasContext = (): CanvasRenderingContext2D | null =>
props.canvasRef.current?.getContext('2d') || null;

const setTransform = (context: CanvasRenderingContext2D) => {
context.setTransform(
scaleRef.current,
0,
0,
scaleRef.current,
viewPosRef.current.x,
viewPosRef.current.y,
);
};

const loadIconImage = () => {
const img = new Image();
img.src = markerImage;
img.onload = () => setIconImage(img);
};

const mark = () => {
if (!iconImage || markerPoint === null) return;
const context = getCanvasContext();
if (!context) return;

const canvas = props.canvasRef.current;
if (!canvas) return;

context.clearRect(0, 0, canvas.width, canvas.height);
setTransform(context);

context.drawImage(
iconImage,
props.point.x - iconSize / 2,
props.point.y - iconSize,
iconSize,
iconSize,
);
};
const deleteMarker = (point: IPoint) => {
if (point.x === 0 && point.y === 0) return;
if (markerPoint) {
const iconLeft = markerPoint.x - iconSize / 2;
const iconRight = markerPoint.x + iconSize / 2;
const iconTop = markerPoint.y - iconSize;
const iconBottom = markerPoint.y;

const isInsideIcon =
point.x >= iconLeft && point.x <= iconRight && point.y >= iconTop && point.y <= iconBottom;

if (isInsideIcon) {
setMarkerPoint(null);
return;
}
}
setMarkerPoint(point);
};

useEffect(() => {
setMarkerPoint(props.point);
loadIconImage();
}, []);

const clearCanvas = () => {
const context = getCanvasContext();
const canvas = props.canvasRef.current;
if (canvas && context) {
context.clearRect(0, 0, canvas.width, canvas.height);
}
};

useEffect(() => {
deleteMarker(props.point);
}, [props.point]);

useEffect(() => {
if (!iconImage) return;

if (markerPoint === null) {
clearCanvas();
} else {
mark();
}
}, [markerPoint, iconImage]);

return { mark, scaleRef, viewPosRef };
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

근데 이름에 point, mark 뭔지는 알겠으나 더 구체적인 이름으로하면 더 좋을듯.

};
Loading