Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics (2014) Creating a Simple Drawing Application Perfect

Placid Rodrigues
Placid Rodrigues
12,630 Points

Can we save the drawings?

Can we save the drawings created on the canvas? Can someone tell how should we go about it?

2 Answers

Hey Placid Rodrigues,

There are a couple ways to save the image from a canvas. The easiest way is to just right click the canvas and select "Save Image As..." (or something similar). You can check out my paint app here. If you wanted to add a button for users on mobile device, tablet, etc., it is easy to do that as well.

Let's say you have a button:

<button id="saveDrawing">Save Drawing</button>

And when the user clicks this button, it will pop up with a window that has the filename pre-filled out for them (they can of course change it):

//From my own paint program:
$("#saveDrawing").click(function () {
//Select the canvas with id of "canvas" and convert it into a data URL
//The MIME type supported by almost every browser in existence is PNG
//However, other MIME types may be available depending on the browser
    var dataURL = document.getElementById("canvas").toDataURL("image/png");
//Create new anchor element
    var imga = document.createElement("a");
//Make its href value be the data URL received from the canvas
    imga.href = dataURL;
//This sets the default filename
    imga.download = "myImage.png";
//Append the anchor to the body
    document.body.appendChild(imga);
//Activate its click function, which will activate the data URL and allow the user to save the image
    imga.click();
});
Placid Rodrigues
Placid Rodrigues
12,630 Points

Thanks Marcus Parsons and congrats on great job on your project !!

Anytime, Placid! And I appreciate that very much. Good luck on your projects! :)

Love Brynge
Love Brynge
12,635 Points

Hi! and thanks for the help!

I cant get this to work though. I get this error message: Uncaught TypeError: Cannot read property 'toDataURL' of null

here is my code:

//Problem: No user interaction causes no change to application
//Solution: When user interacts cause changes appropriately
var color = $(".selected").css("background-color");
var $canvas = $("canvas");
var context = $canvas[0].getContext("2d");
var lastEvent;
var mouseDown = false;

//When clicking on control list items
$(".controls").on("click", "li", function(){
  //Deselect sibling elements
  $(this).siblings().removeClass("selected");
  //Select clicked element
  $(this).addClass("selected");
  //cache current color
  color = $(this).css("background-color");
});


//save drawing
$("#saveDrawing").click(function () {
//Select the canvas with id of "canvas" and convert it into a data URL
//The MIME type supported by almost every browser in existence is PNG
//However, other MIME types may be available depending on the browser
    var dataURL = document.getElementById("canvas").toDataURL("image/png");
//Create new anchor element
    var imga = document.createElement("a");
//Make its href value be the data URL received from the canvas
   imga.href = dataURL;
//This sets the default filename
    imga.download = "myImage.png";
//Append the anchor to the body
   document.body.appendChild(imga);
//Activate its click function, which will activate the data URL and allow the user to save the image
    imga.click();

});

//When clicked reset the canvas back to an empy canvas 
$("#resetCanvas").click(function(){
        //Used the [0] to select the correct element in canvas
    context.clearRect(0,0, $canvas[0].width, $canvas[0].height); 
});


//On mouse events on the canvas
$canvas.mousedown(function(e){
  lastEvent = e;
  mouseDown = true;
}).mousemove(function(e){
  //Draw lines
  if(mouseDown) {
    context.beginPath();
    context.moveTo(lastEvent.offsetX, lastEvent.offsetY);
    context.lineTo(e.offsetX, e.offsetY);
    context.strokeStyle = color;
    context.lineWidth = 5;
    context.stroke();
    lastEvent = e;
  }
}).mouseup(function(){
  mouseDown = false;
}).mouseleave(function(){
  $canvas.mouseup();
});

Do you have any ideas on what might be wrong?

Love Brynge
Love Brynge
12,635 Points

Actually I solved it!! I had forgotten to add an ID to the canvas :)