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

Defining a Canvas Area

We will start our tutorial using a pre-built template HTML file. The first thing we will want to do is figure out where to place our HTML5 canvas information followed by filling the canvas background color so we can find it using the canvas JavaScript API.

The HTML5 Canvas API is not available on all browsers but limited to browsers that have adopted HTML5. In this title we don't cover the background story of HTML5 and the supported API's. We will see in a very quick and interactive tutorial the steps involved with creating a new Canvas element.

The first step of any canvas element is creating the CANVAS tag in HTML. After creating the HTML element we are ready to start interacting with it in JavaScript.

There are a few mandatory steps in any JavaScript canvas tutorial before we can start really to engage with our canvas drawing capabilities. The first one is actually getting our canvas element. We can use any of the known ways of fetching HTML via JavaScript. One popular way is getting our element by using its id by calling the getElementByID method of our document.

Once we got our Canvas HTML element in JavaScript we are almost ready to start drawing into it. Before we start drawing we need decide what will be the drawing API that we will use. We will always pick a 2D drawing option. The way to trigger the 2d drawing API is by creating a context for it by triggering the getContext method of the canvas element and requesting from it the "2d" API.

At this stage in our HTML5 canvas tutorial we can start drawing and interacting with the 2D canvas API. The last step we do in this task is to draw a background to make our canvas visible in the HTML document.

Initially when the canvas html5 element is created it is empty with no content and no boarder (it is transparent as well).

HTML5 canvas tutorial

The canvas element came to replace other drawing tools that are used on the web such as silver light, java applets and Flash. The Canvas enables us to draw into HTML using JavaScript. Currently there are two supported APIS. One for 2 denotational drawing while the other is for three dimensional drawing.

