-
Notifications
You must be signed in to change notification settings - Fork 166
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
[성능 베이스캠프 미션] 병민(윤병인) 미션 제출합니다. #39
Changes from 7 commits
08bb90f
de7d9ee
a5d9140
b288088
8854693
f537499
1866a85
20d92db
280b570
6b30115
ab562c3
14114d8
e4a6d32
804519c
c9387e5
72ad55c
18926ae
bc88dd1
0dd0d9b
80edbf2
ca79ac8
5986c2c
89f1129
1167829
38b364f
cadfde4
c2770a8
d91ca57
9ca8935
569dd4d
60cffe5
c4810fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import useMousePosition from '../../hooks/useMousePosition'; | ||
|
||
import styles from './CustomCursor.module.css'; | ||
|
||
|
@@ -9,15 +8,26 @@ type CustomCursorProps = { | |
|
||
const CustomCursor = ({ text = '' }: CustomCursorProps) => { | ||
const [...cursorTextChars] = text; | ||
const mousePosition = useMousePosition(); | ||
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. r : useMousePotion 파일 제거해도 되겠네요~ 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. 수정했습니다 :D |
||
const cursorRef = useRef<HTMLDivElement>(null); | ||
|
||
const updateCursorPosition = (e: MouseEvent) => { | ||
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. a : animation path 부분과 비슷한 맥락인 것 같네요~ 스크롤 이벤트 관련해서 많은 신경을 쓰셨네요. 덕분에 개선 포인트를 알게 되었네요. |
||
if (!cursorRef.current) return; | ||
const { current: $cursor } = cursorRef; | ||
const { pageX, pageY } = e; | ||
const cursorBefore = window.getComputedStyle($cursor, ':before'); | ||
const { width: widthPx, height: heightPx } = cursorBefore; | ||
const width = parseInt(widthPx, 10); | ||
const height = parseInt(heightPx, 10); | ||
const xCoordinate = pageX - Math.floor(width / 2) + 'px'; | ||
const yCoordinate = pageY - Math.floor(height / 2) + 'px'; | ||
cursorRef.current.style.transform = `translate3d(${xCoordinate},${yCoordinate}, 0px)`; | ||
}; | ||
|
||
useEffect(() => { | ||
if (cursorRef.current) { | ||
cursorRef.current.style.top = `${mousePosition.pageY}px`; | ||
cursorRef.current.style.left = `${mousePosition.pageX}px`; | ||
} | ||
}, [mousePosition]); | ||
console.log('useEffect is called'); | ||
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. 커서마저 최적화 해버린 그에게 남아있는 한 톨의 인간미 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. |
||
window.addEventListener('mousemove', updateCursorPosition); | ||
return () => window.removeEventListener('mousemove', updateCursorPosition); | ||
}, [cursorRef.current, updateCursorPosition]); | ||
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. c : updateCursorPosition을 의존성 배열에 넣은 이유가 있을까요? 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. 중첩함수이고 re-render할때마다 새로운 함수가 생겨서 의존성 배열에 넣지 않아도 작동은 하지만!! 종종 staled 함수가 들어갈 수도 있어서 당장 필요하지는 않아도, 넣어주는게 안전하다고 생각했습니다. 예를들어서,, 커스텀훅 안에서 |
||
|
||
return ( | ||
<div ref={cursorRef} className={styles.cursor}> | ||
|
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.
r :
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.
수정했습니다 :D
cadfde4