Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. JavaScript

How to Create a Web-Based Drawing Application Using Canvas

Read Time:21 minsLanguages:

Combining HTML with the all new <canvas> feature, you can make some pretty awesome web apps! In this tutorial, we will create a neat interactive drawing application using HTML and JavaScript. Along the way, we'll also learn the basic concepts of the all new <canvas> feature.

The <canvas> tag is a relatively new element which is quickly gaining in popularity. It can be used for a variety of different things, such as drawing graphs, shapes, images, applying styles and colors, making photo compositions, and even some simple, neat animations. Today, we'll build a simple online drawing application, similar to http://www.mugtug.com/sketchpad/

Creating a canvas in your site is as simple as adding the <canvas> tag to your HTML document, as shown below:

The element 'ID' is not required, but is highly recommended so that you can find it later in your JavaScript and/or CSS.

Step 1 What We're Making

Below is an image of what we will be making. Yours may look a bit different when we are completely finished, depending on the type of styles you wish to use, etc.

As you can see, I am a terrible drawer, but maybe after you are done creating your chalkboard, you can create an awesome masterpiece! Besides, we're building the chalboard, not the drawing!

Step 2 What You'll Need

You'll need a basic understanding of HTML and JavaScript for this tutorial.

  • Photoshop or another image editor
  • Code editor of your choice
  • Basic knowledge of HTML and JavaScript
  • <canvas> supported browser (Chrome, Firefox, Opera, Safari)

I will also be using the free Fugue Icon Pack that can be downloaded here: http://p.yusukekamiyamane.com/

Furthermore, we will be using the following scripts to accomplish the final outcome.

Step 3 Getting Started

Without further ado, let's jump right in!

The HTML Markup

Begin by opening your coding application, and create a regular HTML document. Copy and paste the code below. This is the basic structure of our drawing application. Save the document in a new folder, labeled canvas (or anything you prefer). Now in the same level as your HTML file, create three new folders.

  • JS
  • CSS
  • Images

This is the basic markup of our page. Before we begin writing the actual code, let's style this up a bit. I will not be showing you how to make the design in Photoshop, but you can download the images here. Be sure to copy all of the images into the images folder that you just created.

The CSS Markup

Create a new stylesheet named "styles.css," and save it in your "css" folder. Copy and paste the following basic CSS code below to your stylesheet.

Step 4 The Canvas Tag

We are going to insert the canvas tag right into the blackboardPlaceholder. Place the below code inside the 'blackboardPlaceholder' ID.

This is the exact width and height of the canvas we are creating so no need to change that. Also, as I mentioned above, anything in between the canvas tags will be displayed only if JavaScript has been disabled, or the browser does not support canvas.

Next, let's style the ID up a little bit. In your styles.css file, add:

I will explain the #tempCanvas ID shortly, when we begin writing the JavaScript.

Step 5 JavaScript Implementation

Let's now make our canvas function the way it should. Begin by creating a new JS file, and naming it "canvas". Save this file within the 'JS' folder.

Next, we will add a function to determine if there is a canvas tag in the HTML document using the addEventListener and onLoad function. Paste this right underneath the variable we created.

Now, let's add some default variables and some error messages - if anything went wrong.

We've created some new variables and methods that will be used later.

The context.strokeStyle is the color of our stroke. We will make the default '#FFFFFF', which is the hex value for white. Next is the context.lineWidth. This is the stroke of our line. We will leave the default value '1.0'. Now we will make a grey rectangle to use later on when we add the ability to save our image. We'll fill the rectangle with grey or '#424242' and make it the exact size of our canvas.

Continuing on, let's create our drop down menu, where we can select between chalk, a rectangle, or a line.

We've just made the drawing function for the chalk! Obviously our canvas still won't work at this stage but we're almost there!

Let's make our rectangle tool now.

And now for the line tool.

And that just about wraps up our JavaScript! Now let's close the function and variables we created a while back by ending with this:

Step 6 Inserting The Tool Selection

Let's go back to our HTML page. We will now create the tool selection using a basic HTML dropdown form tag.

Directly below the start of the 'blackboardPlaceholder' tag and right before our canvas tag, add the code below to create the dropdown tool selection.

And that's it! Now we have a drop down form, let's add some styling to it.

The .noscript is the style for the text in between the canvas tag that we never added above.

Step 7 Changing The Color

Just to inform you, those are chalk pieces. I felt that due to my design skills, I had to inform you what they were because many people were telling me how they don't look remotely close to a piece of chalk :)

It's actually quite easy to change the color in the HTML. All you need to do is use the onclick function. Links, images, text, classes, etc. The function we use to change the color is below.

To change the color, just change the hex color code in between the parentheses. #FF00D2, used above, will change the stroke color to pink.

Now let's add the HTML markup for the chalk pieces.

Add the above code right underneath the </canvas> tag.

Your HTML code should look like the following thus far.

Step 8 Adding An Eraser

The eraser is simply an image that has the same stroke color as the rectangle we created in the JavaScript, '#424242'. Copy and paste the HTML below the chalk pieces we just created above.

Step 9 Styling The Chalk And Eraser

Now let's add some styling to the chalk pieces and eraser before we moev any further. Add the following to your styles.css file.

Step 10 Changing The Stroke Weight

Changing the stroke is just as easy as changing the color in the HTML. All you have to do is use the 'onclick' function:

'1.0' is the smallest stroke allowed. You can increase the stroke as much as you want.

