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

Jim Schofield
Jim Schofield
11,566 Points

lastEvent undefined... not drawing?

I'm trying to figure out why the drawing is not working. (This is also my first question. How can I embed script formatting to my question?)

// no user interaction
// when user interacts cause changes
 var color = $(".selected").css("background-color");
var $canvas = $("canvas");
var context = $canvas[0].getContext("2d");
var lastEvent; 

// when clicking on list
$(".controls").on("click", "li", function() {
  // deselect sibling elements
  $(this).siblings().removeClass("selected");
  // select clicked item
  $(this).addClass("selected");
  // cache current color
  color = $(this).css("background-color");
});

// when button is pressed
$("#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 update new color span
$("input[type=range]").on("input", changeColor);

// when add color is pressed
$("#addNewColor").click( function() {
  //append the color to the controls
  var $newcolor = $("<li></li>");
  $newcolor.css("background-color", $("#newColor").css("background-color"));
  $(".controls ul").append($newcolor);
  $newcolor.click();
});



// on mouse events on canvas
$canvas.mousedown( function(e) {
  lastEvent = e;
}).mousemove( function(e) {
  context.beginPath();
  context.moveTo(lastEvent.offsetX, lastEvent.offestY);
  context.lineTo(e.offsetX, e.offsetY);
  context.stroke();
});
    //draw lines
Greg Kaleka
Greg Kaleka
39,021 Points

Hi Jim,

For formatting code, you do the following:

```javascript

[your code here]

```

Note those are back-tics, not apostrophes. Top left of your keyboard, under escape. If you're using html or css, you would type those in place of javascript. For console logging type "code" you can leave off the language for a more generic, unhighlighted code block.

I took the liberty of adding the opening tag to your question (if there's nothing after your code, you can leave off the closing one).

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hey Jim,

Wow. It took me WAY too long to figure that out.

You have a simple typo. Your third-to-last line of code is:

context.moveTo(lastEvent.offsetX, lastEvent.offestY);

See it? off es tY, instead of off se tY.

Little things are always the ones that get you.

Cheers :beers:

-Greg

Jim Schofield
Jim Schofield
11,566 Points

Thank you much! Things are squared away now.