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

Preparing our JavaScript for Canvas

Although most modern browsers support Canvas not all of them do. While we won't create an optimal experience for users without HTML5 canvas support we want to create a graceful fallback experience. So before we start our journey into canvas in this lecture we will prepare our source files to accommodate failure.

Keynotes:

  • The basics of browser compatibility tests
  • Creating a canvas fallback
  • Working with the ternary operator

Video Transcript

00:00In section number one, we're going to get started
00:03with HTML5 Canvas, the 2D API.
00:07But even before that,
00:08we're going to prepare our JavaScript for Canvas
00:11by introducing it and doing our low world sample
00:14and drawing our first something,
00:17which will be a circle actually.
00:19Once we do that,
00:20we're going to create the Canvas element dynamically as well.
00:24So really the main focus of this session
00:27is getting us through the wrapper of the Canvas.
00:31So let's go ahead and start with the first lecture.
00:34And in the first lecture,
00:35we're going to prepare the groundwork
00:36and the JavaScript for Canvas.
00:38What does that mean?
00:40That means that we want to make sure
00:41that the browser actually supports Canvas
00:43and is compatible to it.
00:45And we want to also make sure that if they're not,
00:47that we'll create a fallback
00:48that will give the user some indication
00:50that their browser doesn't support the operations
00:53that we're going to do.
00:54Beyond that, we're also going to look at something
00:56that's called a ternary operator
00:58because I'm going to use that throughout this title
01:00instead of using if conditions.
01:02So let's jump right into it.
01:06Alrighty, let's get started.
01:08And you'll find immediately in your source files,
01:10we're going to start off with a Canvas
01:12directly already embedded inside of our HTML
01:15inside of our Canvas wrapper.
01:18You'll see I also assigned an ID to this Canvas element.
01:21And I went ahead in our CSS
01:23and I've also added a width, a height
01:25and a background color for our Canvas.
01:28And if I just go ahead here and just save this,
01:30go back into my browser,
01:31you'll see that here is my Canvas area, completely empty.
01:35Now, before we continue onwards to the Hello World sample,
01:39which we'll see in the next lecture,
01:40we still need to make sure
01:42that our users actually support Canvas, HTML5 Canvas.
01:46And yes, it's true.
01:47Most browsers this day support, if not all browsers,
01:50but some users are still lagging behind
01:53and we want to create something for them.
01:55Although not a full experience,
01:57we just want to let them know
01:58that it's probably a good idea for them
02:00to update their browser.
02:02To do that, I'm going to go ahead in my source files
02:04and I already have pre-configured our source,
02:07our script file, our JS script file.
02:10So I'm going to go right into it
02:11and I'm going to start building our application.
02:14Now, throughout all the source files in this course,
02:16we're going to use an anonymous function.
02:19What does that mean?
02:20All that literally is is a function,
02:22which is wrapped around,
02:24it's basically a function that is self-executing.
02:27It doesn't have a name.
02:28To be able to do that,
02:29I'm just going to wrap that function with a round bracket
02:32because some browser versions would throw an error
02:36if you don't give a name to a browser at all.
02:38So by doing this, we're avoiding an error.
02:41Now that we have the function,
02:42I'm going to automatically trigger it.
02:44The idea behind this
02:46in setting all of our code inside of here
02:48is that now we're not going to affect the global scope
02:51because if we create variables inside of this mega function,
02:55all these variables will have an internal scope
02:57and they won't affect the global scope.
03:00With that said,
03:00let's go ahead and create our first internal function
03:03and I'm going to call it by ID.
03:05Now, by ID accepts an ID
03:07and the reason why I'm doing this
03:08is literally just to save us a little bit of space
03:11on the screen and I'm going to make sure
03:13that I'm just going to call get element by ID.
03:17By ID, not by tag name.
03:19And I'm going to go ahead and also assign the ID
03:23that is being sent inside of that function to that element.
03:26And we're going to use this throughout our samples.
03:28Really, if there's no logic here,
03:30it's really just to make it a little bit shorter
03:32so we don't have to call document.get element by ID.
03:36Now that we created that helper function,
03:38the next step for us is to actually wait for the document
03:41to complete loading.
03:42And again, most browsers support the document.add event
03:49listener.
03:50The document add event listener
03:53is an event listener that has been around
03:56for quite a long time
03:57and is supported by all modern browsers besides IE8,
04:01which is kind of modern, not really modern,
04:03but still it is widely used around four or 5% of users
04:07today, 2015, nearly 2016 are still using that browser.
04:12So although we're not going to give them the experience
04:14that we would hope for them to get,
04:15we don't want them to get errors.
04:17We want to give them a graceful fallback.
04:19To do that, I'm going to create a ternary operator
04:21and I'm literally, all I'm asking here is,
04:23is there a value here?
04:25If there's no value here, then nothing will come back.
04:27And then I set a question mark.
04:29Once I set the question mark,
04:30I'm going to put inside the if conditions content.
04:33And in this case, what I want to do,
04:34if that condition is met,
04:36I'm going to go ahead here and literally add the event
04:39of DOM content loaded.
04:42When the DOM content is loaded,
04:44what I want to do is I want to call a function
04:46which I'll call on DOM content loaded.
04:50Now that's case number one.
04:52Case number two, I'm going to put here a colon
04:54which is our else condition.
04:56And in our else condition,
04:57I'm going to go ahead and call the window dot on load
05:00and call the do canvas not supported.
05:04So just to follow back on this,
05:06if the browser supports add event listener,
05:09that means we're in a modern browser
05:11or modernish type of browser.
05:13And then I'm going to call the on DOM content loaded.
05:15So let me just go ahead here and create that function
05:17really quickly on DOM content loaded.
05:21And we're going to fill it up in a second, TDB.
05:24The next function is the do not,
05:27do canvas not supported.
05:29And in this case, we want to trigger the events
05:32that will happen when canvas is not supported.
05:34And again, TBD.
05:36All right, so we set the logic here
05:39and literally the logic is simple.
05:40We're asking, do you have add event listener?
05:42Great, if you do then add this event
05:44because all items that have add event listener
05:46have the event DOM content loaded.
05:49Whoever doesn't, we're going to go ahead
05:51and call the window on loaded,
05:52which is for the major part,
05:54we're talking about internet explorer,
05:55but it might be also really old browsers.
05:57For that matter, whoever doesn't support this,
05:59but supports that, we'll have an on load for them
06:01that will then, once a document is loaded,
06:04we'll go ahead and actually do the things
06:05that relate to the browser not being supported.
06:08All right, now that we know that we're inside
06:10of our function on DOM content loaded,
06:13we know that we have a relatively modern browser,
06:16but it does not mean that that relatively modern browser
06:18supports canvas.
06:20To be able to trigger that logic,
06:22I'm going to go ahead and just fetch my item
06:24and I'm going to ask for my canvas,
06:27which is a canvas element,
06:28which we've seen in our HTML down right here.
06:32Canvas, my canvas.
06:34What do I want to do with my canvas?
06:35I want to check to see if it has the function get context.
06:39Now get context is a function of,
06:43a method really of canvas,
06:44which we'll look at in the next lecture.
06:47But for now, I'm literally asking,
06:48do you have the get context?
06:50If you do, great, let's do canvas.
06:54If you don't, I'm going to trigger the do canvas function.
06:56So let me go ahead here and create that function.
06:58Do canvas.
07:00And this is going to be the TDB
07:02of the area where we know that canvas exists.
07:05On the other hand, we want to trigger
07:07the do canvas not supported.
07:09So it's an either or type of condition.
07:11And literally what I did here in these two phases,
07:14if I just go through it really quickly,
07:15I checked to see if we had an add event listener.
07:17If I did, and I had a get context,
07:20that meant that canvas is fully supported
07:22and I could start doing canvas related things.
07:25If not in all other cases,
07:26I would end up at our do canvas not supported,
07:29which is what we're going to,
07:30the logic that we're going to apply right now.
07:33So now that we're in our do canvas not supported,
07:35what I really want to do is I want to go ahead
07:37and replace the content in our canvas wrapper
07:40and replace it with an image that I created in advance
07:43and that image you'll find in your source files,
07:45which I called JS slash dash, sorry, dot JPEG.
07:50So let me go ahead there.
07:51And what I'm going to do is I'm going to go by ID,
07:54by ID, and I'm going to call the canvas wrapper.
07:58And I want to go ahead to this inner HTML.
08:00And again, the inner HTML is so widely supported
08:03that we're not going to even check
08:04to see if it's not available
08:05because most 99.99% of all modern browsers support it.
08:10If they don't, we're willing for them
08:12not to have a good experience
08:13because we're really talking about really old browsers
08:15and there's a limit of how far back
08:17could we create a support.
08:19And I'm going to go ahead
08:20and I'm just going to paste inside here the image source.
08:22And I'm going to just set here as our new inner HTML
08:25to replace the canvas with an image with our sorry JPEG.
08:29If I go back into my browser,
08:31if everything is working according to plan,
08:33we won't see anything inside of our error area here.
08:36But instead of that, we should be able to see nothing.
08:38Beautiful.
08:39Let me just go ahead here and also in my do canvas,
08:41I'm going to go ahead and do a console.log.
08:45What will we write here?
08:46Let's write here, let's write here, let's write here.
08:49We are ready to canvas.
08:52And let me just go ahead here, save my file,
08:55go back into my browser.
08:56And if we see that we are ready to canvas,
08:58that means we're in a browser that supports canvas,
09:01which is basically says that we're ready
09:03for the next lecture.
09:04But just to test out and see what happens in a browser
09:06that doesn't support it,
09:07we could go to a browser that doesn't support,
09:09or we could go ahead here and just alter the logic.
09:12You're saying that if there is no canvas,
09:14so we're going to force ourselves to do the canvas
09:16not supported to see what would happen.
09:19If I go ahead here, we'll see that a JS sorry,
09:22to view the section of the site,
09:23you must be running a modern browser.
09:25And that's really the idea of this section
09:28of this lecture.
09:28We basically created the groundwork.
09:30From the next lecture onwards,
09:32we're going to assume that the user has a browser
09:35that supports canvas.
09:37And we're going to assume you're working
09:38with a browser that does as well.
09:41But I'm going to go ahead and just remove that not
09:43to make sure that that condition
09:44would actually really would apply.
09:46And we'll see you in the next lecture
09:47where we actually start working with the canvas element.
09:51In this lesson, we were introduced
09:53to the canvas environment and created our first check
09:57to see if canvas is supported or not.
09:59In the next lecture, we'll go ahead and continue
10:02and build our first hello world canvas example.

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!