Atlaslog
Atlaslog / Examples / OpenLayers / Webgl Layer Swipe
OpenLayers · Runnable Example

Webgl Layer Swipe

Runnable OpenLayers example: Webgl Layer Swipe. 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-webgl-layer-swipe.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-webgl-layer-swipe.html
<!--
  示例:图层裁剪式卷帘 — OpenLayers webgl-layer-swipe
  核心逻辑:滑块改写图层的 extent,只渲染切分线左侧,右侧露出底图
  依赖: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}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:10px;align-items:center;font-size:12px;color:#58606e}
  .bar input[type=range]{flex:1 1 auto;max-width:420px}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <span>切分线</span>
  <input type="range" id="split" min="0" max="100" value="50">
  <b id="splitVal">50%</b>
  <span id="info">—</span>
</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 (typeof ol === 'undefined') {
    document.getElementById('map').innerHTML =
      '<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  var EXTENT = ol.proj.transformExtent(
    [116.22, 39.82, 116.56, 40.00], 'EPSG:4326', 'EPSG:3857'
  );

  /* 生成一张与底图风格不同的专题图,用于对比 */
  function makeThematic() {
    var w = 900, h = 620;
    var c = document.createElement('canvas');
    c.width = w;
    c.height = h;
    var ctx = c.getContext('2d');
    ctx.fillStyle = '#f4efe4';
    ctx.fillRect(0, 0, w, h);

    var cols = 12, rows = 8;
    for (var i = 0; i < cols; i++) {
      for (var j = 0; j < rows; j++) {
        var v = Math.abs(Math.sin(i / 2.4 + j / 3.1));
        ctx.fillStyle = 'rgba(' + Math.round(80 + 140 * v) + ',' +
          Math.round(150 + 60 * v) + ',' + Math.round(70 + 30 * v) + ',0.85)';
        ctx.fillRect(i * w / cols, j * h / rows, w / cols + 1, h / rows + 1);
      }
    }

    ctx.strokeStyle = 'rgba(31,95,208,0.7)';
    ctx.lineWidth = 3;
    ctx.beginPath();
    ctx.moveTo(0, h * 0.55);
    ctx.bezierCurveTo(w * 0.3, h * 0.4, w * 0.6, h * 0.75, w, h * 0.5);
    ctx.stroke();

    ctx.fillStyle = 'rgba(20,20,20,0.7)';
    ctx.font = '15px sans-serif';
    ctx.fillText('专题面 · 分级设色', 16, 28);
    return c.toDataURL();
  }

  var thematicLayer = new ol.layer.Image({
    source: new ol.source.ImageStatic({
      url: makeThematic(),
      projection: 'EPSG:3857',
      imageExtent: EXTENT,
      crossOrigin: 'anonymous'
    }),
    opacity: 0.92
  });

  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'}) }),
      thematicLayer
    ],
    view: new ol.View({
      center: ol.proj.fromLonLat([116.38, 39.91]),
      zoom: 11,
      minZoom: 2
    }),
    controls: ol.control.defaults.defaults({ attribution: false })
  });

  var splitEl = document.getElementById('split');

  function applySplit() {
    var ratio = Number(splitEl.value) / 100;
    var minX = EXTENT[0] + (EXTENT[2] - EXTENT[0]) * ratio;

    /* 只保留左侧,右侧自动让位给底图 */
    thematicLayer.setExtent([EXTENT[0], EXTENT[1], minX, EXTENT[3]]);

    document.getElementById('splitVal').textContent = splitEl.value + '%';
    document.getElementById('info').textContent =
      '专题图显示范围 ' + Math.round(ratio * 100) + '% · 右侧为纯底图';
  }

  splitEl.addEventListener('input', applySplit);
  applySplit();

  /* 切分线随视图变化保持定位 */
  map.on('moveend', function () {
    void map.getView();
  });
})();
</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 · EXAMPLEWebgl Points Layer OpenLayers · EXAMPLEWebgl Sea Level OpenLayers · EXAMPLEWebgl Shaded Relief