lottie-react icon indicating copy to clipboard operation
lottie-react copied to clipboard

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

Open lazarus2019 opened this issue 2 years ago • 2 comments

lazarus2019 avatar Mar 29 '23 07:03 lazarus2019

@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 avatar Jul 11 '23 17:07 joshua-isaac

@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",
    }}
 />

joshua-isaac avatar Jul 11 '23 17:07 joshua-isaac