Heatmap
Runnable deck.gl example: Heatmap. 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
<!--
示例:deck.gl 热力图
要点:HeatmapLayer 用核密度把四点二万个点压成连续密度场,核半径可实时调节
依赖:deck.gl 9.0 · jsDelivr CDN
-->
<!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>
<style>
html,body{height:100%;margin:0;background:#0d1117}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
.bar{position:relative;z-index:5;flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
display:flex;gap:18px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.bar input[type=range]{width:150px;accent-color:#4f9ef8}
#cv{flex:1 1 auto;min-height:0;width:100%}
</style>
</head>
<body>
<div class="bar">
<span>点数 <b id="n">—</b></span>
<span>核半径 <b id="rv">44</b> px</span>
<label>半径 <input type="range" id="radius" min="12" max="80" step="2" value="44"></label>
<span>拖拽旋转 · 滚轮缩放</span>
</div>
<canvas id="cv"></canvas>
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.0.35/dist.min.js"></script>
<script>
(function () {
'use strict';
if (typeof deck === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#ff8c74">deck.gl 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
/* 五个聚集中心,模拟不同强度的活动区域 */
var centers = [
[121.470, 31.232, 15000, 0.26],
[121.620, 31.120, 9000, 0.18],
[121.320, 31.300, 7000, 0.15],
[121.545, 31.345, 6000, 0.12],
[121.405, 31.055, 5000, 0.14]
];
var points = [];
function gauss() {
var u = 1 - Math.random();
var v = Math.random();
return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v) * 0.38;
}
for (var i = 0; i < centers.length; i++) {
var c = centers[i];
for (var j = 0; j < c[2]; j++) {
points.push({
lng: c[0] + gauss() * c[3],
lat: c[1] + gauss() * c[3] * 0.8,
w: 0.55 + Math.random() * 0.75
});
}
}
document.getElementById('n').textContent = points.length.toLocaleString();
var colorRange = [
[8, 24, 58, 0],
[22, 68, 130, 110],
[32, 130, 190, 175],
[96, 200, 190, 205],
[248, 208, 88, 225],
[243, 120, 48, 240],
[208, 34, 45, 255]
];
var radiusPixels = 44;
function buildLayer() {
return new deck.HeatmapLayer({
id: 'density',
data: points,
getPosition: function (d) { return [d.lng, d.lat]; },
getWeight: function (d) { return d.w; },
radiusPixels: radiusPixels,
intensity: 1.15,
threshold: 0.04,
aggregation: 'SUM',
colorRange: colorRange
});
}
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
initialViewState: {
longitude: 121.46,
latitude: 31.20,
zoom: 10.4,
pitch: 0,
bearing: 0
},
controller: true,
layers: [buildLayer()]
});
document.getElementById('radius').addEventListener('input', function (e) {
radiusPixels = Number(e.target.value);
document.getElementById('rv').textContent = radiusPixels;
deckgl.setProps({ layers: [buildLayer()] });
});
window.__deck = deckgl;
})();
</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.