Plot tide gauge data in Three.js
At 15:00 UTC on 1 January 2023, the San Francisco tide gauge recorded 1.969 metres. Seven hours later it read 0.201 metres. Drag the hour slider to follow the fall and read each observation on a Three.js chart.
Get the gauge readings
NOAA’s hourly_height product supplies verified hourly observations. Use predictions if you’re building a tide timetable. Weather can move the measured level away from the predicted tide, so label the series you chose. The example bundles one complete day from station 9414290 so its readings stay reproducible. Download the original JSON · NOAA data API
Set units=metric, time_zone=gmt and datum=MLLW in the request. MLLW stands for mean lower low water, a local reference elevation. A reading of zero puts the surface at that reference. You still need the bed elevation to calculate water depth.
Place each sample in time
The JSON response gives the timestamp in t and height in v. To load another day, set url to your NOAA request and change start to midnight UTC on its first date. Derive the horizontal coordinate from elapsed hours. If an observation is missing, later points will still appear at the right times.
// The request uses time_zone=gmt, so these timestamps are UTC.
const response = await fetch(url);
if (!response.ok) throw new Error('NOAA returned HTTP ' + response.status);
const report = await response.json();
if (!Array.isArray(report.data)) throw new Error(report.error?.message ?? 'Missing data');
const start = Date.UTC(2023, 0, 1);
const samples = report.data.flatMap(row => {
const time = Date.parse(row.t.replace(' ', 'T') + ':00Z');
const metres = parseFloat(row.v);
if (!Number.isFinite(time) || !Number.isFinite(metres)) return [];
return [{ hour: (time - start) / 3600000, metres }];
});
Before joining two samples, check their timestamps. An interval longer than an hour in this product leaves a gap in the line. In a filled chart, start a new shape after the gap so the fill doesn’t imply observations there. The 24 readings in the bundled example are exactly an hour apart.
Draw the tide chart
Trace the readings into a THREE.Shape with hours on X and metres on Y. This day stays above zero, so closing the shape at zero gives the face a straight base. If your series crosses below the datum, use a base below the lowest observation and keep a separate tick at zero. A little extrusion makes the curve visible from an angle. An orthographic camera keeps the scale even across the chart.
Full example source
Save this as index.html and serve it over HTTP. The only network dependency is Three.js r184.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="noindex,follow">
<title>San Francisco tide heights in Three.js</title>
<link rel="icon" href="data:,">
<style>
* { box-sizing: border-box; }
body { margin: 0; height: 100svh; min-height: 400px; display: grid; grid-template-rows: auto 1fr auto; background: #f4f4ef; color: #172826; font: 1rem system-ui, sans-serif; }
header, form { padding: clamp(1rem, 4vw, 3rem) 7vw; }
h1 { margin: 0; font-size: clamp(1.75rem, 4vw, 3.2rem); font-weight: 500; letter-spacing: -.045em; }
header p { margin: .35rem 0 0; }
main { min-height: 0; position: relative; }
canvas { display: block; width: 100%; height: 100%; }
form { display: grid; gap: .4rem; }
label { display: flex; justify-content: space-between; gap: 1rem; align-items: baseline; }
output { font-weight: 600; font-variant-numeric: tabular-nums; }
input { width: 100%; min-height: 44px; margin: 0; accent-color: #206b79; }
</style>
</head>
<body>
<header><h1>San Francisco tide heights</h1><p>1 January 2023 · Metres above MLLW</p></header>
<main><canvas aria-label="Hourly tide heights from 00 to 23 UTC">Use the hour slider to read the values.</canvas></main>
<form><label for="hour">Hour (UTC) <output id="reading" for="hour"></output></label>
<input id="hour" type="range" min="0" max="23" value="15" step="1">
</form>
<script type="module">
import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.184.0/build/three.module.js';
// NOAA station 9414290, hourly_height, metric, GMT, MLLW.
const metres = [0.890, 1.099, 1.277, 1.393, 1.379, 1.299, 1.095, 0.933,
0.798, 0.801, 0.955, 1.152, 1.440, 1.687, 1.882, 1.969,
1.924, 1.669, 1.286, 0.883, 0.535, 0.295, 0.201, 0.229];
const slider = document.querySelector('#hour');
const reading = document.querySelector('#reading');
const canvas = document.querySelector('canvas');
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf4f4ef);
const camera = new THREE.OrthographicCamera();
const surface = new THREE.Shape();
surface.moveTo(0, 0);
for (const [hour, height] of metres.entries()) surface.lineTo(hour, height);
surface.lineTo(23, 0);
surface.closePath();
const geometry = new THREE.ExtrudeGeometry(surface, { depth: 0.85, bevelEnabled: false });
const ribbon = new THREE.Mesh(geometry, [
new THREE.MeshStandardMaterial({ color: 0x247f89 }),
new THREE.MeshStandardMaterial({ color: 0x58aeb3 }),
]);
scene.add(ribbon);
const sun = new THREE.DirectionalLight(0xfff4de, 2.1);
sun.position.set(-3, 10, 7);
scene.add(sun, new THREE.HemisphereLight(0xffffff, 0xaac1bd, 1.8));
function line(points, color) {
const shape = new THREE.BufferGeometry().setFromPoints(points);
const mesh = new THREE.Line(shape, new THREE.LineBasicMaterial({ color }));
scene.add(mesh);
return mesh;
}
function label(text, x, y, z) {
const paper = document.createElement('canvas');
paper.width = 192;
paper.height = 80;
const pen = paper.getContext('2d');
pen.fillStyle = '#334743';
pen.font = '34px system-ui';
pen.textAlign = 'center';
pen.textBaseline = 'middle';
pen.fillText(text, 96, 40);
const texture = new THREE.CanvasTexture(paper);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({ map: texture, depthTest: false }));
sprite.position.set(x, y, z);
scene.add(sprite);
return sprite;
}
const axes = [];
for (let step = 0; step <= 4; step++) {
const grid = line([new THREE.Vector3(0, 0, -0.04), new THREE.Vector3(23, 0, -0.04)], 0xb7c9c4);
axes.push({ mesh: grid, caption: label((step / 2).toFixed(1), 0, 0, 0), height: step / 2 });
}
const hours = [0, 6, 12, 18, 23].map(hour =>
label(String(hour).padStart(2, '0'), hour, -0.65, 1));
const stem = line([new THREE.Vector3(), new THREE.Vector3(0, 1, 0)], 0xf5f5e4);
const dot = new THREE.Mesh(new THREE.SphereGeometry(0.15, 20, 12),
new THREE.MeshStandardMaterial({ color: 0xffbc55 }));
scene.add(dot);
function draw() {
const hour = Number(slider.value);
const height = metres[hour];
const width = canvas.clientWidth;
const heightPx = canvas.clientHeight;
// Match chart bounds to the canvas and keep the tick labels legible on phones.
const aspect = width / heightPx;
const halfWidth = Math.max(14.6, 4.8 * aspect);
const halfHeight = halfWidth / aspect;
const yScale = halfHeight * 0.68;
const worldPerPixel = 2 * halfWidth / width;
ribbon.scale.y = yScale;
camera.left = -halfWidth;
camera.right = halfWidth;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.position.set(17, yScale + 5, 32);
camera.lookAt(11.5, yScale, 0);
camera.updateProjectionMatrix();
for (const { mesh, caption, height } of axes) {
mesh.position.y = height * yScale;
caption.position.set(-1.35, mesh.position.y, 0);
}
for (const tick of hours) tick.position.y = -Math.max(0.65, 19 * worldPerPixel);
for (const text of [...hours, ...axes.map(axis => axis.caption)]) {
text.scale.set(76 * worldPerPixel, 32 * worldPerPixel, 1);
}
stem.position.set(hour, 0, 0.87);
stem.scale.y = height * yScale;
dot.position.set(hour, stem.scale.y, 0.87);
dot.scale.setScalar(Math.max(1, worldPerPixel * 22));
const value = String(hour).padStart(2, '0') + ':00 UTC · ' + height.toFixed(3) + ' m';
reading.value = value;
slider.setAttribute('aria-valuetext', value);
renderer.render(scene, camera);
}
function resize() {
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
draw();
}
slider.addEventListener('input', draw);
new ResizeObserver(resize).observe(canvas);
resize();
</script>
</body>
</html>
Use the level in a scene
Keep the chart’s vertical scale out of your waterfront model. If one scene unit is one metre and the model origin is 1.5 m above MLLW, the 15:00 reading puts the water surface at 1.969 - 1.5 = 0.469 on the Y axis. For a site surveyed in NAVD 88, request datum=NAVD if the station supports it, then subtract the model origin in NAVD 88. The same station reading can then drive a waterline beside a quay or a depth readout above a known bed elevation. San Francisco station datums · Set a waterfront waterline · Map the bed from depth samples