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 Perform: Part 5

Maurice Tafolla- Cunningham
Maurice Tafolla- Cunningham
7,708 Points

Nothing is drawing on the canvas

My problem is the title. I'm using Chrome- my code looks exactly the same as Andrew's from what I can tell and nothing is happening. Check out my code.

//Problem: No user inteatcion causes no changwe to application
//Solution: When user interacts causes changes approptiately 

var color = $(".selected").css("background-color");
var $canvas = $("canvas");
var context = $canvas[0].getContext("2d");
// same as document.getElementsByTagName("canvas")[0]
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");
});


//When "new color" is clicked
$("#revealColorSelect").click(function(){
  //show color select or hide color select
  changeColor();
  $("#colorSelect").toggle();

});

//update new color span
 function changeColor() {
var r = $("#red").val();
var g = $("#green").val();
var b = $("#blue").val();
$("#newColor").css("background-color", "rgb(" + r + "," + g + "," + b + ")");
}

//When color sliders change
$("input[type=range]").change(changeColor);

//When "add color" is pressed
$("#addNewColor").click(function(){

//append the color to the controls ul
 var $newColor = $("<li></li>");
  $newColor.css("background-color", $("#newColor").css("background-color"));
  $(".controls ul").append($newColor);
//select the new color
  $newColor.click();

});


//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.stroke();
  lastEvent = e;
  }
}).mouseup(function(){
 mouseDown = false;      
});

1 Answer

rydavim
rydavim
18,814 Points

Looks like there's just a minor typo in your mouse events.

context.moveTo(lastEvent.offsetX, lastEvent.offSetY); // offSetY should be offsetY (no capital s)

I think that should do it - everything else looks good. Happy coding! :)