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

Canvas Hello World

We start our canvas journey creating a simple rectangle in the process we will learn how to define the canvas size, how to define a fill style and how to fill it up using the fillRect method.

Keynotes:

  • Our first Canvas drawing
  • Creating a rectangle.
  • Defining a style with fillStyle

Video Transcript

00:00It's time for us to create our actual canvas
00:03enemy drawing so to do that we're gonna create in the process our first drawing and it's gonna be a rectangle and
00:10We're gonna define our styles with fill style and see how to work with the basics of canvas
00:16So let's jump right into it and draw our first drawing in canvas
00:20Our goal is to create a very simple shape the flag of Palau now notice this
00:29version right now that I created previously just to showcase is
00:33Kind of grainy then we're gonna look at why it's so grainy as we explore this lecture
00:40So let's go ahead and first start creating and to do that
00:43I'm gonna go ahead into my do canvas area and I'm gonna go ahead and call the method create
00:50Pa
00:51Lao
00:53And I'm gonna go ahead and create that function. So create Palau, which is the flag of Palau
00:58Let me just go ahead here. I'm gonna create that function and let me just break a few lines
01:04It'll be easy for us to take a peek at it and that's gonna be our main focus the create Palau method
01:11Now the first step that I want to do is I'm gonna create my helper variable starting with access to my canvas
01:17And I'm gonna gain access using my get ID or by ID function that we've created earlier and literally
01:24I'm just gonna go ahead and send in the ID of the canvas to get it
01:28The next thing I'm gonna do is I'm gonna get my canvas with and just store it locally in a small variable
01:34So it'll be easy for me to access it same thing. I'll do for my height itself
01:40Last but not least I'm gonna go ahead and create access to the context of the canvas as we met in the previous lecture
01:48Canvas has can this has a property or a method called get context more accurately a method
01:56Now the get context deliberately is built where you're asking for it for its API
02:01Because there is a few different APIs and it's expected in the future to have even more of them the API
02:07We're going to be focused on is the 2d API the two-dimensional which basically says yes
02:13There is an option for 3d, which is a completely different topic
02:18So that's really the way that we gain access to the API to be able to control the element
02:23From there on after everything we do with our canvas is going to be done through the canvas through the context itself and not directly
02:31With the canvas object. So again, the first step is we got a reference to the canvas and then we asked from its get context
02:39What context do we want? How do we want to manipulate it?
02:41We want to manipulate it in 2d
02:44Once we got our context. It's time for us to start
02:47Manipulating it and we could literally go ahead immediately start working with it
02:51But before I do that, I want to do a quick console dot log
02:55And I want to log out our width and our height to show you something that you might get confused with if you don't
03:01Set it properly. So let me go ahead here
03:03And I'm just gonna click on refresh in my browser and you're gonna see that I'm now I'm gonna get that in the
03:08Console that the size of my canvas is 300 by 150
03:13But if you check in your CSS, you'll see that in our CSS our width is 400 and our height is 250
03:20What gives well the CSS doesn't control canvas or its properties
03:24It controls HTML and its properties and thus when it's resizing the canvas
03:29It's literally stretching a bitmap that already exists for us to define the canvas. We have two
03:36Mechanisms that we could do one. We could go ahead inside of our HTML and define a width and height as well by defining a width and
03:43Setting a height as well and literally and explicitly
03:47defining it to be the width and height that we want it to be but which is 400 and
03:52250 and I'm gonna go ahead and save that and if I go back into my browser and click on refresh
04:00We'll see that now it's 400 and 250 the size that I actually want it to be without any stretch going on
04:06Another way for me to do it is to literally access the API and after I got access to the canvas object itself
04:13Just go and define its width and height dynamically in JavaScript to whatever style with an height that was there
04:20But in our case, we're happy with defining it explicitly inside of our HTML
04:25And with that said, let's go ahead and continue building the Palau flag
04:29So I'm gonna go ahead and delete that and the first step that we're gonna learn and the first thing we're gonna learn inside
04:34of
04:35Canvas is that we need to define a color now to define the color. I'm gonna set the fill color
04:41Fill style by setting the fill style and I'm gonna send inside of it a string
04:46Now the string that I'm going to be sending in is the color of the Palau flag
04:50Which is the background color this background color could go ahead and copy it the next step
04:56I'm going to go ahead and call the first method in canvas that will meet and that is the fill
05:01Rect now the fill rect as the name implies fills out a rectangle now that we already have a defined color
05:09for that fill the next step is to define the width and height of that rectangle starting with the
05:15Top coordinate the top X and Y coordinate leading to the bottom
05:20X and Y coordinates, which would be the width and height
05:23So I'm creating a rectangle that is the full size of our shape
05:28So if I go ahead here and click on refresh
05:30We'll see that now we're filling up the canvas in the color that we expect it to be
05:35Now if I didn't set it to be the full height or I just set it half of the height and half of the width
05:40Then I would just get a rectangle that would take a quarter of this of the screen
05:45From the top I could have if I wanted to gone ahead and also said that the beginning point would be maybe also that
05:52With half and then it would start from the next tier
05:55So it would start from this point if I go ahead and click on refresh
05:58It would start from there and if I wanted it to start from the bottom coordinate
06:01I would just go ahead and also change the Y coordinate to also start from half of the width
06:07Okay, so play around with those values get comfortable with them
06:10We're gonna definitely play with them a lot throughout this title in
06:14This lesson we've seen our initial look at how to work with canvas literally with the API itself in the next lecture
06:23We're gonna go ahead and create a sample. That's a bit more complicated by drawing out the flag of Paulo and with it
06:30We're gonna learn how to create a perfect circle or a perfect arc, which is a circle

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!