-
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
[FEAT] 라운드 통계 화면 및 게임 결과 화면 구현 #73
Changes from 1 commit
e61ea09
0128c04
752c497
9650f21
91f40b3
c65abe3
aa3b05a
a200136
be3f49a
7103261
2d26903
a04c200
d8a6d53
f46054d
090e2e4
27715e0
73c9f90
93dd3c3
76a259c
6e4ec20
0a65739
261aa4e
347cfb5
adbed42
dfc98e4
7a2deb5
8defa35
04ffc25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
// "ease out" : 빨라졌다가 천천히 끝나는 애니메이션 | ||
const easeOutRate = (timingRate: number) => { | ||
return timingRate === 1 ? 1 : 1 - Math.pow(2, -10 * timingRate); | ||
}; | ||
|
||
interface UseCountAnimationProps { | ||
target: number; | ||
start?: number; | ||
duration?: number; | ||
} | ||
|
||
const useCountAnimation = ({ target, start = 50, duration = 2000 }: UseCountAnimationProps) => { | ||
const [count, setCount] = useState(start); | ||
const frameRate = 500 / 60; | ||
const totalFrame = Math.round(duration / frameRate); | ||
|
||
useEffect(() => { | ||
if (target === start) return; // target 값을 API로 불러올 경우 초기값이 애니메이션에 반영되므로 예외처리 | ||
let currentNumber = start; | ||
const counter = setInterval(() => { | ||
const progress = easeOutRate(++currentNumber / totalFrame); | ||
setCount(Math.round(target * progress)); | ||
|
||
if (progress === 1) { | ||
clearInterval(counter); | ||
} | ||
}, frameRate); | ||
}, [target, frameRate, start, totalFrame]); | ||
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. L3 - 중요 질문의존성 배열에 상수로 정의된 frameRate 와 totalFrame도 들어간 이유가 무엇인가요? 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. 의존성 배열에는 반응형 값이 들어가야하는데, states, props, 컴포넌트 내에서 선언한 변수 중 useEffect에서 사용되는 데이터들이 추가되어야 합니다! frameRate와 totalFrame은 컴포넌트 내에 선언되었고, useEffect에서 사용되므로 의존성 배열에 추가되는 것이 맞습니다. 그런데 다시 보니 상수가 불필요하게 의존되는 것 같아 useEffect 내부로 옮겨 반응형 값이 안되도록 수정하겠습니다! |
||
|
||
return count; | ||
}; | ||
|
||
export default useCountAnimation; |
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.
L5 - 참고 의견
setInterval이 외에도 웹 애니메이션을 최적화하는 requestAnimationFrame 이라는 것도 있더라고요? 저도 이번에 리뷰하면서 알게되었답니다 ㅎ .ㅎ 다음에 최적화하면서 적용해 봐도 좋을 것 같아요 ~!
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.
오 저도 숫자 올라갈 때마다 렌더링되는게 문제가 될 수 있다고 생각했는데 requestAnimationFrame 을 적용해볼 수 있겠군요! 나중에 시간이나 성능 비교해보면서 한번에 적용해보면 좋을 것 같아요 ㅎㅎ
https://velog.io/@younghwanjoe/requestAnimationFrame%EC%9D%84-%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC-%EC%95%A0%EB%8B%88%EB%A9%94%EC%9D%B4%EC%85%98-%EA%B5%AC%ED%98%84%ED%95%98%EA%B8%B0-%EC%83%81
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.
말우 ! 좋은 자료 공유 호라락 읽어보았읍니다 감사룡. . 지금 당장은 이해가 완벽히 안됐지만 역시 해 봐야겠죠 ? 푸하하 최적화 때 야무지게 써 버립시다 ^ .^ ~ ~