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 trialJapneet Singh
4,675 PointsColor of stroke not changing
var color = $(".selected").css("background-color");
var context = $("canvas")[0].getContext("2d");
var $canvas = $("canvas");
var lastevent;
var mouseDown = false;
$(".controls").on("click", "li" ,function(){
// cache current color here
var color = $(this).css("background-color");
$(this).siblings().removeClass("selected");
$(this).addClass("selected");
});
$("#revealColorSelect").click(function(){
changeColor();
$("#colorSelect").toggle();
});
//update the color
function changeColor() {
var r =$("#red").val();
var b =$("#blue").val();
var g =$("#green").val();
$("#newColor").css("background-color", "rgb("+ r +","+ g +", " + b + ")");
$("input[type=range]").change(changeColor);
}
$("#addNewColor").click(function(){
var $newColor = $("<li></li>");
$newColor.css("background-color" , $("#newColor").css("background-color"));
$(".controls ul").append($newColor);
$newColor.click();
});
$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.stroke();
context.strokeStyle = color;
lastevent = e;
}
}).mouseup(function(){
mouseDown = false;
});
1 Answer
Dee K
17,815 PointsHello Japneet,
The color is not changing because of this line of code.
$(".controls").on("click", "li" ,function(){
// cache current color here
var color = $(this).css("background-color"); <-------
$(this).siblings().removeClass("selected");
$(this).addClass("selected");
});
You've created a variable that will only exist inside this function.
Remove the var in front of color, to create a global variable that can be accessed anywhere.
$(".controls").on("click", "li" ,function(){
// cache current color here
color = $(this).css("background-color");
$(this).siblings().removeClass("selected");
$(this).addClass("selected");
});
Cheers!
Japneet Singh
4,675 PointsJapneet Singh
4,675 Pointsthanks for pointing that out . such a rookie mistake !!!