Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
pmascis

String Variable within Tween

Recommended Posts

Hi all!

 

I'm trying to make some animations customizable for the user within the backend of a CMS (need to stay using 2.x for now).

I'm having JS reference some HTML5 data-* attributes and have had success so far doing simple things like:
 

let duration = $(this).data('anim-custom-duration');
let xFrom = $(this).data('anim-custom-xfrom');
let xTo = $(this).data('anim-custom-xto');
let yFrom = $(this).data('anim-custom-yfrom');
let yTo = $(this).data('anim-custom-yto');

// Create tween
let tween = TweenMax.fromTo($(this), duration, 
{delay: 0.25, autoAlpha: 0, x: xFrom + 'px', y: yFrom + 'px'},
{autoAlpha: 1, x: xTo + 'px', y: yTo + 'px'});


But I'm now running into a problem where I'm wanting the user to change their animation ease and timing.

I am trying to combine ease Types as well as ease Timings into an 'ease' variable that I can ultimately use in my tween.

 

The code/problem is as follows:

// Easing
let ease;
let easeType = 'easeOut'; // $(this).data('anim-custom-ease-type'); // Would normally take from the data attribute, but for ease of reference I've just replaced it with the 'easeOut' option.
let easeTiming = 'elastic'; // $(this).data('anim-custom-ease-timing'); // Would normally take from the data attribute, but for ease of reference I've just replaced it with the 'elastic' option.

if (easeTiming === 'elastic') {
  ease = 'Elastic.' + easeType + '.config(1, 0.3)';
} // Would be some other else animation statements here

let varNotWorkingReference = 'Elastic.easeOut.config(1, 0.3)';
let varWorkingReference = Elastic.easeOut.config(1, 0.3);

alert(ease);
alert(varNotWorkingReference);
alert(varWorkingReference);


// Create tween
let tween = TweenMax.fromTo($(this), duration,
{delay: 0.25, autoAlpha: 0, x: xFrom + 'px', y: yFrom + 'px'},
{autoAlpha: 1, ease: varWorkingReference, x: xTo + 'px', y: yTo + 'px'});

(In the end within my tween I would obviously want to replace ease: varWorkingReference with ease: ease)
So 'ease' and 'varNotWorkingReference' are essentially the same thing,  based on my other readings within this forum I assume it's because they are strings, but am unsure about a way around it? 


Any help would be much appreciated!
Sorry for posting a question every few years about variables,  I only dig into this stuff every so often :)

 

Cheers,

See the Pen dyodWPq by pmang (@pmang) on CodePen

Share this post


Link to post
Share on other sites

Hey pmascis. 

 

You're essentially asking, "How do I interpret a string as an object reference in JS?" The easiest fix is to use JS's eval to interpret the string (disclaimer: you should be careful using it). For more information, see this StackOverflow post

 

With that being said, this would be much easier in GSAP 3! GSAP 3 allows a condensed string format for eases which the user could use instead. They're easier to use and the implementation would be straightforward - just pass the variable in string form to the ease property of the tween. Why do you need to stay using 2.x for now?

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites

Hi Zach,

 

Thanks for your prompt reply & for the eval information, I read about people discussing it on other posts but at the time wasn't exactly certain if it applied to my case or not, I'll just steer clear as what you have suggested with GSAP 3 sounds like it will work perfectly.

 

I'll just bite the bullet and upgrade to 3.0. The reason I have been hesitant is that I use GSAP along with ScrollMagic as well as NPM/ES6 etc and I have been running into issues migrating thanks to ScrollMagic's current lack of support.

In fact, I have been running into the exact console error problems mentioned here where you have already been helping.

And my mentality at this point is the same of cTigerDev's to finally drop it and use Intersection Observer instead.

 

When I have some free time I will read through that post in detail as well as other posts and see if I can get things working the way I need.
If not I'll hassle you in a new thread on here ;) Thanks again for your help.

 

Cheers,

 

Share this post


Link to post
Share on other sites
On 3/11/2020 at 1:39 AM, pmascis said:

So 'ease' and 'varNotWorkingReference' are essentially the same thing,  based on my other readings within this forum I assume it's because they are strings, but am unsure about a way around it? 

 

v2 can't parse config methods in ease strings, so you need to use object form.

 

ease = Elastic[easeType].config(1, 0.3);

 

Other notes. It's wasteful to keep creating the same jQuery object over and over again. Just do it once.

 

let $this = $(this);
  
let duration = $this.data('anim-custom-duration');
let xFrom = $this.data('anim-custom-xfrom');
let xTo = $this.data('anim-custom-xto');
let yFrom = $this.data('anim-custom-yfrom');
let yTo = $this.data('anim-custom-yto');

 

Or don't use jQuery. 

let duration = Number(this.dataset.animCustomDuration);
let xFrom = Number(this.dataset.animCustomXfrom);
let xTo = Number(this.dataset.animCustomXto);
let yFrom = Number(this.dataset.animCustomYfrom);
let yTo = Number(this.dataset.animCustomYto);

 

You don't need to pass a jQuery object to gsap. And you don't need to set units as px is the default.

TweenMax.fromTo(this, duration,
  {delay: 0.25, autoAlpha: 0, x: xFrom, y: yFrom},
  {autoAlpha: 1, ease: ease, x: xTo, y: yTo});

 

See the Pen 2278497285883a7f09befd0046c728cf by osublake (@osublake) on CodePen

 

 

  • Like 4
  • Thanks 1

Share this post


Link to post
Share on other sites

Hi Blake,
That's pretty much solved it and I can keep working, I'll migrate over to GSAP 3 when I have more time later.

I have taken on board and implemented your other notes too.
Thank you!

  • Like 2

Share this post


Link to post
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.

×