Do you control time?
For the first time in your life your about to take control over time. Moving into timelines is about controlling animations. Part of this control is knowing how long is the animation running and how much time is left. In this lecture, we will explore the TimelineLite.time and TimelineLite.totalDuration methods.
Video Transcript
00:00Time and time related methods are probably one of the most complicated things in the library,
00:06but that's because it's a really powerful library.
00:09So we saved the conversation about time for a really late stage such as now,
00:14and we're going to see the time methods over and over as we progress and create animations that control the time
00:20and take over the time and things that relate to time.
00:24With that said, I want to talk about what are the methods relating to time,
00:28such as what are we in the position in the timeline right now,
00:31and what is the total amount of our timeline.
00:34And that's what we're going to find out in this lecture.
00:37But this is going to be a topic that we're going to follow through with in chapters in the future as well.
00:43Alright, so let's do it. Let's see what we're talking about.
00:45And really that's to sort out our scrubber and make it really fully controllable the way we want it to be.
00:52So what I want to notice if you've been playing around, and I'm sure you have with the tween light, tween max, and timeline light and max,
00:59is that all of them, they have a lot of different functions related to time.
01:03We talked about them a bit in previous courses, and now we're going to talk about them a little bit further.
01:08But really by the end of this course, you're going to know everything you need to know about how to control time with your timelines.
01:15So the first thing is I'm not going to go into every intricate detail right now,
01:19but one thing is that we need to figure out a way to know what place are we in our timeline.
01:24To get that reference, we could basically approach the property, and I'm just going to go here console.log,
01:31and we could approach the property of time, which you've seen auto exists also in the tween light and tween max and also timeline max.
01:40Now, that property is going to tell me what second it currently in my element in my end,
01:45I could also use it to leverage it to change the position in time.
01:48We're going to look into that in the future video probably,
01:51but we've already seen how to change time using the progress function by setting a progress inside of our progress function,
01:58which works in a very similar way, only time will accept actual seconds.
02:02Now, the second property that we want to approach is, and it's going to be confusing,
02:07you don't want to go to TLL total time, because that is not going to be useful for you right now.
02:13Don't worry about it.
02:14The only contour function that you need is how long is the total animation,
02:18and in our case, we're going to approach the total duration.
02:22Now, if you try to approach any other function, for example, the total time,
02:26you're going to get a very confusing value, and I just want to show it to you.
02:30Just make sure that you know that as you're working with our timeline light,
02:33and when we move into timeline max, we're going to go back to total duration and total time
02:38and explain the differences between them and why they are so different.
02:43But if I click here on refresh and just look into my item, when I click on pause,
02:46you're going to see that my time itself is whatever, under a second.
02:50Total duration is seven seconds, and again, the time that I'm in right now for my total time
02:55is again the exact same amount of time.
02:57So don't use the total time at all at this stage.
03:01Just use to know your time, the time function,
03:04and to know the total amount of time, use the total duration.
03:09And we're going to look at the total time later on and explain
03:12where would you actually use it and what does it actually return to you.
03:16Okay, so with that said, we're going to go back and create a toggle here
03:20because what we really want to happen is as our animation progresses
03:24and it gets close to the end,
03:26as it gets close to the end and it gets really, really close there
03:30and beyond getting close after it's there, we want something to happen
03:34when the user clicks on the play again because just clicking here
03:37isn't really doing anything.
03:38And I know that my time equals the total duration,
03:41so how about I'll just reverse the animation
03:43if the user clicks it all the way in the end.
03:46So to do that, I'm just going to go ahead here
03:48and literally just add here a very simple function
03:51and I'm basically going to say, what's the amount of time that passed already?
03:56Does it equal to the total duration of the animation?
04:01And if it does equal to the total duration of the animation,
04:04I'm going to say, hey, reverse yourself.
04:06If I say this, go back and click on refresh.
04:11So first of all, if I pause it and I'll have to let it play through first
04:15and if I just click on it again, you'll see that now it's going to animate backwards
04:22but if I click on it again, it's just going to pause
04:25and continue in whatever direction it was going.
04:28It's not going to change our direction.
04:30Now, next problem that we have is if I click on here,
04:32we're going to see that now we're at position zero
04:35and I really want if we are in position zero to move our playhead forwards.
04:40So in that case, I'm just going to add here another else if
04:44our timeline time equals zero, basically saying that we're in the beginning.
04:49In that case, I'm going to ask it to play again
04:52and playing is not reversing, playing goes forward.
04:55So if I click here play and I don't have to say to where to go playing,
05:00I could have used the progress and pushed us to position zero.
05:03I could have used the seek and pushed us to second zero.
05:06I could have used the time.
05:08A lot of different functions to do the exact same thing
05:10but because I know I'm already in the right place in my playhead,
05:13all I really want to do is tell it,
05:15hey, I want you to play over again, start playing now.
05:18So if we click on refresh, our item could go all the way to the end
05:21and if we click here again, it's going to go in the opposite direction
05:27and animate in the other direction.
05:29We could pause at any point that we want to
05:31and beyond that, when it reaches the beginning,
05:33if we click on it again, it will start animating
05:36and we could have here now a cycle that goes on forever
05:39and give the user complete control, which is kind of intuitive.
05:42So if the user clicks on again, then it makes sense for it to go backwards
05:46or to restart and at any point,
05:49the user could flow into a specific spot in our timeline.
05:53Now, another item that probably would be useful is if we're playing
05:56and the user goes on top of it,
05:59it makes sense to pause everything
06:01and it's not a click event in our case.
06:03What we could do is maybe prevent this control when it's animating
06:07but we probably wouldn't want to do that.
06:09So one way around this would probably be
06:12when we actually do this kind of control
06:16where a user is actually rolling over with a mouse move,
06:20we could literally say, if you are paused,
06:23if you are not paused more accurately,
06:26so if you're not paused,
06:32if we're not paused, then go to TLL and paused.
06:39So literally, I'm saying if you're not paused, then pause yourself,
06:42but only if you're not paused, pause yourself.
06:44Now, if I really want to make this fancy,
06:46which you sure do, but I'm going to let you make it fancy
06:50if you choose to do so, is add here a set interval
06:54and literally, you could set here a set timeout
06:57and call a function that would resume play
07:01and have it maybe in a delay of one second.
07:04The idea behind here, and you know what, let's just create it.
07:07So I'm going to create here a global variable.
07:09I'm going to call it n resume play.
07:13I'm going to set the variable value to set timeout here
07:17because it is an important lesson here.
07:19Oh, you know what, let's do it in the last lesson.
07:21Like that, we could talk a little bit more deeply
07:23about how time works if you're using animations
07:26that relate to time separately from the tween animation.
07:29Okay, so pause this, pause this.
07:31Let's just test this out.
07:32You know, I'm going to even delete it
07:33and we're going to do it all over again
07:34because it's such an important topic
07:36and I'm going to make it separately.
07:37All I really want to test right here
07:39is that if we go onto our buffer,
07:41click on refresh here after our animation goes through
07:44and our pan actually works, la, la, la, la.
07:49And it's playing.
07:50If I go onto it, it's going to pause it
07:52so it's not going to actually look crazy
07:54and animate on its own.
07:56The only thing that's missing for me right now
07:57is that once I get off of there,
07:59I really want it to start animating and it's not.
08:02So what I want to do is add some sort of an extra logic
08:05that would basically resume the animation
08:08once the user is complete with this moving
08:11and finding the position where they want it to be.
08:13So one logic that I could do is basically have your,
08:16as long as I'm on here, it will be paused
08:19and once the user moves his mouse out,
08:21then I could resume it, which makes sense.
08:23Or I could use an interval.
08:25So if already we brought that up,
08:27how about we do the interval?
08:28Because as I'm thinking about it, I think it's a bad idea.
08:31So instead of using an interval,
08:33which in this case, that's what I'm doing,
08:34creating an interval, what I would do is also make sure
08:37that every time I call this, I would clear that time out,
08:42which would make sure that I'm not going to call
08:44this time out over and over and over.
08:46And it's fine if you clear a time out
08:48with sending it nothing,
08:49it's not going to generate any error.
08:50And in our resume play,
08:55we would basically just resume the play of TTL.
08:58Now the only problem we have here with the resume play
09:04is that if the status actually changed.
09:06So I probably would need also another variable here
09:09and I would have to call this pause status.
09:16Let's not do this whole, you could figure it out on your own.
09:19But basically what that would do is it would resume it.
09:22Obviously in this time around,
09:23this is not going to be the smartest
09:24because if I resume it in this stage,
09:26let it play for a second here.
09:32And if I play it to play again and I go over in here,
09:34first of all, it's going to freeze it.
09:36If I step out and wait for a second,
09:38then it's going to continue animating.
09:40And I could do this as many times as I want to
09:42have full control over it, move out.
09:44Now the only problem I have with this
09:46is what if I already paused it.
09:47And if I paused it already and I find that spot
09:50that I want to go to and I want to stay in that spot,
09:52it's going to start animating anyways,
09:54which is a problem.
09:55So that's basically our problem,
09:57which we're not going to solve.
09:58I'm going to let you solve that problem.
10:00But in the next lesson,
10:01I do want to take one more peek at this,
10:03showing you another way that you could do this
10:05instead of using a set interval
10:07or a set timeout in our case,
10:10because I think it makes more sense to do this
10:12using or even better.
10:14That will be your homework.
10:15And if you do it, I'll reply in the comments.
10:17In this next section,
10:18we'll move on to a different topic.
10:19But this is your homework.
10:21Find a better solution such as
10:22we have your mouse move.
10:23How about a logic that has also on mouse out?
10:26So whenever we go out of the space of that element
10:29or come up with your own solution
10:30that would actually work without any bugs,
10:32in this case, I already see a few problems.
10:35We kind of did it live,
10:36but that is your homework assignment
10:38if you choose to do it.
10:39And I would love to see your version
10:41in the comment section
10:42and I will definitely reply to it
10:44and take part in whatever you're doing with that
10:46if you just comment in our comment section below.
10:49So that's it.
10:50See you in the next lesson
10:51where we continue
10:52and go back into the world of timeline life.
