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 trialHabibur Rahman Dipto
8,496 PointsIf i click "add color" button multiple time. It adds single color several time
If i click add color button multiple time it adds one color sevelr time in list item. But i want to use one color only one list item. try to use if,else but i cant figure which traverse need to use
//Problem: No user interaction causes no 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 element
$(this).siblings().removeClass('selected');
//Select click element
$(this).addClass('selected');
//Cache current color
changeColor();
color = $(this).css('background-color');
});
//When new color is pressed
$('#revealColorSelect').click(function () {
//Show color select or hide the color select
changeColor();
$('#colorSelect').toggle();
});
//Update the 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]').change(changeColor);
//When add color is pressed
$('#addNewColor').click(function() {
//Append the color to the controls ul
var $newColor = $('<li></li>');
$newColor.css('background-color', $('#newColor').css("background-color"));
if ($('.controls li').siblings().css('background-color') !== $newColor.css('background-color')) {
$('.controls ul').append($newColor);
//Select the new color
$newColor.click();
}
});
//On mouse events on the 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;
}).mouseleave(function(){
$canvas.mouseup();
});
1 Answer
rydavim
18,814 PointsI'm not sure that I understand your problem. Your 'Add Color' button doesn't work for me as it is now, but I'm seeing different behavior than what I think you're describing. For me, it doesn't add a color at all.
// This is what fixed it for me:
var $newColor = $(''); // This is what you have now.
var $newColor = $('<li></li>'); // I believe this needs to be a list item tag.
Please let me know if this is not the behavior you're trying to fix.
Habibur Rahman Dipto
8,496 PointsHabibur Rahman Dipto
8,496 Pointsi update the question :) hope now u can help me to solve TIA