Atlaslog
Atlaslog / Examples / deck.gl / Business scenarios / Ocean Current Arrows and Streamlines
deck.gl · Business scenario

Ocean Current Arrows and Streamlines

One current field sampled as 3,000 short arrows and 400 streamlines; a button switches between them to compare the information each sampling density carries.

Open standalone → Chinese notes All business examples
/demos/deckgl-biz-env-ocean-current.htmlLIVE
single HTML file · CDN dependencies onlyinterface labels are in Chinese

Business context

Marine forecasting, shipping and offshore engineering use current maps to judge direction and speed, assess routes and construction windows, and trace oil spills and drifting objects. Data comes from operational ocean models such as HYCOM and ROMS, or from ADCP and drifting buoy measurements. A regional model level can reach hundreds of thousands of cells; this example uses one rectangular coastal domain.

Model output is interpolated to a regular grid and normalised to WGS84, with land masks and gaps left empty rather than set to zero flow. Arrows and streamlines are two samplings of the same field: 3,000 origins each connect one segment, expressing instantaneous direction and speed; 400 seeds integrate 26 steps, expressing transport paths. Both share a blue-to-red speed ramp, and switching only toggles visibility.

Arrow length is the easiest thing to misread: length is written in degrees, so pixel length scales with zoom, arrows overlap when zoomed in and fall below two pixels when zoomed out, so reading speed from length holds only at a fixed scale. Coverage is also confused: 3,000 arrows cover roughly seven times the area of 400 streamlines, and more arrows do not mean a more complete field.

How it works

The domain is 118–124°E, 32.2–36.6°N, with u = 0.55 + 0.75cos(1.3y) + 0.35sin(0.7x) and v = 0.65sin(1.1x) − 0.25cos(0.6y), composing a field that drifts northeast overall. Arrows use 3,000 random origins with length 0.02 + min(speed, 1.8) × 0.05 degrees, the endpoint coming from the normalised direction times that length, drawn by LineLayer at 1.2 px; streamlines take 400 separate seeds integrated 26 steps at 0.045 degrees, with PathLayer getWidth at 1.1 + mean speed × 0.9 px. Both datasets share a four-band blue-cyan-yellow-red ramp over 0–1.7.

At regional scale the 3,000 arrows work out to roughly one per 90 km², which shows directional texture but no continuous path; 400 streamlines carry a complete transport path but cover about a seventh of that area. Arrow length is expressed in degrees, so 0.07 degrees is about 10 px at zoom 7.6, grows to 55 px at zoom 10 where heads overlap, and shrinks below 2 px at zoom 5 where direction disappears. Fixed 1.2 px width compounds that length distortion, which is why the arrow layer suits browsing at one fixed scale only.

ConfigurationWhat it does
3,000 arrows × 1 segmentLineLayer expresses instantaneous direction, dense coverage without continuity
400 streamlines × 26 steps / 0.045°PathLayer expresses paths; coverage is about 1/7 of the arrows
Arrow length 0.02 + speed × 0.05°Length encodes speed; pixel length changes with zoom
widthUnits: 'pixels'Both layers fix width in pixels, so strokes do not thicken when zooming

Note. Vector arrows suit fixed extents and static maps; for zooming, use streamlines or rescale arrow length by zoom. Degree-based fixed-length arrows are not readable across scales.

Source

