WebGL 瓦片图层样式
WebGL 瓦片图层样式示例。 WebGL 样式
WebGL 瓦片图层的 style 属性可用于调整 exposure(曝光)、contrast(对比度)和 saturation(饱和度)等属性。通常这些值会被设置为数值常量,以对影像应用滤镜效果。在本示例中,样式属性被设置为可以根据应用状态更新的变量。调整滑块会触发对 layer.updateStyleVariables() 的调用,从而使相应的样式属性使用新的值。
main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/WebGLTile.js';
import ImageTile from 'ol/source/ImageTile.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 ImageTile({
attributions: attributions,
url: 'https://api.maptiler.com/maps/satellite/{z}/{x}/{y}.jpg?key=' + key,
tileSize: 512,
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);
layer.updateStyleVariables({[name]: value});
});
}index.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebGL 瓦片图层样式</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>曝光 <span id="exposure-value"></span>
</label>
<label>
<input id="contrast" type="range" min="-0.5" max="0.5" step="0.01">
<br>对比度 <span id="contrast-value"></span>
</label>
<label>
<input id="saturation" type="range" min="-0.5" max="0.5" step="0.01">
<br>饱和度 <span id="saturation-value"></span>
</label>
</div>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "webgl-tile-style",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}