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

Animating the Graphics API

So far everything we created was static its time for us to take our work and make it animate we will use the graphics API and update our screen and change our visuals to create the effect of a spinning.

Video Transcript

00:02All right, so it's time for us to start animating our gradients and I want us to see how this works
00:08So first of all, I kind of did a little bit of homework
00:11I cleaned around our graphic from our last video and trying to make it a little bit more kind of spacey
00:17What I want to do next is I want to create our orb that will be in the center of our universe
00:23To do that. I'm gonna create a new shape
00:31So, let me just create that shape really quickly. We're gonna create it's gonna we called it shape
00:36we're gonna call we're gonna make it a new shape and
00:40Right away. We are going to start creating our globe. All right, so
00:46Going right into our shape and we're gonna yeah. All right. Let's do it
00:53Graphics
00:55Begin radial begin gradient film. We want to make sure that our type is going to be a
01:02We just copy that to save me a little bit of time. We want it to be
01:09radio
01:12Radial there we go. All right, so we're gonna give it a
01:15basically almost a
01:17Basically one color if I go back to one of our earlier samples to grab out of it our
01:24Color spectrum. Oh, I put in the wrong software, but not the end of the world. Let me just find it
01:30here's our color, you know, let's grab these two colors that we're actually not using and
01:36Go right back into our L
01:38Application comment that out. All right, so we got our colors. So our first color is gonna be that really really beautiful
01:45glow, I
01:47thought it was beautiful and the next one is gonna be our
01:51secondary color and
01:52Let's move on really quickly our alphas. We want to set up our alphas to be just always one and maybe
01:59Actually not maybe towards the end as the we get closer to the edge of it
02:03Maybe we want it now to be a little bit transparent. So let's give it seven
02:06So it's gonna make it a more feel like it's glowing
02:10To the next array, it's our ratios
02:12So first of all, we want our core color to be the majority of our new sphere
02:17So what I want to do with our sphere right now is I want to make sure that it's color is going to be
02:23Very dramatic. So instead of from 0 to 255
02:26I'm gonna say the beginning is gonna be like 180 and we want it to spread out to 255
02:31I have no idea if this is gonna work
02:33Well now obviously I can't use the same that that was using before but just for now just to see what will happen
02:38We're gonna do that. I'm gonna click on shape
02:41I'm gonna create our circle. So I'm gonna go our graphics draw
02:47Circle, we're gonna make sure that it's at 0 0 because we're gonna physically move it and we're gonna give it a radius of 30
02:54we're gonna just end it up by making sure that we
02:58close it and
03:01By adding it to the stage. So I'm gonna add child to our main
03:05It's gonna be this shape and I want to make sure that it's on the center of the stage. I'm gonna go in my x stage
03:12With
03:14Divide by 2 and this time around because our shape is actually a radial it's gonna automatically a circle
03:20It's gonna automatically start from our 0 0 position
03:22So we don't have to do any further adjustments
03:25All we have to do is set it in the stage in the center of the stage and that should take care of everything
03:30So if we run this just for a second, we're gonna have errors and we're gonna have to fix them
03:34Oh, right because we didn't put here our zero X before our numbers zero X before our numbers
03:41Give it another spin. Alright, so we're getting our
03:44Radial but it's not where we want it to be. We're seeing that we having issues with the Y
03:48So if I'm looking at my Y here
03:51I see that did he remind us instead of a equal because I was going too quickly
03:54And you could see how I find the bug
03:56So basically you look at what actually is happening on the stage and then go to the line that you know affects it
04:01all right, so now we know that our orb is now
04:05looks a bit off-center, but it's as
04:08Sentry as we could, you know, one thing that we could do else is if it looks for us to us or uncensored
04:12we can always just comment out and see if
04:15Our rule applied and our rule does apply so it's just an effect on the eye because of the colors and it should be really in
04:21the center of the stage. All right, so
04:24The next step for us is to actually put here a gradient that we could actually see because we're using that map that doesn't have
04:31The matrix that just doesn't have any logic to our element
04:34And by the way, we could recreate the same matrix again because you already used it and we were done with it
04:39So we could before we actually start our gradient fill
04:43We could draw our create
04:45Gradient bugs again
04:46We don't have to create a new one and this time around give it a width and height that is closer to our actual
04:52Element, which is going to be 60 by 60 because that's the size of our element and we could leave the rest in its default
05:00If we run it again, we should be getting a much better picture here
05:03And we could definitely see that our circle is starting to pop up right here
05:07But we want to push it up a bit more and you can see already that our globe is starting to look a little
05:12Bit more like some sort of globe which is kind of cool
05:17Now just as the looking of this makes me feel like I would love for this globe to have some sort of glow an
05:22External glow and we know that didn't know we know that didn't flash itself and we're gonna look at those samples in a minute
05:29Or actually more like five minutes cuz it's not gonna be on this video
05:32We could create glows. How do you create glows?
05:34So first of all not everything could have glows and I don't remember maybe how if I could add a filter to an element
05:42like a shape and I see that I could and
05:44Basically, it's dot filters, which is an array and we just provide an array of filters into it now to provide that array of filters
05:52Because again don't remember it by heart. I don't need to I could go and import it
05:56It would probably be in my flash now. I have to think twice to figure out where maybe it's in my display
06:00And I could search for a filter
06:04So I know let's say that there's a blur filter
06:07So the blur filter isn't here. That means it just can't be inside of the display package. There must be somewhere else
06:12So I just scan through this quickly. I'll see that it doesn't have anything to do with that
06:17So it's not going to be in the display. So maybe it's going to be in filters
06:20But there we go, and we want to use a glow filter
06:27Alrighty now
06:29Just to keep it condensed. So we imported our glow filter. We didn't have to but I didn't remember where it was
06:34So I did it
06:36So I'm gonna create here a new glow filter just for a second glow filter and the colors
06:48I want to give it I want to give it a color that's gonna be really radiant
06:51How about I just take the color that's right here the color that we're rating using for the globe in
06:59Well, maybe the internal color which is lighter, I believe no the internal color is darker
07:05All right. Let's just give it a try. Although for me. I'm kind of thinking almost maybe we just go all the way white and
07:11And see how it works instead of giving it an alpha of a hundred percent
07:15Maybe we give it a half of half of that
07:17Do we want to blur it the default is six that sounds about good to me
07:22The strength the quality always sounds about right to me
07:25So I'm just gonna keep it the way it is obviously you could play with it and then see how it works for you
07:28And obviously the last thing you have to do to remember is to put your filter in and you could have skipped the creating
07:34Of the variable by just grabbing this and putting it into your array both work. There's nothing wrong
07:39It's really just a matter of how you want to build it out and how you want it to look like it doesn't really matter
07:44So now we can see that we have here a little bit of a glow that I would love for that glow to be more
07:49Dramatic larger. So if I'm looking into this first of all would to make it more dramatic
07:53maybe let's notch it up to 75% and maybe
07:57Make our blur even more
07:58so one thing to note is by the way the best type of numbers to put for blurs and her
08:03Filters in general is duplications of two it makes it run smoother and makes it run faster
08:09So basically 2 times 2 is 4 times 2 is 8 times 2 is 16
08:13If you could live with 8 or 16 and not pick a number in between your application will run smooth more smoothly
08:19So I'm gonna pick 8 and see how that works out
08:23So I'm already much happier, but I want to check and see how does 16 how would 16 work?
08:28So I'm putting here 16 and I'm really happy with the result
08:32Okay, the one thing I would only do is maybe notch down now the alphabet to make it less dramatic
08:38So I'm gonna cut it down by half. So I like that glowiness of it
08:42I still really like the glowiness of it
08:44Maybe the color isn't perfect, but just by notching down the glow the alpha of it
08:50It blends it with the background so the color isn't as important
08:53Although it is, but I'm not gonna worry about it too much. Let's just put it three that works for me
08:58That's what we're gonna put it at and we'll take it from there
09:02Alright, so we got our glow now our shape looks really good
09:05Our next step is actually to start animating our globe now to do that
09:09We're gonna have to do a few changes in our application
09:13But let's start off. Let's first start some sort of animation. So let's start by creating some sort of time out
09:18I'm not gonna use the timer
09:19She's the quickest thing because I know it's gonna run forever so I don't have to worry about killing it or terminating it ever
09:25It's only gonna run once so I'm gonna set an interval and I'm gonna call a new function which we're gonna call update globe and
09:33Update globe is gonna happen every 50 milliseconds
09:38All right, so update globe happens every 50 milliseconds. All that's left for us is crew to create that function
09:44I'm gonna create a private function
09:46If you don't know what that is and just create a function or create a private one because we're not talking about that topic yet
09:52And don't worry about it. If you don't know what it is. It's not critical. You could always just not right there anything
09:58There's no nothing passed in and we're ready to start figuring out. What do we want to do every 50 milliseconds?
10:05So let's look at what we want to animate. I think it would be really cool to actually animate the gradient itself and
10:11What better thing to animate in the gradient?
10:15More than or
10:17The actual values of the spread itself or to animate the actual mat
10:23So one thing that we could do is we could start figuring out and saying what do we like doing more?
10:28We could do both
10:29So before I do the interval I'm gonna look at it right now
10:32Just in a freeze frame is how it is right now and I'm gonna pick a drastic different scenario and see if I like it
10:38So if we look at our create gradient box and we have your rotation, let's see if our rotation changing the rotation is
10:46Interesting and don't forget that we still need to work with degrees
10:49So I'm gonna take that degree that we saved earlier and let's duplicate it by 90 and let's see if it's a very dramatic change
10:57Or not really and I don't even see a change to be honest
11:02so I'm not that keen on that we could always push around our our
11:11Transition of our XY so in our scenario our X what would happen if we would push our X maybe to be minus 40
11:17So we're definitely seeing a much more interesting change and what would happen if we change our Y also to be negative
11:24Let's say minus 40
11:27That is actually really cool. So basically by changing the values of our transitions here. We're
11:34Creating a really interesting animation. The only problem is is at some point if we put here a value
11:39That's too extreme or it gets a value that's extreme
11:42We're just not gonna see anything which is not that great for us and we have to figure out a way
11:47When we get to that max maximum extreme value to be able to push back
11:52So my bet my guess is is that we get to minus 60 minus 60
11:57We're gonna need to start our way all over again. So if I select here 60 and 60
12:03I have a feeling that okay, we're almost out so not yet, but we're very close to it
12:09My get my guess was not that far away. So if we maybe we push it down to now minus 80
12:15Minus 80 we were definitely out
12:17So let's see what's my minus 70 if we're still in because end of the day
12:21Even if we don't see anything for a bit and then we see something it actually isn't true
12:25We actually could see a little bit of a change there
12:27I am a big fan of actually going all the way to minus 120
12:31Because minus 120 imagine that or the earth is roaming around the Sun or the light source is roaming around and it's taking it
12:38A while until the light comes back. Now, the big question is is where do we start our animation again?
12:43We have to then afterwards jump this number a lot forwards because when it was at zero zero
12:50which was where we were at or when we when we're gonna start the animation is
12:57We already could see the light coming in from the other side and we would want it to start when there's actually no light
13:01At all. So let's push it forwards until we don't see the light anymore. So let's try
13:0630 so first of all, we probably should write an annotation for us that we want to go from minus 20
13:11That our range is minus 20 and 30 is definitely working
13:15So let's try a lower number such as 10 and and as you can see here, I'm not doing mathematics here
13:20I'm just doing just checking things to see what works
13:23So I could see that 10 is in that range if I put your 5, let's see if it's in that range
13:28So we could already see stuff at 5. So 10 is our as our magic our magic number
13:33So basically we want our animation to move from 120 to 10 to minus from from the number
13:39It is to basically go down go down go down until it gets 100 minus 120 and when it does just jump right
13:46Right right to the beginning. All right. So now that we know that that's what we want to do
13:50All we have to do is create a global variable for in our fun in our class
13:54So I'm gonna you know, let's just keep it all nice. So all this should be private
13:59So I'm gonna make it private and again, don't worry. Don't know what private is just ignore it
14:04All right. So variable our variable we're gonna call it
14:10T for transition it's gonna be a
14:14integer and
14:16Its default initial value is gonna be zero and our T value
14:23We're gonna set that T in as soon as we create it. That's gonna be instead of zeros from now on and
14:29Now our range we're gonna control right inside of our update globe. So in our update globe, I'm basically just gonna ask your if
14:36T is smaller or
14:39Or equal well, that's greater if it's smaller or equal to minus 20 120
14:45Sorry, then make T to be equaled
14:4710
14:49That's all I had to do now
14:51I could go through and basically redo almost everything inside here each time
14:55So what I would actually want to do is I want to create that mat again
14:59And because I want to create that mat again
15:01I have to make sure that it actually still exists when we're in that function because we're gonna reuse it over and over
15:06It's time for me to actually save it out and actually just grab this matrix
15:11save that definition of the matrix outside of in my class level outside of that function and
15:18Clean up my code here making sure that I'm instantiating that matrix
15:25We like putting variables in the top so I'm just reordering reshuffling stuff
15:29All right. So now we have our matrix, which will be available externally beautiful
15:34We're creating the shape only one so we don't have to send that out. We are gonna recreate our box each time
15:39So we want that we want to create a gradient fill
15:43We want to create all this good stuff that we've been doing before
15:47There's nothing here that we need to worry about but we have to recreate it
15:51we don't need to add the child again to the stage nor do we have to reposition it again and
15:56Our glow for now, we're just gonna keep it static for now
16:00so let me just cut this out put it right in here and
16:06Just for the sake of our example. I'm just gonna come here in line 23 where I took it out
16:10I'm just gonna call a function to make sure that it draws out now
16:14I'm only gonna have one problem that each time is gonna draw on top of itself and on top of itself
16:18And it's never going to clear
16:20To fix that issue
16:21All I have to do is call my graphics and call the clear function which will basically empty the slate each time and each time
16:28We'll be redrawing it from scratch and the last last thing that's left now that we have an interval going on and we moved our
16:35Creation of the shape out is to change our T each time now
16:40We're only changing it if it's in the extreme value so all I have to do is subtract one out of T
16:45Each time it's called and let's see what happens. It's gonna be slow, but it hopefully will be interesting and let's run it. Oh
16:54Let's just fix here whatever issues we have first of all we have to import the set interval
16:58That's what it's telling us. So let's fix that. So we're gonna go right into our import flash utils
17:06Set interval
17:08Act as an undefined property degree very true. So we have to make sure that our degree property which we created
17:16Let's see. Where did we create it? Let's make the screen larger and
17:22I'm finding it hard to control my mouse. So if we find our degree, which is right here, which we kind of need it globally
17:28so I'm just gonna grab this degree cut it out create another private variable and
17:35Just save it as an external variable so it could be used throughout all the different functions
17:39So now we have the degree and hopefully we won't get any errors and let's run this again and see if we get any errors
17:44We'll just fix them. And there we go. Our globe is now
17:47Going a full cycle and if we wanted to animate faster or slower
17:52all we have to do is now all we have to do is just
17:57Change the pace of our set interval so we could see that it finishes the transition
18:01Now we have to wait a bit and after we wait a bit it starts cycling again. I'm really happy with this
18:07I'm actually really really happy with this. It's looking really good
18:11Download the source code. I'm quite sure that in a video it doesn't look as good as it's looking right now
18:16And we're not done which is really really cool
18:18Our next video is going to be smashing and I think you're gonna really really love our next video
18:22So definitely definitely don't forget to check it out

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!