Now let's add some stroke features to our HTML. Copy and paste the code below into your HTML document. You will need to place this outside the 'blackboardPlaceholder', immediately following the closing div tag (before the wrapper ends).

Now some CSS:

Step 11 The Save Feature

This is probably the most complex section of our tutorial. We'll be using the image/data JavaScript function to save our image. But first, we need an image library. We will use the Canvas2Image and the Base64 Library, created by Jacob Seidelin at nihilogic.dk.

If you haven't already, you can download these files here, and place them in your 'js' folder.

We need to modify the canvas.js file. Add the following to the bottom of your file.

Essentially, when you click the save button, it will convert your canvas into a PNG, and from there, you will be able to right click and download the image. Pretty neat, right!?

Now let's add the HTML:

Add the above code directly after the wrapper; this will create the save and clear buttons. Now we will add the CSS to style it up a bit.

Simply add this code to the styles.css stylesheet.

Step 12 The Advanced Color Table

To add more flexibility, we should create a color table, similar to the one used in Dreamweaver CS4.

So to do this I figured it would be actually quite easy. I would save the above color table, and then use it as a background image of a div id, and then inside, have a seperate div class for each color with the same onclick function we used above. I soon realized that this was not the best solution as it would create a mess in the HTML and would take forever to load each div. So instead I cam up with a new solution that soley consists of JavaScript. I would create each div dynamically in my JavaScript file, and insert the color code for each 'color box' in an array. So before I explain any further, we will do just that. Open your JavaScript file we have been working on, and at the very top of the file right underneath are context variable, create a new variable named "colorPalette". Then we will begin the array. Each new line of colorsI seperated with one line of whitespace in the JavaScript. There are twelve columns total so we will have a total of twelve sections in our array.

Here is the JavaScript:

And that's every color in our color table. Now let's add the JavaScript to make it work.

Okay, so the code above, we start by adding some functions to show or hide the colorTable ID in our HTML (which we will get to in a moment). This is just basic jquery for when you click the colorTable or anywhere outside the colorTable it will hide the ID. Now we will load the colorTable. Do do this we will populate it with all of the color codes we added in the 'colorPalette' array. And finally to make everything work, we will set the color of the drawing tool when a color swatch is clicked. To do this, we will simply make each color a class of 'color' and then use the array to make each hex color value the background. And finally use a function so when a color is clicked, the context.strokeStyle will equal the background image.

And for the HTML markup, you will add the following code immediately after the wrapper is started in your HTML.

And now let's add some style to our color table.

Add this anywhere into your styles.css file you created a while back.

And there you have it! Now we have over 200 different colors that we can choose from!

Step 13 Using the Reload Function to Clear the Canvas

Now we will create a simple button to clear the canvas. The easiest way to do this is simply refresh the page. So we are going to use a function I am sure you are all familiar with. If not, the below code will simply refresh the page using JavaScript.

That's it. So now let's add it into our HTML. We will use the image in your 'images' folder that looks like the radioactive symbol. I'm not sure why I used a radioactive symbol for this, but I couldn't find any other icon in the icon pack to use, so this will suffice.

Add the following image to you HTML below. Because it has an absolute position it really doesn't matter where you place it as long as place it outside of the 'blackboardPlaceholder' and within the 'wrapper'. So just to make the code look "in order" we are going to place the following code right after our ID "color".

And of course, some CSS:

And there you have it. A nice looking 'clear' button.

Step 14 Adding an Extra Feature to the Stroke Menu

After I completed this drawing application I still felt that the weight of the strokes you were able to choose were still a bit low. So I though I would make a new little function for that. I will not remove the stroke menu we already currently have, I will simply add on to it a bit. My idea was to have a plus and a minus button. The plus button would increase the stroke weight by one, and the minus button would obviously decrease the stroke by one.

So in order to do this we will find some nice plus and minus buttons in our icon pack to use. We will then write the HTML write above the ID 'storkeWeight'.

The onclick function is a bit different than what we used before, basically the '--' and the '++' values will simply add or subtract 1 from the default value. The default value is 1.0 so when you click the plus button, the stroke will now be 2.0, a bit bigger than the default.

And now the CSS:

Step 15 Adding Tooltips

In order to make are application more user-friendly we will need to add some titles to all of our buttons. Luckily I have already included all of the code in the HTML, you just didn't notice it. Basically all of our icons in the HTML code have the title tag. You all should know what title tags are, but we are going to use those tags to implement a rather nice jQuery tooltip effect known as Tipsy. I have used Tipsy many times in other projects and it is really a great plugin. You will need to download the Tipsy files here. Place the 'tipsy.css' file in your css folder, the 'jquery.tipsy.js' in your js folder, and the 'tipsy.gif' in your images folder if it is not there already.

You will need to add the jQuery library to your header. Here is the direct link to the latest jQuery library. You can link directly to this or download it to your 'js' folder:


Now we will need to call the JavaScript file for tipsy. Make sure when you call this in your header, the jQuery script comes before this script. It should like like this:

And then call the 'tipsy.css' file in your HTML. That should look like the following:

And now let's call the actually JavaScript. Place this in your header below all of the external JavaScript and CSS sources.

You will also need to add one more thing to your styles.css file to fancy up the tooltips a little bit:

Wrapping It Up

And that just about wraps it up! Congratulations. Please do let me know if you have any questions or thoughts on ways to improve this application. Thanks for reading!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.