Gamote / lottie-react

A lightweight React library for rendering complex After Effects animations in real time using Lottie.
https://lottiereact.com
Other
795 stars 58 forks source link

How to let lottie file have full width height of screen when resize #87

Open lazarus2019 opened 1 year ago

joshua-isaac commented 1 year ago

@lazarus2019 I created a helper function to help with this, changing the preserveAspectRatio attribute helped with the lottie size on resize:

import { LottieContainer } from "./constants";

export const svgHelper = () => {
  const lottie = document.getElementsByClassName(
    LottieContainer
  )[0] as HTMLDivElement;

  const svg = lottie.getElementsByTagName("svg")[0];

  if (svg) {
    svg.setAttribute("preserveAspectRatio", "xMidYMid slice");
  }
};

Then I just call it inside a useEffect:

  useEffect(() => {
    svgHelper();
  }, []);

Works for me

joshua-isaac commented 1 year ago

@lazarus2019 looking at it now, looks like you can use the renderSettings prop on the <Lottie /> component:

 <Lottie
    lottieRef={lottieRef}
    animationData={scrollLottieReveal}
    autoplay={false}
    rendererSettings={{
       preserveAspectRatio: "xMidYMid slice",
    }}
 />