I'm a bit rough with jQuery so bear with me here. :)
The goal of the code is to on click, if the square is white, color it with the selected color. If it is the selected color, set it back to white. It works except when going from a color to another color, there's a spot of white inbetween. For example:
When I do black > blue, it processes as black > white > blue.
Here's the snippet of my code
$(pixelCanvas).on("click", "td", function(){
let background = $(this).css("background-color");
let isEmpty = !background || background === "rgb(255, 255, 255)" ||
background === "#ffffff";
let isNotColor = background !== color;
if(isEmpty){
$(this).css("background-color", color);
} else if(isNotColor){
$(this).css("background-color", color);
} else {
$(this).css("background-color", "#ffffff");
}
});
And here's my codepen link
https://codepen.io/ashleighc207/pen/QaezPO
Thank you in advance!!
.css()create css class for each color then useif($('element').hasClass())and add/remove Class will be better and simple way to do that$(pixelCanvas).on("click"event, why do you need it? If you really need to check if background !== color then think of creating a boolean variable and change it accordingly.