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

Video Source

Runnable MapLibre GL JS example: Video 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-video-source.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-video-source.html
<!--
  示例:视频源
  核心逻辑:video 源把 mp4 贴到四角坐标,按钮调用 play / pause,读取视频时长
  依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 样式 · MDN 公共视频
-->
<!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/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:10px;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}
  .btn{border:1px solid #cdd3db;background:#fff;border-radius:4px;padding:3px 9px;
       font:12px/1.5 inherit;color:#16181d;cursor:pointer}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <button class="btn" id="play">播放</button>
  <button class="btn" id="pause">暂停</button>
  <span>时长 <b id="dur">—</b></span>
  <span>状态 <b id="state">等待元数据</b></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 map = new maplibregl.Map({
    container: 'map',
    style: 'https://demotiles.maplibre.org/style.json',
    center: [110.0, 34.0],
    zoom: 3.4,
    attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles · Video: MDN' }
  });

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

  map.on('load', function () {
    map.addSource('clip', {
      type: 'video',
      urls: [
        'https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4'
      ],
      coordinates: [
        [100.0, 39.0],
        [118.0, 39.0],
        [118.0, 28.0],
        [100.0, 28.0]
      ]
    });

    map.addLayer({
      id: 'clip',
      type: 'raster',
      source: 'clip',
      paint: {
        'raster-opacity': 0.95,
        'raster-fade-duration': 0
      }
    });

    map.on('sourcedata', function (e) {
      if (e.sourceId !== 'clip' || !e.isSourceLoaded) return;
      var video = map.getSource('clip').getVideo();
      if (!video) return;
      document.getElementById('dur').textContent = video.duration.toFixed(2) + ' s';
      document.getElementById('state').textContent =
        video.paused ? '已暂停(浏览器可能拦下自动播放)' : '播放中';
    });
  });

  document.getElementById('play').addEventListener('click', function () {
    var src = map.getSource('clip');
    if (!src) return;
    src.play();
    document.getElementById('state').textContent = '播放中';
  });

  document.getElementById('pause').addEventListener('click', function () {
    var src = map.getSource('clip');
    if (!src) return;
    src.pause();
    document.getElementById('state').textContent = '已暂停';
  });

  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 · EXAMPLEAnimate Line MapLibre GL JS · EXAMPLEAnimate Marker MapLibre GL JS · EXAMPLEBasic Map