Drawing images into canvas
let's turn our heads to Haiti and get their flag up and running. To create this flag, we need to have the image of the symbol that is placed in the center of the flag. In this lesson we will learn how to add images into canvas, how to resize them and how to maintain there original ratio.
Keynotes:
- Adding images into canvas.
- Resizing images in canvas.
- Maintaining image ratio when resizing.
Video Transcript
00:00It's time for us to curve away from curves, no pun intended.
00:04What we're going to do is we're going to take a break from
00:07curves and draw an image onto Canvas.
00:09It actually is really simple to do.
00:11So what we're going to do is we're going to add an image onto our Canvas,
00:14we'll resize that image,
00:16and we'll maintain that image ratio,
00:19making sure that we control how big that image will be inside of our Canvas.
00:24So let's jump right into it.
00:27So let's go to our next level of complication.
00:30Again, in your references,
00:32you're going to find the image,
00:34which is really, really big of Haiti.
00:38Now, in the image of Haiti,
00:39there's actually an image itself or really in our case,
00:42we'll use it as an image and not a vector.
00:44How do we integrate images into Canvas?
00:47That's exactly what we're going to do in this lecture.
00:49It's really easy. So let's jump right into it.
00:51Let's start with creating the base of this flag with the upper square,
00:55the rectangle that is blue,
00:57and the rectangle that is red.
00:59So I'm going to go ahead directly into my JavaScript and go to the create
01:03Haiti function or really create it,
01:05and it's going to have inside the regular stuff we always do with
01:08the center point of our X and Y coordinates.
01:11Now that we've got that in place,
01:13all that's left for me to do is to start drawing.
01:15So I'm going to go ahead, not call the console,
01:17but call our context,
01:19set a fill style,
01:20and I already pre-configured that fill style,
01:22that red color,
01:24and I'm going to go ahead and just set it up here,
01:27and that's our color.
01:28Now let's go ahead and draw out that fill rectangle with the fill
01:34rect command,
01:35and I'm going to go ahead and set the X and Y coordinates to be zero,
01:38and we want to take the full width of our item,
01:42and we want to take half of the height or our Y position,
01:45which is half of our height.
01:48If I do that and save that, go back and click on refresh,
01:50it should have the first quadrant of our flag.
01:53Let's continue.
01:54The next quadrant of our flag is literally more or less the
01:57same thing, only contrary,
01:59we're going to start in our Y position,
02:02we're going to push down in our Y,
02:03but really for our width and height is the same.
02:07All that's left for me to do is to change the color,
02:09and I already have that pre-configured as well,
02:11and we have ourselves our baseline of our flag.
02:15That was the easy part.
02:16Now the next part in our flag is actually drawing in this
02:20image.
02:21Now to do that, I already put in our source files,
02:23and if you go into your source files,
02:25there's an image folder called Haiti.
02:28Now this image is literally just that shape,
02:30and its size could be any size,
02:32but let's assume we don't know in advance the size,
02:34and let's look at the flag and try to figure out
02:37where would be, what size,
02:39what relativeness does that image,
02:41this element actually have.
02:43Now to do that, what I'm going to do,
02:45if I go back and find that Haiti flag,
02:48and zoom out a little bit,
02:49I just want to look at the ratios to figure it out.
02:52And if I'm looking at this,
02:53I'll see that this height is more or less twice that,
02:58this twice is more or less this,
03:00maybe a bit more even,
03:01this is maybe a bit smaller,
03:02but I'm going to assume just from looking at visually,
03:05I think that this is about twice,
03:07this is basically half of the flag,
03:09that means this is supposed to take a quarter of the size
03:13of our space, give or take.
03:15Assuming that, let's go ahead into our code
03:17and continue to our next step,
03:18and I'm going to go ahead and actually load in that image.
03:22To load that image,
03:23all I'm going to do is I'm going to create an image variable
03:25and create a new HTML image, a new JavaScript image.
03:29And I'm going to go with that image
03:31and have an onload event to it,
03:33an onload callback more accurately.
03:35And then I'm going to do something when it's done.
03:38But let's go ahead and also make sure
03:39that we're loading in that image.
03:41So I'm going to set the source file,
03:42the source of that image,
03:44to be, to be, to be, to be, what is it?
03:46It's actually image.hayi.hayi.png.
03:52That's where our image is located.
03:54Let's go ahead and first,
03:55as soon as we get our image ready,
03:56what I want to do is I just want to show you
03:58the drawing of images.
04:00And the way we do that is I call the context
04:03and I draw the image,
04:08and I send in the image as the first parameter.
04:10In this case, because I'm inside of this onload,
04:12the image is the this.
04:13And then I set my X and Y coordinates,
04:16so I'm just going to go ahead there and set them to zero
04:19and just save this.
04:20And let's go back into our application
04:21and click on refresh,
04:22and we should see our image is going to sit
04:23all the way on the top here.
04:25And it's probably going to be too big.
04:27So the next step for us to be able
04:29to figure out the right size,
04:30we're going to have to come up with some algorithm.
04:33Now, the algorithm that we're going to come up with
04:35is really simple.
04:36I'm going to call this ratio.
04:38Now, ratio is literally going to be,
04:40I'm trying to find out what is the right ratio.
04:43So to do that, I'm going to go to my image
04:45and take its height.
04:46And I want to divide its height by the size
04:48I think it should be,
04:50which is basically a quarter of our height.
04:53This value that's going to come back
04:54is the number that I'm going to want to divide
04:57the actual width and height, the image width.
05:00I want to divide the actual, this image.
05:04Oh, sorry, this width.
05:06I want to divide by the ratio to get that new ratio.
05:10For the height itself,
05:10I want to do the exact same thing.
05:12Now that I've got the right ratio,
05:14I want to go for my height and divide it by the ratio.
05:17So what I've done here literally is in this ratio,
05:20which is really the heart of the logic here,
05:22is I took the height of our map, or really of our image,
05:26and I divided it by the height I want it to be,
05:28because I want to find what is that variable
05:30that I need to divide the height to get to the right height.
05:34Once I found the ratio,
05:35I went ahead and divided our image
05:38and its width and height by that ratio
05:40so I could actually resize the image
05:42to be the size that I want it to be.
05:45The next step I'm going to do
05:46is I'm going to go into the draw image,
05:47and it actually accepts two more parameters
05:50that let me resize our image.
05:52So I could go ahead there and say
05:53I want the image's width to be the new image width,
05:56and the height to be that new image height.
05:59Now, if I go ahead here and save this
06:00and click on refresh,
06:01we'll see that our image is now going to be the right size.
06:04All that's left for me to do
06:05is to position it in the center of the screen.
06:07So I'm going to go ahead here,
06:08and for my X coordinate,
06:10I want to take it to our X,
06:11which is the center of the screen,
06:12but I also want to divide our image's width itself
06:16so we are offset in half.
06:18So again, if I go ahead here and click on refresh,
06:21we'll see that now our image is going to be slapped
06:23in the center on the top here.
06:25Now we want to push our Y coordinate,
06:27so I'm going to go to my Y coordinate,
06:29and I also want to subtract from it
06:30that half of the height
06:32so it's going to be exactly in the center.
06:34Because again, our coordinate is actually here.
06:36So if we wouldn't do that,
06:38if we wouldn't subtract the width
06:40and subtract the height,
06:42the image would be in the center,
06:44but its top left corner would be in the center.
06:47And what we're trying to do is we're trying to get
06:49that image to be slapped exactly in the center,
06:52taking into a point that image size itself.
06:56And that is how you work and add images
06:58working with Canvas.
07:02In this lesson, we learned how to control
07:04the ratio of an image and how to add it into our Canvas.
07:07In the next lecture, we're going back to controls,
07:10and we're going back to something very similar
07:12to angles, only this time we're going to control
07:15the colors of our screen.
07:17So we're going to control a gradient,
07:19and we'll start with the linear gradient.