The HTML JavaScript Canvas API enables us to draw dynamically onto the browser within the canvas drawing area without the use of external 3rd party tools. The HTML5 part of creating the canvas is just an empty box but the magic happens when you connect the JavaScript API that enables us to draw many shapes and forms. Direct drawing of lines, squares, circles, arcs curves are built in (with it the capability to draw any 2 dimensional drawing. The JavaScript Canvas API enables us to do pixel manipulation as well but that is out of scope of this project for more about that check some of our other HTML5 titles.

Not all Browsers support the canvas API. Although the list is growing and changing all modern browsers support in some level the API.

Why learn tutorials with us?

There are many HTML5 canvas tutorials out in the market some are free while others are paid. We go beyond shallow scans of technology. Our mission is consistently helping developers and new to development individuals to expand there skills. While others will create isolated titles on Canvas or other HTML5 topics our approach is integrated. The more you learn with us the more you will pick up on many other technologies based on need instead of basing it off a technology.

The best way to learn is through repetition and change. Sounds like conflicting ideas right? Its true you don't want to do the same thing over and over as you just learn to be a "parrot" and you will only master the skills of copy paste. While change can be hard and challenging without the right level of repetition its very hard to absorb new information. While other titles will focus on one topic our titles focus on an integrated learning experience that has the right level of repetition and new content.

Beyond our strategic content build up that will help your growth we focus on expanding your understanding of technologies that surround the ones you are working on in a very in depth strategy that works. The best way to find out is to give us a go.

Other interesting titles:

AS3 Drawing API

if you have a background in programing in flash you will find the Canvas API very similar to other drawing apis such as the flash Drawing API.

Our AS3 Drawing API course will walk you through all the basic things you need to know about how to get rolling with the Flash graphics API. starting from the basics of creating basic shapes and all the way through dynamic animations such as the one in the preview page of this course.

In this title you will learn how to draw lines using the AS3 graphics API, draw a full shape and fill it with color. using beginFill and endFill used in AS3 while in JavaSCript API we will learn the names and interactions within our current Canvas video. In many ways we will learn in our AS3 title many of the same things we are learning in this title so if you are one of our members I strongly encourage you to check out this title as well - as its more in depth.

HTML5 101

If you are into the future and into JavaScript then you are into HTML5. We got a great title for you that covers all of the major updates from HTML4 to HTML5. We called this title HTML5 101 and is designed for people that have a basic understanding and worked with HTML before but have no or a low amount of experience with HTML5.

Ever have a dream that you where an HTML5 Hero? That's odd most people do... well let the dreams rain on you with this supper cool new course that will drive in and make your new dream come true. The topics in this course range from the technical changes to HTML into the depths of working in JavaScript in an html5 world. It's time to start to learn html5 before everything things your an old school developer. Did someone say html5 video?. In this title we cover an HTML5 canvas tutorial that is more in depth then the one in this title.

Video Transcript

00:00So let's begin by just going right into our file.
00:02We're going to have to do a couple of changes
00:04to make this work.
00:05The first of them is that we're going
00:06to want to add our canvas.
00:08So I'm going to pick here right underneath the header
00:12because I want to keep it inside of that top div.
00:15So inside of there, I'm going to create a new canvas.
00:20And I'm going to give it an ID of my canvas.
00:26And again, although we didn't really learn about canvas,
00:28this is an HTML5 new property that
00:32enables us to draw dynamically using JavaScript
00:36into our stage.
00:38All right, so our canvas element is saved.
00:40Now, the last thing that's left for us to do
00:42is to add a little bit of CSS.
00:44So let's start, even though we need to add the CSS,
00:48let's start first going into the JavaScript.
00:50I created a colors.js.
00:52We'll see what's wrong with it, and then we'll fix it.
00:56So we're going to start off by going into our JavaScript file.
00:59And the first thing we want to do in the JavaScript
01:02is we want to listen to when the document is done loading
01:05because we don't want to try to access our canvas
01:07before our application is completed to load.
01:11Now, until now, we haven't really
01:12worked with something that's called
01:14events or listeners.
01:16And this is exactly how we work with them.
01:18It's very easy.
01:19Events or listeners are basically
01:22functions that get triggered whenever something
01:25happens in the machine.
01:26There's mouse events.
01:27There's background events.
01:30In this scenario, we want to listen to the event
01:32where that document completes the load
01:34or our window completes the load.
01:36And the way we do it is we ask our window
01:39by typing window.onload.
01:41And we say onload.
01:43Let's put inside of it a function.
01:45Just as much as we did with object properties,
01:48this function, we're basically assigning a function
01:50into that variable, making it possible in the background
01:54for that function to be called.
01:56So I'm going to set it to be init.
01:58And right after that, I'll create a function called init.
02:02And basically, this init function will be called
02:04as soon as our application loads.
02:06And we could test this out by putting your little alert.
02:10And we could say here, hello world.
02:13And our hello world, if we go into our element again,
02:16in our document and refresh it,
02:18we'll see our JavaScript alert, hello world.
02:21We know that it works.
02:22It gets called as soon as our load completes.
02:25All right, so let's move on to the next step.
02:27And our next step is going to be,
02:29we want to define our canvas size.
02:31So I'm going to start off with defining
02:33what width I want my canvas to be.
02:37And the width I want it to be is 484 pixels,
02:40which is actually the width of our element.
02:43And I want to set my height to be 280.
02:46Now don't worry about the colors here.
02:49You know, sometimes these colors mean something.
02:51Sometimes they don't.
02:52The word width or height are not reserved variables
02:56versus var and function window are.
03:00So, you know, sometimes it's hard
03:02and there's a plane passing by
03:04because I live next to the airport as well.
03:07Okay, which sometimes is funny
03:09because like you guys might not even hear it
03:11after all the filtering,
03:12but it's so confusing to have all these noises.
03:15All right, so moving on.
03:17So I'm going to set a variable
03:18and I'm going to call it my canvas.
03:23And into it, I'm going to call
03:24and I'm going to get that element using document.
03:28I'm going to go with the document itself
03:30and I'm going to use a function called get elements by ID.
03:35Oh, not elements, but element by ID.
03:39After I set that in notice,
03:40again, the camel casing is important.
03:43The exact way is important.
03:46And I'm going to call my canvas element,
03:49which is where is our canvas?
03:52My canvas.
03:53So I'm going to call my canvas.
03:55And now I have the canvas element in this variable.
03:59I'm not ready yet to use it,
04:01but I am definitely ready to set its width and its height.
04:05So I'm going to go to that canvas dot width
04:09and we could set here.
04:10And by the way, in our scenario, it doesn't matter.
04:14So I'm going to set here my width to be the width.
04:17I'm going to set my canvas height to be the height.
04:22Now, even if we refresh our application now
04:25and run our browser, we're not going to see anything.
04:28We're not going to see anything
04:29because we didn't draw anything
04:31really into our canvas element, all right?
04:33But what we are getting here,
04:36oh, it's actually, we are getting something
04:39because it's pushing all our content down.
04:41And the only reason it's pushing our content down
04:43is because it's actually, our canvas is right here.
04:46So it's pushing our footer down,
04:48although we didn't put anything in it.
04:49But just to resolve this issue,
04:51how about we go inside there
04:53and actually draw something inside
04:56so it's more visible to us, all right?
04:58So the next step is to be able
05:01to actually draw into a canvas.
05:03We have to call something that's called a context.
05:06And there's a lot of different types of contexts,
05:08but for the level of our training right now,
05:11it's probably not important.
05:13So I'm going to go into my canvas
05:15and the way it works is basically I call a function
05:17that's called get context.
05:20The only thing you need to know
05:21is that there are a few different types of context.
05:23And basically these operations
05:26that we're doing right now,
05:27every single time you work with a canvas,
05:29you want to do, you want to get the element.
05:31And then you take that element itself
05:33and you go to a method within it
05:36which is called get context.
05:38And then you decide what context you want.
05:40Now really the context itself
05:42is basically what API do we want to use
05:44to be able to draw?
05:46And there's a few different APIs
05:47and there might be more in the future.
05:49But we're focused on the 2D API
05:51which is a two-dimensional drawing API.
05:55Now by the way, if you want to learn more in depth
05:58about things related to drawing
06:00with a drawing API in general,
06:02we have our HTML5 course
06:05and we also have our action script drawing API class
06:11which is very, very similar
06:12and has a lot of, it's not exactly the same thing
06:15but it has a lot of similarities.
06:17All right, so let's move on with our context
06:19and we want to actually do something in it
06:21and I'm just going to do something really small.
06:22I'm going to take my context and draw my first something.
06:26So I'm going to first pick a color
06:28by setting the set style
06:31and I'm going to set a style.
06:33And in my case, I know exactly what color I want to use.
06:35I want to use this very predefined color
06:39that I already selected earlier.
06:42And I want to also fill a rectangle,
06:46basically enabling me to draw a rectangle
06:49in whatever area I want
06:51starting with my starting X and Y points
06:53which are going to be on the top left corner.
06:57And we're going to continue with our width that we want.
07:00In our case, we want the full width of our canvas
07:02and the height that we want
07:03which currently we want the full height of our canvas.
07:07And that's all we had to do
07:08to actually do something.
07:10Now, if you go into our application and we refresh,
07:12now we'll be able to see our element.
07:14And the next step that we want to do
07:16is we want to make sure that this element
07:18is really aligned right in the top corner of our HTML.

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!