Buffer
Build a buffer polygon around points or lines, with configurable steps.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:生成缓冲区 — 拖动半径滑块,用 turf.buffer 生成等距多边形
核心逻辑:半径以公里传入,steps 固定 64,面积用 turf.area 核对
依赖:Turf.js 7 · 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}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d;background:#fff}
.bar{flex:0 0 auto;display:flex;gap:12px;align-items:center;padding:8px 12px;
background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar input[type=range]{width:150px}
.bar b{color:#16181d;font-weight:600}
svg{flex:1 1 auto;min-height:0;width:100%;display:block;background:#fbfcfd}
.out{flex:0 0 auto;display:flex;gap:20px;flex-wrap:wrap;padding:8px 12px;
border-top:1px solid #e3e6ea;font-family:ui-monospace,Consolas,monospace;font-size:11.5px;color:#3a4250}
.out b{font-weight:600;color:#1f5fd0}
</style>
</head>
<body>
<div class="bar">
<label>半径 <input id="r" type="range" min="0.3" max="10" step="0.1" value="3"> <b id="rv">3.0 km</b></label>
<span>steps 64 · 球面等距扩展</span>
</div>
<svg id="stage" viewBox="0 0 640 320" preserveAspectRatio="xMidYMid meet"></svg>
<div class="out">
<span>面积 <b id="area">—</b></span>
<span>顶点 <span id="n">—</span></span>
<span>类型 <span id="type">—</span></span>
</div>
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js"></script>
<script>
(function () {
'use strict';
if (typeof turf === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#b42318">Turf.js 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var W = 640, H = 320, PAD = 40;
var BOX = [116.25, 39.80, 116.53, 40.01];
var center = turf.point([116.39, 39.905], { name: '站点' });
var stage = document.getElementById('stage');
var slider = document.getElementById('r');
var kx = Math.cos(((BOX[1] + BOX[3]) / 2) * Math.PI / 180);
var x0 = BOX[0] * kx, x1 = BOX[2] * kx;
var y0 = BOX[1], y1 = BOX[3];
var s = Math.min((W - PAD * 2) / (x1 - x0), (H - PAD * 2) / (y1 - y0));
var ox = (W - (x1 - x0) * s) / 2;
var oy = (H - (y1 - y0) * s) / 2;
function pr(c) {
return [(c[0] * kx - x0) * s + ox, (y1 - c[1]) * s + oy];
}
function ringPath(ring) {
var d = '';
for (var i = 0; i < ring.length; i++) {
var p = pr(ring[i]);
d += (i ? 'L' : 'M') + p[0].toFixed(1) + ' ' + p[1].toFixed(1);
}
return d + 'Z';
}
function draw() {
var radius = parseFloat(slider.value);
var ring = turf.buffer(center, radius, { units: 'kilometers', steps: 64 });
var coords = ring.geometry.coordinates;
var polys = ring.geometry.type === 'MultiPolygon' ? coords : [coords];
var parts = ['<rect x="0" y="0" width="640" height="320" fill="none" stroke="#e3e6ea"/>'];
polys.forEach(function (polyCoords) {
polyCoords.forEach(function (r, idx) {
parts.push('<path d="' + ringPath(r) + '" fill="' +
(idx === 0 ? 'rgba(31,95,208,0.13)' : '#fff') +
'" fill-rule="evenodd" stroke="#1f5fd0" stroke-width="1.5"/>');
});
});
var c = pr(center.geometry.coordinates);
parts.push('<circle cx="' + c[0].toFixed(1) + '" cy="' + c[1].toFixed(1) +
'" r="4" fill="#1f5fd0" stroke="#fff" stroke-width="2"/>');
/* 半径示意线:从中心向东画到缓冲边界 */
var east = pr([center.geometry.coordinates[0] + radius / (111.32 * kx),
center.geometry.coordinates[1]]);
parts.push('<line x1="' + c[0].toFixed(1) + '" y1="' + c[1].toFixed(1) +
'" x2="' + east[0].toFixed(1) + '" y2="' + east[1].toFixed(1) +
'" stroke="#8a929f" stroke-width="1" stroke-dasharray="4 3"/>');
parts.push('<text x="' + ((c[0] + east[0]) / 2).toFixed(1) + '" y="' +
(c[1] - 6).toFixed(1) + '" font-size="11" fill="#58606e">' +
radius.toFixed(1) + ' km</text>');
stage.innerHTML = parts.join('');
document.getElementById('rv').textContent = radius.toFixed(1) + ' km';
document.getElementById('area').textContent =
(turf.area(ring) / 1e6).toFixed(2) + ' km²';
document.getElementById('n').textContent = coords[0].length;
document.getElementById('type').textContent = ring.geometry.type;
}
slider.addEventListener('input', draw);
draw();
})();
</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.