Performances issue with Lenis , GSAP & R3F on mobile #431
-
Hi , currently working on a webgl project using r3f ( any problem with that) , GSAP for animations, and Lenis for smoothing. import Lenis from "@studio-freight/lenis";
import gsap from "gsap";
import ScrollTrigger from "gsap/dist/ScrollTrigger";
import { useEffect, useLayoutEffect } from "react";
gsap.registerPlugin(ScrollTrigger);
function SmoothScrolling({ children }) {
useEffect(() => {
const wrapper = document.querySelector(".scrollWrapper");
const content = document.querySelector(".scrollContent");
const lenis = new Lenis({
wrapper: wrapper,
content: content,
duration: 3,
smoothTouch: false,
touchMultiplier: 1.1,
direction: "vertical",
gestureDirection: "vertical",
syncTouch: true,
easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
eventsTarget: document.documentElement,
});
// Synchronize Lenis scrolling with GSAP's ScrollTrigger plugin
lenis.on("scroll", () => {
ScrollTrigger.update();
});
// Add Lenis's requestAnimationFrame (raf) method to GSAP's ticker
// This ensures Lenis's smooth scroll animation updates on each GSAP tick
gsap.ticker.add((time) => {
lenis.raf(time * 1000); // Convert time from seconds to milliseconds
});
// Disable lag smoothing in GSAP to prevent any delay in scroll animations
gsap.ticker.lagSmoothing(0);
ScrollTrigger.defaults({ scroller: wrapper });
ScrollTrigger.config({
limitCallbacks: true,
ignoreMobileResize: true,
autoRefreshEvents: "DOMContentLoaded,load,resize",
});
//ScrollTrigger.normalizeScroll({ target: wrapper });
ScrollTrigger.scrollerProxy(wrapper, {
scrollTop(value) {
return arguments.length
? lenis.scrollTo(value, { duration: 0, immediate: true })
: lenis.scroll;
},
getBoundingClientRect() {
return {
top: 0,
left: 0,
width: window.innerWidth,
height: window.innerHeight,
};
},
});
}, []);
return;
}
export default SmoothScrolling; I basically use a proxy to avoid the adressbar to show/hide, everything is perfect on desktop, but a real strugge to maintain "smoothness" on mobile, I added syncTouch: true, it fixed my fps drop occuring while scrolling, but I loose the "smooth" effect of lenis, and sometime it seems I loose the scroll focus on the page ( jumps between sections etc) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Please use |
Beta Was this translation helpful? Give feedback.
-
please move from the deprecated package "@studio-freight/lenis" to the "lenis" package and use our "lenis/react" integration. as a guess, the performance issue probably lies within the 3d scene rather then lenis. |
Beta Was this translation helpful? Give feedback.
@aronanol45 https://github.com/darkroomengineering/lenis/blob/main/packages/react/README.md#gsap-integration
lenisRef.current?.lenis?.raf(time * 1000)
lenisRef refers to the ReactLenis component that includes: wrapper element, content element and lenis instance