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

Drawing with Canvas

Time for us to take our newly learned skills and implement them on an internal JavaScript interface. This chapter is dedicated to canvas.

Video Transcript

00:00So now that we got this rolling, the next step for us is actually to get access to this canvas element.
00:06To get access to this canvas element, this is exactly where we're going to step into this world of objects in JavaScript.
00:13So one object and the first object that we're going to meet right now is an object that's called the document.
00:20Now the document, the document itself is basically everything.
00:26It's our document, it's basically our HTML document, it's representation in JavaScript.
00:32So now we could actually approach this document and we could say document.getElementByID
00:38and then request our element, our canvas element.
00:42And if you remember, if we go back into our HTML, you'll see that we gave our element an ID and that ID is pacman.
00:49So if I go into my object and I try to get pacman by going to document. and notice our dot syntax,
00:56getElementByID and setting our ID name.
01:01If we refresh our code again, we're going to see that we're going to get that canvas HTML canvas element.
01:07All right, so that easy. Okay, so this is really the beginning.
01:10So I'm going to create your variable, I'm going to call it can for canvas.
01:14And we're just going to put what our result that we just got back here.
01:18And by the way, if you're doing this in JavaScript and you want to select everything inside of an opening between an open and closed bracket,
01:24you just need to type command, comma, and we'll select it all.
01:29All right, so we got our canvas.
01:32The next step is we want to actually, the canvas itself is just a HTML wrapper.
01:37We can't start drawing into it.
01:39We actually have to get from within that canvas, they're actually drawing context.
01:45So we're going to call this context.
01:48And the way we get the context is we could approach that canvas, type dot,
01:53and then we could just type get context.
01:58And we have to give it the type of context because we're working with 2D elements.
02:03This is how it works in HTML5.
02:05And I want to take a couple of steps back.
02:07So first of all, we're talking about objects and we're seeing how we're actually going to be working with objects.
02:13And the steps are relatively simple.
02:15There are a lot of objects in JavaScript.
02:18We're not going to meet all of them today.
02:20But as you become deeper and deeper involved in JavaScript, you're going to meet a lot of different built-in objects.
02:25We've met in previous classes.
02:27In previous classes, we've met the math object.
02:31Now we're meeting the document, which represents the DOM.
02:33It actually represents our HTML document itself.
02:37And one of the new functions in HTML5 that enable us to access information is to be able to get elements by ID using the function,
02:45which is actually called a method.
02:46When it belongs to an object, get element by ID, we then send to it a string.
02:51It got us that element.
02:53But at this stage, we have the HTML wrapper.
02:55We have the element itself.
02:56We don't have access to the drawing capabilities.
02:59So the next step for us is to access that object that we got back, which represents the canvas block of code.
03:06And we're going to ask for the canvas block of code, basically access to be able to start drawing.
03:10To get that access, we asked for that get context.
03:14Now, there are a lot of different types of context.
03:16For example, well, today, there's 2D and there's also 3D.
03:19Now, it's a different name in the 3D, but we're not going to cover that.
03:23But basically, in our scenario, what we're actually trying to do is we're actually trying to get our 2D context.
03:29So we can then start doing 2D drawings.
03:33All right.
03:33So now that we got our 2D context, let's start calling a couple of functions and a couple of properties and just do a little small illustration of something
03:44that's a little bit more visual.
03:45So the first step with canvas itself, we always have to define what color you want to draw something.
03:52And we're not going to delve too deep into the world of canvas, but we're going to see this in action of there's two things,
03:57two types of things that we could draw.
03:59We could draw lines or we could draw fills.
04:02Now, we want to draw a line.
04:03We usually call it a stroke.
04:05When we want to draw a fill, we usually call it fill.
04:09All right.
04:10So let's start off.
04:10First of all, we want to define our fill style because right now we only want to do fills.
04:15We don't want to create a line itself, but let's start.
04:19All right.
04:19So context and in our context itself, I'm going to call the method fill style.
04:27And I'm going to set the fill style to just be a hex value, a certain color.
04:32So I'm going to fill.
04:33I'm going to select a color, let's say of 0 0 0 0 0, which basically means black.
04:39So I'm going to create a black color here for our fill.
04:42The next step after that is when we want to get our context is we want to take our context
04:47and want to tell the context, let's create now, let's say a filled rectangle.
04:54So it's called fill rec the function itself and notice how I'm approaching that object each time
04:59and I'm asking for I'm basically setting something within it.
05:02So now I'm going to first set the color that I want using a property and the next step is
05:07calling a method and really the difference is a property.
05:09We put an equal sign and just put the value while a function, a method will open a bracket
05:14and then we want to send some information here because we're creating a rectangle.
05:17Now the fill rec expects two parameters.
05:22The first parameter is going to be the position XY coordinate that we want to draw in.
05:26Now the canvas area itself, if we look at the canvas when we create it,
05:30basically from our starting point at the top left is going to be our 0 0 and the more
05:35we progress our Y will, our X will grow when we go horizontally to the right and our Y will
05:43grow as we go vertically down.
05:46Okay, so let's start with something simple.
05:49We're going to start at 0 0 and we want to go to the width itself of our canvas
05:53and we want to go to the height of the canvas creating this complete rectangle.
05:57So to do that, I'm going to click, I'm going to ask for the canvas itself,
06:00that original object that we had earlier, going to ask for its width.
06:04I'm going to go to that original object and I'm going to ask for its height as well.
06:07This way our first two parameters are 0 0.
06:10Our next two parameters are the width and the height of our canvas.
06:14Now if I save this and I just run to our design itself,
06:17we'll see that I've just created a black rectangle.
06:20Now to make things more interesting, obviously just creating a rectangle is not going to be that fun.
06:26So let's move on to the next step and in our next step,
06:29we want to create actually a more complicated object.
06:32Alright, so the next step to make this happen is we want to start drawing basically step by step.
06:37Now before we do that, I want to change our color.
06:39I want to change our color to be a color of Pac-Man.
06:43So I'm going to again go to that context.
06:45I'm going to change my fill style and this time around I'm going to use this yellowy type of color
06:51because we're going to try to match up to get us a color that is similar to Pac-Man.
06:56So in my case, I typed your F3F 100 starting with I don't even know how you call that sign,
07:03whatever you call that sign.
07:05Hashtag, hashtag.
07:06There you go.
07:07Alright, so next step is we're going to continue on and this time around instead of calling the fill
07:12rect which would create a rectangle for us,
07:14we're actually going to draw our element line by line.
07:18Now to do that, I'm going to have this first define and say okay,
07:21I'm creating a shape now.
07:22So I'm basically telling I'm telling the browser.
07:26I'm telling our canvas.
07:27I'm telling HTML5 that I'm beginning basically a new element.
07:32I'm beginning a new path.
07:34I'm beginning a new shape that I'm creating.
07:36So we always start with begin path and we always end with I bet you could guess it close path.
07:43Alright, so if you said end path, you're not that far away.
07:46So we always start and we always end with begin a path.
07:49We close a path and ideally we want to start creating a draw something in our case of Pac-Man.
07:54We want to start from the same point and end in the same point.
07:56This way we have a complete closed shape.
08:00Now because I need to use the width and height a couple of times throughout this drawing,
08:04I want to actually save these as variables that I could then access.
08:08So I'm going to create a new variable called width and I'm just going to grab our width itself.
08:14Paste that there, cut that there.
08:16And I'm going to put inside there just that reference to that variable.
08:20And by the way,
08:20the only reason I'm doing that is because it's more efficient to not call properties
08:25or functions of methods too often.
08:27If you need to call something a lot of times a good practices is to create a local variable
08:32because then it's going to cut down the amount of time it takes your browser to access information.
08:37It's small little things like this are going to make sure that your application continues
08:41and runs efficiently for long periods of time.
08:45Alright, so the next step is for us to start drawing.
08:47So let's start drawing.
08:48So we have a begin path.
08:50We have a close path.
08:51We have a quick reference to our width and our height of our canvas itself.
08:55So it's time for us to actually start drawing.
08:57Alright, so first step is I want to go to the exact center of the stage to do that.
09:02I'm going to call the move to function and the move to function basically will move
09:06our point pinpoint and is going to move it to an XY coordinates,
09:09whatever I define that they are and I want to go to the center of the stage.
09:12So I'm going to take the width divided by two and the height divided by two
09:16and I'm actually now exactly in the center of the stage.
09:21Now, the next step is I want to create actually an arc.
09:25An arc is basically like a circle, only not a complete circle.
09:31And the way that arcs work is you just want to call the arc function.
09:35The arc function expects 1, 2, 3, 4, 5, 6 parameters.
09:40The first two parameters is the position where we're going to start
09:43and in our case, we want to start in the exact same position where we just moved to.
09:48So we're going to start with our width and our height divided by two.
09:52Now, the next step after that, after we set our X and Y position that we want to start from,
09:56we also want to define how basically we want to define how big is our circle going to be
10:03or in other words, what will be our radius.
10:05So I'm going to adjust for this example because I know that our element is 550 by 400 large.
10:11So I'm just going to set here a large number.
10:13Let's say we're going to put here a number such as 50 making our circle circumsphere 100 pixels
10:20because it's two times our radius.
10:23So after I set our radius to be 50, I want to now define basically an angle
10:28and really to create Pacman, I want to open up an angle from basically drawing a line from,
10:34let's say from the angle 40 all the way to 320.
10:38Now, unfortunately in programming languages in general and mainly client side programming languages,
10:44they don't work with angles.
10:46You can't just type 40, 300 to 20 in these two next parameters basically saying
10:51where do we want to start to draw?
10:52Where do we want to end?
10:54We actually need to use radians.
10:57Now, to make our lives easier, the way to access a radian, basically radians,
11:02they're basically a full circle as math, the full circumsphere.
11:07If you take the whole line that's created in a circle and just draw a straight line,
11:12you're going to get the value of math pi times two.
11:15Basically, that's the angle itself, the full angle is math pi times two,
11:22which is equivalent to 360 degrees, which is something that we're probably more familiar with.
11:28So what I want to do is I want to create a radian and I want to set my radian to be math.pi
11:36because that's a value that we have there and I want to divide that by 180.
11:40By doing that, I'm actually going to get one radian, which then I could use one radian times 360.
11:46That would give me a full circle.
11:47So now I could actually take that radian, that value that I get back,
11:51I could duplicate it by 40 to get that specific radian that we were looking for.
11:55I could duplicate the 320 by that radian.
11:58And again, I will get that radian, that 320, which is that number that we would expect.
12:03Now you could just put in the next parameter, just false, don't worry what that next parameter is for now.
12:09And what I want to do is to complete our drawing before we actually reach our destination,
12:15I want to go back to our zero zero because I want to create a full mouth
12:20because if not, we're going to be from this point, we're going to have a half a shape,
12:24we're not going to complete a shape, we want to move, not move to, we want to actually draw to.
12:28And to do that, we basically, instead of calling the move to function,
12:31we call the line to function because this time around,
12:34we want to draw a line from wherever we are, right to the center point where we started from.
12:41So now that we have a complete closed shape.
12:43Now, by the way, if I try to run this right now,
12:45we're going to see that it's not going to show anything.
12:47The reason why it's not going to show anything is because in creating these shapes,
12:52when we're using these invisible drawings,
12:55we have to call one more method and that one more method we call after we finish our closed path,
13:00which is called fill.
13:03And our fill basically will then create our fill of our element
13:07and then we're going to get our packment.
13:10Now, by the way, just one more thing as we're talking about fill style
13:13and if we're already talking about this topic of canvas
13:17and we're talking about how to work with objects,
13:20I just want to show one more element.
13:21So context, if we're ready, so if you start playing with the canvas itself,
13:26instead of fill style, this is called stroke style.
13:30So let's see what type of number we're going to set here.
13:32Let's say some sort of red or let's make it a really sharp red.
13:35So it'll be very obvious that we've just created this.
13:38So I'm going to put here FF for red for the green and blue.
13:43We're going to put there zero and last but not least,
13:46we actually want to call that like just as we did when we called the fill,
13:50we want to actually call the stroke.
13:51So context.stroke and if we save this and if we run our design,
13:58we're going to see that we have a red stroke
13:59and just so make it a little bit more visible.
14:02Maybe we actually should make it white because then it'll be easier to see.
14:06And if I look at this again, we're going to see that it's white.
14:08It's very hard for us to see it.
14:09But you know what?
14:10If I undo that, keep it red and I just comment out our fill color,
14:15our rectangle that we created, that black rectangle we created earlier.
14:18Now it's very easy to see that we just created a red overlay
14:22and that's really all we need to know about the basic way of interacting
14:26with our gym.

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!