Jump to content
Search Community

Animation starts at final state

PhilipS test
Moderator Tag

Recommended Posts

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

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

If you're using something like React/Next/Vue/Nuxt or some other framework, you may find StackBlitz easier to use. We have a series of collections with different templates for you to get started on these different frameworks: React/Next/Vue/Nuxt.

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...