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
michaelkyllo
11,330 PointsMy lastEvent is undefined in console. Can anyone help?
//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;
//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 pressed
$("#revealColorSelect").click(function(){
//Show color select or hide the color select
changeColor();
$("#colorSelect").toggle();
});
//update the 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
//Draw lines
$canvas.mousedown(function(e){
lastEvent = e;
});
context.beginPath();
context.moveTo(10, 10);
context.lineTo(20, 10);
context.lineTo(20, 20);
context.lineTo(10, 20);
context.closePath();
context.stroke();
michaelkyllo
11,330 PointsThanks Oliver, can you help me with my question?
David Bath
25,940 PointsSeems like if $canvas is a jQuery object, then you should attach the event handler like:
$canvas.on('mousedown', function(e) {
lastEvent = e;
});
Luke Pettway
16,593 PointsCould you past your HTML here? As David Bath said, $canvas should reference a jQuery object, so it should work. You can test this in codepen. Is your canvas styled in a such a way that it has width and height so it can be clicked? If you console.log($canvas); do you get an HTML output in the console?
Oliver Duncan
16,642 PointsOliver Duncan
16,642 PointsTo post code, you need a line break and the language name after the three ticks (i.e. " ```javascript")