Jump to content
Search Community

I can't seem to make the slow ease work in React.

Zyrus

Go to solution Solved by GreenSock,

Recommended Posts

Posted (edited)

I am following the CreativeCodingClub SVG lesson: Line Jumper Advanced: Clip Path, Holes and Nesting Animations I kind of copy the code from the tutorial then translate it to React.
the SVG was made in Figma and then used svgview.dev to translate this into a React component.

I can't seem to feel the
firstJump timelineis doing any slow at all. I even tweaked the timeline defaults duration and parameters of slow() just to see its effect but no luck.
I am really confused why it doesn't seem to work, I already import a EasePack.

animation goes like this: a line jumps out of the hole at the middle it will slowdown then animation goes normal again then  jumps to the second hole.

there is probably a different approach to this without using slow() but the lesson is about it so I am avoiding putting like customEase etc.
any tips you can suggest to make the slow works? thank you.

import { useGSAP } from "@gsap/react";
import gsap from "gsap";
import { GSDevTools } from "gsap/GSDevTools";
import React, { useRef } from "react";
import { DrawSVGPlugin } from "gsap/DrawSVGPlugin";
import { EasePack } from "gsap/EasePack";

gsap.registerPlugin(useGSAP, DrawSVGPlugin, EasePack, GSDevTools);

export default function JumpingWormSVG() {
  const container = useRef(null);

  useGSAP(
    () => {
      //holes
      gsap.set("#holes", { opacity: 1 });
      gsap.set("#holes ellipse", { scale: 0, transformOrigin: "50% 50%" });
      function openHole(hole: string): gsap.core.Timeline {
        const tl = gsap
          .timeline({ defaults: { duration: 1 } })
          .to(hole, { scale: 1, ease: "back" })
          .to(hole, { scale: 0, duration: 0.5, ease: "power2.in" }, "+=0.1");
        return tl;
      }

      //worm-1 (short)
      const pointsShort = { start: 0, end: 0 };
      const drawShort = gsap.quickSetter("#worm-1", "drawSVG");
      function updateShort(): void {
        drawShort(`${pointsShort.start}% ${pointsShort.end}%`);
      }

      //setup the initial value of start:0 and end:0 to #worm-1
      updateShort();

      const firstJump = gsap
        .timeline({
          onUpdate: updateShort,
          repeat: -1,
          defaults: { duration: 0.8, ease: "slow(0.6, 0.5)" },
        })
        .add(openHole("#hole-1"))
        .to(
          pointsShort,
          {
            end: 100,

            onUpdate: () => console.log(`end: ${pointsShort.end}`),
          },
          0.5,
        )
        .to(pointsShort, { start: 100 }, 0.3);
      // .add(openHole("#hole-2"));

      GSDevTools.create({ animation: firstJump });
    },
    { scope: container },
  );

  return (
    <div ref={container}>
      <svg
        width={1200}
        height={614}
        viewBox="0 0 1200 614"
        fill="none"
        xmlns="http://www.w3.org/2000/svg"
      >
        <g id="jump-hole">
          <rect width={1200} height={614} fill="white" />
          <g id="holes" opacity={0}>
            <ellipse
              id="hole-1"
              cx={100}
              cy={510}
              rx={77}
              ry={28}
              fill="#1F1F1F"
            />
            <ellipse
              id="hole-2"
              cx={500}
              cy={510}
              rx={77}
              ry={28}
              fill="#1F1F1F"
            />
            <ellipse
              id="hole-3"
              cx={800}
              cy={510}
              rx={77}
              ry={28}
              fill="#1F1F1F"
            />
            <ellipse
              id="hole-4"
              cx={1100}
              cy={510}
              rx={77}
              ry={28}
              fill="#1F1F1F"
            />
          </g>
          <g id="first-jump">
            <mask
              id="mask0_3610_9890"
              style={{
                maskType: "alpha",
              }}
              maskUnits="userSpaceOnUse"
              x={70}
              y={257}
              width={456}
              height={268}
            >
              <path
                id="worm-mask"
                d="M100 495C100 495 131.59 286.5 299.5 286.5C467.41 286.5 497 495 497 495"
                stroke="#45DBAC"
                strokeWidth={58}
                strokeLinecap="round"
              />
            </mask>
            <g mask="url(#mask0_3610_9890)">
              <path
                id="worm-1"
                d="M94 545C94 545 123.631 287 297.022 287C470.412 287 502 543.503 502 543.503"
                stroke="#DB4545"
                strokeWidth={38}
                strokeLinecap="round"
              />
            </g>
          </g>
          <g id="second-jump">
            <mask
              id="mask1_3610_9890"
              style={{
                maskType: "alpha",
              }}
              maskUnits="userSpaceOnUse"
              x={751}
              y={16}
              width={389}
              height={509}
            >
              <path
                id="worm-mask_2"
                d="M794.361 483.633C794.361 483.633 759.916 56.5 943.5 56.5C1127.08 56.5 1097.33 475.649 1097.33 475.649"
                stroke="#207D2E"
                strokeWidth={81}
                strokeLinecap="round"
              />
            </mask>
            <g mask="url(#mask1_3610_9890)">
              <path
                id="worm-2"
                d="M801.071 557.996C801.071 557.996 755.893 66.0859 939.477 66.0859C1123.06 66.0859 1096.91 558 1096.91 558"
                stroke="#DB4545"
                strokeWidth={38}
                strokeLinecap="round"
              />
            </g>
          </g>
        </g>
      </svg>
    </div>
  );
}

 

