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

Christopher van Ruitenbeek
Christopher van Ruitenbeek
13,705 Points

Code seems ok, but is not drawing.

This is my current code.

// select color
var color = $(".selected").css("background-color");
var $controlList = $(".controls ul");

var $newColorButton = $("#revealColorSelect");

var $canvas = $("canvas");
var context = $canvas[0].getContext("2d");
var lastEvent;
var mouseDown = false;

  // change active class
$(".controls").on("click", "li", function() {
  // deselect
  $(this).siblings().removeClass("selected");

  // add class to clicked element
  $(this).addClass("selected");

  color = $(".selected").css("background-color");
});
  // change color

// open/close new color pop-up
$newColorButton.click(function() {
  changeColor();
  $("#colorSelect").toggle();
});
  // enable slider event
function changeColor() {
  var r = $("#red").val();
  var g = $("#green").val();
  var b = $("#blue").val();

  $("#newColor").css("background-color", "rgb(" + r + ", " + g +"," + b +")");
}

$("input[type=range]").change(changeColor);
  // fix add color button
$("#addNewColor").click(function() {
  var $newColor = $("<li></li>");
  $newColor.css("background-color", $("#newColor").css("background-color"));
  $controlList.append($newColor);
  $newColor.click();
});

// enable canvas drawing
$canvas.mousedown(function(e) {
  lastEvent = e;
  mouseDown = true;
}).mousemove(function(e) {
  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;
});

I'm not getting any console errors, but it doesn't draw a line.

6 Answers

This is an issue with firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=69787#c37

If you are using firefox that's why it isn't working, offsetX and offsetY aren't supported by firefox's event model. Try it out in chrome.

Iain Diamond
Iain Diamond
29,379 Points

Hi Christopher, I copied your JS into a working version of the drawing app and it seems to work without any problems. This leads me to believe there's something wrong elsewhere. If the problem isn't immediately apparently, (not loading JS in HTML, jQuery, loading in wrong order, etc.) perhaps you could post a link to your workspace here?

iain

Christopher van Ruitenbeek
Christopher van Ruitenbeek
13,705 Points

Thank you both for your help, I believe it is indeed a Firefox bug.

Its FROZEN on Firefox and Google for me. lost the newColor box, dont understand!

Its FROZEN on Firefox and Google for me. lost the newColor box, dont understand!

Its FROZEN on Firefox and Google for me. lost the newColor box, dont understand!