Creating a timeline scrubber
In the first chapter, we created an animation scrubber. We saw how complicated it is to take into consideration the various aspects of each animation. By moving our animations into TimelineLite, we removed the need to manage these relationships. In this lecture we will update the scrubber code to work with TimelineLite.
Video Transcript
00:00Let's go back to our scrubber and really all the code that isn't really functional
00:04right now because it was all built to work with the tweenLite.
00:10But because the tweenLite and timelineLite and tweenMax and timelineMax really work off
00:15of the same methods, besides extra methods that are only relevant to specific classes,
00:22then we could very easily and very quickly update everything because really everything
00:25that we have in the tweenLite exists also inside of the timeline, almost everything.
00:30So let's jump into it and change our code to make it work and it's going to be really
00:34easy because we're going to basically use the same type of mythologies and logic that
00:39we had also using our tweenLite when we were working with the tweenLite.
00:43You could find in the timelineLite, you would find also most of them, you would also find
00:48in the timelineLite itself.
00:50So the things you would find in tweenLite, you would probably find also in the timelineLite,
00:54mainly the things that relate to controlling animations.
00:58So let's see how we could basically, so with that said, a lot of the callbacks that you
01:03would have for the tweenLite exist also in the timelineLite, although we refer to them
01:08when we're actually creating that constructor when we're creating the timelineLite.
01:14And there we could send in a helper function and in that helper function, we could send
01:18in four different onComplete types, onListener is basically onComplete, onStart, onUpdate,
01:26and onReverseComplete.
01:28In our case, we want to use the onUpdate and the onComplete, so I'm going to go ahead
01:33and send in that onUpdate and literally I could go ahead here and just copy this onUpdate
01:45and put it right here and I could copy also the onComplete and copy it right here.
01:56So let's start with the onUpdate because that's going to be the first thing that we see
01:59when we start our animations.
02:01So I'm going to go onImageAnimUpdate, just go to that function really quickly.
02:06OnImageAnimUpdate, we'll see that first of all, it's asking for this progress,
02:10which probably isn't what we're calling for because the scope itself,
02:14if I do a console.log, I could ask, is this equal timelineLite?
02:21Just to see, am I getting, is this the timelineLite, which is really what I want it to refer to?
02:27If I save this and I go back into my application and click on refresh and just look
02:31at my screen here, we're going to see that yes, we are approaching our timelineLite.
02:34This is the item itself and by the way, we could also see that our animation went exactly
02:39in the flow that we wanted it to be.
02:40Don't worry about the errors because we didn't go through further than that.
02:44So we know that our scope is correct.
02:45It's going to our timelineLite, so I could get rid of that.
02:48So this item is totally good.
02:50This item is totally good.
02:51There's nothing here we have to change.
02:53Let me just delete here all the extra goodies that we don't really need.
02:57And let's see the next thing.
02:58So the next item is onComplete and we're going to call that onImageAnimDone.
03:02If we go to onImageAnimDone, let's see what we have here.
03:05For our tracker itself, onMount, we're basically adding the mouse move.
03:10Okay, that's fine.
03:11betweenPan doesn't exist anymore.
03:14We do have a timelineLite, but let me comment this out for a second
03:17because it doesn't really exist.
03:19Let's just undo it.
03:21And if we go to the onMouseMove, let's see in our onMouseMove.
03:24Again, we're approaching the progress, which is not really what we want.
03:29We don't want to approach the tweenPan and the tweenWidth
03:33because they don't exist anymore.
03:34So I want to approach the timelineLite progress.
03:37Okay, so that's good.
03:39And for my onImageAnimUpdate, I don't want to send the tweenPan anymore
03:43because that doesn't exist.
03:44What we really want to send in is our TTL, really.
03:48And if I save this, if we just run our application one more time,
03:52we shouldn't be getting errors or at least we'll be getting a lot less errors.
03:56If we could see the animation is going on, it's animating.
03:59Moreover, then it's animating.
04:01One thing that you probably haven't noticed or maybe you have
04:03is that our element is we're getting also that fade in.
04:08We're getting also those delays.
04:10Now, we are automatically animating it,
04:12so we do want to actually turn off the animation when it's complete.
04:16So we could go back to where we did that commenting out.
04:19We could just go ahead there and save our TTL,
04:24or we could just literally check really quickly in our console.log
04:28if our this equals the TTL.
04:31And if it does, then we could go ahead and then use it
04:35and use it to basically stop things.
04:38So if I just save that, go back and click refresh, it should
04:40because that is the animation library that has been running our element.
04:44There's no reason why it wouldn't be,
04:45but that's a great way for you to test out, by the way,
04:48if you're not 100% sure.
04:49And you could say, okay, I am sure because that is the item.
04:52And then I could just undo this, put it back where our element that was there.
04:56And instead of that tween pan, just use the this.
04:59We don't even have to refer to it directly
05:00because we're inside of that element still.
05:03So if I save this, this should definitely work.
05:05And last but not least, we want to turn on that image click again
05:08so we could toggle the play.
05:10And this time around, we don't need to get the tweens of,
05:14we don't need to go through such a huge process of looping through elements.
05:18No need for that anymore.
05:21And literally, we don't need a loop anymore
05:23because we only need to access our timeline light.
05:26So literally, all I have to do is just, I don't need this anymore.
05:29I am with the timeline light.
05:31So I literally could just go ahead here
05:33and I'm just going to do it really quickly.
05:35I could go to my timeline light and I could say paused
05:39and just be exactly the opposite of what you were.
05:42Timeline light, paused.
05:44Literally, what I'm doing, I'm asking it, is it paused?
05:47It's going to return a true or false.
05:49Then I'm going to switch it around and basically change it.
05:52And the last thing that I probably would want to do
05:54is instead of all this stuff is basically ask, am I in the last frame?
05:59And if I'm in the last frame, then maybe I'm going to want to do something.
06:02So how about if I'm in the last frame,
06:04I'm going to flip around and animate to the other direction.
06:08So if I click on the toggle and I was paused and I wasn't in the last frame,
06:12then I would toggle it basically to the other direction.
06:15But let me just see that everything is working here.
06:17So if I click on refresh and I pause it, we could pause it any part.
06:20We could go back, oh, well, first time around it
06:23has to animate first all the way through before we turned on our logic.
06:27But once we turned on our logic, we could go ahead, go to any spot,
06:30and then press play again.
06:32And it's going to take into consideration all the delays at any point
06:36and we have full control over it.
06:38Now, the only problem that we have right now, which isn't really a problem,
06:41is if I click on play right now, it's not really doing anything,
06:44although it is.
06:45It's moving to a play state, but we can't really
06:48see it because we're in the end of our animation.
06:52So one thing that we could do is basically leverage the knowledge
06:55that we have already that basically all the different functions that
06:59exist for our timeline exists also in our animations.
