如何对网页上的某个图像进行基于颜色的点击测试?

时间:2014-10-10 15:40:10

标签: javascript jquery html html5

我需要知道我的鼠标指针何时在某个图像上,而在该图像中,它会超过一种非常特定的颜色。这是可能的,我该怎么做?

3 个答案:

答案 0 :(得分:1)

刚刚写了这个:

基本上,它是@nepeo告诉你要做的 +它会清理画布并在计算后重置原始图像 +我还添加了一个可以禁用的自定义光标。


用法:在colorPicker.callback中设置您自己的回调函数,然后在所需图像的colorPicker.init事件上附加mouseover函数。

N.B :您必须在自己的服务器上托管图像或将图像转换为base64才能使用此脚本。


var colorPicker = function() {};
//Settings

colorPicker.radius = 1, //square px

colorPicker.customCursor = true; 

colorPicker.cursorColor = '#FFF';

//the fomatting for color return : "hex" || "rgba" || "array"
// !! hex is upperCase and returns "transparent" if opacity == 0; rgba is 'rgba(r,g,b,a)';  array is '[r,g,b,a]'
colorPicker.colorFormat = "hex";

//What to do with that color? Edit this to whatever you want
colorPicker.callback = function (color) {
    var res = document.getElementById('ColorPickerResult');
    res.style.backgroundColor = color;
    res.style.color = invertHex(color);
    res.innerHTML = color;
    //Here I'm searching for some Hex color
    if (color == '#FC771F') {
        res.innerHTML = '!!! Found some orange !!!';
    }
}

//What to do when we leave the image (Additionally to reset the original image)
colorPicker.callbackOut = function(){
    var res = document.getElementById('ColorPickerResult');
    res.innerHTML = '';
  }

//END Settings

//This is optional
colorPicker.setCursor = function (canvas) {
    if (!colorPicker.setCursor) return;
    var can = document.createElement('canvas');
    can.width = colorPicker.radius;
    can.height = colorPicker.radius;
    var ctx = can.getContext('2d');
    ctx.strokeStyle = colorPicker.cursorColor;
    ctx.rect(0, 0, colorPicker.radius, colorPicker.radius);
    ctx.stroke();
    canvas.style.cursor = 'url("' + can.toDataURL("image/png") + '"),default';
}
colorPicker.Init = function () {
    var img = this;
    var can = document.createElement('canvas');
    can.id = 'ColorPicker';
    can.width = img.width;
    can.height = img.height;
    can.ori = img;
    can.oridisp = img.style.display;
    var ctx = can.getContext('2d');
    ctx.drawImage(img, 0, 0);
    can.addEventListener('mousemove', colorPicker.pick);
    can.addEventListener('mouseout', colorPicker.out);
    colorPicker.setCursor(can);
    img.parentNode.insertBefore(can, img);
    img.style.display = 'none';
};
colorPicker.pick = function (evt) {
    var rect = this.getBoundingClientRect();
    ctx = this.getContext('2d'),
    imageData = ctx.getImageData(evt.clientX - rect.left, evt.clientY - rect.top, colorPicker.radius, colorPicker.radius);
    data = imageData.data;
    r = 0, g = 0, b = 0, a = 0;
    for (i = 0; i < data.length; i++) {
        r += data[i];
        g += data[++i];
        b += data[++i];
        a += data[++i];
    }
    var p = data.length / 4;

    var mR = function (i) {
        return Math.round(i);
    };
    r = mR(r/p);
    g = mR(g/p);
    b = mR(b/p);
    a = (mR((a/p)/255 * 100)/100).toFixed(2);
  
    if(colorPicker.colorFormat === "hex"){
      var color = colorPicker.toHex(r,g,b,a);
      }
    else if(colorPicker.colorFormat === "rgba"){
      var color = 'rgba('+r+', '+g+', '+b+', '+a+')';
      }
    else if(colorPicker.colorFormat === "array"){
      var color = [r, g, b, a];
      }
 
    colorPicker.callback(color);
}
colorPicker.out = function () {
    this.ori.style.display = this.oridisp;
    this.parentNode.removeChild(this);
    colorPicker.callbackOut();
}

//toHex function originally posted by http://stackoverflow.com/users/10474/felipec in http://stackoverflow.com/a/19765382/3702797
colorPicker.toHex= function(r,g,b,a){
        if(a=="0.00") return "transparent";
        var rgb = b | (g << 8) | (r << 16);
        return '#' + (0x1000000 + rgb).toString(16).slice(1).toUpperCase()
  }
