Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在HTML5+中检测画布上的颜色冲突;JavaScript_Javascript_Html_Canvas_Html5 Canvas - Fatal编程技术网

在HTML5+中检测画布上的颜色冲突;JavaScript

在HTML5+中检测画布上的颜色冲突;JavaScript,javascript,html,canvas,html5-canvas,Javascript,Html,Canvas,Html5 Canvas,我正在JavaScript中寻找一种方法,如何检测对象是否如下所示: var box = { x: 5, y: 19, width: 10, height: 5, draw: function(ctx){...draw Box on context "ctx"}, touchingColor: function(ctx,color){...} } 因此,基本上我要寻找的是一个touchingColor函数,如果我的框在指定的上下文中接触指定的颜色

我正在JavaScript中寻找一种方法,如何检测
对象是否如下所示:

var box = {
    x: 5,
    y: 19,
    width: 10,
    height: 5,
    draw: function(ctx){...draw Box on context "ctx"},
    touchingColor: function(ctx,color){...}
}
因此,基本上我要寻找的是一个touchingColor函数,如果我的
在指定的上下文中接触指定的颜色,它将返回
true


有没有办法做到这一点,或者我需要跟踪我在画布上画的东西吗?

嗯,我找到了解决这个问题的方法:)希望它能帮助某人

var box = {
    x: 5,
    y: 19,
    width: 10,
    height: 5,
    draw: function(ctx){...draw Box on context "ctx"},
    touchingColor: function(ctx,r,g,b){
        var data = ctx.getImageData(this.x,this.y,this.width,this.height);
        for(var i=0;i<data.length;i+=4){
            if(
                data[i+0]==r&&
                data[i+1]==g&&
                data[i+2]==b
            ){
                return true;
            }
        }
        return false;
    }
};
var框={
x:5,
y:19,
宽度:10,
身高:5,,
draw:function(ctx){…在上下文“ctx”上绘制框},
触摸颜色:功能(ctx、r、g、b){
var data=ctx.getImageData(this.x,this.y,this.width,this.height);

对于(var i=0;iBest选项是跟踪画布上绘制的内容,例如获取某个区域的像素数据,然后查找这些像素中是否有颜色是非常缓慢的www…..如果添加背景、前景、过滤器、合成操作、阴影等,则几乎不可能。。。