Jump to content
Search Community

TypeError: Cannot read properties of undefined (reading 'pin')

Fusion Journey test
Moderator Tag

Recommended Posts

I have utilized the premium version and incorporated a frame-by-frame PNG animation in the next js project. However, I consistently encounter issues; the page functions correctly only after refreshing it. I have added my code below.

'use client';
import React, { useEffect, useRef } from "react";
import gsap from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
gsap.registerPlugin(ScrollTrigger);

const LineAnimation1 = () => {

  const canvasRef = useRef(null);

  useEffect(() => {
    const initCanvas = () => {
      const canvas = canvasRef.current;
      const context = canvas.getContext("2d");

      canvas.width = window.innerHeight * 0.1;
      canvas.height = window.innerHeight * 0.2;

      window.addEventListener("resize", function () {
        canvas.width = window.innerHeight * 0.1;
        canvas.height = window.innerHeight * 0.2;
        render();
      });

      const frameCount = 14;
      const currentFrame = (index) => {
        const imagePath = `https://candokidsbastg.wpengine.com/test/lineanimation/1/Line_${(index + 1)
          .toString()
          .padStart(2, "0")}.png`;
        console.log(imagePath);

        return imagePath;
      };

      const images = [];
      const sequence = {
        frame: 0,
      };

      let loadedImages = 0;

      for (let i = 0; i < frameCount; i++) {
        const img = new Image();
        img.src = currentFrame(i);
        img.onload = () => {
          loadedImages++;
          if (loadedImages === frameCount) {
            // All images have loaded, trigger render
            render();
          }
        };
        images.push(img);
      }

      gsap.to(sequence, {
        frame: frameCount + 1,
        snap: "frame",
        ease: "power1.inOut",
        duration: 4,
        scrollTrigger: {
          scrub: 3,
          start: "top 60%",
          end: "top 100%",
          trigger: ".case-banner",
          markers: true
        },
        onUpdate: render,
      });

      function render() {
        if (loadedImages === frameCount) {
          const frameIndex = Math.max(0, Math.min(sequence.frame, frameCount - 1));
          scaleImage(images[frameIndex], context);
        }
      }

      function scaleImage(img, ctx) {
        var canvas = ctx.canvas;
        var hRatio = canvas.width / img.width;
        var vRatio = canvas.height / img.height;
        var ratio = Math.max(hRatio, vRatio);
        var centerShift_x = (canvas.width - img.width * ratio) / 2;
        var centerShift_y = (canvas.height - img.height * ratio) / 2;
        context.clearRect(0, 0, canvas.width, canvas.height);
        context.drawImage(
          img,
          0,
          0,
          img.width,
          img.height,
          centerShift_x,
          centerShift_y,
          img.width * ratio,
          img.height * ratio
        );
      }
    };

    initCanvas();
  }, []);

  return <canvas id="page01" className="" ref={canvasRef}></canvas>;
};

export default LineAnimation1;

 

Screenshot 2024-02-05 111047.png

Link to comment
Share on other sites

Hi there! I see you're using React -

Proper cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

 

Since GSAP 3.12, we have the useGSAP() hook (the NPM package is here) that simplifies creating and cleaning up animations in React (including Next, Remix, etc). It's a drop-in replacement for useEffect()/useLayoutEffect(). All the GSAP-related objects (animations, ScrollTriggers, etc.) created while the function executes get collected and then reverted when the hook gets torn down.

 

Here is how it works:

const container = useRef(); // the root level element of your component (for scoping selector text which is optional)

useGSAP(() => {
  // gsap code here...
}, { dependencies: [endX], scope: container }); // config object offers maximum flexibility

Or if you prefer, you can use the same method signature as useEffect():

useGSAP(() => {
  // gsap code here...
}, [endX]); // simple dependency Array setup like useEffect()

This pattern follows React's best practices.

 

We strongly recommend reading the React guide we've put together at https://gsap.com/resources/React/

 

If you still need help, here's a React starter template that you can fork to create a minimal demo illustrating whatever issue you're running into. Post a link to your fork back here and we'd be happy to take a peek and answer any GSAP-related questions you have. Just use simple colored <div> elements in your demo; no need to recreate your whole project with client artwork, etc. The simpler the better. 

Link to comment
Share on other sites

On 2/5/2024 at 1:45 PM, GSAP Helper said:

Hi there! I see you're using React -

Proper cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

 

Since GSAP 3.12, we have the useGSAP() hook (the NPM package is here) that simplifies creating and cleaning up animations in React (including Next, Remix, etc). It's a drop-in replacement for useEffect()/useLayoutEffect(). All the GSAP-related objects (animations, ScrollTriggers, etc.) created while the function executes get collected and then reverted when the hook gets torn down.

 

Here is how it works:

const container = useRef(); // the root level element of your component (for scoping selector text which is optional)

useGSAP(() => {
  // gsap code here...
}, { dependencies: [endX], scope: container }); // config object offers maximum flexibility

Or if you prefer, you can use the same method signature as useEffect():

useGSAP(() => {
  // gsap code here...
}, [endX]); // simple dependency Array setup like useEffect()

This pattern follows React's best practices.

 

We strongly recommend reading the React guide we've put together at https://gsap.com/resources/React/

 

If you still need help, here's a React starter template that you can fork to create a minimal demo illustrating whatever issue you're running into. Post a link to your fork back here and we'd be happy to take a peek and answer any GSAP-related questions you have. Just use simple colored <div> elements in your demo; no need to recreate your whole project with client artwork, etc. The simpler the better. 

It gives me an error 
 

  const marqueeTimeline = gsap.timeline();
  useGSAP(() => {
  
    // Animation for initial state
    marqueeTimeline.from('.journey-section .marquee-gsap', {
      xPercent: 12,
      duration: 2,
    });
  
    // Animation with ScrollTrigger
    marqueeTimeline.to('.journey-section .marquee-gsap', {
      xPercent: -55,
      scrollTrigger: {
        trigger: '.journey-section .marquee-container',
        start: 'top 80%',
        end: 'top 1%',
        scrub: 6,
        duration: 2,
        // markers: true
      },
    });
  }, {scope: marqueeTimeline });

 

Screenshot 2024-02-07 110421.png

Link to comment
Share on other sites

It's very difficult to troubleshoot without a minimal demo - would you mind providing a Stackblitz that clearly illustrates the problem? Here's a starter template that you can fork: 

https://stackblitz.com/edit/stackblitz-starters-u6rgzq

 

That error kinda sounds like you might have a problem with your imports. Did you try importing from the /dist/ directory? 

import gsap from "gsap/dist/gsap";
import ScrollTrigger from "gsap/dist/ScrollTrigger";

Oh, and you definitely shouldn't be creating your timeline outside of the useGSAP() hook. Do it inside of that, otherwise you won't get the proper cleanup. 

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...