Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a greensock animation reverse but faster than original animation?

I have an animation that i am using Greensock for. When this animation is complete and when the user clicks on a button the animation will play in reverse. The issue i have is that i want the animation to play in reverse 3 times faster than when it was originally played. I am stumped on how to do this.

The current code i have works fine, i just need it to animate in reverse faster. Any help is greatly appreciated, thank you. Current code:

var tl = new TimelineMax();
var page4 = function(){
    console.log('hello');
    tl.staggerFrom("input", 0.5, {
      marginLeft:"-300px", 
      opacity:0, 
      delay:0.5, 
      force3D:true
    }, 0.2);
    tl.staggerFrom("label", 0.5, {
      opacity:0
    }, 0.2);
    tl.staggerFrom("#submit", 0.5, {
      opacity:0
    }, 0.2);
  };

$('#submit').on('click', function(e){
   e.preventDefault();
   tl.reverse();
   setTimeout(function(){ 
     contact();
   }, 3000 );
 });

function contact(){
   var msg = '<p>Thank you for your submission! Please give us 1 business day to reply back.</p>';
  $('.title').append(msg).fadeIn();
}
like image 934
Travis Michael Heller Avatar asked Aug 29 '16 15:08

Travis Michael Heller


People also ask

Is GSAP worth it?

GSAP is just another way to add overhead and bloat that is unnecessary. Scripting requires script libraries that increase the load time of a page. They require memory, and make the parsers do extra work for serialization on the Document object.

What is gsap3?

New plugin for animating things along motion paths (you can even edit the motion path in the browser!)

Why use Greensock?

Greensock Animation Platform (GSAP) is an easy-to-use JavaScript library for web animation. It lets you animate just about anything that can be accessed with JavaScript including SVG, generic objects, canvases, and more.

What is Gsap timeline?

A Timeline is a powerful sequencing tool that acts as a container for tweens and other timelines, making it simple to control them as a whole and precisely manage their timing. Without Timelines, building complex sequences would be far more cumbersome because you'd need to use a delay for every animation.


1 Answers

It should be as simple as setting the timeScale() to 3 (meaning it'll play 3x faster than normal):

tl.timeScale(3);

And remember, you can chain things, so you could reverse() it at the same time:

tl.reverse().timeScale(3);

And then when you need to go forward again:

tl.play().timeScale(1);

It's all in the docs: http://greensock.com/docs/#/HTML5/GSAP/

I also noticed that you're using a setTimeout() to call contact() after 3 seconds. That's fine, but if you want to have it called as soon as the timeline finishes (in either direction), you can set onComplete:contact and onReverseComplete:contact. Or there's also a TweenLite.delayedCall() that'll act much like setTimeout() but always remain perfectly synchronized with the whole GSAP engine and likely be more performant.

Happy tweening!

like image 60
Jack Avatar answered Sep 29 '22 17:09

Jack