Hello everyone! I am trying to animate a 3D-Object and an html object. The animation for the 3D-Object works as intended. With the html element I ran into some trouble however. The html element gets first rendered at its final animation state, only after scrolling down tiny bit, which advances the animation in the timeline, does the element snap into its correct state. After pausing the scrolling, it snaps back to the wrong/final state of its animation.
I am guessing that this is caused by not continuously scrolling, and therefore not continuously updating the animation state of the element (which is handled by the useFrame function). So how do I fix this?
Code:
IKEA_ALEX_drawer.jsx (file of the 3D Model, where the gsap animations are handled as well)
import React, { useRef, useLayoutEffect } from "react";
import { useGLTF, useScroll } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import gsap from "gsap";
const Drawer_Model = ({ props }) => {
const group = useRef();
const { nodes, materials, animations } = useGLTF(
"../../../public/drawer/IKEA_ALEX_drawer.glb"
);
//const { actions } = useAnimations(animations, group);
const tl = useRef(gsap.timeline());
const scroll = useScroll();
const drawer_one = useRef();
useFrame(() => {
if (tl.current) {
tl.current.seek(scroll.offset * tl.current.duration());
}
}, []);
useLayoutEffect(() => {
setTimeout(() => {
tl.current
.to(drawer_one.current.position, {
duration: 0.3,
y: -0.5,
})
.to(
"#direct_work_link",
{
duration: 0.3,
opacity: 0,
},
1.1
);
}, 10);
}, []);
//// framer-motion effect
//useEffect(() => {
// if (isDrawerVis) {
// actions.extend.play();
// }
//}, [isDrawerVis, actions]);
return (
<group ref={group} {...props} dispose={null}>
<group name="Scene">
<group
name="Sketchfab_model"
rotation={[-Math.PI / 2, 0, 0]}
scale={0.066}
>
<group
name="a2486ef730cd444f8a04c9f6a10981a3fbx"
rotation={[Math.PI / 2, 0, 0]}
>
<group name="RootNode">
<group
name="ALEX_Frame"
rotation={[-Math.PI / 2, 0, 0]}
scale={100}
>
<mesh
name="ALEX_Frame_ALEX_0"
geometry={nodes.ALEX_Frame_ALEX_0.geometry}
material={materials.ALEX}
/>
</group>
<group
name="Drawers_Large"
rotation={[-Math.PI / 2, 0, 0]}
scale={100}
>
<mesh
name="Drawer_Large_1"
geometry={nodes.Drawer_Large_1.geometry}
material={materials.ALEX}
/>
<mesh
name="Drawer_Large_2"
geometry={nodes.Drawer_Large_2.geometry}
material={materials.ALEX}
/>
<mesh
name="Drawer_Large_3"
geometry={nodes.Drawer_Large_3.geometry}
material={materials.ALEX}
/>
</group>
<group
name="Drawers_Small"
rotation={[-Math.PI / 2, 0, 0]}
scale={100}
>
<group ref={drawer_one}>
<mesh
name="Drawer_Small_1"
geometry={nodes.Drawer_Small_1.geometry}
material={materials.ALEX}
/>
</group>
<mesh
name="Drawer_Small_2"
geometry={nodes.Drawer_Small_2.geometry}
material={materials.ALEX}
/>
</group>
<group name="Legs" rotation={[-Math.PI / 2, 0, 0]} scale={100}>
<mesh
name="Legs_Legs_0"
geometry={nodes.Legs_Legs_0.geometry}
material={materials.Legs}
/>
</group>
<group
name="Plane"
position={[0, 33.2, 28.1]}
rotation={[-Math.PI / 2, 0, 0]}
scale={100}
>
<mesh
name="Plane__0"
geometry={nodes.Plane__0.geometry}
material={materials.Plane__0}
/>
</group>
</group>
</group>
</group>
</group>
</group>
);
};
export default Drawer_Model;
useGLTF.preload("../../../public/drawer/IKEA_ALEX_drawer.glb");
Drawer.jsx (file of the Canvas component)
import { Suspense, useEffect, useState, useRef } from "react";
import { Canvas } from "@react-three/fiber";
import { OrbitControls, Preload, ScrollControls } from "@react-three/drei";
import CanvasLoader from "../Loader";
import { AnimatePresence } from "framer-motion";
import { useInView } from "framer-motion";
import Drawer_Model from "./IKEA_ALEX_drawer";
const DrawerCanvas = () => {
const ref = useRef(null);
const isInView = useInView(ref);
const [isDrawerVis, setisDrawerVis] = useState(false);
useEffect(() => {
if (isInView) {
setisDrawerVis(true);
}
}, [isInView]);
return (
<div ref={ref} style={{ height: "100vh", overflow: "show" }}>
<Canvas
frameloop="demand"
shadows
camera={{ position: [0, 0, 15], fov: 35 }}
gl={{ preserveDrawingBuffer: true }}
className="mt-10"
>
<ScrollControls damping={0.25}>
<hemisphereLight
intensity={0.3}
position={[0, 10, 10]}
groundColor="white"
/>
<pointLight
intensity={1}
position={[0, 10, 20]}
rotation={[0, 0, 0]}
/>
<Suspense fallback={<CanvasLoader />}>
<Drawer_Model isDrawerVis={isDrawerVis} />
</Suspense>
<Preload all />
</ScrollControls>
</Canvas>
</div>
);
};
export default DrawerCanvas;
The html-element #direct_work_link is in an additional Hero.jsx file (DoubleSlideReveal is just a custom framer-motion animation):
<div className="text-secondary mt-10 z-10">
<DoubleSlideReveal>
<a href="#work" id="direct_work_link">
Direkt zu den Projekten ⇀
</a>
</DoubleSlideReveal>
</div>
Best regards, Philip