Animate Line
Runnable MapLibre GL JS example: Animate Line. 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
<!--
示例:线条流动动画
核心逻辑:定时切换 line-dasharray 的取值,虚线间隔变化形成前进的动感
数据:页面内构造的一条环形巡检路线
依赖: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>线条流动动画</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 button{background:#fff;border:1px solid #cfd6df;border-radius:4px;padding:3px 10px;
font:12px/1.4 inherit;color:#3a4250;cursor:pointer}
.bar button:hover{border-color:#2e86de;color:#2e86de}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<span>节拍 <b id="f">0</b></span>
<span>序列 <b id="seq">4 4</b></span>
<button id="pause">暂停</button>
<span>虚线间隔轮流切换,方向沿线的走向</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 loop = [
[100.0, 38.0], [104.0, 36.2], [108.0, 35.4],
[112.0, 33.6], [116.0, 32.2], [118.2, 29.8],
[115.0, 27.8], [110.0, 28.8], [105.0, 31.0],
[100.8, 34.2], [100.0, 38.0]
];
var map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [109, 33],
zoom: 4.4,
attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
/* 每一帧换一组 dasharray,视觉上虚线在向前走 */
var STEPS = [[4, 4], [0, 4, 3], [0, 0, 3, 3], [0, 4], [4, 0], [3, 0, 3, 0], [2, 0, 2, 0, 2, 0]];
var seqEl = document.getElementById('seq');
var fEl = document.getElementById('f');
var timer = null;
var idx = 0;
var frames = 0;
var running = true;
map.on('load', function () {
map.addSource('loop', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {},
geometry: { type: 'LineString', coordinates: loop }
}]
}
});
/* 底层实线做衬底,虚线叠在上面才看得清 */
map.addLayer({
id: 'loop-base',
type: 'line',
source: 'loop',
layout: { 'line-cap': 'round', 'line-join': 'round' },
paint: { 'line-color': '#2e86de', 'line-width': 5, 'line-opacity': 0.25 }
});
map.addLayer({
id: 'loop-dash',
type: 'line',
source: 'loop',
layout: { 'line-cap': 'butt', 'line-join': 'round' },
paint: { 'line-color': '#f03e3e', 'line-width': 4, 'line-dasharray': STEPS[0] }
});
timer = setInterval(function () {
if (!running) return;
idx = (idx + 1) % STEPS.length;
frames++;
seqEl.textContent = STEPS[idx].join(' ');
fEl.textContent = frames;
map.setPaintProperty('loop-dash', 'line-dasharray', STEPS[idx]);
}, 90);
});
document.getElementById('pause').addEventListener('click', function () {
running = !running;
this.textContent = running ? '暂停' : '继续';
});
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.