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 trialOsama Awan
Courses Plus Student 8,676 PointsWhat does `lastEvent = e` means?
I didn't get why Andrew reassign the LastEvent
to the current e object in mouse move function .
$canvas.mousedown(function(e) {
lastEvent = e;
}).mousemove(function(e) {
context.beginPath();
context.moveTo(lastEvent.offsetX, lastEvent.offsetY);
context.lineTo(e.offsetX, e.offsetY);
context.stroke();
lastEvent = e;
});`
1 Answer
Steven Parker
231,275 PointsThe event object (e) is saved to use the location for drawing.
The location where the last event occurred is passed to context.moveTo (about the center of your code snippet) to establish the beginning of the line segment being drawn. So for any mouse click or move, the last thing done by the handler is to save the current location by assigning the event object to lastEvent.
The location coordinates could have been saved individually, but it is convenient to save the entire event object.