Skip to content

Instantly share code, notes, and snippets.

@aubricus
Last active January 11, 2018 09:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aubricus/10783862 to your computer and use it in GitHub Desktop.
Save aubricus/10783862 to your computer and use it in GitHub Desktop.
Load Greensock TweenLite and TimelineLite with Require.js
require.config({
baseUrl: '/js',
/* non-essential config removed for berevity */
packages: [
/* gsap package looks like:
vendor/
gsap/
non-amd/
tween-lite.js (renamed from TweenLite.js)
timeline-lite.js (renamed from TimelineLite.js)
tween-lite.js
timeline-lite.js
*/
{
location: 'vendor/gsap',
name: 'gsap'
}
]
});
// vendor/gsap/tween-lite.js
define(function(require, exports, module){
// requires the TweenLite source js
// I've moved it to "gsap/non-amd" so I can reference this file conveniently
require('gsap/non-amd/tween-lite');
/**
* GreenSock namespace looks like:
*
* Ticker
* TweenLite
* easing
* events
* plugins
*/
// export the com.greensock object exported by the TweenLite js
module.exports = window.com.greensock;
});
// vendor/gsap/timeline-lite.js
define(function(require, exports, module){
// references the non-amd (original) versions of tween-lite and timeline-lite
// since timeline-lite depends on tween-lite we have to require it here.
require('gsap/non-amd/tween-lite');
require('gsap/non-amd/timeline-lite');
// TimelineLite only adds a single namespace
module.exports = window.com.greensock.TimelineLite;
});
define(function(require, exports, module){
// it's a little confusing; but since the tween-lite file
// exposes SO MANY name spaces, it's more convenient to
// set a single var to the amd module's export object
var greensock = require('gsap/tween-lite');
var TimelineLite = require('gsap/timline-lite');
// then without a whole lot of redundant typing we can access the namespaces individually
// note this is just an example; it's unlikely you'll need to define all of these
var TweenLite = greensock.TweenLite;
var easing = greensock.easing;
/*
example:
var $el = $('.foo');
var dur = 1; // second
var ease = easing.Quart.easeOut;
TweenLite.to($el, dur, {top:'+=100px', easing: ease});
*/
});
@aubricus
Copy link
Author

Note: Using the require shim plugin here will fail because TweenLite defines 14 unique namespaces. TimelineLite might work in shim, but for consistency I've created a custom shim for it as well.

Note: Moved the shim to use the com.greensock namespace exported by the TweenLite js.
Note: For whatever reason; despite the single namespace export; the shim plugin was still not working. Maybe someone can figure it out. For now this works well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment