A simple drawing grid with HTML5 part 3

This is a continuation of our simple drawing grid series. Last week, we ended up with a functional drawing grid with a few expected controls like setting pen size and pen color. While it was functional, it left a lot to be desired from a presentation standpoint. This week, we will work on giving our application a little bit of style with css.
First, we will want to arrange the HTML a bit to support our controls being rendered to the right of the grid rather than below. This could be done with css, but for the purpose of simplicity, we will do it with a good old fashioned table.

```html





Your browser does not support HTML5 canvas









```

Instead of using buttons, to improve the appearance, we will use images for some of the controls. For the pen size selection, I chose to use text. For this, you can simply open up your favorite image editor (I chose gimp), select your preferred font, type in your label and save the image. For the other controls, you can simply use google to find images available for free via the creative commons license.
Once you have selected the images, all that is required is to change the input type of the elements from button to image and add a source path. I’ve created an images folder for this purpose, but you can place them anywhere that you choose.

```html

```

If you remember from last week, we were using the mousedown event on any button other than mouse button one to provide the eraser functionality. To make it more intuitive, a control has been added to provide that functionality instead.
The process of choosing colors by defining a button or image input per color as we did last week isn’t very scalable. To solve this problem, we will add a color selector to the the controls section. We’ll discuss the implementation a bit later, but will define the element while working with the html.

```html

```

Now that we have all of the controls defined, the html table should look similar to the following.

```html



Your browser does not support HTML5 canvas












```

Last week, our canvas used a blue background color so that it was clearly visible on the page. To improve the appearance, we will use a white background and use css to define a border. To do this, create a folder called css and an empty text file within it named grid.css. Then simply use the following to create a nice border around the canvas

```css
#container {
width: 400px; margin: 0px 12px 24px 12px;
box-shadow: 0px 5px 25px #343434;
-moz-box-shadow: 0px 5px 25px #343434;
-webkit-box-shadow: 0px 5px 25px #343434;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
```

Next, add a reference to the css script in the head section of the page.

```html

```

The last step is to implement the color picker functionality. Rather than re-inventing this functionality, we will use the fantastic color picker called [Spectrum from Brian Gridstead](http://bgrins.github.com/spectrum/). After evaluating several freely available color picker controls, [Spectrum](http://bgrins.github.com/spectrum/) proved to be far and away the easiest to implement, most compatible with all of the major browsers, and feature complete.
The first step in implementing Spectrum is to download spectrum.js and spectrum.css. I’ve placed them in the scripts folder and the css folder respectively. Once the files are in place, add them to the page.

```html


```

And finally, bind the extension to the “colorpicker” element that we defined earlier while creating the controls section. To do this, simply add the following to grid.js .ready function.

```javascript
$("#colorpicker").spectrum({
color: "#0000ff",
change: function(color) {
penColor = color.toHexString();
},
show: function(color) {
erase = false;
}
});
```

As you can see, the change event of the color picker sets our pen color and the show event disables the erase functionality. Be sure to spend some time reviewing the [excellent documentation provided on the Spectrum website](http://bgrins.github.com/spectrum/) to learn more about all of the options available with the extension.
For now, we have completed our face lift for the drawing application. The full [source code](http://www.yojimbocorp.com/demos/drawing/part3/drawing_grid.html) is available via the [demo](http://www.yojimbocorp.com/demos/drawing/part3/drawing_grid.html).