// END colorPicker \\

// Just an example of external function beeing called 
function invertHex(c){
  if(c==="transparent") return "#000000";
  c = c.replace('#', '');
  var reqHex = "#";
    for(var i=0;i<6;i++){
        reqHex = reqHex + (15-parseInt(c[i],16)).toString(16);
    }
    return reqHex;
}

//Attach the function to whatever images you want
document.getElementsByTagName('img')[0].addEventListener('mouseover', colorPicker.Init);
body {
    background: #175;
}
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAO4AAABDCAYAAACbbDkSA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7hjbXGjKR3eOX/48Z7ooAGLhzvvLDq3jaM7IwDd3PDJ3fUnP8tr8XPvqntYqBIhRkLjOo9O5yMFQD63qq5750LRVP/B0YplEB/kC4aLwgn/eORgpOHeU++QFgeWlSHO85rCrIjvq26uU53Dk9HIvnDi1U7aubfjUXyvjsV8RUP7oGfu8s+JTpLQZ+Qqv+OZoe18npXIt+F6sHgritMl5n0xPAesQHFzPYEelHlOfRjKYvzkpUHjYTELKJwRZoXZge5506B4SDwzSs2QvXeYpDQuNyxWXviYjM6r01CXJC//iqfzllbGy5Q0pLo72YgdKH9euRhfR3HSEi6fTB6eJD+eojPf6sunuIAfYepu+Hl5e2UwTI3nQN0sOdxjPHxk3p6B1gdMf2hb1y4D9TM6N/WJ67GojaIC3bTLnuflY74tnIKRixadXKqtjH+5FwQKAgWBgkBBoCBQECgIFAQKAgWBgkBBoCBQECgIFAQKAgWBgkBBoCBQECgIFAQKAgWBgkBBoCBQECgIFAQKAgWBdxOB/wcP4cIU+D70agAAAABJRU5ErkJggg==" />
<span id="ColorPickerResult"></span>

答案 1 :(得分:0)

使用画布代替图像,将画布调整为图像大小并将图像绘制到画布中。

然后,您可以使用canvas.onmousemove事件获取光标的像素位置。 Canvas可能在它的api中有一些用于查询某个位置的颜色(此刻不记得)但是否则你可以使用getImageData来获取每个像素的rgba数据并在事件期间查找。塔达!

答案 2 :(得分:0)

我在这里尝试过。在此示例中,脚本搜索颜色#FF0000,如在options对象中传递的那样。 http://jsfiddle.net/2sw3pzs4/

var ColorChecker = function( options ) {
    var canvas, ctx, output;
    this.init = function() {
        canvas = document.getElementById('canvas');
        output = document.getElementById('output');
        ctx = canvas.getContext("2d");
        color = this.hexToRGB(options.searchedColor);
        this.drawOnCanvas();

        canvas.addEventListener('mousemove', function(e) {
            if(e.offsetX) {
                mouseX = e.offsetX;
                mouseY = e.offsetY;
            }
            else if(e.layerX) {
                mouseX = e.layerX;
                mouseY = e.layerY;
            }
            var c = ctx.getImageData(mouseX, mouseY, 1, 1).data;
            var foundColor = [c[0], c[1], c [2]];
            var is_identical = foundColor.join() === color.join();
            var message = is_identical == true ? 'red found' : 'not found';
            output.innerHTML = message;
        });
    }

    this.drawOnCanvas = function() {
        ctx.fillStyle = "#FF0000";
        ctx.fillRect(0,0, 20, 20);
    }

    this.hexToRGB = function( hex ) {
        var r = parseInt((this.cutHex(hex)).substring(0,2),16);
        var g = parseInt((this.cutHex(hex)).substring(2,4),16);
        var b = parseInt((this.cutHex(hex)).substring(4,6),16);

        return [r,g,b];
    }

    this.cutHex = function( hex ) {
         return hex.replace("#",'');
    }
    this.init();
}

var colorChecker = new ColorChecker({
    searchedColor: "#FF0000",
});

如果您想使用图片,只需使用putImageData()在画布上绘制图像。您可以在我的代码中使用drawOnCanvas函数在画布中绘制您想要的任何内容,或者调整它或其他内容。