Imagery Canvas Tile
Runnable Cesium example: Imagery Canvas Tile. 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 画一张地图当影像底图
要点:SingleTileImageryProvider 把一张图铺到指定经纬矩形,图层 alpha 可调
依赖:CesiumJS 1.121 · jsDelivr CDN,图像由 2D canvas 绘制并转 dataURL
-->
<!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/cesium@1.121.0/Build/Cesium/Widgets/widgets.css">
<style>
html,body{height:100%;margin:0}
body{background:#0e1116;color:#e6e8ec;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
#c{height:100%}
.hud{position:absolute;left:12px;top:12px;z-index:5;background:rgba(23,28,35,.9);
border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
color:#a2a9b6;max-width:250px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:6px}
input[type=range]{width:100%}
.cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
.cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="hud">
<div><b>canvas 底图</b></div>
<div class="row">图层透明度 <span id="aVal">1.00</span></div>
<div class="row"><input id="alpha" type="range" min="0" max="1" step="0.05" value="1"></div>
<div class="row">1024 × 768 · 覆盖 115.8~117.0°E</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Cesium.js"></script>
<script>
(function () {
'use strict';
var viewer = new Cesium.Viewer('c', {
baseLayer: false,
baseLayerPicker: false, geocoder: false, homeButton: false,
sceneModePicker: false, navigationHelpButton: false,
animation: false, timeline: false, fullscreenButton: false,
infoBox: false, selectionIndicator: false
});
var scene = viewer.scene;
scene.globe.baseColor = Cesium.Color.fromCssColorString('#16202c');
scene.backgroundColor = Cesium.Color.fromCssColorString('#0e1116');
scene.skyBox.show = false;
scene.sun.show = false;
scene.moon.show = false;
if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;
/* ---------- 画一张示意地图 ---------- */
function drawMap() {
var W = 1024, H = 768;
var c = document.createElement('canvas');
c.width = W; c.height = H;
var ctx = c.getContext('2d');
ctx.fillStyle = '#1b2634';
ctx.fillRect(0, 0, W, H);
/* 水系 */
ctx.fillStyle = '#173346';
ctx.beginPath();
ctx.moveTo(0, 520);
ctx.bezierCurveTo(220, 470, 380, 620, 620, 560);
ctx.bezierCurveTo(800, 520, 900, 600, W, 560);
ctx.lineTo(W, H); ctx.lineTo(0, H); ctx.closePath();
ctx.fill();
/* 街区网格 */
ctx.strokeStyle = 'rgba(90,120,155,0.35)';
ctx.lineWidth = 1;
for (var x = 40; x < W; x += 64) {
ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, H); ctx.stroke();
}
for (var y = 40; y < H; y += 64) {
ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(W, y); ctx.stroke();
}
/* 主干道 */
ctx.strokeStyle = '#3f5a78';
ctx.lineWidth = 10;
ctx.beginPath();
ctx.moveTo(0, 300);
ctx.bezierCurveTo(300, 260, 500, 360, W, 300);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(420, 0);
ctx.bezierCurveTo(460, 260, 380, 520, 440, H);
ctx.stroke();
ctx.strokeStyle = 'rgba(255,177,61,0.85)';
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(0, 300);
ctx.bezierCurveTo(300, 260, 500, 360, W, 300);
ctx.stroke();
/* 绿地 */
ctx.fillStyle = 'rgba(60,130,110,0.55)';
ctx.beginPath();
ctx.arc(760, 180, 90, 0, Math.PI * 2);
ctx.fill();
/* 标注 */
ctx.fillStyle = '#cfd9e6';
ctx.font = '600 26px "Microsoft YaHei", sans-serif';
ctx.fillText('示意影像', 40, 70);
ctx.font = '18px "Microsoft YaHei", sans-serif';
ctx.fillStyle = '#8fa3b8';
ctx.fillText('canvas 生成 · 非真实地图', 40, 104);
return c.toDataURL('image/png');
}
var rectangle = Cesium.Rectangle.fromDegrees(115.8, 39.5, 117.0, 40.3);
var provider = new Cesium.SingleTileImageryProvider({
url: drawMap(),
rectangle: rectangle,
credit: 'canvas 生成'
});
var layer = viewer.imageryLayers.addImageryProvider(provider);
/* 边界线,标出影像覆盖范围 */
var corners = [
Cesium.Cartesian3.fromDegrees(115.8, 39.5),
Cesium.Cartesian3.fromDegrees(117.0, 39.5),
Cesium.Cartesian3.fromDegrees(117.0, 40.3),
Cesium.Cartesian3.fromDegrees(115.8, 40.3)
];
viewer.entities.add({
polyline: {
positions: corners.concat([corners[0]]),
width: 2,
material: new Cesium.PolylineDashMaterialProperty({
color: Cesium.Color.fromCssColorString('#ffb13d'),
dashLength: 20
}),
clampToGround: true
}
});
document.getElementById('alpha').addEventListener('input', function () {
layer.alpha = parseFloat(this.value);
document.getElementById('aVal').textContent = parseFloat(this.value).toFixed(2);
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.4, 39.2, 420000),
orientation: {
heading: 0,
pitch: Cesium.Math.toRadians(-78),
roll: 0
},
duration: 0
});
})();
</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.