Edited by Zyrus
updated the description.
  • Solution
GreenSock
Posted

Ah, try registering SlowMo specifically (or any of the eases inside EasePack): 

import { SlowMo } from "gsap/EasePack";

gsap.registerPlugin(useGSAP, DrawSVGPlugin, SlowMo, GSDevTools);

Does that resolve things for you? 

Posted
8 hours ago, GreenSock said:

Ah, try registering SlowMo specifically (or any of the eases inside EasePack): 

import { SlowMo } from "gsap/EasePack";

gsap.registerPlugin(useGSAP, DrawSVGPlugin, SlowMo, GSDevTools);

Does that resolve things for you? 

Hi! this one solved the problem. but I am still wondering why it is not working with just importing{ ease pack }

I also want to ask, 
so for example, I have 2 components in react that use gsap  and both of them use gsap.registerPlugin()

//component-A (has a gsap timeline with slow ease)
gsap.registerPlugin(useGSAP,GSDevTools)

//component-B(the slow I am working on right now)
gsap.registerPlugin(useGSAP,GSDevTools,SlowMo)

is the plugins applies to all instance ofregisterPlugin()? I noticed that the slow ease from component-A worked when I register the SlowMo in the component-B. so ease works for both component-A and B now. 

I want to ask if I should just make an instance of registerPlugin once from the main page? right now, I use registerPlugin (useGSAP, GSDevTools) to all of my components. maybe I am overloading it.

GreenSock
Posted
12 hours ago, Zyrus said:

I am still wondering why it is not working with just importing{ ease pack }

Because the EasePack object itself doesn't have a register() method on it. We'll add it for convenience to the next release. It works fine when people load the UMD file (like from a CDN) because that automatically registers itself on load (assuming GSAP already exists on the page). 

 

In short, just registering the individual ease that you're using is best, plus it allows better tree shaking. 

 

12 hours ago, Zyrus said:

is the plugins applies to all instance ofregisterPlugin()? I noticed that the slow ease from component-A worked when I register the SlowMo in the component-B. so ease works for both component-A and B now. 

Yeah, you only need to register it once. If you want to organize your code so that it does that in a central place, that's totally fine. It doesn't hurt to register more than once. Basically, do whatever is easiest for your codebase. 

 

Enjoy!

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