Atlaslog
Atlaslog / Examples / OpenLayers / Multiple Cogs
OpenLayers · Runnable Example

Multiple Cogs

Runnable OpenLayers example: Multiple Cogs. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All OpenLayers examples
/demos/openlayers-multiple-cogs.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-multiple-cogs.html
<!--
  示例:多期栅格叠加
  要点:同一范围挂三份 ol.source.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>多期栅格叠加</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;color:#16181d}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:14px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
  .bar input[type=range]{width:90px}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <label><input type="checkbox" id="p1" checked> 第一期</label>
  <label><input type="checkbox" id="p2" checked> 第二期</label>
  <label><input type="checkbox" id="p3" checked> 第三期</label>
  <label for="op">叠加不透明度 <b id="opv">0.55</b></label>
  <input type="range" id="op" min="0.2" max="1" step="0.05" value="0.55">
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
  'use strict';
  if (!window.ol) {
    document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
    return;
  }

  var N = 128;
  var extent = ol.proj.transformExtent([116.20, 39.75, 116.62, 40.05], 'EPSG:4326', 'EPSG:3857');

  /* 三期数据:同一场过程的不同阶段,用相位差模拟变化 */
  function periodURL(phase) {
    var c = document.createElement('canvas');
    c.width = c.height = N;
    var ctx = c.getContext('2d');
    var img = ctx.createImageData(N, N);
    for (var y = 0; y < N; y++) {
      for (var x = 0; x < N; x++) {
        var u = x / N, v = y / N;
        var t = 0.5 + 0.34 * Math.sin(u * 5.4 + phase) * Math.cos(v * 4.6 - phase * 0.7)
          + 0.12 * Math.sin((u + v) * 8.8 + phase);
        var g = Math.round(Math.max(0, Math.min(1, t)) * 255);
        var i = (y * N + x) * 4;
        img.data[i] = img.data[i + 1] = img.data[i + 2] = g;
        img.data[i + 3] = 255;
      }
    }
    ctx.putImageData(img, 0, 0);
    return c.toDataURL('image/png');
  }

  function ramp(stops, t) {
    for (var i = 0; i < stops.length - 1; i++) {
      if (t >= stops[i][0] && t <= stops[i + 1][0]) {
        var f = (t - stops[i][0]) / Math.max(1e-6, stops[i + 1][0] - stops[i][0]);
        return [
          stops[i][1] + (stops[i + 1][1] - stops[i][1]) * f,
          stops[i][2] + (stops[i + 1][2] - stops[i][2]) * f,
          stops[i][3] + (stops[i + 1][3] - stops[i][3]) * f
        ];
      }
    }
    var last = stops[stops.length - 1];
    return [last[1], last[2], last[3]];
  }

  var bands = {
    p1: [[0, 12, 46, 130], [0.5, 90, 170, 220], [1, 240, 248, 255]],
    p2: [[0, 40, 20, 90], [0.5, 200, 120, 160], [1, 255, 235, 210]],
    p3: [[0, 10, 60, 40], [0.5, 110, 190, 90], [1, 245, 250, 190]]
  };

  function build(key, phase) {
    var base = new ol.source.ImageStatic({
      url: periodURL(phase), imageExtent: extent, projection: 'EPSG:3857'
    });
    var lut = bands[key];
    var raster = new ol.source.Raster({
      sources: [base],
      operation: function (pixels) {
        var input = pixels[0];
        var out = new ImageData(input.width, input.height);
        for (var i = 0; i < input.data.length; i += 4) {
          var col = ramp(lut, input.data[i] / 255);
          out.data[i] = col[0]; out.data[i + 1] = col[1]; out.data[i + 2] = col[2];
          out.data[i + 3] = 235;
        }
        return out;
      }
    });
    return new ol.layer.Image({ source: raster, opacity: 0.55 });
  }

  var layers = {
    p1: build('p1', 0),
    p2: build('p2', 2.1),
    p3: build('p3', 4.2)
  };

  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'}) }), layers.p1, layers.p2, layers.p3],
    view: new ol.View({ center: ol.proj.fromLonLat([116.40, 39.90]), zoom: 10 }),
    controls: ol.control.defaults.defaults({ rotate: false })
  });

  ['p1', 'p2', 'p3'].forEach(function (key) {
    document.getElementById(key).addEventListener('change', function () {
      layers[key].setVisible(this.checked);
    });
  });

  document.getElementById('op').addEventListener('input', function () {
    var v = Number(this.value);
    document.getElementById('opv').textContent = v.toFixed(2);
    Object.keys(layers).forEach(function (k) { layers[k].setOpacity(v); });
  });
})();
</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.

More OpenLayers examples

OpenLayers · EXAMPLENavigation Controls OpenLayers · EXAMPLENumpytile OpenLayers · EXAMPLEOffscreen Canvas