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

Starting to draw in Canvas

Now that we know the users have canvas available on their browsers, it's time for us to pick the API we will be working with. We will pick the 2D api. Once we do so we will go ahead and pick a color and start drawing out our first shape %u2013 a rectangle.

Keynotes:

  • The 2D API.
  • Configuring colors.
  • Drawing it out.

Video Transcript

00:00In the last lecture, we set the groundwork
00:03before we could actually start drawing,
00:04and that's what we're gonna do in this lecture.
00:06And in this lecture, we'll get introduced
00:09to the 2D Canvas API, and with it,
00:12we'll learn how to configure colors
00:14and how to draw out our first shape,
00:16which will be a rectangle.
00:17So let's jump right into it,
00:18and let's see the Canvas 2D API.
00:22We have our canvas in place.
00:24The next step in our process is we want to also make sure
00:27that we could start drawing.
00:29Now, before we even go into that update canvas,
00:31I want to continue and make a few updates.
00:33We were talking about the context itself,
00:35and I want to create a variable for that context.
00:39And I just want to create another variable.
00:41Well, no, that's all I want to create.
00:43Okay, so the next step that I want to do
00:45is I want to define a width and a height for our canvas,
00:48because right now we don't have a pre-configured
00:50width and height, and then the default will set in place.
00:53So I'm gonna go ahead, I'm gonna go to my canvas,
00:56and just go to my width, and define a width.
00:58Now, I could have, if I wanted to,
01:00just go directly inside of my canvas
01:02and set here a new width and a new path.
01:05Do not, by all means, do not do it in CSS,
01:07but you could go ahead and do it directly on the canvas.
01:10If you do it in CSS,
01:11it's actually gonna resize, stretch the item.
01:13It's not gonna actually render it to that size.
01:15So we want to go to that API interface itself
01:18and define it.
01:19I personally prefer defining it directly in JavaScript,
01:22and I'm gonna go ahead and set a width of 500,
01:25and I'm gonna go ahead and set my canvas height
01:29I'm gonna go ahead and set it to be 100.
01:31So my size will be 500 by 100, beautiful.
01:35Next step, and it's not gonna be visible,
01:37we won't be able to really see anything, but that's fine.
01:40We won't be able to see anything yet,
01:41but if we really went to our inspector and found our item
01:44and then tried to like over,
01:46to shadow on top of that inside of our wrapper,
01:50and side of our object, and inside of the front,
01:53and if I went to that specific canvas,
01:55you would see that it's currently positioned, it's offset,
01:58but yeah, you see its size,
01:59it's sitting right there on the top there.
02:01We'll worry about positioning a little bit later
02:03down the line, and push our canvas element
02:05to where we need it to stand.
02:07Okay, so our next step, what we wanna do,
02:10I'm gonna go back into my code,
02:11is I wanna get that context.
02:13And we were talking about the context
02:14and saying that the context itself is literally
02:19the API with which we're gonna actually draw.
02:24Theoretically really speaking,
02:25there's gonna be a few different ways
02:27to draw inside of canvas.
02:29If it's 3D, if it's 2D,
02:31or maybe even other versions and ways.
02:33Basically, canvas is gonna be a drawing interface
02:35that will have different APIs to interact with it.
02:39So what I'm doing is I'm setting a context,
02:41or setting the API, and to do that,
02:43I'm basically going to that function get context,
02:46and sending to it the type of API that I want to receive.
02:49In this case, I wanna get the two-dimensional API,
02:53and by getting the two-dimensional API,
02:55then that's it, we're gonna have now
02:56all the functions and methods that we're gonna need
02:58to draw with that API.
03:01With that said, I'm gonna go ahead and also
03:03send this API to that canvas.
03:06So I'm gonna go ahead and send one more parameter,
03:08and send inside, or maybe even just do this
03:11inside of that update canvas.
03:13But the reason why I'm not gonna do that there
03:15is because I'm planning to call this method a few times.
03:17So I don't wanna create a new variable
03:19that I have already stored.
03:21So instead of that, I'm gonna send in a second parameter,
03:23I'm gonna go ahead and maybe even instead of that,
03:26just send the context, and I could also,
03:28instead of that, just send a with,
03:30which is gonna be that canvas with,
03:33and I'm gonna go ahead and copy that,
03:36and I'm also gonna send a height,
03:39and I'm gonna send that as my third parameter.
03:41Now that I have the three parameters,
03:42I'll go ahead here and just make sure
03:44that I'm calling the right names context with height.
03:47So I have all the parameters that I need,
03:49and now I could start manipulating and working with it.
03:52So let's go ahead with all these intros in place,
03:54and after we configured and set and got the right API,
03:58how about we draw something?
04:00So I'm gonna go ahead, and I'm gonna show you
04:01the first thing that we could draw.
04:03And the first step that we need to do
04:04is we need to define a color for our rectangle,
04:09because we're gonna draw a rectangle.
04:11To do that, I'm gonna call the context,
04:13and call the API fill style.
04:16The fill style API accepts a string
04:18which represents a number with hexadecimal value.
04:21And I'm gonna go ahead and just paste here a color
04:24that I already predefined and pre-configured
04:26and pre-decided for it to be our background color.
04:29If I go ahead and after that,
04:31once I created that fill style,
04:33if I go ahead and also now actually start drawing it,
04:36so line 18, I define the color
04:38that I want us to use for our fill.
04:41The next line is gonna be our context dot fill rect
04:46for rectangle, and it's gonna accept four parameters,
04:49the X and Y coordinates, which in our case,
04:51we want it to be zero, zero,
04:52and the width and height of the item that we want it to be.
04:56So in this case, I'm creating a rectangle
04:58that is gonna be filled throughout our whole interface.
05:02So if I go ahead here and click here on refresh,
05:03we're gonna see that now
05:04we should be able to see our rectangle.
05:07And notice how our rectangle is getting resized,
05:10and the reason why it's getting resized
05:11is because inside of our construct,
05:14inside of our CSS, we're forcing a resize
05:17by setting it to be 100 inside of our content.
05:21For us to be able to change that,
05:23we're gonna have to or manually go to the item
05:25and update it CSS to reflect the fact
05:27that we don't want it to be 100%.
05:30Or we wanna go inside of our JavaScript here
05:32and make sure that we're actually
05:33sending in the right size.
05:35It won't be very noticeable to you now,
05:38but it will become very noticeable down the line.
05:41And the reason is is because if you create something
05:43such as a circle and you stretch it disproportionately,
05:47it's gonna be completely smashed.
05:48But in our case, we more or less know
05:50the width and height of our elements.
05:52Although it's a relative path,
05:54if we dig up in our code a bit,
05:56we have here somewhere up the line,
05:57a defined width, which is 900.
06:00So we could go ahead and make sure
06:01that in our script, we'll define our width to be 900.
06:04And in our height, we'll have to just dig through
06:07and find the place where we defined our height,
06:09which in our case, I don't see height,
06:11here we go, 250 pixels is our height.
06:14So I'm gonna go ahead here and define our height
06:16to be 250 as well.
06:18Now, we're gonna be much better
06:19even though it's not very visible to change,
06:21there's not gonna be any stretches,
06:23which is really important.
06:25All that's left for me to do is to make sure
06:26that the item is gonna be positioned in the right spot,
06:29which right now it's not.
06:31So I'm gonna have to go and define its actual position.
06:34And let's find if we have here for our SVG,
06:37we already did that.
06:38Let's go ahead and also do the same thing
06:39and copy the same logic and just define it
06:42not only to our SVG, but define it also for our canvas.
06:46And if I go ahead and save this,
06:49did I put here a comma?
06:50No, we'll put here a comma.
06:52And I'll go ahead and save this.
06:54We should have our position in the right position
06:56and we probably would also need to be able
06:58to push it up to the top zero.
07:01And if we go do all of that, then,
07:04oh, no, top zero didn't work.
07:06Okay, so I'm gonna play with the CSS
07:07for a minute in between videos
07:08and then I'll show you what I did.
07:10So I played around with it a bit
07:12and I actually by mistake
07:13have your little bit of extra code.
07:15I'm gonna go ahead and just get rid of this
07:16from line 105 to 110.
07:18It's not needed, I just had that as a straggler.
07:21And if I go ahead and save that
07:23and go back and click on refresh,
07:25we should be in the right spot the way we wanted it to be.
07:28So there we go.
07:29We created a canvas element
07:30and drew into it a square and a rectangle.
07:34In the next lecture, we're gonna make it
07:35a little bit more than just a square.
07:39In this lecture, we were introduced to the process
07:42of fetching our context, the 2D API,
07:45and then we drew out a rectangle.
07:48Obviously, we could do much more complicated things than that.
07:51And in the next lecture, we're gonna dedicate it
07:52to drawing out a grid of circles in canvas.

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!