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!

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

What 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
Steven Parker
224,872 Points

:point_right: The 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.