简体   繁体   English

递归洪水填充在 Javascript 中不起作用

[英]Recursive flood fill not working in Javascript

Here is my code.这是我的代码。 I don't think my logic is wrong but if it is please tell me:我不认为我的逻辑是错误的,但如果是,请告诉我:

function floodrecursive()
{
    let x=0;
    let y=0;
    
    canvas.addEventListener('click', e => {
        x = e.offsetX;
        y = e.offsetY;
        
        var basecolor = c.getImageData(x, y, 1, 1).data;
        //alert(basecolor);
        var fillcolor = document.getElementById("colorinput").value;
        
        flood(c, x, y, basecolor, fillcolor);
    });
}
    
function flood(c, x, y, basecolor, fillcolor)
{
    var currentpixel = c.getImageData(x, y, 1, 1).data;
    if (currentpixel === basecolor)
    {
        putpixel(c, x, y, fillcolor);
        flood(c, x+1, y, basecolor, fillcolor);
        flood(c, x-1, y, basecolor, fillcolor);
        flood(c, x, y+1, basecolor, fillcolor);
        flood(c, x, y-1, basecolor, fillcolor);
    }
}

function putpixel(c, x, y, fillcolor)
{
    c.fillStyle = "#" + fillcolor;
    c.fillRect(x, y, 1, 1);
}

I tried putting the flood and putpixel functions inside the floodrecursive function, or inside the addEventListener but it still won't work.我尝试将floodputpixel函数放入floodrecursive function 或addEventListener中,但它仍然不起作用。 I don't know why.我不知道为什么。

I already have this function:我已经有了这个 function:

window.onload=function() {covercanvas()};
function covercanvas()
{
    c.fillStyle="plum";
    c.fillRect(0, 0, 1030, 430);
}

so that the basecolor isn't transparent.使basecolor不透明。 And the alert(basecolor) works too so I don't know what's going wrong here. alert(basecolor)也有效,所以我不知道这里出了什么问题。

Here's the HTML:这是 HTML:

<button class="floodbutton" onclick="floodrecursive()"> Flood fill </button>

<input type="text" id="colorinput"></input>

How I expected the code to work is that after I draw on the canvas, put the hexa value of a color in the input box, then click the button, and click the canvas again, it should start flood filling.我期望代码可以工作的是,在我在 canvas 上绘制后,将颜色的十六进制值放入输入框中,然后单击按钮,然后再次单击 canvas,它应该开始填充填充。

I've added a hexAtoRGBA function and now it works perfectly.我添加了一个hexAtoRGBA function 现在它可以完美运行了。 But I still... kinda don't understand.但我还是……有点不明白。 Here's the updated code:这是更新的代码:

function floodrecursive()
{
    let x=0;
    let y=0;
    
    canvas.addEventListener('click', e => {
        x = e.offsetX;
        y = e.offsetY;
        
        var basecolor = c.getImageData(x, y, 1, 1).data;
        var base = hexAToRGBA(basecolor);
        var fillcolor = document.getElementById("colorinput").value;
        
        flood(c, x, y, base, fillcolor);
    });
}
    
function flood(c, x, y, base, fillcolor)
{
    var currentpixel = c.getImageData(x, y, 1, 1).data;
    var currentpix = hexAToRGBA(currentpixel);
    if (currentpix === base)
    {
        putpixel(c, x, y, fillcolor);
        flood(c, x+1, y, base, fillcolor);
        flood(c, x-1, y, base, fillcolor);
        flood(c, x, y+1, base, fillcolor);
        flood(c, x, y-1, base, fillcolor);
    }
}

function hexAToRGBA(h) {
  let r = 0, g = 0, b = 0, a = 1;

  if (h.length == 4) {
    r = "0x" + h[1] + h[1];
    g = "0x" + h[2] + h[2];
    b = "0x" + h[3] + h[3];
    a = "0x" + h[4] + h[4];

  } else if (h.length == 8) {
    r = "0x" + h[1] + h[2];
    g = "0x" + h[3] + h[4];
    b = "0x" + h[5] + h[6];
    a = "0x" + h[7] + h[8];
  }
  a = +(a / 255).toFixed(3);

  return "rgba(" + +r + "," + +g + "," + +b + "," + a + ")";
}

The thing I don't understand is, the c.getImageData already returns the value in RGBA format, so why does it work only after I've added the hexAtoRGBA function for both base and currentpix ?我不明白的是, c.getImageData已经返回 RGBA 格式的值,那么为什么只有在我为basecurrentpix添加了hexAtoRGBA function 之后它才起作用? They're both using c.getImageData so they're already comparing RGBA, right?他们都在使用c.getImageData所以他们已经在比较 RGBA,对吧?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM