Wind Field Streamlines
Two sine fields compose a wind field; 700 seeds integrate 24 steps each, LineLayer joins them into streamlines, and opacity cycles frame by frame.
Business context
Meteorology, oceanography and wind power use wind streamlines to read large-scale circulation, identify eddies and jets, and support route planning, power forecasting and pollution dispersion studies. Data comes from numerical model U/V component grids such as GFS and ECMWF, with horizontal resolution from 0.25 degrees down to 9 km. A regional grid holds tens to hundreds of thousands of cells per level; this example synthesises an equivalent field from sines.
Model output is interpolated to a common grid, gaps are filled, and the step is chosen against the target zoom level: 0.05 degrees of longitude is about 4.5 km at 36 degrees north, roughly 7 pixels per step at zoom 7.6, which still shows curvature; finer steps break into rows of dots. LineLayer is used rather than PathLayer because per-frame animation only rewrites colour and opacity while the geometry and data references stay untouched, at about 67 KB per frame.
Density is easily misread as wind speed: denser lines mean more seeds, not stronger wind, which colour encodes. Streamlines are integral paths of a steady field, not true particle trajectories, so they cannot answer how long wind takes to reach a place. Model wind is a cell average, and near-surface wind deviates strongly with terrain and buildings, so city-scale conclusions cannot be taken directly from the model field.
How it works
Mapping 114–120°E and 34–38°N onto 2π gives u = sin(1.2y)·cos(0.7x) + 0.45cos(0.5y+0.8) and v = 0.85cos(1.4x) − 0.5sin(0.8y), a steady field with eddies. Each seed advances 24 steps along the normalised direction, at 0.05 degrees in longitude and 0.042 in latitude per step; 700 seeds yield 16,800 segments, with LineLayer getSourcePosition and getTargetPosition reading adjacent points and getWidth fixed at 1.3 px. Colour takes four bands, blue, cyan, yellow and red, by the wind speed at the segment, and opacity is 0.16 + 0.84 × (0.5 + 0.5·sin(phase − 3.4t)), where phase is the seed phase plus 0.42 × step index.
A longitude step of 0.05 degrees is about 4.5 km at 36°N and about 7 px at zoom 7.6, which is enough to show curvature; dropping to 0.01 degrees puts each step under 2 px and the 24-step polyline breaks into a dot row, while 0.2 degrees spans 29 px per step and bends eddies into corners. The per-frame animation walks the 16,800 segments and rewrites the fourth component of the colour array, without rebuilding geometry vertices or the data reference, and updateTriggers re-uploads the colour attribute at roughly 67 KB per frame; only the seed slider reintegrates geometry, with 900 seeds giving 21,600 segments.
| Configuration | What it does |
|---|---|
| getSourcePosition / getTargetPosition | Splits adjacent streamline points into separate segments |
| Step 0.05° / 0.042° | About 7 px per step at zoom 7.6, balancing curvature and readability |
| updateTriggers: { getColor: phaseT } | Triggers colour recomputation only; geometry is not rebuilt |
| widthUnits: 'pixels' | Fixed 1.3 px width that does not thicken with zoom |
Note. Derive the streamline step in pixels at the target zoom, with 3–8 px per segment easiest to read; per-frame animation should change colour or opacity, not recompute paths.
Source
<!--
示例:风场流线
要点:700 个种子点积分 24 步生成流线,逐帧只改线段颜色透明度,几何数组保持不变
依赖: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:12px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.bar label{display:flex;align-items:center;gap:5px;cursor:pointer}
.bar input[type=checkbox]{accent-color:#4f9ef8}
.bar input[type=range]{width:140px;accent-color:#4f9ef8}
.bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:5px;cursor:pointer;
border:1px solid #303845;background:#1d2531;color:#c8d2e0}
.bar button:hover{background:#253040}
.bar button.on{background:#2c4a7a;border-color:#4f8ad6;color:#dfe9ff}
.bar .sep{width:1px;height:18px;background:#2a313c;margin:0 4px}
#cv{flex:1 1 auto;min-height:0;width:100%;display:block}
</style>
</head>
<body>
<div class="bar">
<span>种子 <b id="n">700</b> · 线段 <b id="seg">—</b></span>
<input id="seeds" type="range" min="200" max="900" step="100" value="700">
<span class="sep"></span>
<button id="run" class="on">动态</button>
<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;
}
function rng(seed) {
var a = seed >>> 0;
return function () {
a = (a + 0x6D2B79F5) >>> 0;
var t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
var X0 = 114.0;
var Y0 = 34.0;
var W = 6.0;
var H = 4.0;
function windAt(lng, lat) {
var x = (lng - X0) / W * Math.PI * 2;
var y = (lat - Y0) / H * Math.PI * 2;
var u = Math.sin(y * 1.2) * Math.cos(x * 0.7) + 0.45 * Math.cos(y * 0.5 + 0.8);
var v = Math.cos(x * 1.4) * 0.85 - 0.5 * Math.sin(y * 0.8);
return [u, v];
}
function windColor(s) {
var t = s / 1.7;
t = t < 0 ? 0 : (t > 1 ? 1 : t);
var ramp = [[48, 92, 168], [56, 176, 196], [236, 216, 108], [238, 108, 72]];
var k = t * 3;
var i0 = Math.floor(k);
if (i0 > 2) i0 = 2;
var f = k - i0;
var c0 = ramp[i0];
var c1 = ramp[i0 + 1];
return [
Math.round(c0[0] + (c1[0] - c0[0]) * f),
Math.round(c0[1] + (c1[1] - c0[1]) * f),
Math.round(c0[2] + (c1[2] - c0[2]) * f),
255
];
}
function buildField(seedCount) {
var rnd = rng(97);
var out = [];
var s, k;
for (s = 0; s < seedCount; s++) {
var lng = X0 + 0.3 + rnd() * (W - 0.6);
var lat = Y0 + 0.3 + rnd() * (H - 0.6);
var ph = rnd() * Math.PI * 2;
var path = [[lng, lat]];
var speeds = [];
for (k = 0; k < 24; k++) {
var f = windAt(lng, lat);
var m = Math.sqrt(f[0] * f[0] + f[1] * f[1]);
if (m < 0.02) m = 0.02;
speeds.push(m);
lng += (f[0] / m) * 0.05;
lat += (f[1] / m) * 0.042;
path.push([lng, lat]);
}
for (k = 0; k < 24; k++) {
out.push({
s: path[k],
t: path[k + 1],
c: windColor(speeds[k]),
phase: ph + k * 0.42
});
}
}
return out;
}
var segs = buildField(700);
var phaseT = 0;
var dynamic = true;
function refreshAlphas() {
for (var i = 0; i < segs.length; i++) {
var s = segs[i];
var a = 0.16 + 0.84 * (0.5 + 0.5 * Math.sin(s.phase - phaseT * 3.4));
s.c[3] = Math.round(a * 255);
}
}
refreshAlphas();
function getSrc(d) { return d.s; }
function getDst(d) { return d.t; }
function getCol(d) { return d.c; }
function buildLayers() {
return [new deck.LineLayer({
id: 'wind-flow',
data: segs,
getSourcePosition: getSrc,
getTargetPosition: getDst,
getColor: getCol,
getWidth: 1.3,
widthUnits: 'pixels',
widthMinPixels: 0.9,
updateTriggers: { getColor: phaseT }
})];
}
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
initialViewState: { longitude: 117.0, latitude: 36.0, zoom: 7.6, pitch: 0, bearing: 0 },
controller: true,
layers: buildLayers()
});
document.getElementById('seg').textContent = segs.length;
document.getElementById('seeds').addEventListener('input', function (e) {
var n = Number(e.target.value);
segs = buildField(n);
refreshAlphas();
document.getElementById('n').textContent = n;
document.getElementById('seg').textContent = segs.length;
deckgl.setProps({ layers: buildLayers() });
});
document.getElementById('run').addEventListener('click', function (e) {
dynamic = !dynamic;
e.currentTarget.textContent = dynamic ? '动态' : '静止';
e.currentTarget.classList.toggle('on', dynamic);
});
var last = performance.now();
function frame(now) {
if (dynamic) {
phaseT += (now - last) / 1000;
last = now;
refreshAlphas();
deckgl.setProps({ layers: buildLayers() });
} else {
last = now;
}
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
window.__deck = deckgl;
})();
</script>
</body>
</html>