02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Creating a toggle controller

As part of our mission to control animations, we need to control their play state. In this lesson, we will pause and play animations through user controls. So let's get started.

Video Transcript

00:00As we get near to the end of our chapter, that's really kind of the starting to interact with our elements and making them not only play but also control them.
00:10What better way is there to start and create a toggle control so we could pause and play our video whenever we want to.
00:18You see I'm saying video already because it feels like a video but our tween.
00:21And that's exactly what we're going to do in this lesson.
00:23We're going to see how to control and really I urge you to check out all the other functions in the tween library to see how could you use them yourself beyond the topics that we're going to cover.
00:32If you are not familiar with them, this is a good time right now to go into the documentation and just take a gander and see how they work because as we progress throughout the chapters things are going to get more heavy and more complicated.
00:44So let's start by creating a toggle play for our animation.
00:49This is really close to the end of this chapter.
00:51But before we wrap it up, I wanted to talk further about the controls you have over every single animation in tween like tween max.
00:59And everything we're learning here really applies also for timeline, which is going to be our main topic in the next chapter.
01:06But even before we get to that next chapter, what I want to do is I want to go back into our element.
01:11And what I want to do is create a toggle pause play for our animation whenever we click on the image.
01:18So to do that, I'm just going to go ahead into my onload image and add a control for my image, adding a click event using jQuery.
01:26And I'm just going to call it on toggle play.
01:30And on toggle play will basically just switch our play position from whatever state it is to the next stage.
01:37So let me just create here really quickly the function on toggle play.
01:43The on toggle play is an event. So we have an event up here.
01:47And the next thing is I want to actually fetch all of the tweens that are currently available for my element.
01:53So to do that, I'm going to create a new array, call it AT for AT tweens.
01:59I'm going to call it tween light and library.
02:01I'm going to ask for it to get for me all the tweens of the object that we're animating, which in this case is the e.target.
02:09Now again, this is again the talk about the static versus actual properties of an event.
02:15We've seen a lot of the static ones, and this is just one of them, which enables us to fetch all the tweens of a specific object.
02:21And by just calling get tweens of, I could literally just fetch all the tweens that are currently active for the item that I'm trying to animate.
02:29All that's left for me to do is to test and see that I'm actually getting tweens back.
02:35And what I'm going to do is I'm just going to do a for loop and just loop through each item inside of our array here.
02:41I'm just going to make sure that we loop through that our eye as long as our eye is smaller than the length of our array.
02:51And each time we're going to upgrade our eye.
02:54And let's just pause everything.
02:56So I'm just going to go ahead to my AT and go to that item I and just ask to pause it.
03:02Now, if I set that and run this, I should get all those animations paused.
03:07So I click your save and go back into my application and click on refresh.
03:11Anytime I click on here, I should pause the video.
03:15And that's it. Once I pause it, it's not going to move further image.
03:19You know, it feels like a video already, right?
03:22All right. So we also want to be able to toggle.
03:24So what I'm going to do is I'm going to go right back in there and I'm just going to add here basically a toggle effect, basically asking
03:31if we're paused, let's switch to be playing.
03:33If we're playing, we'll switch to be paused.
03:35So I'm just literally just going to add here a, it's a if condition, but I forgot the name of it.
03:43It's a, oh, nevermind.
03:45I don't remember the name.
03:46But basically what we're going to do is let me just open this up.
03:49I'll probably put the name in the name of the title or somewhere nearby.
03:53And what we're basically doing is I'm basically asking for my object,
03:57what is its pause state by calling the paused function.
04:02And I'm asking it that if it's paused, what do I want to do?
04:06I want to do something.
04:07If it else, I want to do something else.
04:10So in the case that it's paused, what I really want to do is I want to play it.
04:13Okay. So I'm going to go ahead and play it.
04:20And every, any other case, I want to make it pause.
04:23And this is a ternary, there we go, a ternary operator.
04:26It's very similar to an if condition where you have your condition right here.
04:30If this comes back as true, question mark, and then we'll do what's inside here.
04:35And then colon will be if it doesn't come back true.
04:37This way we know that we're all calling the play or calling the pause.
04:42So I go ahead and save this and go back into my application and click on refresh.
04:46This time around, anytime I click, I should pause my event.
04:49And anytime I play, I should be continuing them and pausing, playing, pausing, playing, and so on and so on.
04:58And that's it.
04:59That was how do we control pause play?
05:01Now, before we wrap this up, I want to say that there's a lot of functions that control the timeline in jQuery from the pause function, pause, resume, restart, play.
05:12We've seen pause already.
05:14There's a lot of controllers such as seek that enable us to go to a specific time in our timeline.
05:20We've seen O2, the progress one.
05:23We're not going to go every single function.
05:26Just make sure that you familiarize with them because then you could use the one that's the most effective for your scenario.
05:32In our case, we didn't even need this play pause logic.
05:35We could have done because all of our animations are going through the same flow.
05:39And we're assuming that we always have at least one.
05:42And the reason is because we have these properties saved here.
05:45And by the way, if we just really, really tie this and take this home, if I go to my console and I ask what how much animations are active right now,
05:54notice what's going to be outputted.
05:56If I click on refresh and I click immediately, we'll see that we have three animations going on at the same time because we have that initial fade in.
06:05Once that fade in is complete, we only have two animations going on now.
06:09And the logic behind it is very simple.
06:11That animation has gone forever.
06:13It's basically been removed from our cache because we didn't save a global variable for it.
06:19As these animations are running, they're going to still be there.
06:22But once animations are done, the value is going to go down to zero because there are no active animations right now.
06:29Although we have access to those variables, there will be zero there.
06:33So literally what we want to do is because of that, we want to make sure that we don't just go ahead here and just try to approach and set like some sort of a variable saying is pause, for example.
06:45And then just set here the value, just go to AT position zero and ask basically for the paused value.
06:54Let me just copy that because if we do that when we reach the position zero, once we're at our animation is complete and I just save this and I'll just trace out the is pause here.
07:06And let me just run it really quickly.
07:09So we'll see that as long as our animation is running, then we have a value.
07:14It's coming back as false or true once we click on play.
07:18But once we are done with this and we click on again, we'll see that we're going to get an error.
07:22And that is because we don't have access to those tweens.
07:25We're not fetching them in this algorithm anymore.
07:28So we want to make sure that we have here a logic that basically says we could put this if of our length basically if our if AT basically zero and and and that is paused.
07:46Now, the logic that applies here is if this value is undefined, it's going to send undefined into it.
07:54If the value is there, it's going to send the value there.
07:57So if I save this and run my application one more time, notice what's going to happen.
08:01Now, if I press pause play, I'll get those values while the values are there.
08:06Once the values are not accessible to me anymore and I click it on again, I'm going to get the value undefined instead of error.
08:14And in this case, that is really all I really needed for my application to really work because I could literally take this value.
08:21And what I really want to do is I want to flip the values around.
08:25So literally, I could just take whatever value.
08:27It doesn't really matter if it's undefined.
08:29If I do a command and wrap everything here, whatever value that comes back is going to turn it into a Boolean.
08:36The opposite of what it is and undefined is like a false, which would then it would translate into a true.
08:43So if I go ahead here and I click on refresh, we'll see that the first time I press, it's going to come back as true, which is the opposite of what it was.
08:52And it's going to be able to be the controller for me instead of using those if conditions inside.
08:57Now, if I click on it again, we'll see it's going to come back as true and it's not going to generate any error because it's true.
09:02I want to replay it even though I don't have any access to these elements.
09:06So now, instead of creating this if condition, which is a little bit convoluted, I could go ahead and just call the pause function,
09:14which is a toggle function on its own and just send inside of it the value is paused or not.
09:20By default, pause is going to accept, by default, it's going to pause things.
09:25But if I send the value of false, it's going to actually play it.
09:28So if I save this and go back into my application and click on refresh, let's see what happens.
09:34So if I set here once, it's going to pause it, set it again, it's going to play it.
09:37And I got the same exact logic that I wanted.
09:40And I'm not going to get any error when I press here and change things because it's just going to stay true because it's at the end of our animation.
09:49And that's it. This is it for this lesson.
09:51But we're not done with the chapter, although we're getting very close to being done with the chapter.
09:56See you in the next lesson before we wrap this baby up.
09:59And we're not going to talk about every single function.
10:01So just make sure that you're familiarize yourself with those functions.
10:05We're really talking big picture control and how you could influence and control your animation
10:09and make them just better than what you could have without this ground view.
10:14So I do hope you go inside and explore and understand the differences between all the different functions that control time in our tween light library.
10:23See you in the next lesson.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!