Creating an interactive linear Gradient
Since we started our course all the colors we used where solid colors. It's time for us to take a step into gradients. By the end of this lesson you will know how to work with linear gradients.
Keynotes:
- Creating Gradients
- Understanding the control points
- Adding colors to a gradient.
Video Transcript
00:00In this lesson, we're going to create an interactive linear gradient. In the process,
00:06we'll learn how to work with gradients and understand the control points that control
00:10a linear gradient. Beyond that, we'll also learn how to add colors into it. So let's jump right
00:16into it and see a linear gradient in action. What I want to do next is to start working on
00:22colors themselves. And the idea here, we're going to create an interactive element to show you how
00:28to work with gradients, linear gradients. The idea will be to be able to draw from one point to another
00:34and dynamically draw in a gradient. So to do that, I'm going to go ahead and I already pre-created
00:40the core starting point of create linear gradient. Make sure that you go ahead and do the same,
00:47setting up your width, height, and context. Once you have that configured, I'm going to go ahead.
00:53Once you have that configured, I'm going to go ahead and what I want to do is I want to go to
00:57my canvas and add an event listener to it. Because what I want to do is I want to know whenever my
01:03mouse is clicked down, so mouse down, and I also want to know when my mouse is up. So let me just
01:08go ahead and create the mouse down and then we'll duplicate that function there. So first of all,
01:13we have here the step that we want to hear is collect information. What information do we want
01:18to collect? Well, we want to collect a point. So I'm going to go ahead and save a point one,
01:23and let's call it point one, point two, just so we have a reference point. No pun intended.
01:28The next step is we need to calculate and figure out where are we in our canvas. Now,
01:35technically canvas doesn't have any mouse engagement. All this mouse engagement is happening
01:41externally to it. And this external engagement doesn't give us our local information that we
01:47need for our canvas. So we're going to need to find a way to somehow transform this information
01:52that we're getting. And let me show you the information console dot log. And let's just log
01:57out the event itself that's coming out, go back inside of our application, click here on refresh,
02:02and I'm just going to press somewhere. Now notice when I press somewhere, I'm going to have here
02:06a client x and client y, but these values are representing the full area, the full zone
02:12outside of here. What I really want to do is I want to go into this element. And I want to
02:17basically get this information transformed into the R00 coordinates get taking effect.
02:24So to do that, I'm going to go ahead and create a function that's going to transform because we're
02:28going to use it now and throughout the remaining lectures of this title. And I'm going to call
02:33this transform position. Now transform position is going to take in our canvas because we need
02:39to know where our canvas is located. And we're also going to send our client x that we get
02:45from the event and our client y that we're getting from our event. And I'm going to go
02:50ahead and store the value that is coming back into the point one variable. Now let me go ahead
02:56here and just delete that. And I'm going to go and create that function right above our create
03:00linear gradient. And we said we're going to call it transform position. Now transform position
03:06again gets a con gets a canvas, it gets an x point, it gets a y coordinate. What's the
03:12next step for us once we have access to these points, the next step for us that we want to
03:17really do is we want to transform the information. Now canvas on its own, we could go to our canvas
03:23and go to our HTML element and ask for me to get our bounding rectangle. To do that,
03:29I'm going to go and create a new rectangle variable. And I'm going to approach the canvas
03:33and ask for its get bounding client rectangle, which will literally give us the variable though
03:40the client variable, the client values in a set set of a rectangle. Once I've got that information,
03:48I could go ahead and return our new object with x and y coordinates. And literally what I want to
03:53do, I want to take my x value, and I want to subtract from it our rectangle left, which is
03:58the value that will be in our rectangle left right top bottom, I'm going to go ahead and also
04:03make sure that this value is a integer. So I'm going to round that value out. And I'm going
04:08to go ahead and do this also for our y coordinate. And for our y coordinate, I'm going to go ahead
04:13and set a math round and take my y and subtract from it the rectangle top. By doing this,
04:20I just created a point object that will have our x and y coordinates that relate to our
04:25internal context. Now that I have that point, I could go ahead now and also duplicate this
04:31because we want to store the information of point two. But we're going to do that when our
04:35mouse is up. So now we're going to have both points stored in memory and we'll point one
04:40and point two. All that's left for us to do is actually start drawing out our gradient.
04:47So to do that, I'm going to go ahead and inside of our mouse up, because I want this to happen
04:52whenever the mouse goes up, I'm going to go ahead and create a new variable called grad for
04:56gradient. And I'm going to call our context. And I'm going to call a create linear gradient,
05:04and our create linear gradient expects two points or four properties, the x y coordinates of our
05:10starting point and the x y coordinates of our ending point. So I'm going to go ahead here and
05:15send our point one dot x, our point one dot y. And we also want our point two dot x. And we
05:26want also dot y. So let me just copy that. And we just send in the information that we needed to
05:31create our gradient boundaries. Now that we created our big gradient boundaries, it's time
05:37for us to define the colors that are going to be inside of this bound. And to do that,
05:41I'm going to use the GRD our gradient object and call the add color stop. Now the add color stop
05:49accepts two parameters. The first parameter is a value between zero and one that would literally
05:55tell it where in that area should the color begin. Zero would say that we wanted to start
06:01from the complete absolute beginning of our gradient, technically where our point one is
06:06starting out. Next is the actual color itself. And the color itself is just an object. And I just
06:11went ahead and created here just a simple color in advance, nothing fancy. I'm going to go ahead
06:16and also create another step. And for our next step, because we want to have at least two colors,
06:21and you can add as many colors as you want. I'll add here the next step. And in our next
06:25step, I'm just going to set the color to be a different color, very similar, but different.
06:30Once we have our gradient, and it really is ready, all that's left for me to do is to go to my context
06:35and change my fill style, but this time around, send inside that object instead of the strings
06:40that we've been accustomed to send so far. And now we're ready to go to our content and fill a
06:45rectangle. And I'm going to go ahead and fill a rectangle for the full area of our screen,
06:50to be able to see it in all of its glory. And it's literally all I had to do to create a
06:55gradient, a linear gradient that is interactive at this stage that is going to be affected when
07:00the mouse is up. So let's go ahead and save this and take a look and take a look and see if it works.
07:05So I'm going to refresh and have a little error. Let me go ahead and fix this at line four,
07:09seven, something is wrong here. Let me see what's wrong here. 74. So that looks okay to me. Okay,
07:20I put here an equal sign, there's no equal needed there. So let me save that. Let's go
07:24ahead and give it another shot. And here we go. So now when I go into my gradient, and I'm going
07:28to go and just copy front all the way to the top and just release my mouse, when I get to the
07:33bottom, you'll see that I have this beautiful gradient, I could also control it from this
07:37point to that point, if I wanted to, or from this point to this point, if I wanted to,
07:42and so on and so on. Beyond that, I could also go ahead and say, Hey, you know, like,
07:46I don't want it to actually be all the way in the edge, I want to have your zero dot 1%
07:52just to go from zero dot one, and I want it to end at zero dot nine. And if I wanted to,
07:56I could have also gone ahead and maybe in the center added here, another color. So I could have
08:00gone here and said, you know what, let me put this to be nine three. But I also want this to
08:06be darker. So I'm going to put this at one one. And I'm going to put this at one one. So there's
08:11going to be another step. And I could say, okay, I want this to be somewhere in the middle
08:14point. Now by doing this, if I go back into my application and click on refresh, we'll see
08:18that if I go ahead and draw it out, we'll have your three colors, various variants. Now let me
08:23put there a really sharp, different color. So it'll be very noticeable. How about we go for
08:28red? So full zero zero zero. So complete red, absolute redness in our point zero dot five.
08:35Okay, let's go ahead here and give it a shot. And we should see you're a really,
08:38really ugly gradient with a red zone inside of it. Absolutely ugly. But I'm going to go ahead
08:44and get rid of that because I actually really liked our initial color gamut. This would be one,
08:50this would be zero, I'll save it, click here on refresh. And with that, we are literally done
08:58with this lecture as well, and created our first interactive canvas element where we're drawing
09:03dynamically, our elements were not done with gradients. But we're definitely done with a
09:07linear gradient. In this last lesson, we learned how to create an interactive linear gradient.
09:14Very exciting. The step above next is to learn how to do the exact same thing only with a radial
09:21gradient.
