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 trialGrace Nellore
9,447 PointsUncaught ReferenceError: e is not defined (chrome & safari)
Hello, I was wondering if anyone else has come across this issue, and has any ideas?
In jQuery Basics > Creating a Simple Drawing Application > Perform: Part 5 (https://teamtreehouse.com/library/jquery-basics/creating-a-simple-drawing-application/perform-part-5)
I am getting following errors:
- Uncaught TypeError: Cannot read property 'offsetX' of undefined
- Uncaught ReferenceError: e is not defined
Code so far (~6:19m in video):
// 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; // <--initializing lastEvent to "{offsetX:0, offsetY:0" addressed 1st issue
// 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 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
var $newColor = $("<li></li>");
$newColor.css("background-color", $("#newColor").css("background-color"));
$(".controls ul").append($newColor);
// Select the new color
});
// On mouse events on the canvas
$canvas.mousedown(function(e){
lastEvent = e;
}).mousemove(function(){
// Draw lines
context.beginPath();
context.moveTo(lastEvent.offsetX, lastEvent.offsetY);
context.lineTo(e.offsetX, e.offsetY);
context.stroke();
});
Following GΓΈran Smistad's proposed solution in (https://teamtreehouse.com/community/simple-drawing-application-pt-11) resolved 1st error. (I don't understand this either, as Andrew's code worked without setting value after declaring lastEvent. Is this just something that changed with time?)
But this 2nd error ("Uncaught ReferenceError: e is not defined") is not going away, and I am unable to find a resolution so far...
If anyone has any idea, please share...
Thanks in advance!
1 Answer
Grace Nellore
9,447 PointsWow I looked at this line by line, and I still totally missed it! Sorry all, and I will keep this just in case anyone else has similar issue/for reference.
Original:
// On mouse events on the canvas
$canvas.mousedown(function(e){
lastEvent = e;
}).mousemove(function(){ // missing e
// Draw lines
context.beginPath();
context.moveTo(lastEvent.offsetX, lastEvent.offsetY);
context.lineTo(e.offsetX, e.offsetY);
context.stroke();
});
Working now:
// On mouse events on the canvas
$canvas.mousedown(function(e){
lastEvent = e;
}).mousemove(function(e){ // e added
// Draw lines
context.beginPath();
context.moveTo(lastEvent.offsetX, lastEvent.offsetY);
context.lineTo(e.offsetX, e.offsetY);
context.stroke();
});