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

Hotfix: 에코 그림자에 마우스 이동 이벤트 적용 #261

Merged
merged 1 commit into from
Apr 1, 2024
Merged
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
10 changes: 5 additions & 5 deletions src/components/landing/Echo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import styles from './Echo.module.scss';
const cx = classNames.bind(styles);

const Echo = () => {
const { containerRef, elementRef, secondElementRef, reverseElementRef } = useMouseMoveEffect(8);
const { containerRef, mainTextRef, subTextRef, echoRef, echoShadowRef } = useMouseMoveEffect(8);

return (
<section ref={containerRef} className={cx('container')}>
<div className={cx('echo-shadow')}></div>
<div ref={reverseElementRef} className={cx('echo')}></div>
<div ref={echoShadowRef} className={cx('echo-shadow')}></div>
<div ref={echoRef} className={cx('echo')}></div>
<div className={cx('side-scroll', 'left')}>
<ul className={cx('scroll-content', 'left')}>
{GAME_NAME_LIST_EN.map((gameName, index) => (
Expand All @@ -38,10 +38,10 @@ const Echo = () => {
</ul>
</div>
<div>
<h2 ref={elementRef} className={cx('landing-text', 'main')}>
<h2 ref={mainTextRef} className={cx('landing-text', 'main')}>
BEST TEAMWORK
</h2>
<h2 ref={secondElementRef} className={cx('landing-text', 'sub')}>
<h2 ref={subTextRef} className={cx('landing-text', 'sub')}>
IT&apos;s UP TO YOU
</h2>
</div>
Expand Down
39 changes: 22 additions & 17 deletions src/hooks/useMouseMoveEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@ import { DeviceType } from '@/types';

const useMouseMoveEffect = (moveScale: number) => {
const containerRef = useRef<HTMLDivElement>(null);
const elementRef = useRef<HTMLDivElement>(null);
const secondElementRef = useRef<HTMLDivElement>(null);
const reverseElementRef = useRef<HTMLDivElement>(null);
const mainTextRef = useRef<HTMLDivElement>(null);
const subTextRef = useRef<HTMLDivElement>(null);
const echoRef = useRef<HTMLDivElement>(null);
const echoShadowRef = useRef<HTMLDivElement>(null);
const currentDeviceType = useDeviceType();

useEffect(() => {
const container = containerRef.current;
const element = elementRef.current;
const secondElement = secondElementRef.current;
const reverseElement = reverseElementRef.current;
const mainText = mainTextRef.current;
const subText = subTextRef.current;
const echo = echoRef.current;
const echoShadow = echoShadowRef.current;

if (!container || !element || !reverseElement || !secondElement) {
if (!container || !mainText || !subText || !echo || !echoShadow) {
return;
}

element.style.willChange = 'transform';
secondElement.style.willChange = 'transform';
reverseElement.style.willChange = 'transform';
mainText.style.willChange = 'transform';
subText.style.willChange = 'transform';
echo.style.willChange = 'transform';
echoShadow.style.willChange = 'transform';

const handleMouseMove = (event: MouseEvent) => {
requestAnimationFrame(() => {
Expand All @@ -37,9 +40,10 @@ const useMouseMoveEffect = (moveScale: number) => {
const offsetX = (mouseX - centerX) / centerX;
const offsetY = (mouseY - centerY) / centerY;

element.style.transform = `translate(${offsetX * moveScale}px, ${offsetY * moveScale}px)`;
secondElement.style.transform = `translate(${offsetX * moveScale}px, ${offsetY * moveScale}px)`;
reverseElement.style.transform = `translate(${-offsetX * moveScale}px, ${-offsetY * moveScale}px)`;
mainText.style.transform = `translate(${offsetX * moveScale}px, ${offsetY * moveScale}px)`;
subText.style.transform = `translate(${offsetX * moveScale}px, ${offsetY * moveScale}px)`;
echo.style.transform = `translate(${-offsetX * moveScale}px, ${-offsetY * moveScale}px)`;
echoShadow.style.transform = `translate(${offsetX * 1}px, ${offsetY * 1}px)`;
});
};

Expand All @@ -52,13 +56,14 @@ const useMouseMoveEffect = (moveScale: number) => {
container.removeEventListener('mousemove', handleMouseMove);
}

element.style.willChange = 'transform';
secondElement.style.willChange = 'transform';
reverseElement.style.willChange = 'transform';
mainText.style.willChange = 'transform';
subText.style.willChange = 'transform';
echo.style.willChange = 'transform';
echoShadow.style.willChange = 'transform';
};
}, [moveScale, currentDeviceType]);

return { containerRef, elementRef, secondElementRef, reverseElementRef };
return { containerRef, mainTextRef, subTextRef, echoRef, echoShadowRef };
};

export default useMouseMoveEffect;