Geozarr Storytelling
Runnable OpenLayers example: Geozarr Storytelling. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:GeoZarr 事件叙事 — OpenLayers geozarr-storytelling
核心逻辑:三幅不同淹没范围的合成影像按事件阶段切换,Raster 源统一按水体指数出图
依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GeoZarr 事件叙事</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:8px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar button{font:inherit;padding:3px 10px;border:1px solid #ccd2da;background:#fff;
border-radius:3px;cursor:pointer;color:#2b3140}
.bar button.on{background:#1f5fd0;border-color:#1f5fd0;color:#fff}
#map{flex:1 1 auto;min-height:0;background:#eef0f3;position:relative}
.caption{position:absolute;left:12px;bottom:12px;max-width:360px;
background:rgba(255,255,255,0.94);border:1px solid #d7dce3;border-radius:4px;
padding:10px 12px;font-size:12px;color:#2b3140;box-shadow:0 2px 8px rgba(0,0,0,0.12)}
.caption b{display:block;margin-bottom:4px;font-size:13px}
</style>
</head>
<body>
<div class="bar">
<button data-stage="before" class="on">事件前</button>
<button data-stage="during">事件中</button>
<button data-stage="after">事件后</button>
<span id="info">—</span>
</div>
<div id="map"></div>
<div class="caption" id="caption"><b>事件前</b><span>常规水位,河网只占低洼地带。</span></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (typeof ol === 'undefined') {
document.getElementById('map').innerHTML =
'<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var SIZE = 512;
var extent = ol.proj.transformExtent(
[116.15, 39.78, 116.60, 40.06], 'EPSG:4326', 'EPSG:3857'
);
var STAGES = {
before: {
flood: 0.30,
title: '事件前',
text: '常规水位,河网只占低洼地带,城区建成区完整。'
},
during: {
flood: 0.72,
title: '事件中',
text: '强降雨后水位抬升,河道两侧大片区域转为水面。'
},
after: {
flood: 0.42,
title: '事件后',
text: '水位回落,退水后的湿地与积水仍沿河道分布。'
}
};
var cache = {};
function sceneFor(stage) {
if (cache[stage]) return cache[stage];
var conf = STAGES[stage];
var c = document.createElement('canvas');
c.width = SIZE;
c.height = SIZE;
var ctx = c.getContext('2d');
var img = ctx.createImageData(SIZE, SIZE);
for (var y = 0; y < SIZE; y++) {
for (var x = 0; x < SIZE; x++) {
var i = (y * SIZE + x) * 4;
/* 河道:一条弯曲主干加两条支流 */
var river = Math.abs(y - (SIZE * 0.62 + 60 * Math.sin(x / 90)));
var branch = Math.abs(y - (SIZE * 0.3 - 40 * Math.cos(x / 70)));
var band = Math.min(river, branch);
var isWater = band < conf.flood * 90;
var veg = Math.max(0, Math.min(1,
0.45 + 0.35 * Math.sin(x / 33) * Math.cos(y / 51)));
if (isWater) {
img.data[i] = 40;
img.data[i + 1] = 85;
img.data[i + 2] = 140;
} else {
img.data[i] = Math.round(90 + 40 * veg);
img.data[i + 1] = Math.round(120 + 70 * veg);
img.data[i + 2] = Math.round(70 + 30 * veg);
}
img.data[i + 3] = 255;
}
}
ctx.putImageData(img, 0, 0);
cache[stage] = { url: c.toDataURL(), water: conf.flood };
return cache[stage];
}
var layer = new ol.layer.Image({ opacity: 0.9 });
var current = 'before';
function rebuild(stage) {
var scene = sceneFor(stage);
layer.setSource(new ol.source.Raster({
sources: [new ol.source.ImageStatic({
url: scene.url,
projection: 'EPSG:3857',
imageExtent: extent,
crossOrigin: 'anonymous'
})],
operation: function (pixels) {
var src = pixels[0];
var out = new Uint8ClampedArray(src.length);
var water = 0, count = 0;
for (var i = 0; i < src.length; i += 4) {
var r = src[i], g = src[i + 1], b = src[i + 2];
/* 水体指数:蓝色明显高于红色时判为水面 */
var isWater = (b - r) > 45;
if (isWater) {
out[i] = 32;
out[i + 1] = 78;
out[i + 2] = 132;
water++;
} else {
out[i] = Math.min(255, r * 1.15);
out[i + 1] = Math.min(255, g * 1.1);
out[i + 2] = Math.min(255, b * 1.2);
}
out[i + 3] = 255;
count++;
}
layer.__waterRatio = water / count;
return out;
}
}));
var conf = STAGES[stage];
document.getElementById('caption').innerHTML =
'<b>' + conf.title + '</b><span>' + conf.text + '</span>';
document.getElementById('info').textContent =
'水面占比 ' + (scene.water * 100).toFixed(0) + '%(合成参数)';
}
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}), opacity: 0.45 }),
layer
],
view: new ol.View({
center: ol.proj.fromLonLat([116.38, 39.92]),
zoom: 11,
minZoom: 2
}),
controls: ol.control.defaults.defaults({ attribution: false })
});
Array.prototype.forEach.call(
document.querySelectorAll('[data-stage]'),
function (btn) {
btn.addEventListener('click', function () {
Array.prototype.forEach.call(
document.querySelectorAll('[data-stage]'),
function (b) { b.classList.remove('on'); }
);
btn.classList.add('on');
current = btn.getAttribute('data-stage');
rebuild(current);
});
}
);
rebuild(current);
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.