demos/deckgl-biz-env-ocean-current.html
<!--
  示例:洋流箭头与流线
  要点:同一份洋流场分别抽成 3000 条短线箭头与 400 条流线,按钮切换两种表达
  依赖: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>3000</b> · 流线 <b>400</b></span>
  <span class="sep"></span>
  <button data-m="arrow" class="on">箭头</button>
  <button data-m="line">流线</button>
  <span class="sep"></span>
  <span>色带 0–1.7 · 蓝慢红快</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;
    };
  }

  function currentAt(lng, lat) {
    var x = (lng - 118.0) / 6.0 * Math.PI * 2;
    var y = (lat - 32.2) / 4.4 * Math.PI * 2;
    var u = 0.55 + Math.cos(y * 1.3) * 0.75 + Math.sin(x * 0.7) * 0.35;
    var v = Math.sin(x * 1.1) * 0.65 - Math.cos(y * 0.6) * 0.25;
    return [u, v];
  }

  function speedColor(sp) {
    var t = sp / 1.7;
    t = t < 0 ? 0 : (t > 1 ? 1 : t);
    var ramp = [[40, 92, 168], [58, 178, 202], [238, 212, 108], [236, 94, 68]];
    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),
      238
    ];
  }

  var arrows = [];
  var rnd = rng(8123);
  var i, k;
  for (i = 0; i < 3000; i++) {
    var lng = 118.2 + rnd() * 5.6;
    var lat = 32.4 + rnd() * 4.0;
    var f = currentAt(lng, lat);
    var m = Math.sqrt(f[0] * f[0] + f[1] * f[1]);
    if (m < 0.01) m = 0.01;
    var len = 0.02 + Math.min(m, 1.8) * 0.05;
    arrows.push({
      s: [lng, lat],
      t: [lng + f[0] / m * len, lat + f[1] / m * len * 0.8],
      c: speedColor(m)
    });
  }

  var lines = [];
  var rnd2 = rng(9901);
  for (i = 0; i < 400; i++) {
    lng = 118.3 + rnd2() * 5.4;
    lat = 32.5 + rnd2() * 3.8;
    var path = [[lng, lat]];
    var sum = 0;
    for (k = 0; k < 26; k++) {
      var f2 = currentAt(lng, lat);
      var m2 = Math.sqrt(f2[0] * f2[0] + f2[1] * f2[1]);
      if (m2 < 0.01) m2 = 0.01;
      sum += m2;
      lng += f2[0] / m2 * 0.045;
      lat += f2[1] / m2 * 0.038;
      path.push([lng, lat]);
    }
    var avg = sum / 26;
    lines.push({ path: path, sp: avg, c: speedColor(avg) });
  }

  var mode = 'arrow';

  function buildLayers() {
    if (mode === 'arrow') {
      return [new deck.LineLayer({
        id: 'ocean-arrow',
        data: arrows,
        getSourcePosition: function (d) { return d.s; },
        getTargetPosition: function (d) { return d.t; },
        getColor: function (d) { return d.c; },
        getWidth: 1.2,
        widthUnits: 'pixels',
        widthMinPixels: 0.8
      })];
    }
    return [new deck.PathLayer({
      id: 'ocean-line',
      data: lines,
      getPath: function (d) { return d.path; },
      getColor: function (d) { return d.c; },
      getWidth: function (d) { return 1.1 + d.sp * 0.9; },
      widthUnits: 'pixels',
      widthMinPixels: 0.9,
      capRounded: true,
      jointRounded: true
    })];
  }

  var deckgl = new deck.Deck({
    canvas: 'cv',
    width: '100%',
    height: '100%',
    initialViewState: { longitude: 121.0, latitude: 34.4, zoom: 7.6, pitch: 0, bearing: 0 },
    controller: true,
    layers: buildLayers()
  });

  var buttons = document.querySelectorAll('[data-m]');
  for (i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener('click', function (e) {
      mode = e.currentTarget.getAttribute('data-m');
      for (var b = 0; b < buttons.length; b++) {
        buttons[b].classList.toggle('on', buttons[b] === e.currentTarget);
      }
      deckgl.setProps({ layers: buildLayers() });
    });
  }

  window.__deck = deckgl;
})();
</script>
</body>
</html>
Language: this page is in English; the demo keeps Chinese interface labels and code comments. The Chinese page carries the full notes and the same runnable file.

More business examples

deck.gl · APILineLayer deck.gl · APIPathLayer