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

Building Gradients using The Matrix class

Create Gradients using The Matrix class learn the basic operations of the Matrix class and see it in action.

Video Transcript

00:00Gradients, or gradients, or gradientos, I think.
00:05I have no idea.
00:06All right, so let's start creating gradients.
00:08So to create gradients is actually really easy,
00:10although I spent probably over an hour
00:12breaking my head over an issue
00:14that I didn't notice that I was making.
00:16I'll show you what issue I actually made
00:18so hopefully you'll notice it and you won't do it yourself.
00:21But that's what happens when you don't do something
00:23for a while and then you do one wrong thing,
00:25you could sit there really driving you crazy for a while.
00:28So let's first see how we do it.
00:31So the first thing I wanna do is I just wanna create,
00:33let's start off with just my goal
00:35is I wanna create a nice background
00:36that's gonna take the whole screen.
00:38Now I want this background to be a gradient.
00:42All right, so what I'm gonna do
00:43is I'm gonna approach my object directly
00:45and I'm gonna start beginning a fill.
00:47But instead of choosing to begin a fill
00:49in our graphics object,
00:51I'm gonna decide to do a begin gradient fill.
00:58Begin gradient fill.
01:00Now the type is a little bit tricksy.
01:02So what I'm gonna do is I'm gonna import
01:04the different types that are optional.
01:06There are basically two types that are available.
01:08You could find the package and by the way,
01:10it's just a string but Flash has been doing this,
01:12Adobe has been doing this since ActionScript 3
01:14where basically creating classes
01:17that have static properties that basically just enable us
01:20to grab the copy involved with them.
01:22And then basically if you search for gradient type
01:25inside of our flash.display,
01:27you're gonna find the gradient type.
01:28Now that we have the gradient type,
01:31we could pick between the two options
01:32which is basically linear or radial.
01:34We're gonna start off with a linear gradient
01:37and we're gonna look at a radial one right after them.
01:40All right, so creating a linear gradient
01:47which basically means it's gonna be a straight line
01:49and the way the straight line works by the way
01:51is from left to right.
01:54So whatever colors we pick are gonna align themselves
01:57from left to right.
01:59Not obviously but I guess you could assume
02:02that with the radial on the other hand,
02:04it would start from the center spreading outward.
02:07Now the next thing that it would ask for me
02:09is for a color.
02:09So I'm gonna deliberately just pick some sort of color.
02:12Let's say some sort of green of some sort.
02:15So RGB, red, green, blue.
02:18So I'm gonna skip the red,
02:19go right for green like some sort of darkish type of green
02:25and that will be my first color.
02:28My second color, I'm just gonna take green all the way.
02:32So I have here an array with two colors.
02:34Now the next element that will ask for me
02:36is to give it its alphas
02:38and now we wanna make sure
02:39that we're putting the same amount of data.
02:41So if we pick two colors,
02:42in our case, that's what we did.
02:44We're gonna set the alphas to be one
02:46because I just want them to have a full alpha.
02:48I don't want them to be transparent in any way.
02:51Now the ratios, the way that ratio works
02:53basically says give it a value
02:56between zero to 255.
03:00If you remember our numbers in our hex values
03:04were from zero to 255 as well.
03:09So there's a very deep correlation
03:11between our range of color spectrum
03:13that we're gonna show on the screen
03:15and that range of color
03:16that exists inside of that hex values.
03:20All right, so one thing to note here
03:23and one thing that's important
03:24is what we're basically deciding
03:26is the star point and the end point of the color.
03:29It actually doesn't define
03:31how it's actually gonna look like in our screen.
03:33It just decides this information
03:35that would then be connected
03:37to something that we have to talk about next.
03:39But basically if I say that I wanted to go
03:40from zero through 255,
03:44what I'm basically saying is I want to start off
03:47with our original color
03:49and end off with our other color.
03:51So in theory, we wouldn't even see the color.
03:53We would only see the transition,
03:56the translucent, whatever you wanna call it,
04:00of those keywords, of those colors.
04:02Now, one thing to note is that the matrix
04:04although is not mandatory is really, really needed.
04:08Now, if we run our application now
04:09and we're gonna look at it,
04:11it's not even gonna look like,
04:12oh, we need to also do something, right?
04:15So begin gradient fill.
04:16Now we actually have to draw something.
04:17So we're gonna draw that rectangle
04:22and I'm gonna make sure that it starts at zero, zero.
04:24And again, I'm just gonna take my stage width,
04:26stage, stage height,
04:30and I wanna make sure that I end my fill.
04:34And if I run this now, we're gonna see
04:36we're seeing a gradient,
04:37but it's not really what we were hoping to see.
04:40We would wanna see that full spread out.
04:41It looks, doesn't even look close
04:44to what we would hope for it to look like.
04:47All right, so let's start looking into the matrix
04:49and that's where I kind of got lost by mistake.
04:52All right, so let's see how this works.
04:55First of all, I wanna create a new matrix
04:57and the matrix is basically just like information
05:01for our class that we're creating,
05:04for our gradient that we're creating
05:05that basically enables it to know
05:07what information to expect.
05:10And don't worry about filling this information.
05:12It's really complicated.
05:13It's not easy to even understand it.
05:15So just skip that part
05:17and just go directly into the matrix create box.
05:20Now this box is basically,
05:21and we're gonna see how this works right now,
05:23basically gives all the parameters for our gradient
05:25to be able to work.
05:26I by mistake when I was working went for create box
05:29when I actually had to go for create gradient box.
05:32Creating box, I played with it and played with it.
05:34I couldn't understand why it wasn't working.
05:36It wasn't working
05:37because I didn't pick the right type of box.
05:39I needed a gradient box.
05:41And now when I pick the gradient box
05:42it enables me to input into this gradient box
05:45the information that I wanna put on this gradient.
05:48So let's say for example,
05:49I want my gradient width to be a hundred by a hundred.
05:54I don't want it to have any rotation.
05:56I don't wanna transition it to the left or to the right.
06:00And we're gonna see how that works in a second.
06:02Now the next step,
06:03last step that's actually left here for me to do
06:05is actually to go right into that third parameter
06:08and just pass in our matrix.
06:10I'm gonna ignore the other parameters
06:13although they have a lot of deep meaning
06:14but for our getting started working with
06:17the API, no point in throwing too much information.
06:23So obviously I encourage you to look more into it
06:25or even ask us questions such as in the comments
06:27and we'd be more than happy to create more videos on it
06:29kind of off topic and maybe even throw them up on YouTube.
06:32So definitely ask if you wanna know more about it
06:35and you already played with it
06:36and you want more information.
06:38Very, very happy to make those type of videos.
06:40All right, so let's check this out.
06:42So if I run this out now, again,
06:43we're gonna see that our gradient
06:45still doesn't look like we want it to look like
06:47but it's maybe starting to look a little bit closer.
06:49Now, if we go to our create box
06:52and we input it into it instead of that hard coded values,
06:56the actual width of our stage
06:58which is the width of our rectangle
07:01and we run it one more time,
07:02we're gonna see that it's gonna change.
07:04Now we see our gradient is gonna take over our full box
07:07here and now we don't really have to worry too much
07:10about the height on the stage
07:13but just for the sake of logic,
07:15if I'm already doing that,
07:16I'm just gonna set it to be the exact size of my box.
07:19But if, for example, I wanted the gradient
07:21to be everything minus 100, for example,
07:23then I'm gonna see that my gradient is gonna end earlier
07:26but I'm still having a much longer gradient.
07:29So that basically what enables me
07:30with the create gradient box and notice that
07:32and don't do that mistake as I did
07:34of going for create box when you're working
07:36with a gradient, all right?
07:38So those are the first two parameters
07:40of the create gradient box.
07:42The next parameters which are really, really cool
07:45and let's look at them.
07:46The next parameter that's really cool is rotation.
07:48So for example, if we wanted this to have a rotation,
07:52we could put a value and see what happens.
07:55Now, one thing to note is that these numbers
07:58usually in Flash and I don't know it by heart right now
08:01because I didn't write that in my cliff notes.
08:03I didn't prepare for it.
08:04I didn't think I was gonna talk about it at all.
08:06Flash usually works on radians.
08:08It doesn't work on, although there's degrees
08:10of between zero and 360 and you might think that
08:14the best way for you to test this out is in our scenario,
08:17let's make our colors more extreme.
08:19So I'm gonna change that to be red and green
08:22because I wanna be able to see very clearly
08:23where our directions are.
08:25I'm gonna keep it at zero for a second,
08:27run it for a second or obviously I could look
08:29at the documentation and now I'm just gonna go
08:31and type here one and I wanna see
08:33if there's any real difference when I go from zero to one.
08:36When I go to zero to one, I see
08:38that there's a very dramatic difference,
08:40basically telling me that my thoughts
08:42about what I thought earlier were absolutely wrong.
08:47We can't put here a number between zero and 360.
08:51If I put a small value, I'm gonna see that still
08:54I'm seeing dramatic differences
08:56and these dramatic differences are indicating to me
08:59that we're working with our radians or degrees.
09:02So we're gonna have to put your actual degrees
09:04and to do that, we could use the math dot pi
09:07and divide our pi, which basically,
09:09because if you remember from basic math,
09:12how do you find the amount of info
09:15that you have inside of a circle, two pi r.
09:19Remember that it's just like one of those famous things
09:21that people remember, two pi r.
09:22Basically, the r is the radius and the two pi is the,
09:27how much line, like if we draw a line
09:30all around throughout the circle is our two pi.
09:33And that gives us that whole 360 degrees.
09:36So in our case, because we're using only one pi,
09:38we wanna divide it by 180, that would give us one degree.
09:42And now that we have one degree,
09:45I could take this value
09:47and maybe just to make it a little bit easier
09:48to see on the eyes, I'm gonna write here degree.
09:53Degree, it's gonna be a number.
09:55And I'm just gonna take that math pi divided by 180,
09:59just so we could see that this is one degree.
10:01And I could take this degree
10:03and let's say duplicate it by 45
10:05to see if we're getting a 45 degree
10:07or even before, let's take a smaller degree,
10:09a 10 degree and we could see
10:11we're definitely getting here 10.
10:13So let's try now 45 and see if we're getting a sharper angle
10:16and we're definitely getting a sharper angle.
10:18And if we try 90,
10:21we should be getting our line now turning around.
10:24That's exactly what we hope to get.
10:26So now we know that this parameter works with degrees.
10:29We could have also, if we wanted to go
10:31onto the documentations and search the document
10:33and try to figure out,
10:34unfortunately, when we saw our hints right here,
10:37the hint wasn't good enough to actually to tell us
10:39that the rotation was a type of degree.
10:43But in flash, sometimes it really depends on the situation.
10:46Not that hard to test it out.
10:47Degrees are very simple after you just know that fact
10:50that a full circle is two pis.
10:53So one pi that we get in our math class,
10:55we just divided by 180, we got one degree.
10:58Now that we have one degree,
10:59we could decide how much we want it to be at any point.
11:02All right, so we're done now with the linear
11:09and the round one is gonna be,
11:10the radial one is gonna be so similar.
11:12So all I want to do right here
11:13is I'm gonna comment that out
11:15and I'm just gonna create a,
11:18instead of a linear, we're gonna create a radial one
11:20and this time around, we're gonna use a lot more colors.
11:23So to help me out, I actually saved a few colors
11:26and I found a picture of outer space
11:29because I wanted to use the colors
11:30that are using outer space to create that radial effect
11:32that will hopefully give us a feeling of stars.
11:36And let's see if that happens.
11:37I'm gonna open up my text editor that I have here.
11:40I saved here a couple of colors.
11:42I'm gonna copy them,
11:43which I definitely can't remember
11:45these type of stuff by heart and I don't even want to
11:47and I don't wanna pretend that I have a cliff note,
11:50because basically all I did is took an image
11:53and then just cut out of it,
11:55just picked out of the colors
11:56based on the edges from a real photo.
11:59That is gonna make it easier for me to pick colors
12:01that have a real relationship between each other
12:03because anything that's in nature usually looks beautiful
12:07and outer space is definitely one of those beautiful things.
12:12All right, so we have your four values.
12:13Now that we have four values,
12:14we wanna make sure that we're picking four elements here.
12:19This time around, obviously we need to give more information.
12:22Here, zero to 255 is just not gonna work.
12:25We don't really have to change anything
12:26in our mat right here.
12:30And because it's a radial,
12:32it doesn't really give us too much of importance.
12:34So what I wanna do here is first,
12:36because we have four, we have to give four.
12:37So zero is gonna be really our pivot center point.
12:40And we wanna kinda give it a bit of a range.
12:43So for the initial color,
12:45I wanna give it something like,
12:47let's say it starts at zero.
12:49Now the next color, to transition into the next color,
12:52I wanna give it, let's say 40.
12:55Now to the next color, it's going even further out of way.
12:57Let's say I wanna give it 100.
12:59All right, so I have here a few colors
13:00and obviously I could change that out after I see it.
13:02So let's give it a run, see how it worked.
13:04The most important part is making sure
13:05that if you have four here, you should put four here,
13:07you should put four here.
13:08So every array should have the same amount of values
13:11or you're not gonna get what you were expecting.
13:14So let's give this a run.
13:15I basically did the exact same thing,
13:17used the same mat, the same matrix.
13:20And you can see that our values are coming together,
13:22but it's the exact opposite of what I would hope
13:24it would be.
13:25So let me just switch the colors up now quickly
13:28because actually I wanted to start off with this color
13:31and then transition into these colors.
13:38And last but not least,
13:44and let's get rid of those extra commas.
13:46Let's run this and just see if it's starting to get there.
13:48And it's definitely starting to get there,
13:50really ugly, way more ugly than I would hope it would be.
13:54But let's try to clean it up a bit.
13:57So first of all, I wanna start this off
13:59at maybe give us a little bit more space here,
14:02condense this a lot more,
14:05condense this a lot more as well,
14:09and condense this even more.
14:12So I want a lot of emptiness,
14:14a lot of the same exact color that's right here
14:17to take us throughout our screen.
14:19Let's give this one more run.
14:20And now we're getting way closer.
14:22Our colors might be not right,
14:24but we're definitely in a much better condition
14:27than we were earlier.
14:29All right, the last thing that I wanna do
14:31is before we end up with this one,
14:33we might decide to take one color out if we wanted to.
14:35But the next thing that I wanna do
14:36is I actually wanna go here
14:38and adjust a couple of small stuff.
14:40So first I wanna show you the element
14:42that we kind of skipped earlier in our radial one.
14:46In our linear, now in our radial,
14:48I wanna move the transition X and reason Y.
14:50What if we didn't want our circle to be all the way there?
14:53And bluntly speaking, a lot of times
14:56it's not gonna even look like,
14:58well, not bluntly when I say something dirty,
15:01it's not gonna look like a circle.
15:02If you're taking only two colors
15:03and you're stretching them out
15:04and they could go out of your screen as well,
15:07what you're gonna look at that in a second.
15:08So first of all, I pushed it to the side.
15:10Now, our matrix box could be even bigger.
15:13Our matrix box would be much bigger.
15:15So I could take our stage with times four.
15:18And what that would do
15:19is gonna completely distort our element
15:21because our star is gonna be absolutely out of our screen
15:25that we won't be able to see it.
15:27But if I duplicate it times, let's say 1.1,
15:32we're gonna see that it's getting bigger
15:34and getting pushed out.
15:35Or if I make it smaller by half,
15:38we're gonna see that it's gonna get more shriveled up,
15:41more condensed because we're only doing it to the X
15:44and we're not doing it to the Y.
15:45We're only doing it to the width.
15:46We're not doing it to the height.
15:48So there's a lot of control that you have over here.
15:50And obviously the more creative you are
15:52and the more time you have,
15:53the more you're gonna find that perfect color
15:55to create your elements.
15:57And that really covers working with gradients.
16:00The only last thing that I wanted to say about this
16:02is the exact same way
16:03that we just created our gradient fill,
16:08there's a method called line gradient style,
16:11which will enable you to do the exact same thing.
16:14Gradients only for lines instead of fills.
16:17And you could see more info on that.
16:19You don't even need more info.
16:20Everything we just did right here,
16:21this is your homework or your little break.
16:24Just try to do it with the line as well.
16:28I just find out what the name of that method is.
16:30If you don't remember what I said, don't rewind it.
16:33All you have to do is type this.graphics
16:35and search for that element
16:37that's gonna enable you to create a gradient line
16:40instead of a gradient fill.
16:42All right, so that covers this topic.

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!