Atlaslog
Atlaslog / Examples / MapLibre GL JS / Canvas Source
MapLibre GL JS · Runnable Example

Canvas Source

Runnable MapLibre GL JS example: Canvas Source. 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 MapLibre GL JS examples
/demos/maplibre-canvas-source.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-canvas-source.html
<!--
  示例:Canvas 动态栅格源
  核心逻辑:CanvasSource 把一块 canvas 当作栅格图层,animate 让纹理逐帧刷新
  数据:页面内绘制的雷达扫描动画,替代外部的雷达图服务
  依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 样式
-->
<!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>Canvas 动态栅格源</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.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;display:flex;gap:14px;align-items:center;padding:8px 12px;
       background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar b{color:#16181d;font-weight:600}
  .bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
  .bar input[type=range]{width:120px}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <span>画布 <b>256 × 256</b></span>
  <label>不透明度 <input id="o" type="range" min="0" max="1" step="0.05" value="0.7"> <b id="ov">0.70</b></label>
  <label><input id="anim" type="checkbox" checked> 播放扫描</label>
  <span>canvas 每帧重绘,纹理自动上传</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.js"></script>
<script>
(function () {
  'use strict';

  if (typeof maplibregl === 'undefined') {
    document.body.innerHTML =
      '<div style="padding:24px;color:#b42318">MapLibre 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  var W = 256;
  var canvas = document.createElement('canvas');
  canvas.width = W;
  canvas.height = W;
  var g = canvas.getContext('2d');

  /* 雷达回波:几团随机噪点,加上一条旋转扫描线 */
  var blobs = [];
  for (var i = 0; i < 26; i++) {
    blobs.push({
      x: Math.random() * W,
      y: Math.random() * W,
      r: 6 + Math.random() * 26,
      v: Math.random()
    });
  }

  var angle = 0;
  var running = true;

  function draw() {
    /* 清成透明,让底图透出来 */
    g.clearRect(0, 0, W, W);

    g.fillStyle = 'rgba(46, 134, 222, 0.42)';
    blobs.forEach(function (b) {
      g.beginPath();
      g.arc(b.x, b.y, b.r, 0, Math.PI * 2);
      g.fill();
    });

    if (running) angle += 0.03;

    var cx = W / 2;
    var cy = W / 2;
    var grad = g.createLinearGradient(
      cx, cy,
      cx + Math.cos(angle) * W, cy + Math.sin(angle) * W
    );
    grad.addColorStop(0, 'rgba(255, 245, 120, 0.85)');
    grad.addColorStop(1, 'rgba(255, 245, 120, 0)');

    g.strokeStyle = grad;
    g.lineWidth = 3;
    g.beginPath();
    g.moveTo(cx, cy);
    g.lineTo(cx + Math.cos(angle) * W, cy + Math.sin(angle) * W);
    g.stroke();

    g.strokeStyle = 'rgba(255,255,255,0.25)';
    g.lineWidth = 1;
    for (var r = 40; r < W / 2; r += 40) {
      g.beginPath();
      g.arc(cx, cy, r, 0, Math.PI * 2);
      g.stroke();
    }
  }

  draw();
  setInterval(draw, 80);

  var map = new maplibregl.Map({
    container: 'map',
    style: 'https://demotiles.maplibre.org/style.json',
    center: [116.39, 39.90],
    zoom: 6,
    attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
  });

  map.addControl(new maplibregl.NavigationControl(), 'top-right');

  map.on('load', function () {
    /* 四个角按 左上、右上、右下、左下 的顺序给 */
    map.addSource('radar', {
      type: 'canvas',
      canvas: canvas,
      animate: true,
      coordinates: [
        [113.0, 42.5],
        [119.8, 42.5],
        [119.8, 37.3],
        [113.0, 37.3]
      ]
    });

    map.addLayer({
      id: 'radar',
      type: 'raster',
      source: 'radar',
      paint: { 'raster-opacity': 0.7 }
    });
  });

  document.getElementById('o').addEventListener('input', function () {
    var v = parseFloat(this.value);
    document.getElementById('ov').textContent = v.toFixed(2);
    if (map.getLayer('radar')) {
      map.setPaintProperty('radar', 'raster-opacity', v);
    }
  });

  document.getElementById('anim').addEventListener('change', function () {
    running = this.checked;
  });

  map.on('error', function (e) {
    if (e.error) console.warn('地图错误:' + e.error.message);
  });
})();
</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 MapLibre GL JS examples

MapLibre GL JS · EXAMPLECenter on Click MapLibre GL JS · EXAMPLECluster MapLibre GL JS · EXAMPLECustom Control