javascript中是否存在与getColorBoundsRect()等效的代码?

javascript中是否存在与getColorBoundsRect()等效的代码?,javascript,actionscript-3,html,canvas,Javascript,Actionscript 3,Html,Canvas,我只是想知道是否有人已经用Javascript做了这件事,或者我是否必须自己做——如果是后者:我将如何做?(不需要一段代码,只想知道你会使用哪种方法)这是我的快速脏解决方案,也许有人会发现它很有用;) /** *得到一个围绕颜色的矩形 *要扫描的@param{…}ctx 2dCanvasObject *@return{Object}Object存储矩形的数据(x,y,w(idth),h(八)) */ 函数getColorBoundsRect(ctx){ /** *画布上下文的数据属性(简写) *

我只是想知道是否有人已经用Javascript做了这件事,或者我是否必须自己做——如果是后者:我将如何做?(不需要一段代码,只想知道你会使用哪种方法)

这是我的快速脏解决方案,也许有人会发现它很有用;)

/**
*得到一个围绕颜色的矩形
*要扫描的@param{…}ctx 2dCanvasObject
*@return{Object}Object存储矩形的数据(x,y,w(idth),h(八))
*/
函数getColorBoundsRect(ctx){
/**
*画布上下文的数据属性(简写)
*@type{…}
*/
var数据=ctx.data,
/**
*反变量
*@type{Number}
*/
i=0,
/**
*非黑色的“最左”像素(从右开始,因为我们检查当前循环像素(非黑色)是否比当前外部像素“左”)
*@type{Number}
*/
outerLeftPixel=w-1,
/**
*非黑色的“最右边”像素(从左边开始,我们检查当前循环像素(非黑色)是否比当前outerRightPixel“更右边”)
*@type{Number}
*/
outerRightPixel=0,
/**
*非黑色的“最顶端”像素(从底部开始,我们检查当前循环像素(非黑色)是否比当前外部像素“最顶端”)
*@type{Number}
*/
outerTopPixel=h-1,
/**
*非黑色的“最底部”像素(从顶部开始,我们检查当前循环像素(非黑色)是否比当前外部像素“最底部”)
*@type{Number}
*/
outerBottomPixel=0,
/**
*当前循环像素的x坐标
*@type{Number}
*/
x,,
/**
*当前循环像素的y坐标
*@type{Number}
*/
Y
//循环遍历所有像素
//i等于第i个像素(0为左上像素,w*h为右下像素)
而(i<(数据长度/4)){
//检查当前循环像素是否不是黑色-->颜色
如果((数据[i*4]+数据[i*4+1]+数据[i*4+2])>0){
//设置当前循环像素的坐标
x=i%w;//如果一行有10px且i=35,则当前像素的x坐标为35%10=5
y=Math.floor(i/w);//如果一行有10px且i=35,则当前像素的y坐标为35/10=3.5(->四舍五入=3)
//如果当前(彩色)像素的x坐标小于当前“最左侧”像素,则将x坐标设置为新的“最左侧像素”
//其他值的步骤相同
如果(xouterRightPixel){
outerRightPixel=x;
}
if(youterBottomPixel){
outerBottomPixel=y;
}
}
++一,;
}
//如果画布上有颜色,则外部[Right | Left | Bottom | Top]像素属性应相应更新,并且以下条件应为真
if(outerRightPixel>outerLeftPixel&&outerBottomPixel>outerTopPixel){
返回{
x:outerLeftPixel,
y:外置像素,
w:outerRightPixel-outerRightPixel,
h:outerBottomPixel-outerTopPixel
};
} 
//如果画布上没有颜色,则返回false,因为没有矩形
否则{
返回false;
}
}

我有一个更好的解决方案。不必遍历所有像素,只需遍历边界框外的像素。这样想,如果你想在1D中做同样的事情:在数组中找到一个值的第一个和最后一个位置,你会遍历整个数组吗?最好从开始走到找到第一个值,然后从结束走到找到最后一个值。以下代码对2D执行相同的操作。我还没有对它进行彻底的测试(无论是正确性还是速度),但它似乎可以工作,而且常识告诉我它更快

BitmapData.prototype.getColorBoundsRect = function(mask, color, findColor, rect){
    findColor = typeof findColor !== 'undefined' ? findColor : true;
    rect = typeof rect !== 'undefined' ? rect : new module.Rect(0, 0, this.width, this.height);

    var l = rect.w - 1;
    var r = 0;
    var t = rect.h - 1;
    var b = 0;

    var data = this.context.getImageData(rect.x, rect.y, rect.w, rect.h).data;

    // Scan from top to first pixel.
    for (var i = 0; i < data.length; i += 4){
        var val = module.RGBToHex({r:data[i], g:data[i+1], b:data[i+2], a:data[i+3]});
        // console.log(val, mask, color, (val & mask) >>> 0)
        if ((findColor && ((val & mask) >>> 0 == color)) || (!findColor && ((val & mask) >>> 0 != color))){
            l = r = ((i / 4) % rect.w);
            t = b = Math.floor(i / 4 / rect.w);
            break;
        }
    }

    // We found nothing.
    if (i >= data.length) {
        return null;
    }


    // Scan from bottom to first pixel
    for (var j = data.length - 4; j > i; j -= 4){
        var val = module.RGBToHex({r:data[j], g:data[j+1], b:data[j+2], a:data[j+3]});
        if ((findColor && ((val & mask) >>> 0 == color)) || (!findColor && ((val & mask) >>> 0 != color))){
            l = Math.min(l, ((j / 4) % rect.w))
            r = Math.max(r, ((j / 4) % rect.w))
            b = Math.floor(j / 4 / rect.w);
            break;
        }
    }

    console.log(l, r, t, b);

    // Scan from left
    for (var x = 0; x < l; x ++){
        for (var y = t + 1; y <= b; y ++){
            i = (y * rect.w + x) * 4
            var val = module.RGBToHex({r:data[i], g:data[i+1], b:data[i+2], a:data[i+3]});
            if ((findColor && ((val & mask) >>> 0 == color)) || (!findColor && ((val & mask) >>> 0 != color))){
                l = Math.min(l, x);
                break;
            }
        }
    }

    console.log(l, r, t, b);


    // Scan from right
    for (var x = rect.w - 1; x > r; x --){
        for (var y = t; y < b; y ++){
            i = (y * rect.w + x) * 4
            var val = module.RGBToHex({r:data[i], g:data[i+1], b:data[i+2], a:data[i+3]});
            if ((findColor && ((val & mask) >>> 0 == color)) || (!findColor && ((val & mask) >>> 0 != color))){
                r = Math.max(r, x);
                break;
            }
        }
    }

    console.log(l, r, t, b)

    return new module.Rect(l + rect.x, t + rect.y, (r - l), (b - t));
}

你可能应该在这里描述你想做什么,而不是仅仅链接到某个外部站点。如果该网站更改其URL或脱机,您的问题将变得无法理解。您不需要此链接-我想指出我需要它的原因会很好,但我可以删除它;)
BitmapData.prototype.getColorBoundsRect = function(mask, color, findColor, rect){
    findColor = typeof findColor !== 'undefined' ? findColor : true;
    rect = typeof rect !== 'undefined' ? rect : new module.Rect(0, 0, this.width, this.height);

    var l = rect.w - 1;
    var r = 0;
    var t = rect.h - 1;
    var b = 0;

    var data = this.context.getImageData(rect.x, rect.y, rect.w, rect.h).data;

    // Scan from top to first pixel.
    for (var i = 0; i < data.length; i += 4){
        var val = module.RGBToHex({r:data[i], g:data[i+1], b:data[i+2], a:data[i+3]});
        // console.log(val, mask, color, (val & mask) >>> 0)
        if ((findColor && ((val & mask) >>> 0 == color)) || (!findColor && ((val & mask) >>> 0 != color))){
            l = r = ((i / 4) % rect.w);
            t = b = Math.floor(i / 4 / rect.w);
            break;
        }
    }

    // We found nothing.
    if (i >= data.length) {
        return null;
    }


    // Scan from bottom to first pixel
    for (var j = data.length - 4; j > i; j -= 4){
        var val = module.RGBToHex({r:data[j], g:data[j+1], b:data[j+2], a:data[j+3]});
        if ((findColor && ((val & mask) >>> 0 == color)) || (!findColor && ((val & mask) >>> 0 != color))){
            l = Math.min(l, ((j / 4) % rect.w))
            r = Math.max(r, ((j / 4) % rect.w))
            b = Math.floor(j / 4 / rect.w);
            break;
        }
    }

    console.log(l, r, t, b);

    // Scan from left
    for (var x = 0; x < l; x ++){
        for (var y = t + 1; y <= b; y ++){
            i = (y * rect.w + x) * 4
            var val = module.RGBToHex({r:data[i], g:data[i+1], b:data[i+2], a:data[i+3]});
            if ((findColor && ((val & mask) >>> 0 == color)) || (!findColor && ((val & mask) >>> 0 != color))){
                l = Math.min(l, x);
                break;
            }
        }
    }

    console.log(l, r, t, b);


    // Scan from right
    for (var x = rect.w - 1; x > r; x --){
        for (var y = t; y < b; y ++){
            i = (y * rect.w + x) * 4
            var val = module.RGBToHex({r:data[i], g:data[i+1], b:data[i+2], a:data[i+3]});
            if ((findColor && ((val & mask) >>> 0 == color)) || (!findColor && ((val & mask) >>> 0 != color))){
                r = Math.max(r, x);
                break;
            }
        }
    }

    console.log(l, r, t, b)

    return new module.Rect(l + rect.x, t + rect.y, (r - l), (b - t));
}
module.RGBToHex = function(rgb) { 
    return (rgb.a << 24 | rgb.r<<16 | rgb.g<<8 | rgb.b) >>> 0; 
};