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

Create a Simple Drawing Application - Perform: Part 5

From what I can tell, my code is identical to that in the video. The only thing that doesn't work is the color of the stroke. When drawing, it draws a red line, regardless of what other color is selected.

Code entered below:

//Problem: No user interaction causes any 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 elements
  $(this).addClass("selected");
  //Cache current color here
  color = $(".this").css("background-color");
});

//When "New Color" is pressed
$("#revealColorSelect").click(function () {
  //Show Color Select or Hide Color Select
  changeColor();
  $("#colorSelect").toggle();
});

//Update 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]").on("input", changeColor);

//When Add Color is pressed
$("#addNewColor").click(function () {
  //Append new color to 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 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;
});

Thanks!

I haven't checked but what about

color = $(this).css("background-color"); 
/* instead of color = $(".this").css("background-color"); */

@ Floris Creyf: That didn't fix it, but it was something that needed to be taken care of. Thanks for pointing it out :)

That's curious. I copy / pasted your code, applied Floris' fix and it did work for me inside a workspace.

1 Answer

I think the problem could be here:

//Cache current color here
  color = $(".this").css("background-color");

.this means it targets a class named this. it should target the color itself, therefore you need to remove the dot.

Yes; I removed the dot but it still wouldn't change colors. Thanks though :)

William,

Also need to remove the quotation marks around this. Here is a working CodePen showing the only change made was removing the dot and quotation marks.

Aha! The quotation marks! Thanks Robert! (It's always the silly little things, isn't it)

Thanks everyone! :D