Fun with HTML5 Canvas ( HTML5 Canvas Free Hand drawing)


Demo


Fork the code at Github
I have worked on HTML5 Canvas before, for building a Flow Control design application. It was built entirely on HTML and Canvas with jQuery to support.
This is my second trial. What i have tried to build is Free hand drawing tool using Canvas. It is easy to achieve this. The only issue with Canvas is that once a stroke have been made there is no way to edit it, Or make any alterations to the content of the Canvas.

Building a drawing tool will require some additional options like Color selection and Brush size. But the real fun started when i tried adding the Undo Button. I thought it was not possible. But one of a great feature of Canvas made it easy.

Canvas contents can be exported as data.

canvas = document.getElementById('id-of-canvas');
var imageData = canvas.toDataURL('file-encoding'); 
//Encoding can be image/png or image/jpg
//I Think more formats are supported
The idea was to cache the data when ever the user makes a change. Its similar to take a snapshot of the canvas each time the user makes a change. And when the user presses the undo button, we just have to clear the canvas and load the imageData to it.

Comments

The demo page unfortunately does not work on IE8. I see that you've included ExplorerCanvas in your code, but it doesn't seem to make the demo work. Have you been able to resolve this in an updated version of your demo?

Hi,
Well i had tested the demo page in IE8, The canvas seemed to work. Though the main function of drawing was not that good. It was full of glitches.!!
See if you can find anything in the logs as errors.. will check it!
Cheers

Hi guys!
(First of all, i'm sorry because of my English skill, that isn't good)

I'm trying to include your great code in a draggable light-box but, when the user ends his "drag action" the properties "top & left" had changed. It doesn't break the execution but the user cant draw at the new location.

Any ideas?

Ty guys!

Hey Rafael,
its probly cos of the position of the canvas.
in my demo the canvas is positioned normal..
but when its put in a lightbox it will be positioned relative to a container which is positioned absolutely..
you might need to add some compensation for this to get the drawing work correctly..
see if you can find it or share the code on jsfiddle
Cheers.

Can you guys help me how will I implement the freehand drawing part like what is done in paint because im currently studying html5 at the moment and javascript as well. I am still a beginner :)

Hi Romiazon,
You can get the code from the Demo page. Its inside the html file. Its pretty easy if you know the basics of javascript. Also there is a book out called Foundation HTML5 canvas. It could help you learn all the advanced concepts in Canvas.
Cheers

Thank You! I'm on the part of doing the option on brush size :) done with option for colors.

I wanted to try a different approach lately allowing users to select each drawing as a different layer and even letting them add images.
let me know if you are moving in similar lines..
Thanks

It doesn't work and have tried it on the latest versions of Safari, Opera, Chrome and Firefox, all on Mac OS X Lion. Is this supposed to work only on PCs?

It should work on all these browsers on any platform.
Why don't you try some tweaks on it. Get the code here
https://github.com/naughtydavid/canvasFun

Hi,
i really like your code and i wanted to use for a little project, but i needed it's to put an image and then draw over it. So i tried usind drawImage() or just with CSS putting as back-ground the pic i want; the 'issue' is that in the first and second case the code doesnt work anymore, i can't save and it draws wrongly. i tried in other ways but everytime i put an image with drawImage it seems not working! I ask you if by chance you worked on it or if you have some suggestion. Thank you in advance.

Go Home Doodle, You are drunk