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

pleas som one help me to under stand the last part of code

//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;
var mouseDown = false;

//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();
});

//i cant under stand from this  to end 


//On mouse events on the canvas
$canvas.mousedown(function(e){
  lastEvent = e;
  mouseDown = true;
}).mousemove(function(e){
  //Draw lines
  if(mouseDown) {
    context.beginPath();
    context.moveTo(lastEvent.offsetX, lastEvent.offsetY);
    context.lineTo(e.offsetX, e.offsetY);
    context.strokeStyle = color;
    context.stroke();
    lastEvent = e;
  }
}).mouseup(function(){
  mouseDown = false;
}).mouseleave(function(){
  $canvas.mouseup();
});

i cant understand from //On mouse events on the canvas to end

2 Answers

sergiolantigua
sergiolantigua
7,424 Points

step by step: 1- The mousedown event refers to let down the click of the mouse, in the area of ​​the canvas in this case. In the anonymous function being saved in the variable LASTEVENT the coordinates where the click was made.

2- The mousemove event refers to the movement of the mouse within the canvas area. In the anonymous function is checked if the value of the variable boolean mouseDown is true, if so:  1- a new path for the drawing is created.  2- saved coordinates are taken in LASTEVENT variable, this to know where to begin drawing.  3- The method lineTo () joining the coordinates in x and y while the mouse is moving.  4- Finally stroke is made and the style is applied, in this case the color chosen by the user.

3-The mouseUp event occurs when the mouse is released. In this case the Boolean variable is set false, therefore not enter the conditional, and drawing stop.

4- The mouseleave event occurs when you exit the area of ​​the canvas in this case. within the anonymous function is called the mouseUp function that will put false the Boolean variable and stop drawing.

can u xplan this agin plz