Image Source
Runnable MapLibre GL JS example: Image Source. 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
<!--
示例:影像源
核心逻辑:canvas 生成图片后作为 image 源,updateImage 换图、setCoordinates 平移
依赖: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: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="swap">换一张图</button>
<button class="btn" id="shift">向东平移 1°</button>
<button class="btn" id="reset">复位</button>
<span>当前 <b id="cur">方案 A</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;
}
function makeImage(title, bg, line) {
var c = document.createElement('canvas');
c.width = 640;
c.height = 480;
var g = c.getContext('2d');
g.fillStyle = bg;
g.fillRect(0, 0, 640, 480);
g.strokeStyle = line;
g.lineWidth = 1;
for (var x = 0; x <= 640; x += 40) {
g.beginPath(); g.moveTo(x, 0); g.lineTo(x, 480); g.stroke();
}
for (var y = 0; y <= 480; y += 40) {
g.beginPath(); g.moveTo(0, y); g.lineTo(640, y); g.stroke();
}
g.fillStyle = line;
g.font = 'bold 34px sans-serif';
g.fillText(title, 36, 72);
g.font = '16px sans-serif';
g.fillText('640 × 480 · 覆盖范围 103–119°E', 36, 106);
return c.toDataURL('image/png');
}
var IMG_A = makeImage('PLAN A', '#f4ead8', '#b08a54');
var IMG_B = makeImage('PLAN B', '#dbe8f4', '#4a7096');
var BASE = [
[103.0, 41.0],
[119.0, 41.0],
[119.0, 27.0],
[103.0, 27.0]
];
var coords = BASE.slice();
var map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [111.0, 34.0],
zoom: 4,
attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
map.on('load', function () {
map.addSource('plan', {
type: 'image',
url: IMG_A,
coordinates: coords
});
map.addLayer({
id: 'plan',
type: 'raster',
source: 'plan',
paint: { 'raster-opacity': 0.85, 'raster-fade-duration': 0 }
});
});
document.getElementById('swap').addEventListener('click', function () {
var src = map.getSource('plan');
if (!src) return;
var toB = document.getElementById('cur').textContent === '方案 A';
src.updateImage({ url: toB ? IMG_B : IMG_A });
document.getElementById('cur').textContent = toB ? '方案 B' : '方案 A';
});
document.getElementById('shift').addEventListener('click', function () {
var src = map.getSource('plan');
if (!src) return;
coords = coords.map(function (c) { return [c[0] + 1, c[1]]; });
src.setCoordinates(coords);
});
document.getElementById('reset').addEventListener('click', function () {
var src = map.getSource('plan');
if (!src) return;
coords = BASE.slice();
src.setCoordinates(coords);
map.easeTo({ center: [111.0, 34.0], zoom: 4, duration: 400 });
});
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.