I'm creating a paint app with HTML canvas and I'm trying to implement a "paint bucket" tool that would detect any neighboring pixels with the same color as the pixel which was clicked upon, and fill it with a new color.
I'm getting an "Uncaught RangeError: Maximum call stack size exceeded", but I can't figure out what's wrong with my logic:
function fillTool(){
theCanvas.mousedown(function(e){
var baseColor = paintUtilities.getPixelColor(e.offsetX, e.offsetY);
context.fillStyle = color;
fillNeighbors(e.offsetX, e.offsetY, baseColor);
});
}
function fillNeighbors(x, y, baseColor) {
context.fillRect(x, y, 1, 1);
if (x > 0 && paintUtilities.getPixelColor(x - 1, y) === baseColor) {
fillNeighbors(x - 1, y, baseColor);
}
if (y > 0 && paintUtilities.getPixelColor(x, y - 1) === baseColor) {
fillNeighbors(x, y - 1, baseColor);
}
if (x < theCanvas.attr("width") - 1 && paintUtilities.getPixelColor(x + 1, y) === baseColor) {
fillNeighbors(x + 1, y, baseColor);
}
if (y < theCanvas.attr("height") - 1 && paintUtilities.getPixelColor(x, y + 1) === baseColor) {
fillNeighbors(x, y + 1, baseColor);
}
}