Integrating Canvas into your Web App

HTML5 Rocks

Introduction

In this article, I am going to discuss how to use the HTML5 canvas element to create, edit, open and export images. I will also introduce several open-source tools that are relevant to this technology, and provide some tips on how these techniques can be applied to an existing web application.

Check for canvas support

The first thing to do is check that your browser fully supports HTML5 canvas. An easy way to do that is to use Modernizr to check for a certain feature:

if (Modernizr.canvas) {
  // Browser supports native HTML5 canvas.
} else {
  // Fallback to another solution, such as Flash, static image, download link, and so on.
}

Creating a canvas element and importing an image as binary or data URI

First, you will need to have a canvas element in your page.

Using JavaScript, you do the following:

var ctx = document.getElementById('new_canvas').getContext('2d');
var img = new Image();
img.src = "html5.gif"
img.onload = function () {
   ctx.drawImage(img,0,0);
}

This is the result on a canvas element (inspect the source code with your developer tools):


In this code, the first step is to get the 2D context, which gives us access to the API that defines all the drawing methods and properties. Next, we create an image object and set the src property to the location of the binary image. When the image is loaded, we then use the drawImage() method to import the image into the canvas element.

You can also use a data URI instead of the URL of an image. So instead of the URL above, you can do the following:

img.src="data:image/gif;base64,R0lGODlhyAD3APcAAAAAAAoKChISEhsbGyAcHSMjIyomJysrKzMvMDMzMzk2Nzw8PEJCQklFRkpHSExMTFJSUltbW2NjY2hmZmpnaGtra3BtbXNzc3x8fOJHIOJLJeNOKOVRJuhWJ+lZJ+NQK+xdKONVMuRYNeRdOu5iJe5iKPBlKfBoLeVgP/BrMvFyPIF+f+VkQ+dpQ+RnSOVqS+hsTfF2QfJ5RvF9S+RuUehuUORwUuhyVOR0WOl2WeV5Xul5XOV9Yup9YvKAT/KDU/KGWPKJXOWAZ+qBZuWFbOuEa+yIb/OMYPSQZPSUauaJcuyLc+aOeO2PeOaRfO2Se/WZcvWeePWhfISEhIuLi5KSkpqamqOjo6urq7Kysrm3uLu7u+aWgu6WgeaYhO6ahOecie+difCeieegjvWlgvaoh/Chjfari+emlueqm+qrmfCkkfGolfGsm/exk/e1mfi1mfi5nuevoeiuoOizpei1qei4rfKzo/a7ovi9o/O3qPO5q+i9svS+sfnDrOjAt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cA+TdegLAbIYgoFZrPi2NuORWSKWmtCsbKCOhNJgFEXQoQj2xz+mvghLDC4TGYIrljkgW9XWWy+CCo41IEstnDgih4z3tbZK6zAICFGJABlzywQWaCCpbnEwK6AcZMgEWOpEFOiKkSyqqCuRBooRyzywgKlB6qYzqAq8HUIg5KcnaIKVLCo125ELghQlS0eqNgKBlhbwDSsMw6FEUZeQsWxUzzJCFmGUMkIdYM86VEVTz6faGCbbv3Q3gGMs9tODFJ272QMHrIgLVAYyYk5hYYnfZXOGWGuR1SI3zx66YGmasjHBnDzUAEA0JQ9lAZQ5JSGP1cllCsGECCqhVAZXbDp34efNwwCQKQmCHRFRbSRyvvwtAQKaBgD2tQehmwBSFeLXwL+FXiRPeWJCtkqSCgqwD8BVCA/5hqNABx2uuMs0IMffMsW8lIQo9REC2XCwBbc44krTIEKiNAWCGU4Q57EkIY3xGEOdbhDHvbQhz8EYhCFOEQiFtGIRxyiKj4xMyQ2MX6JoEADyHQ2J1YxeopYAAEQgAACQAATVqwhThSSk7xkJC9kNIgZDZITm6jRR2O0GxzL+EYxzvGMXEmjKlCBAQMgYAUUQIABrEDGO9pxjXTE4x3bSMcRCsUtaIxjIdOISDZOcoSoSARrNLlJTnbSk58EZShFOcpRasEBCrCCJxZBBQdpgZSvhGUsZfnKRJARFXpE3S1zeUtc7rKXquhlLnFkCUzUEVOXxfQlMoUZTGXycpfKNCYxTSEBAkjgE4xoAAGmcMxgGjOZxWQmN4c5TGF+s5vh/CU6wbnOY4LxJlcgQAQmYAEHHCAs7sSnxjDARQIoQHH5BGiRUKGFKlQBESMMKEQCAgA7"

You may ask, "Why would we use data URI instead of the binary image?" There are many advantages. And later in this article, you will see how easily we can export a canvas image as data URI.

Here is a tool to convert a binary image file to a data URI

Manipulating the canvas image

If you have ever done any sort of Logo programming, drawing on a canvas uses the same concept.

Mark Pilgrim has a chapter on canvas in his book, Dive Into HTML5. Based on an example in the chapter, we can add a grid diagram to the image that we imported above by using the following:.

var img2 = new Image();
img2.onload = function () {
  var context2 = document.getElementById('new_canvas2').getContext('2d');
  /* vertical lines then horizontal ones */
  for (var x = 0.5; x < 800; x += 10) { context2.moveTo(x, 0); context2.lineTo(x, 500); } 
  for (var y = 0.5; y < 500; y += 10) { context2.moveTo(0, y); context2.lineTo(800, y); }
  context2.strokeStyle = "#bbb";
  context2.stroke();
  context2.drawImage(img2,0,0);
}
img2.src = "html5.gif";

