无法在“ CanvasRenderingContext2D”上执行“ getImageData”:画布已被跨域数据污染

时间:2019-07-24 08:16:58

标签: openlayers geoserver wms

我正在尝试从地图上获取像素颜色。但是使用Google Chrome浏览器时,我遇到了跨源问题。 我更改了导航器,一切正常。

function rgbToHex(rgb){
    return (rgb && rgb.length === 4) ? "#" +
        ("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
        ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
        ("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
}   var layer1  =new OpenLayers.Layer.WMS("Cluter",
 "http://******/geoserver/wms",
 {layers: "******", reaspect: "false", transparent: 'true'},
  {isBaseLayer: false, opacity: 0.7   ,  tileOptions: { crossOriginKeyword: "anonymous"}
  } 
  );
    map.events.register("mousemove", map, function(e){
        document.getElementById("docs").innerHTML = '';
         var point = map.getLonLatFromViewPortPx(e.xy);
         var data = layer1.getTileData(point);


        if (data) {
            var ctx = data.tile.getCanvasContext();

            if (ctx) {
                var x = data.i;
                var y = data.j;
                // get image data
                var p = ctx.getImageData(x, y, 1, 1).data;
                document.getElementById("docs").innerHTML = '<span>   HEX: '+ rgbToHex(p)+'</span>' +
                    '<span>   RGB: rgb(' + p[0] + ',' + p[1] + ',' + p[2] + ')</span>';
            }
        }
    });

0 个答案:

没有答案