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

Exploring the DirectionalRotationPlugin

Now that we have a plugin architecture in place, it's time for us to return back to explore more plugins. In this lesson we'll move our attention to the DirectionalRotationPlugin. The DirectionalRotationPlugin enables us to animate rotations at ease with a lot of baked in goodies. Throughout this lecture, we will explore working with DirectionalRotationPlugin.

Video Transcript

00:00Great news, in this chapter, we're done with complicated topics, although my sentences
00:06have become really complicated.
00:08This lesson is going to be about rotations, we're going to see how could we use the directional
00:13rotation plugin, a very friendly and useful plugin.
00:17Not hard at all.
00:18Let's see what we did.
00:19It is the stuff that excites me.
00:21We just did something so cool, I know it's a bit complicated, but really cool.
00:25We just built a plugin architecture.
00:28Now that we're done with that, let's jump right in.
00:30I want to introduce another plugin.
00:33Now, jumping into the Green Sock library, what I want to add this time around is I want
00:38to add the directional rotation.
00:41Go ahead there, select the directional rotation, I have to say that really slowly.
00:50Select that, copy the file, again, just monitor that your file size is growing slowly, slowly,
00:55so now we're already at, what are we at, 15, 16, 17, getting close.
01:01We're getting close to that size limit.
01:03All right, so back in our code, just make sure that you add it.
01:06I think I've done that already.
01:08Yep, I did.
01:10I just add the script into your code.
01:12Once added, let's jump in the JavaScript area and let's start having fun.
01:17As continuing from our last lesson where we created those triggers, I want to add another
01:22trigger, but this time around, I want to add it to our last item in our animation sequence,
01:27our third item, and I want to animate the whole thing here, the whole shebang and rotate
01:31around using the directional rotation.
01:34Now, you could do this without it, but with more code.
01:37This is saving code, which is always, go ahead, why am I talking like a robot?
01:42Let's continue.
01:43All right, so I'm just going to copy that, add triggers, and I'm just going to paste
01:46it again.
01:47This time around, I want to talk about our third item because we're starting from zero.
01:52I'm going to just make sure that I'm going to call the on slide three enter, on slide
01:57four enter, on slide four, what am I saying, four, on slide three enter, on slide three
02:02exit.
02:03I'm going to go ahead and just duplicate this function, two functions right here, and
02:10we're just going to rename them and make sure that they're named three right here
02:16and three right here, and I'm just going to delete this content right here, and let's
02:21start building up our animation.
02:22This is incredibly easy, incredibly simple.
02:25What we're going to do, step one, is what I want to do is just tween the actual content
02:30of the slide and have it animate, so I'm going to go ahead and just call the tween
02:34light two, and the item that I want to animate is actually the third slide item, so I'm
02:40going to go ahead there and just find the actual, you see those classes of slide,
02:45here we go slide, so I'm going to ask for the slide and then grab the third one, or
02:50actually the fourth one technically speaking, so let's do that.
02:53Let me just send in the slide, and then I'm going to take the third, the fourth item
03:00in the slide element and send that into my tween.
03:03Next step is I need to define also my amount of time that I want to animate, I want it
03:08to animate for one second, and last but not least, I could, and by the way if you
03:13decided you want to do the same animation for all the different elements, you could
03:17very easily just instead of that, put in the dynamic attribute, and then theoretically
03:22we could have added it to all of our elements, in my case I'm just going to
03:25animate that third slide, but that's why we're sending the index, because we might
03:28want to do similar animations, but just have no who are we animating, without
03:33needing to hard code it inside.
03:35Okay, so it's time for us to figure out what we're animating, and I'm just going
03:38to copy, I'm going to type the directional rotation copy, directional
03:45rotation, and I'm going to say what do I want to animate, I want to animate it to
03:50let's say a hundred and eighty, to the value of a hundred and eighty, I want to
03:55add a hundred and eighty, so I'm going to just put it in a string and say hey I
03:58want to add a hundred and eighty. If I save this, go back into my application,
04:03click on refresh, go to my item right here, go to plus 180, we'll see that our
04:08item should work, theoretically, let's see what's going on, let's see if we did it
04:15here, maybe I typed here something incorrectly, let me just copy paste the
04:18actual copy, so I don't have your typo, in case I do have a typo, there we go, I
04:24had a typo, help me of me, because I can even recognize my bugs before they,
04:29before I even need to think. So notice how quickly it animates, obviously that
04:33was probably a little bit too quick, so what we're going to do is we're going to
04:36go back into our code and just add a delay, so we'll be able to see it as well,
04:39and as we know it's going to take a second for our animation to get to this
04:43position, I'm going to add here a delay of one second, and just refresh it one
04:49more time, and now we have our directional rotation, and it happens right
04:55away. Now another thing that we could do is we could animate it in a
04:59negative direction, all we have to do is just change the value to be minus, and
05:02it's going to go in a negative direction, so if I go right here, click on refresh,
05:07it's going to animate in a negative direction. Now what if I wanted it to
05:13animate to minus 180, but I wanted it to go clockwise and not
05:20counterclockwise as it's doing by default, to solve that all I really need
05:25to do is go into my code and add the value here underscore clockwise. Now by
05:31setting underclock underscore clockwise is going to tell the OS, it's going to
05:36tell not the OS, what am I talking about, it's going to tell tween light hey I want you
05:39to animate in the direction of a clicking clock, so if we do this and
05:43we click on refresh one more time and go to our element, we'll see that this
05:47time around it's going to do that flip in the direction that we want it to go.
05:51Now obviously this isn't exactly the animation we want to have, so let's create
05:56the animation we really want to have. So I'm going to continue on and what I'm
06:00going to do is initially I wanted to do a hundred and I want to do a 360 degree
06:05flip, so I'm going to go ahead and say plus 360 degrees, I want it to go
06:10clockwise, so I'm going to set it to be just very directly that I want it to go
06:14clockwise. Now the next thing that I want to do is I want to I already have
06:19a delay, so the next thing that I really want to do is I want to auto in general, I
06:24would love to be able to add another layer to my animation, so how about I'll
06:29continue this animation style, but I'll do it also for the internal component
06:33inside here, which was the image itself, so instead of that I'm going to approach
06:37the image of that index and this time around I'm going to do the exact
06:42opposite, I'm going to go minus 360 degrees flipping around. So if we run our
06:47application one more time go back into our application, click on refresh, whenever
06:51we go into that element we should have like a double flip here. Now this is a
06:55little bit too smooth, I would like this to have a little bit more time so the
06:58user actually could see it, so to do that I'll go back in there and just add a
07:02slightly longer delay making sure that these animations overlap with each other
07:07a bit, so they're a little bit more noticeable. So I do this one more time
07:10and I go back there one more time, we'll see that this element continues
07:13flipping a little bit more after the animation completes. Now we're almost done
07:18but we do have a couple of more issues, for one if I go back to my element in the
07:22first element and then I travel back to that first element the animation is not
07:26going to happen anymore. Now the reason is very simple, although the animation is
07:30being called because I set an actual explicit value when I do plus 360 I'm
07:36just saying 360, when I say minus 360 I'm basically saying go to position
07:42minus 360, but what I really wanted to say is grow by 360 degrees and for that
07:48I'm going to put there a plus equals or minus equals which will basically make
07:52sure that it's growing or getting smaller. And once I do that if I go back
07:57into my application and click on refresh this time around when I go to my element
08:01and have it animate it's going to animate through once I go out of it
08:05and go somewhere else and go back right into it we're going to see that it's
08:08going to animate through again without any issue. With that said we are going
08:12to have a bug and I want to show you that bug if I go into my element and snap
08:15out and snap in really quickly and I go in and go out go in go out go in go
08:20out go and then come back we are going to have an issue it's not going to end
08:25up where we hope for it to be and the reason is very simple is because every
08:29time we reset our tween we're resetting its starting position and we
08:34want to fix that and lucky for us we created a slide exit as well so all we
08:39really need to do is just copy this tween that we have here going on and I
08:44could literally just say hey we're not going to do any delay we're going to
08:47immediately action we want this animation to be relatively fast so I'll
08:52just 0.4 to make sure that it gets its final destination really quickly and all
08:57I have to do is just set here the value of 0 and I don't really care in
09:00what direction it goes or I do if I do then I'll leave it this one is
09:03counterclock in clockwise and this will be in counterclockwise and I'll
09:07just make sure that I'm setting the value 0 to send it to a hard-coded
09:10position to where it was originally so if I save this and go back to my
09:14application and click on refresh then even if we go in and out a couple of
09:18times in and out in and out in and out and go to somewhere else and come
09:22back in our animations should end in the right position
09:25stupid it should have but it doesn't how about that so if we go out and we
09:32go back in oh and I know exactly what the problem was so when I'm doing
09:37this and I'm going in and out in and out in and out and I'm walking out and I
09:40come back in before that animation completes it's not going to reach its
09:44final destination because it was actually in the middle of the animation
09:47so I have a few options here or I could literally go ahead and in my in my
09:53editor basically say every time you start this animation make sure you
09:58reset even if the user didn't finish that original animation in other words I
10:02could literally say let's go to this tween light element and I'm gonna say
10:06set I'm gonna go ahead and I'm gonna set this element and I'm gonna literally
10:13just say hey just to be sure that you know you should be starting from position
10:18zero and I could literally do the same auto for our image so let me just do
10:26that quickly and what this will do is gonna basically force us to start from
10:33a specific position before we start animation or animation or we could have
10:38if we wanted to hard coded a value here that's not relative but basically a
10:43full value but then again you saw how we had that issue where our animation
10:46didn't follow through so this time around if I click on refresh and we'll
10:49give this really a real a real run for our money and we're gonna run this a
10:53really long amount of time go somewhere else and go back in before that
10:56animation completed then that didn't help us but oh there we go to the
11:03second time around it did work but for some reason when we went out and came
11:06back in originally it wasn't good enough one time let's see here let me do
11:15this let me one in one in and out and this is this is this is debugging 101
11:19and this is debugging 101 where you want to make sure that this is
11:22completely solid in our case it's not so we're gonna have to go back and try
11:26to figure it out so for one is I don't really need this animation to be that
11:30long so I could go ahead and put your much faster animation on top of that
11:34what I could do on top of that what I could do is basically because this
11:38should have solved that issue it really should have but really because we're
11:42animating a CSS attribute that's called rotation I could literally just go
11:46directly to my CSS and just make sure that I'm setting my rotation to be at
11:52zero zero degrees you know this is a little bit of a workaround but sometimes
11:59these stuff are arch you just have to figure out a workaround now what might
12:03be happening is that even though we're setting our slide here that this
12:06animation might be still going on so really the real solution that we should
12:11put here is also kill that previous animation but we're gonna look at that
12:15in a future video we're not gonna go into that right now because we want to
12:18stay on the topic of plugins but we're gonna go back and solve this problem
12:24even if we don't fully solve this problem right now I'm just gonna give
12:26this a little run on its money and see if that will solve the problem and if
12:31it doesn't I'm not gonna even fix it because the way I want to fix it is by
12:34basically saving a reference to the animation or killing all animations of the
12:39slide before we move on and create our next thing which we're not doing that
12:43right now so I'm gonna go ahead and just save this click on refresh and
12:48hopefully this time around it will work and if it doesn't don't worry about it
12:51we're gonna get back to this in a future chapter so again we're gonna go
12:54into the element I'm gonna jump around and really really really make it run
12:58crazy and if I go back in here and go back in here we should be working
13:03and it's working perfectly now obviously I am not in love with this animation right
13:07now because it's snapping every time I roll over which I don't personally like
13:11now a personal preference of mine would probably be it's to go inside of our
13:16element right here and instead of instead of resetting each time is making sure
13:21that once our animation is complete to add here an on complete and on on
13:26complete basically reset our value back to its position or animate it really
13:30quickly to the position that it's meant to be so that's it we're done with this
13:35topic now there's a you know let me give you a little lagger here another small
13:39small tidbit about the directional rotation by default it rotates the
13:45rotation attribute but you could very easily go ahead and wrap it into an
13:50object and then animate other things that are rotationable such as the
13:54rotation X now I don't know if this is gonna look very fancy or very cool
14:00and I don't really care if it's gonna break our slide but I just wanted to show
14:04you that you could rotate the X the Y the Z whatever element you want to rotate
14:07and it'll go in this radian of this these degrees you could also set it you
14:12go into the documentations if you want to do anything further than that and
14:16without further ado let's just see what actually happened from that animation
14:19and let's see if it actually is so yeah that's kind of cool right I wasn't I
14:25wasn't expecting that to be that cool I love that awesome all right so we're
14:29done with this lecture see you in our next one when we will explore another
14:33plugin

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!