How to get a pixel's color value from an Openlayers 3 layer?

こ雲淡風輕ζ 提交于 2019-12-23 01:29:14

问题


Is there a way to read pixel color values of an Openlayers 3 layer? Something like this:

layerid.getPixelColor(x, y);

I'm aware of the getImageData() method used with the canvas, but as far as I can see, this only allows you to get the proper color values of the top layer with 100% alpha.

I want to get the colors from lower or even hidden layers. (WMS tiles from same domain.)


回答1:


You can set a postcompose handler directly on a layer and read the pixel value from there. I made a small example based on the layer spy example:

imagery.on('postcompose', function(event) {
  var ctx = event.context;
  var pixelRatio = event.frameState.pixelRatio;
  if (mousePosition) {
    var x = mousePosition[0] * pixelRatio;
    var y = mousePosition[1] * pixelRatio;
    var data = ctx.getImageData(x, y, 1, 1).data;
    var color = 'rgb(' + data[0] + ',' + data[1] + ','+ data[2] + ')';
    $('#box').css('background-color', color);
  }
});

http://jsfiddle.net/m1abjrkm/1/

You might also be interested in ol.Map.html#hasFeatureAtPixel.



来源:https://stackoverflow.com/questions/32873958/how-to-get-a-pixels-color-value-from-an-openlayers-3-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!