如何从Openlayers 3图层获取像素的颜色值? [英] How to get a pixel's color value from an Openlayers 3 layer?
问题描述
有没有办法读取Openlayers 3图层的像素颜色值? 像这样:
Is there a way to read pixel color values of an Openlayers 3 layer? Something like this:
layerid.getPixelColor(x, y);
我知道画布使用的getImageData()方法,但据我所知,这仅允许您使用100%的alpha值获取顶层的正确颜色值.
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.
我想从较低甚至隐藏的图层中获取颜色. (来自相同域的WMS磁贴.)
I want to get the colors from lower or even hidden layers. (WMS tiles from same domain.)
推荐答案
您可以设置图层间谍示例制作了一个小示例:
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/
您可能还对 ol.Map.html#hasFeatureAtPixel .
这篇关于如何从Openlayers 3图层获取像素的颜色值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!