问题
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