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 trialChristopher van Ruitenbeek
13,705 PointsCode 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
joewode
13,803 PointsThis 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
29,379 PointsHi 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
13,705 PointsThank you both for your help, I believe it is indeed a Firefox bug.
James Delaplain
5,872 PointsIts FROZEN on Firefox and Google for me. lost the newColor box, dont understand!
James Delaplain
5,872 PointsIts FROZEN on Firefox and Google for me. lost the newColor box, dont understand!
James Delaplain
5,872 PointsIts FROZEN on Firefox and Google for me. lost the newColor box, dont understand!