//***************************************
// Initialize Map
//***************************************
function Initialize() {
var imagery = new ol.layer.Tile({
source: new ol.source.OSM()
});
var map = new ol.Map({
target: 'map',
layers: [imagery],
view: new ol.View({
center: ol.proj.transform([-2.1833, 41.3833], 'EPSG:4326', 'EPSG:3857'),
zoom: 6
})
});
//Apply a filter on "postcompose" events.
imagery.on('postcompose', function(event) {
greyscale(event.context);
});
}
//***************************************
// Aux Func.
//***************************************
// function applies greyscale to every pixel in canvas
function greyscale(context) {
var width = context.canvas.width;
var height = context.canvas.height;
console.log('width: ' + width);
console.log('height: ' + height);
var inputData = context.getImageData(0, 0, width, height).data;
console.log('inputData.length: ' + inputData.length);
var canvas = document.getElementsByClassName('ol-unselectable')[0];
var ctx = canvas.getContext('2d');
ctx.fillStyle = "rgba(0, 0, 0, 0)"
var myImageData = ctx.createImageData(width, height);
var d = myImageData.data;
for (i = 0; i < inputData.length; i += 4) {
var r = inputData[i];
var g = inputData[i + 1];
var b = inputData[i + 2];
// CIE luminance for the RGB
var v = 0.2126 * r + 0.7152 * g + 0.0722 * b;
d[i + 0] = v; // Red
d[i + 1] = v; // Green
d[i + 2] = v; // Blue
d[i + 3] = 255; // Alpha
}
ctx.putImageData(myImageData, 0, 0);
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.