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
ashok bishnoi
10,160 Pointswhat's wrong with my codes
console.log("var color = $(".selected").css("background-color");
var $canvas = $("canvas");
var context = $canvas[0].getContext("2d");
var lastEvent;
var mousedown = false;
// when clicking on controlr list item
$(".controls").on("click", "li", function(){
// deselect sibling elements
$(this).siblings().removeClass("selected");
// select clicked element
$(this).addClass("selected");
// cache the color
color =$(this).css("background-color");
})
// when newcolor is pressed
$("#revealColorSelect").click(function(){
// show color select or hide the select
changeColor();
$("#colorSelect").toggle();
})
//update the newcolor 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 the addcolor is pressed
$("#addNewColor").click(function(){
// append the color to controle ul
var $newColor = $("<li></li>");
$(".controls ul").append($newColor);
$newColor.css("background-color", $("#newColor").css("background-color"));
// select the new color
$newColor.click();
})
// on mouse events on the canvas
$canvas.mousedown(function(e){
lastEvent = e;
mousedown = true;
}).mousemove(function(e){
lastEvent = 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;
}).mouseleave(function(){
$canvas.mouseup();
})")
1 Answer
Raymond Wach
7,961 PointsThis looks to be the same question that I answered in your other post.