드디어 react-spring을 직접 사용해봤다..⭐️
고칠 부분들이 많이 있었는데 그 중에서도 react-spring
을 먼저 사용해봤다.
예전부터 꼭 적용해보고 싶었는데 역시 대만족이다..😎
useSpring의 3D CARD
예시를 따라 작성했으니까 다시 코드를 내 것으로 만들어보자!
const calc = (x,y) => [-(y-window.innerHeight / 2) / 20, (x-window.innerWidth / 2) / 20, 1.1];
window.innerHeight
: 브라우저 화면의 높이window.innerWidth
: 브라우저 화면의 너비window.outerHeight
: 브라우저 전체의 높이window.outerWidth
: 브라우저 전체의 너비-(y-window.innerHeight / 2) / 20
: (y⬆︎ = 값⬇︎), (y⬇︎ = 값⬆︎)
(x-window.innerWidth / 2) / 20
: (x⬆︎ = 값⬆︎), (x⬇︎ = 값⬇︎)
calc
는 [rotateX, rotateY, scale]의 배열을 가지게 된다.
const trans = (x,y,s) => `perspective(600px) rotateX(${x}deg) rotateY(${y}deg) scale(${s})`;
const trans
는 문자열 리터럴을 반환 받는다.const [props, set] = useSpring(() => ({xys : [0,0,1], config: {mass:5, tension: 350, friction: 40}}));
set
은 움직이는 효과를 주기 위해 값를 업데이트 해준다.xys : [0, 0, 1]
config
에 들어갈 수 있는 요소들 <animated.div
className="card"
onMouseMove={({clientX :x, clientY:y}) => set({xys:calc(x,y)})}
onMouseLeave={()=>set({xys:[0,0,1] })}
style={{transform : props.xys.interpolate(trans)}}
>
<img src={image} alt={name} />
</animated.div>
<animated.div />
안에서 효과가 적용된다.onMouseMove={({clientX :x, clientY:y})=>set({xys:calc(x,y)})}
onMouseLeave={()=>set({xys:[0,0,1] })}
useSpring에 대해 더 이해하고 싶다면 링크 클릭해서 코드와 설명을 보자!