Controlling animation progress
We created many GSAP tweens (TweenLite and TweenMax) in LEVEL 2.Once we created a tween, it was the end of our relationship with that animation. This is where our advanced training comes to play; it's about interacting with animations. In this lesson we are going to create a simple animation scrubber that will deepen our control over animations.
Video Transcript
00:00New things and new horizons and this lesson is gonna be about us controlling physically our animation as we create a progress bar
00:08Both seeing our animation progress, but also controlling it which is gonna be really really cool. So let's do it
00:15It's gonna be really fun and very long like a lot of fun things. They take a while
00:19You can't really wrap it up in a two-minute video, but trust me
00:22It's gonna be a blast and when you're done with this
00:24It's gonna be even hard to recognize that it's not actually a video playing but it's actually
00:29Tween that we created. All right. Welcome back in the last lesson
00:34We created this really cool image slide and in this lesson we want to start controlling it
00:39So for one, I want to create a scrubber that would basically show me where animation is and even control the animation
00:46So how about we get to it before we start?
00:49We have to go into our HTML and I'm gonna go into that tracker area because that's exactly what I'm gonna be using
00:55for one I'm gonna have a background to my tracker so I could then roll over it at any point and
01:00Inside of my tracker what I want to do is I'm gonna put another span inside here. I'm gonna give it a class name of
01:07Tracker body
01:10Idea behind here is to have the body itself
01:13So as the animation goes that I'll be able to see where I'm in as the animation is going
01:19So if I save this and I just go into my CSS, let me just really quickly create also the tracker itself
01:26So I'm just gonna go right in here and
01:29I want to set its width to be a hundred percent because I wanted to take a full space of the screen
01:34I want to give it a height of 20 pixels. I want to position it on the top
01:39So I'm gonna just set it to be absolute and so it won't overlap with anything else
01:44I'm just gonna make sure that it's above everything
01:46So I'll set it at the index of one because it's the only thing that has an index and I'll set also a background
01:52Color and I'll set that color to be white
01:56Last but not least I'm also gonna set an opacity because I want it to be see-through and I'm just gonna set an opacity of let's say
02:030.7 and if I save this and go back into my application and click on refresh
02:07We should see a nice little line here. That is a little bit transparent. That's up above here. It doesn't really do anything yet
02:15The next step is I want to want to define the rules of my tracker body
02:19So I'm gonna go back into the CSS and let me just hard code a value inside of it initially
02:24So I'm just gonna first of all start off by setting my tracker body and I want to set its width
02:35Well, I want to set its height first
02:37Because its height should be 20 pixels because even when it's empty we want to be able to see it and just for now
02:42I'm gonna set a width of 1% because I want to be able to see that I could see it and
02:46I'm gonna set a color for it. So I'm gonna go to my background color and set that to be black and
02:52Last but not least. I want to make sure that my position is absolute as well
02:57So it's gonna overlap and be inside of my element without moving around if anything else would be inside there
03:04So I'm gonna save this go back into my application click on refresh
03:09And we'll see that I'll have your nice
03:11Black box sitting here waiting for something to happen with it. That's it
03:16We're done with the HTML and the CSS and it's time for us to drive into our
03:21jQuery
03:22jQuery and tween light tween light inside of our JavaScript
03:27File because the next thing that we want to do is we want to take we're gonna only track our
03:33Animation that is the longest I'm gonna go ahead and just track it and basically have your non update and I'll call on image
03:40anim update and I'm gonna go ahead and create
03:45that function right here and
03:52The next step is I want to figure out what is the percentage that has changed now by default. There is a function called
04:00Progress that belongs to the tween light tween max
04:03So if I said here this progress and I just run this for a second in my application each time
04:11My application updates we should be able to see the values updating and changing in our application
04:16Now this only applies once the animation starts
04:19It does not apply to the actual delay and you can see that our our values are updating
04:25The values are between zero and one but our element here is going to be between zero and a hundred
04:31So I'm gonna have to duplicate the value by a hundred. So if I go back in here, let me just create that variable really quickly
04:38I'll call it per four percent. I'm gonna go to this dot progress function
04:44I'm gonna duplicate the value by a hundred and
04:48I'm gonna auto
04:50After I'm done with that I could round it up really even if I just add to this
04:55The percent itself then it's gonna be obvious for the browser and it's gonna round it down to whatever amount that it could
05:02So that's it now. I set my percent
05:04All that's really left for me to do is to actually go into my actual
05:08Element and actually set its value so I could just use tween
05:13I could use the tween light library or to you or use jQuery
05:17but I'll use tween light and I'm just gonna go to the tracker body and
05:21I'm just gonna let that body know that it's CSS position
05:26Should be updating its width and we're gonna set it to the percentage that we have there
05:31Let me just make sure that I didn't over close
05:34No over closing. Oh I did hear double. Yeah, that's that would do it. Oh
05:39Okay, perfect. That should be okay now
05:42And if I click on refresh and save this what we should see is our tracker if we don't have an error invalid property ID
05:49In line 30, so let me just go ahead and check what happened in line 30. So I'm setting here a tween light set
05:58Tracker body. It's telling me it's invalid ID. Maybe I didn't type it correctly. So
06:04Tracker body. Let me just copy that paste it in there. Maybe I made a mistake right here. No, that seems okay to me
06:12Let me save this one more time. Oh and that
06:15Parenthesis that I remove seem to have returned
06:19How weird?
06:20Okay
06:21That's how it should be. I believe let's give it another round and see what happens
06:26So pre is not defined. So we're getting closer now. We're at least getting an error that is
06:35Descriptable for us. So it's telling us that pre is not defined
06:38And let's see why pre is not defined because we have your per and not pre. Okay, so that we're getting warmer
06:43These bugs happen. Just don't worry about them. Look at the bug and then just correct whatever you see
06:49If you run it one more time, hopefully now it's gonna work. And there we go. And we're seeing our progress bar
06:54Floating alone now before we wrap up this video
06:57I really want to also add a little bit of control where we could roll over to actually see the animation in specific points of
07:03Time I'm only gonna activate it once our animation is complete and I'll explain why in a second
07:09So to the next step that I want to do is I'm gonna go go ahead and add here an on complete
07:15and I'm gonna call on image and I'm
07:18done and I'm gonna go ahead and just create that function really quickly and I'm gonna make sure it's called anim done on image and
07:32I'm done and
07:34Now what's left for me to do is once I'm I've gotten in there
07:38The what I want to do first of all is I want to pause my well even before I pause it
07:42I'll show you why I want to pause it, but I'm gonna go ahead
07:45I'm just gonna ask for my tracker
07:47Which was basically that the wrapper object the white background that we have inside of our page that whenever it's not completely opaque
07:55Whenever we roll over it, I want to know the information of the rollover
07:59so to do that, I'm gonna call the jQuery function of mouse move, which is a callback and
08:05I'm gonna make sure that I call on mouse move now that I'm only enabling
08:11Enabling it after an animation happened one time and I'll explain the rationale behind it in a second
08:16But I'm gonna go ahead and create that function for it
08:20which has an event and
08:23In that event object exists the actual position of our X and the page itself
08:29so if I want I could literally just
08:32Calculate the percentage right here by going and saying okay my actual page X which is the X position of my mouse right now
08:39I want to divide it by the actual target that I've in right now
08:44Which is the target itself is our to our tracer, so I'm just wrapping it with the jQuery command
08:49So I could fetch its width more easily
08:52and what I'm doing is I'm taking my X position dividing it by the actual width of my tracker and
08:58I could then trace this out now by the way
09:00This is not incredibly efficient because we're doing this every round and this value is static for the most part unless the user
09:06resizes their screen
09:08But I'm gonna keep it the way it is because maybe that user will resize the screen as they're moving the mouse it could happen
09:14So I'm gonna go ahead
09:15I'm just gonna console log it for a second to see that I'm getting a value that is gonna be between 0 and 1 as
09:24I'm traveling through so if I just save this go back click on refresh
09:28Wait for the animation to complete because only once the animation is complete. I'm actually calling it
09:33We should see that when I roll over
09:34I'm gonna see a value and when I get close close to the beginning is gonna be really close to 0
09:39When I get all the way to the end it's gonna be really close to 1
09:42So if I if I'm happy with it
09:44Which I am the next step for me is to actually go ahead and actually change the progress position of my tween
09:51So if I go ahead here and I go to my tween. Oh, I don't have access to the tween, right?
09:56That's my problem. So what I'm gonna have to do is actually create a tween object and I'm gonna make it a global variable
10:03so I'm gonna go ahead and I'm gonna create a new variable called tween pan and
10:09I'm gonna just grab that tween pan
10:12Copy it and just save that tween pan as the result that comes back from the creation of the tween
10:18when the
10:19When the tween creates itself it actually creates a tween object and now that I have a global reference to the tween pan
10:25I could go ahead and refer back to it in our on mouse move
10:29So I could go ahead here on mouse move and ask for my tween pan to change its position
10:36Just like we could ask for the progress we could even set the progress
10:40So I could literally just go ahead call the progress function
10:43But instead of only calling it with an empty parameter
10:46I could send in the actual parameter of the time that we've just calculated
10:51That's all I really need to do to get get it to kind of work
10:55But it's gonna be jittery and weird and the reason is is once we're done is because the play never stop
11:01So even though the application finished animating if I just jump to a specific spot
11:06It's gonna continue animating from that spot where it was
11:09So what I want to do is I want to do two things
11:12One is I want to tell it. Hey, don't animate anymore pause yourself to do that
11:17I'm gonna go ahead into my application here and find the position where I would do that and that would be when my animation is
11:22Done. So I'm gonna go to my tween pan and
11:25I'm gonna go to my tween pan, but really I could just set this because we're inside of the tween
11:31but I'm gonna I'm gonna set the hard-coded value tween pan and
11:35I want to set its value to be paused or
11:38More accurately pause because I'm giving a command to say hey
11:41I want you to pause right now
11:43And if I do that and save that and go back into my application and click on refresh
11:47We're gonna see that now. It's not gonna automatically animate through because I've paused it
11:53But we might have other issues and here we go
11:56We have other issues and the issue is is very logical. Let me explain to you what the issue is the issue that's going on right now
12:03It's not that we're not paused because we are but because we can't be a hundred percent
12:08Accurate in the value that we're putting in our progress
12:11The the progress function is gonna automatically call the on anim update and as it does that
12:17It's gonna change the value in here creating kind of like a cycle between these two that are
12:22Competing with each other and what I really want to do is tell my progress function
12:27Don't call that callback. Don't call this function at all. Don't don't go through the flow of
12:34Dispatching any events and by setting the second parameter in progress to be true. That's exactly what I'm doing
12:40So if I go ahead and just save this really quickly and go back into my application and click on refresh
12:45We're gonna see that now if after the animation is complete and I roll over in specific spots
12:54We're gonna see that my animation is gonna update without it fighting over the with animation tool
13:00So all that's left for me to do is also position my element here for this to be complete
13:05But really for this can be complete
13:07I would want to also affect the animation of the first element because we're only
13:12Affecting this but I really want to expect the width as well as we progress and go from direction to direction
13:18So to do that, I'm gonna create another variable here. I'm gonna call it tween with and
13:26I'm just gonna save the value that comes back from our second tween which starts from that width of
13:332800 2,000 pixels and I'm gonna basically match up the exact same thing and notice how when I'm calling this this animation has been
13:41Complete already. So it's not a problem for me to just go ahead and also pause
13:46this element as well and
13:49I could literally do the exact same command
13:53For the mouse move and we're almost almost almost done. We have only one more thing left to get this
14:00Animation effect to really take place and and work if I click here on refresh just one more time to let it run
14:05And then we'll test it out
14:08We should see this pan effect again where we have also this zoom in as we're going back as well
14:13Which is exactly what we want it to do
14:16The last thing that's left for us to do is also change our scrubber to move with us and to do that
14:21we will want to figure out a way to basically
14:25Reconfigure our position and and and set its progress again
14:29now literally all we have to do is just take this function call that we have right here and
14:34Call it again inside of our on mouse move so I could literally just take this logic that we already have right here
14:41And I could literally just go ahead here. And the only thing that I have to make sure is because this
14:47Takes a value between zero and one and this takes a value between zero and a hundred
14:51I'm going to go ahead and duplicate it by a hundred and then add to it a percentage to make sure that
14:57Our CSS knows that we want this to be a percentage
15:01So once we duplicate the value by a hundred we're gonna set it to percentages
15:05And if I save this and just run my application just one more time and let my application animation run
15:12We should see that now not only could we affect our animation?
15:15But we could also affect the flow itself
15:18But notice that we're having a really big problem here and this problem really relates the problem that right now our
15:25Animation is is kind of like not really a hundred percent working. So we're gonna have to whoo
15:30This is crazy. So let's let's try to figure it out because I wasn't really expecting this bug
15:35But let's try to figure it out together
15:37The next step that I want to do is obviously I could see that it's working
15:40But my big problem is you could see that there is some sort of a competition going on here
15:44Where it's jumping from position to position where it's not a hundred percent sure
15:48Which what it tells me is that the value that we're getting on our rollover of our mouse isn't constant
15:54So just to solve that question or to find out if that is true
15:58What I'm gonna do is I'm just gonna add here another console
16:01Log and I'm just gonna ask to get the actual percentage that we have here or in my case
16:07I'm gonna ask ask for each component separately to make sure that I'm always getting the same target
16:13Because what might be happening is that once I'm getting one target and then another time I'm getting a different target
16:18Which is changing my actual width and in other words
16:22We might not be able to work with the target directly
16:25But actually refer back to the actual object that we want, which is the actual tracker
16:30Which is not a big deal and that's probably what we're gonna do
16:33But I want to just test it out and see is that really what's creating this?
16:38uncomfortable effect so I go ahead here and save this and go back and refresh and
16:43Click on refresh here. Let the animation run for a second and I roll over here
16:53First of all that that was enough for me and I could now test here
16:56I could see are the tracker body tracker body tracker body
16:59I'm not seeing a
17:02problem in the in
17:04In the overall flow here because it seems it seems to be right
17:09But let me test it out anyways, even though I'm not seeing here the effect that I would think that would see
17:15What I'm gonna do is I'm gonna try to hard code that
17:19Tracker because that's really where we want to fetch that information from so I'm just gonna go ahead here
17:24and instead of using a relative path here, I'm just gonna go ahead and set the actual tracker value here and
17:30Now that I've done that I'm gonna go ahead and give this another try
17:34And here we go and that solved our problem and what was happening
17:46There was a competition of resources and sometimes when I was rolling over the the box itself
17:52I was getting the actual tracker body and sometimes I was getting the body itself
17:56But what I brought wanted to get was the actual
17:59What what I wanted to get was the actual tracker so by doing that and overriding that logic I got rid of that problem
18:07And by the way, it seems to me also that that was our problem
18:10Probably also in our in our on anim update and to test that we could literally just comment down the set position that we've set
18:17Here and just set this value for this one to be false
18:21And by doing setting our not for our pan because the pan is yeah for the pan itself to set it for false
18:26It will actually call this on anim update. And if I click on save go back and refresh this. Let's see if that will
18:33Actually work as well. I'm kind of interested. I'm not a hundred percent sure it will but that's part of the game of
18:39Tweaking when you get into this type of environment and there we go in here
18:42It's working exactly as you would expect it as I thought it would and it's actually
18:48Meaning that we didn't have to manually set it. Our problem was really just a problem of
18:54Literally a problem of
18:57Figuring out basically what is our actual with itself and we were actually sending the wrong value
19:02Which was creating that distorted effect earlier now the reason why I'm I personally would probably set this to be false
19:08I would set this to be true and
19:11Override it anyways is because I might have other events and I might call an event that I'm not expecting
19:16And as I know exactly what I want to do
19:18I might just call what I want to do or even call that on on an anim update myself
19:23I want to show you how to do that in the next lesson
19:27Because I want to teach you about some a little bit something that you might not know about
19:30Which is about how to change the scope of a function that already exists in this case
19:35We have a function that has a this inside here
19:37So we can't just go ahead and call on image update because if we do do that
19:42It's gonna throw an error or it's not gonna do what we expect it to do because if we call this
19:47This means completely nothing it can meet something else and this doesn't really exist because it's not the tween anymore
19:53If I go ahead and save this and just go ahead and click on refresh here and call this
19:58We're gonna see that once that animation is complete when I roll over
20:01I'm not gonna get the effect that I was hoping to get
20:04My bar is not gonna update and it's gonna give me an error telling me that the progress a function doesn't exist
20:10And the next lesson which is gonna be really sure we're gonna solve that problem. So let's see you in the last next lesson
