-
Notifications
You must be signed in to change notification settings - Fork 2
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
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
20866ae
Merge branch 'feature/fe/#4-drawroute' of https://github.com/boostcam…
leedongyull 0dd16f0
[FE][Feat] #204 : 마커 아이콘으로 변경중
leedongyull d020180
Merge branch 'frontend' of https://github.com/boostcampwm-2024/web28-…
leedongyull f7147a3
Merge branch 'feature/fe/#204-addMarker' of https://github.com/boostc…
leedongyull af80c15
Merge branch 'frontend' of https://github.com/boostcampwm-2024/web28-…
leedongyull bbeaae6
[FE][Feat] #204 : context, canvas 내부 스코프에서 가져오기
leedongyull f5120dc
[FE][Feat] #204 : 테스트용 파일 원래대로 되돌리기
leedongyull 48f690f
Merge branch 'frontend' of https://github.com/boostcampwm-2024/web28-…
leedongyull 4f20c35
Merge branch 'frontend' into feature/fe/#204-addMarker
leedongyull 8c7f056
[FE][Feat] #204 : Initial zoom index 상수화
leedongyull a23b7d2
Merge branch 'feature/fe/#204-addMarker' of https://github.com/boostc…
leedongyull File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 근데 이름에 point, mark 뭔지는 알겠으나 더 구체적인 이름으로하면 더 좋을듯. |
||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이건 공통상수로 정의해서 같이 사용해도 될듯. 다른 파일에도 보여서.