Jump to content
Search Community

Loading GSAP 3 with RequireJS

szabkel test
Moderator Tag

Recommended Posts

I have to load GSAP 3 with RequireJS (from a Magento 2 frontend).  GSAP 2 worked without a problem, however I can't do the same with version 3.

 

If I use the CDN paths inside the require config paths key, I get an error:

TypeError: gsap.registerPlugin is not a function 

Or I get

Error: Script error for "gsap/gsap.min"
https://requirejs.org/docs/errors.html#scripterror require.min.js:1:1795
Error: Script error for "ScrollTrigger/ScrollTrigger.min"
https://requirejs.org/docs/errors.html#scripterror
Should be something trivial, but It drives me crazy, please help.
Thank you very much
 
Edit.: A tried a lot of solutions posted in the forum, but non of the examples were for version 3 and they usually just use TweenMax..etc.

See the Pen YzwqzWG by szabkel (@szabkel) on CodePen

Link to comment
Share on other sites

Okay, maybe I am just retarded, but requirejs adds an alias? I can use the objects like this:

require(['https://cdnjs.cloudflare.com/ajax/libs/gsap/3.3.1/gsap.min.js', 
         'https://cdnjs.cloudflare.com/ajax/libs/gsap/3.3.1/ScrollTrigger.min.js'], function (GreenSocks3, St) {
  const gsap = GreenSocks3.gsap;
  const ScrollTrigger = St.ScrollTrigger;
  
  console.log(gsap);
  console.log(ScrollTrigger);
    gsap.registerPlugin(ScrollTrigger);
});

 

Link to comment
Share on other sites

I'm not very familiar with RequireJS but you could destructure the arguments:

require.config({
  packages: [
    {
      name: 'gsap',
      location: 'https://cdnjs.cloudflare.com/ajax/libs/gsap/3.3.1/',
      main: 'gsap.min'
    },
    {
      name: 'ScrollTrigger',
      location: 'https://cdnjs.cloudflare.com/ajax/libs/gsap/3.3.1/',
      main: 'ScrollTrigger.min'
    }
  ],
})

require(['gsap', 'ScrollTrigger'], function ({gsap}, {ScrollTrigger}) {
  console.log(gsap);
  console.log(ScrollTrigger);
  gsap.registerPlugin(ScrollTrigger);
});

 

  • Like 2
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...