原文链接及内容

WebGLTile图层的style属性可以用来调整曝光、对比度和饱和度等属性,通常这些属性值会设置为数字常量,方便对图像进行过滤。在这个示例中,样式属性设置为可以更新的变量,如上图调整滑块会更新这些变量,并调用layer.updateStyleVariables()
方法,使得图层关联的样式属性使用新值。
main.js
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| import Map from 'ol/Map.js'; import TileLayer from 'ol/layer/WebGLTile.js'; import View from 'ol/View.js'; import XYZ from 'ol/source/XYZ.js';
const key = 'Get your own API key at https://www.maptiler.com/cloud/'; const attributions = '<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a> ' + '<a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>';
const variables = { exposure: 0, contrast: 0, saturation: 0, };
const layer = new TileLayer({ style: { exposure: ['var', 'exposure'], contrast: ['var', 'contrast'], saturation: ['var', 'saturation'], variables: variables, }, source: new XYZ({ attributions: attributions, url: 'https://api.maptiler.com/tiles/satellite/{z}/{x}/{y}.jpg?key=' + key, maxZoom: 20, }), });
const map = new Map({ target: 'map', layers: [layer], view: new View({ center: [0, 0], zoom: 0, }), });
let variable; for (variable in variables) { const name = variable; const element = document.getElementById(name); const value = variables[name]; element.value = value.toString(); document.getElementById(name + '-value').innerText = value.toFixed(2); element.addEventListener('input', function (event) { const value = parseFloat(event.target.value); document.getElementById(name + '-value').innerText = value.toFixed(2); const updates = {}; updates[name] = value; layer.updateStyleVariables(updates); }); }
|
界面布局文件index.html
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL Tile Layer Styles</title> <link rel="stylesheet" href="node_modules/ol/ol.css"> <style> .map { width: 100%; height: 400px; } #controls { display: flex; justify-content: space-around; } </style> </head> <body> <div id="map" class="map"></div> <div id="controls"> <label> <input id="exposure" type="range" min="-0.5" max="0.5" step="0.01"> <br>exposure <span id="exposure-value"></span> </label> <label> <input id="contrast" type="range" min="-0.5" max="0.5" step="0.01"> <br>contrast <span id="contrast-value"></span> </label> <label> <input id="saturation" type="range" min="-0.5" max="0.5" step="0.01"> <br>saturation <span id="saturation-value"></span> </label> </div> <script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script> <script type="module" src="main.js"></script> </body> </html>
|