In this article I will explain with an example, how to convert (save) HTML5 Canvas to Image using Canvas toDataURL function in JavaScript.
The drawing will be done using the jQuery Sketch plugin’s HTML5 Canvas Sketchpad (Drawing) App and later the drawing will be saved as an Image using the HTML5 Canvas toDataURL function in JavaScript.
HTML Markup
The HTML Markup consists of an HTML DIV consisting of two HTML Anchor elements for selecting Pen and Eraser for the HTML5 Canvas Sketchpad and an HTML5 Canvas element.
Below the HTML5 Canvas element, there’s an HTML Button and an Image element.
<div class="tools">
    <a href="#colors_sketch" data-tool="marker">Marker</a> <a href="#colors_sketch" data-tool="eraser">
<br />
<canvas id="colors_sketch" width="500" height="200">
<br />
<br />
<input type="button" id="btnSave" value="Save as Image" />
<hr />
<img id = "imgCapture" alt = "" style = "display:none;border:1px solid #ccc" />
Convert (Save) HTML5 Canvas to Image using JavaScript
Inside the jQuery document ready event handler, the jQuery HTML5 Sketch plugin is applied to the HTML5 Canvas element.
Both the HTML Anchor elements are assigned an HTML click event handler to highlight the selected element.
The HTML Button has been assigned a jQuery click event handler and when the Button is clicked the HTML5 Canvas Drawing is converted to a Base64 string using the HTML5 Canvas toDataURL function in JavaScript and then assigned as source to the HTML Image element.
<script type="text/javascript" src=""></script>
<script src="" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $(".tools a").eq(0).attr("style", "color:#000");
        $(".tools a").click(function () {
            $(".tools a").removeAttr("style");
            $(this).attr("style", "color:#000");
        $("#btnSave").bind("click", function () {
            var base64 = $('#colors_sketch')[0].toDataURL();
            $("#imgCapture").attr("src", base64);
Save HTML5 Drawing as Image using Canvas toDataURL function