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

Browser support and canvas

Canvas is new and with it some older browsers will not support the full features. Although we won't worry about the overall experience of users that don't support canvas in this title, we want to show you the steps involved in figuring out of the user supports a feature or not.

Keynotes:

  • Creating a canvas
  • Starting our code when page loads.
  • Testing for support.

Video Transcript

00:00Welcome back, we're in section 4 now, and the section is completely and fully dedicated to HTML5 Canvas.
00:08In it, we're going to learn how to make sure that our browser supports Canvas.
00:13We're going to start drawing in Canvas.
00:15We're going to draw even a grid out in Canvas to see a complicated construct.
00:19And we're going to go ahead and animate our Canvas creation.
00:23So let's go ahead and start with the first lecture, which is about how to make sure your Canvas will actually be supported.
00:29And we're going to talk about browser support by the process.
00:33So first, we'll go ahead and create a Canvas element, and we're going to make sure that our code is not,
00:39our JavaScript is not actually started before the page is completed, loaded.
00:44Once we're done, we're going to make sure also that our browser supports Canvas before we continue onwards to the next step,
00:50which will be the integration, which will be the next step of the next lecture.
00:54So let's jump right into it and let's get our setup and configuration ready to start drawing in Canvas.
01:03Welcome back. A new section arrived and it's all about Canvas.
01:07I went ahead and made a small change.
01:09So this animation that you're seeing from the last section is actually now going to be on the back side
01:14because the front side we're going to use to create a Canvas animation.
01:18To do that, go into your source files and just make sure that you make that update
01:22by moving everything inside of the front, which was in the front, the SVG into the back section.
01:28Once you've done that, it's time for us to start creating the Canvas element.
01:32And the way we're going to do that is very simple.
01:34All we're going to do is write the words Canvas, well, the tag Canvas, directly inside of our HTML.
01:41I'm going to go ahead and make it a self-enclosed tag because there will be nothing inside of it.
01:45Although, if you wanted to create some backwards support, you could have also just created a Canvas
01:50and then inside of it put here an image of sort, some sort of an art.
01:54Or something that would say, sorry, this is only working on, or you must have Canvas, you need blah, blah, blah, blah, blah.
02:07Making sure that this would only get rendered, only will be visible if our Canvas was basically not rendering.
02:13In any other scenario, inside of our Canvas, we actually would not see that copy
02:17because that copy would be automatically deleted.
02:20But if the Canvas tag was not supported, the browser would ignore that Canvas
02:25and then just output whatever is inside there.
02:28This could be a really great solution if you wanted to create a backup into Flash,
02:32creating, for example, your Canvas as your default
02:35and maybe a Flash version or an HTML version as the fallback in case Canvas is not supported.
02:41Something that's going to get less, less likely over time.
02:44Okay, so with that said, let's go ahead and give our Canvas an ID
02:47because we're going to need to reference it throughout the next few sections.
02:51And I'm going to go ahead and give it a name to that ID. I'm going to call it My Canvas.
02:56Another few things that we need to do before we could actually do anything is
03:00one is to make sure that I already have that preconfigured
03:04that our script tag, our JavaScript, our script.js is loaded in
03:08so we can then approach it and talk to it.
03:10Unfortunately, we can't just go ahead immediately and approach it
03:13because at this stage when this file is loaded,
03:16everything below it has not been loaded yet.
03:19And we really need to wait until the Canvas is loaded before we could approach it.
03:23There are many ways of doing it and I want to show you the most basic
03:26primitive way of doing it out of many others that you could find throughout my titles.
03:31So I'm going to go ahead and in the bottom,
03:33and this is going to be the most widely supported solution to all browsers really,
03:37and I'm going to go ahead and just open here a tag, a script tag,
03:40and I'm going to go ahead and literally close that script tag.
03:45And let me just put here a few breaks so we could push this up a little bit.
03:49And all I'm going to do is I'm going to call an init function triggering it
03:52and what's happening here that it's going to be called
03:54only after everything else was done.
03:57With that said, now that we have that in place,
03:59all that we need to do is create that init function.
04:02And by creating that init function, now we're going to have a,
04:05I'm going to just test it out console.log, here I am.
04:09And let's go ahead and save it,
04:15and let's go ahead and make sure that we saved our index as well.
04:18And I'm going to go into my browser and click here on refresh,
04:20and I should see that here I am inside of my output,
04:23inside of my console, and that's exactly what's happening.
04:27Beautiful.
04:28The next step for us is to get that canvas element.
04:31To do that, I'm going to go ahead into that init function,
04:34and I'm just going to go ahead and create a canvas variable.
04:38And I'm going to go ahead and ask for that element
04:40by calling the document, get element by ID,
04:45and then fetch it by the ID, my canvas.
04:48Now I have access to the canvas itself.
04:51It's not yet the canvas item
04:53which we're actually going to be controlling, it's the wrapper.
04:57Now for us to be able to control the canvas directly,
04:59we need to tell canvas what type of control do we want.
05:02But even before doing that,
05:04we probably want to test to see if this browser
05:06supports canvas or not.
05:08For us to be able to do that because we're running it
05:10inside of our JavaScript, I could go ahead here
05:13and basically check to see if a specific method exists.
05:17And that method that we're looking for is our canvas,
05:20inside of there we should expect a function called getContext.
05:23If getContext exists, that means we have support.
05:27In any other case, we could create here some alternative,
05:37an alternative environment.
05:39But in this case, we know that we support console.log,
05:42that is, and if I go ahead and save this,
05:44we should now get the we support, oops, canvas, can, canvas.
05:52Let me just make sure that I write it correctly all over.
05:55And if we click here on refresh,
05:57we should see we have support.
05:59Beautiful, so once we're inside of line five,
06:01we know the user has canvas and it's actually supported.
06:04And now that we know that it's supported,
06:06we're ready to start drawing.
06:08Now to draw, we're going to need to get a context.
06:11And we're going to talk about that in the next lecture.
06:13Now that we have a canvas, we know that it's supported
06:16throughout the browser.
06:17So we know that anything we do in line five and onwards
06:20is going to be supported.
06:22Now, what I'm going to do is I'm going to just create here
06:24a function that's going to be our lead in function
06:26for the next one.
06:27And I'm going to call it update canvas.
06:30And I'm going to go ahead and even send inside our canvas into it.
06:33And I'm going to go ahead, create that function,
06:36update canvas.
06:38And in the next lecture, we're going to go on
06:40and continue actually the creation process
06:42once we get our canvas inside.
06:45In this lecture, we did all the prep work
06:47and made sure that our browser supports canvas.
06:50The next lecture, we're going to go ahead
06:52and start drawing inside of 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!