OpenLayers: get pixel color from image layer -


i have simple map static pixel image layer:

<!doctype html> <html>   <head>     <title>simple map</title>     <link rel="stylesheet" href="http://openlayers.org/en/v3.13.0/css/ol.css" type="text/css">     <script src="http://openlayers.org/en/v3.13.0/build/ol.js"></script>     // reference jquery here   </head>   <body>     <div id="map" class="map"></div>     <script>        var extent = [0, 0, 2000000, 2000000];       var projection = new ol.proj.projection({         code: 'xkcd-image',         units: 'pixels',         extent: extent       });        var map = new ol.map({         layers: [           new ol.layer.tile({             source: new ol.source.osm()           })         ],         target: 'map',         view: new ol.view({           center: [0, 0],           zoom: 2         })       });        image = new ol.layer.image({         source: new ol.source.imagestatic({           url: 'http://imgs.xkcd.com/comics/online_communities.png',           projection: projection,           imageextent: extent         })       });        map.addlayer(image);        image.on('singleclick', function(evt) {         var xy = evt.pixel;         console.log(xy);         var canvascontext = $('.ol-unselectable')[0].getcontext('2d');         var pixelatclick = canvascontext.getimagedata(xy[0], xy[1], 1, 1).data;         var red = pixelatclick[0]; // green [1] , blue [2] , alpha [4]       });      </script>   </body> </html> 

when clicking on image want color of pixel clicked on. far understand raster source example (http://openlayers.org/en/v3.13.0/examples/raster.html), possible raster sources converted image raster source. (when add raster source layer, message operation insecure, still use image show on map.)

here (how pixel's color value openlayers 3 layer?) color read evt.context. however, me evt.context undefined.

addendum: there might several image layers overlaying each other. need color single specific image layer.

so isn't ideal might send along right lines:

you can use pixel xy of click event query canvas object openlayers creates , puts map data onto.

map.on('singleclick', function(evt) {         var xy = evt.pixel;     var canvascontext = $('.ol-unselectable')[0].getcontext('2d');        var pixelatclick = canvascontext.getimagedata(xy[0], xy[1], 1, 1).data;     var red = pixeatclick[0]; // green [1] , blue [2] , alpha [4]   }); 

this assumes using jquery , canvas first dom element using class '.ol-unselectable'.

hope helps.


Comments