alissa1228 / gsap-example

gsap 연습
0 stars 0 forks source link

gsap 연습 #1

Open alissa1228 opened 2 years ago

alissa1228 commented 2 years ago
alissa1228 commented 2 years ago

*Targeting elements (엘리먼트 지정) : gsap 애니메이션을 사용하려면 DOM에 있는 엘리먼트에 접근해야함. Refs는 React 구성 요소의 DOM 노드와 상호 작용하고 레퍼런스들을 저장한다.(In order to animate using GSAP we need access to the element in the DOM. Refs provide a way for us to interact with and store references to DOM nodes in a React component.)

alissa1228 commented 2 years ago

*Creating our first animation (첫번째 우리의 애니메이션 만들어보기~!) :gsap는 인라인 스타일 프로퍼티를 업데이트 한다. 그래서 애니메이션을 만들기 전에 DOM이 렌더링되었는지 확인하는 것이 중요하다. 만약 렌더링 되지 않은 엘리멘트를 애니메이션으로 만들어달라고 gsap한테 말하면, 콘솔에 gsap 타겟이 없다고 뜬다(=GSAP target not found.). null 값이 있는 엘리먼스를 피하기 위해서 우리는 useEffect 훅을 사용한다. 이 훅은 리엑트에게 우리 컴포넌트가 렌더링 후에 뭔가를 해야한다고 말해준다.

GSAP updates inline style properties, so it’s important to make sure the DOM has been rendered before trying to animate anything. If we ask GSAP to animate an element that hasn’t been rendered, we’ll get this warning in the console. 'GSAP target not found. 'In order to avoid targeting a null element, we can use the useEffect hook. This hook tells React that our component needs to do something after rendering.

alissa1228 commented 2 years ago

*Targeting descendant elements(자식 요소들 지정)

ex)

const el = useRef();
const q = gsap.utils.selector(el);

useEffect(() => {
   // Target ALL descendants with the class of .box
  gsap.to(q(".box"), { x: 100 });
}, []);

Creating a ref for each and every element we want to animate can add a lot of noise to our code. We can avoid this by making use of GSAP’s selector utility to easily select descendant elements.

alissa1228 commented 2 years ago

*Forwarding refs

+) forwardRef : React 컴포넌트에 ref prop을 넘겨서 그 내부에 있는 HTML 엘리먼트에 접근을 하게 해주는 함수

gsap.utils.selector() will target all descendants in the component tree. Within a component based system, you may need more granular control over the elements you're targeting. You can use ref forwarding to get access to specific nested elements.

alissa1228 commented 2 years ago

*Creating and controlling timelines

Up until now we've just used refs to store references to DOM elements, but they're not just for elements. Refs exists outside of the render loop - so they can be used to store any value that you would like to persist for the life of a component. If you're coming from class based components, this should be familiar to you as it’s essentially the same as using ‘this’.

In order to avoid creating a new timeline on every render, it's important to create the timeline inside an effect and store it in a ref.

alissa1228 commented 2 years ago

*Controlling when React runs our animation. (react가 우리 애니메이션을 실행할 때의 컨트롤)

By default useEffect runs both after the first render and after every update. So every time our component’s state changes, it will cause a re-render, which will run our effect again. We can control when useEffect should run by passing in an array of dependencies. To only run once after the first render, we pass in an empty array.

alissa1228 commented 2 years ago

*Reacting to changes in state

Now that we know how to control when an effect fires, we can use this pattern to react to changes in our component. This is especially useful when passing down props.

alissa1228 commented 2 years ago

*Animating on interaction (상호작용 애니메이션) hover와 같은 사용자 상호 작용에 연결하기 위해 콜백을 사용할 수 있다. (In order to hook into user interactions like hover, we can use callbacks.)

alissa1228 commented 2 years ago

*Avoiding flash of unstyled content (FOUC) (다 그려지기 전에 스타일이 적용되지 않은 콘텐츠가 나오면서 번쩍거리는 현상)

As useEffect fires after the DOM has been painted, when fading in elements you may notice an undesired flash of unstyled content. In order to avoid the flash, we can replace useEffect with useLayoutEffect. useLayoutEffect functions exactly the same as useEffect, but runs before the DOM has been painted.

useLayoutEffect is especially useful when you need to make DOM measurements, so we highly recommend it when using our ScrollTrigger and FLIP plugins.

More information about useEffect vs useLayoutEffect.

alissa1228 commented 2 years ago

*Cleaning Up

It’s a good idea to return a cleanup function in your effects to kill off any running animations and anything else that could cause a memory leak, like an event listener. This is particularly important if an animation runs for a really long time, makes use of ScrollTrigger, or changes the state in a component.