I've been working on implementing the flood fill algorithm pseudocode that I found in Graphics Gemes 1.
Here is the pseudocode:
https://i.stack.imgur.com/X6jV4.png
Unfortunately, when I translated it to JavaScript and used my floodfill tool, it caused my code to hang. Here's a snippet of the code I used:
function inside(x, y) { const q = 4 * (x + y * that.w); const color = [img.data[q], img.data[q + 1], img.data[q + 2], img.data[q + 3]]; return color[0] === toolColor[0] && color[1] === toolColor[1] && color[2] === toolColor[2]; } function set(x, y) { const q = 4 * (x + y * that.w); img.data[q] = that.color.r; img.data[q + 1] = that.color.g; img.data[q + 2] = that.color.b; img.data[q + 3] = that.color.a; } // More code.... <p><code>img.data
is an array displayed in the browser.
toolColor
contains the colors for the area to be filled.
I'm struggling with why the algorithm only fills part of the area.
If you need more code details, feel free to ask.Edit: After some updates, the code now partially fills the area.
https://i.stack.imgur.com/THPTh.png