The result looks like the following:


You can be more creative than this, but I leave it to the other tutorials listed in the appendix of this article for additional instructions on that subject.

We haven't seen anything very exciting yet, but the next section will change that...

Exporting the canvas image as data URI

The canvas element has a toDataURL() method, which takes a MIME type as the parameter. With this, we can export the canvas we used above.

window.open(document.getElementById('ctx').toDataURL("image/png"));

This exports the canvas as a PNG image into a new browser window. The image, however, is not an ordinary binary image, but is instead a base64-encoded data URI that can be rendered by a browser. Thus, from the user's standpoint, there is no difference between that and the binary equivalent.

Note that the line of code above needs to be run on a web server. Running toDataURL() on a local file would fail. See this ticket for the status of this issue in Chrome.

Integrating into your web app

Canvas can be a very powerful add-on to any web applications that store user-uploaded images.

For example, we have an online file storage application that stores user-uploaded images. We can add an edit button to open the image file in a canvas-based picture editor.

If you don't want to write your own canvas editor, Harmony is one of the few canvas editors openly available. It features easy addition of brushes, which can satisfy your artistic tastes.

When you choose "edit image" in the menu illustrated above, a canvas editor should open, and it would make a call to a custom read_file() function in the editor's init() function as follows:

function read_file() {
   var url = file_id;
   // hide a copy of the original image if it is needed to load
   document.getElementById('editableImage').src = url; 
   image = new Image();
   image.src = url;
   image.onload = function() {
      context.drawImage(image,0,0); // context, defined above, as canvas.getContext('2d')
   }
}

Adding HTML5 LocalStorage

A little touchup that you should always consider--if you care about user experience--is applying LocalStorage. For example, if you have a big text area that requires the user to input a lot of information. When the user is about to submit the form, he accidentally closes the browser (or the browser crashes). The user might be frustrated and not bother rewriting the message again.

In the demo below, instead of saving the data onto the server, simply save the image onto LocalStorage as data URI:

// Save Image
function saveToLocalStorage() {
    localStorage.setItem('canvas', canvas.toDataURL('image/png'));
}

// Load Image
function init() {
        // for demo purpose, all variables are declared in the parent scope
        canvas = document.createElement('canvas');
        context = canvas.getContext('2d');

        // Use Modernizr to detect whether localstorage is supported by the browser
        if (Modernizr.localstorage && localStorage.getItem('canvas'))
        {
            localStorageImage = new Image();
            localStorageImage.addEventListener("load", function (event) {
                //...
                context.drawImage(localStorageImage, 0, 0);
            }, false);
            localStorageImage.src = localStorage.getItem('canvas');
        }
//...
}

Harmony Demo - put your signature here and it's going to be stored locally using LocalStorage. What that means is that even after page refresh, what you have drawn in the canvas below is going to stay.

Saving canvas as a binary file onto the server

You may want to save the canvas image as a binary file. There are many ways to do that. For example, you can perform a POST action to pass the data URI to your backend code. Using jQuery, it would look like this:

var url = '/api/write/' + file_id + '?data_url_to_binary=1';
var data_url = flattenCanvas.toDataURL('image/png');
var params = { contents: data_url };

$j.post(url, params, function(json){
   if (json.status == 'upload_ok')
   {
      //ok
   }
}, 'json');

This creates a XHR call with the content being the data URI.

You then need to decode the base64 data URI on the server. In PHP, for example, you can do the following:

if ($_GET['data_url_to_binary'])
{
   $contents_split = explode(',', $contents);
   $encoded = $contents_split[count($contents_split)-1];
   $decoded = "";
   for ($i=0; $i < ceil(strlen($encoded)/256); $i++) {
      $decoded = $decoded . base64_decode(substr($encoded,$i*256,256)); 
   }
   $contents = $decoded; // output
}

In the first two lines, the data URI ($contents) is split into two parts. 'data:image/png;base64', and 'VBORw0KGgoAAAANSUhEUgAAAWwAAAB+CAIAAACPlLzKAAAACXBIWXMAAC4jAAAuIwF4pT92...' We will then use base64_decode() to decode the data URI string. The trick here is that there are issues decoding string greater than 5K, and this "divide-and-conquer" approach will be able to decode the string.

Finally, using fwrite(), you can save the binary file, $contents, onto your server.

Enabling "save image" in browser

Canvas is a HTML element. It looks pretty much like an image, but your browser does not provide a "Save Image As" option for it because it is not really an image element.

To enable "Save Image As," you may dynamically create an Img element, and set the src to the data URI of the canvas element.

You can also use the canvas2image utility.

A more advanced canvas editor

If you are looking for a more advanced canvas editor, PaintWeb is probably worth a try. It was written by Mihai Sucan, a Romanian student, during the Google Summer of Code 2009. He also authored a few tutorials on writing your own online paint application.

For a more professional library, be sure to check out Pixati.

More fun with canvas?

Paul Irish combined Harmony and $1 Unistroke Recognizer to create a little Easter Egg on his website.

You can also learn how to inspect canvas with the Chrome DevTools using our recent inspection features.

Go deeper with additional tutorials on canvas

Comments

0