// HUD, control panel, keyboard bindings and "smart interlock" rules.
import { t, setLang, getLang, onLangChange, LOCALES } from './i18n.js?b=119';
import { DemoDirector } from './demo.js?b=119';
import { track, trackDebounced } from './analytics.js?b=119';
const $ = (id) => document.getElementById(id);
const DEG = Math.PI / 180;
const KMH = 1.852; // knots -> km/h
// keys that count as "taking the controls" (they end a running demo)
const GAME_KEYS = new Set([
'Digit1', 'Digit2', 'Digit3', 'KeyW', 'KeyS', 'KeyQ', 'KeyE',
'KeyD', 'KeyH', 'KeyT', 'KeyG', 'KeyR', 'ArrowLeft', 'ArrowRight',
]);
const MS = 1 / 1.94384; // knots -> m/s
function loadUnits() {
try { const u = localStorage.getItem('ws_units'); if (u === 'kn' || u === 'kmh') return u; } catch { /* ignore */ }
return 'kmh'; // board speed: km/h by default
}
function loadWindUnits() {
try { const u = localStorage.getItem('ws_windUnits'); if (u === 'kn' || u === 'kmh' || u === 'ms') return u; } catch { /* ignore */ }
return 'ms'; // wind speed: m/s by default (the windsurfing convention)
}
// Selectable sail sizes (m²): small = strong-wind/wave, big = light-wind.
const SAIL_SIZES = [4.0, 5.0, 6.5, 8.0, 9.5];
const SAIL_DEFAULT = 6.5; // beginner-intermediate all-round
function loadSailArea() {
try { const a = parseFloat(localStorage.getItem('ws_sailArea')); if (SAIL_SIZES.includes(a)) return a; } catch { /* ignore */ }
return SAIL_DEFAULT;
}
function loadCameraMode() {
try { const m = localStorage.getItem('ws_cameraMode'); if (m === 'free' || m === 'chase') return m; } catch { /* ignore */ }
return 'free'; // static, player-controlled by default
}
function loadShowCaptions() {
try { const v = localStorage.getItem('ws_demoCaptions'); if (v === 'on' || v === 'off') return v === 'on'; } catch { /* ignore */ }
// default: shown on desktop, hidden on phones (the sheet already crowds the screen)
return !matchMedia('(max-width: 768px)').matches;
}
function loadFoil() {
try { const v = localStorage.getItem('ws_foil'); if (v === 'on' || v === 'off') return v === 'on'; } catch { /* ignore */ }
return false; // default: classic board (foil is an opt-in extra)
}
export class UI {
constructor(sim, world = null) {
this.sim = sim;
this.world = world;
this.inputs = {
sheetDeg: 55,
rake: 0,
stance: 'mid',
lean: 25,
dagger: true,
harness: false,
autotrim: true, // beginner assist on by default (full autopilot)
sailArea: loadSailArea(), // m²; scales rig power (see sim.js)
foil: loadFoil(), // hydrofoil mode (opt-in extra; see settings)
};
this.units = loadUnits();
this.windUnits = loadWindUnits();
this.cameraMode = loadCameraMode();
this.showCaptions = loadShowCaptions(); // demo narration banner (settings-toggleable)
this.keysHeld = new Set();
this.flash = { msg: '', until: 0 };
this.paused = false; // pause button: freezes the whole sim (see main.js)
this._capText = null; // last demo caption text (restored when captions re-enabled)
// guided demo: an auto-tour director that steers while autotrim flies the rig
this.demo = new DemoDirector(sim, this);
this.demo.onCaption = (text) => this.#showCaption(text);
this.demo.onState = (mode) => this.#setDemoState(mode);
this.#bindPanel();
this.#bindSettings();
this.#bindPause();
this.#bindDemo();
this.#bindKeys();
this.#bindCrashHold();
this.compassCtx = $('compass').getContext('2d');
// keep the framing centred if the viewport changes (rotate / URL bar)
addEventListener('resize', () => { this.#applyFramingLift(); this.#sizeWindFader(); this.#sizeRadar(); this.#sizeWelcomeCard(); });
this.#sizeWelcomeCard();
// apply the desktop sidebar / mobile fader framing shift on first paint too
this.#applyFramingShiftX();
// size the radar to the HUD+meters column once layout settles
requestAnimationFrame(() => this.#sizeRadar());
// Static markup (incl. the windset readout) is re-templated on language
// change; re-sync the readouts that aren't refreshed every frame.
onLangChange(() => {
this.#refreshWindReadout();
this.#syncLangButtons();
this.demo.refresh(); // re-render the live caption in the new language
requestAnimationFrame(() => this.#sizeRadar()); // label lengths can change the row height
});
}
// Board-speed conversion + label follow the board unit choice (km/h | kn).
#conv(kn) { return this.units === 'kmh' ? kn * KMH : kn; }
#unitLabel() { return t(this.units === 'kmh' ? 'unit.kmh' : 'unit.kn'); }
// Wind-speed conversion + label follow the wind unit choice (m/s | km/h | kn).
#convWind(kn) { return this.windUnits === 'ms' ? kn * MS : this.windUnits === 'kmh' ? kn * KMH : kn; }
#windUnitLabel() { return t(this.windUnits === 'ms' ? 'unit.ms' : this.windUnits === 'kmh' ? 'unit.kmh' : 'unit.kn'); }
// m/s reads best with one decimal; km/h and knots as whole numbers.
#fmtWind(kn) { const v = this.#convWind(kn); return this.windUnits === 'ms' ? v.toFixed(1) : v.toFixed(0); }
#refreshWindReadout() {
// Read the actual base wind (m/s) so the label is exact even when it doesn't
// land on a whole knot (e.g. 10 km/h ≈ 5.4 kn). Number + unit are separate
// spans so the phone fader can stack them (value on top, unit below) and the
// unit never breaks mid-string ("м/с", not "м/" + "с").
const kn = this.sim.baseWind * 1.94384;
$('windset-val').innerHTML =
`${this.#fmtWind(kn)}${this.#windUnitLabel()}`;
}
#syncLangButtons() {
$('lang-select').value = getLang();
}
// ---------------- panel bindings ----------------
#bindPanel() {
$('sheet').addEventListener('input', (e) => {
this.#takeManualControl(); // grabbing the boom switches the assist off
this.inputs.sheetDeg = +e.target.value;
});
$('autotrim').addEventListener('change', (e) => {
// during a demo the autopilot stays on (the override handles temporary
// manual control); otherwise the checkbox is the real toggle
if (this.demo.running) { e.target.checked = true; this.inputs.autotrim = true; this.demo.nudge(); return; }
this.inputs.autotrim = e.target.checked;
track('assist', { on: e.target.checked });
});
$('autotrim').checked = this.inputs.autotrim; // reflect the default (on)
$('rake-seg').addEventListener('click', (e) => {
const b = e.target.closest('button'); if (!b) return;
this.setRake(parseFloat(b.dataset.rake));
track('rake', { rake: parseFloat(b.dataset.rake) });
});
$('stance-seg').addEventListener('click', (e) => {
const b = e.target.closest('button'); if (!b) return;
this.setStance(b.dataset.stance, true);
track('stance', { stance: b.dataset.stance });
});
$('lean').addEventListener('input', (e) => { this.#takeManualControl(); this.setLean(+e.target.value); });
// Robust drag for the two horizontal panel sliders: native
// dragging is flaky on mobile because the bottom sheet is touch-action:pan-y,
// so a grab that lands a hair off the thin input (or starts with a little
// vertical motion) gets stolen by the sheet's vertical pan. Driving them from
// captured pointer events instead means once you touch the slider the drag
// can't be hijacked. Fires 'input', so the listeners above run unchanged.
this.#makeSliderDraggable($('sheet'));
this.#makeSliderDraggable($('lean'));
$('dagger').addEventListener('change', (e) => { this.#takeManualControl(); this.inputs.dagger = e.target.checked; track('dagger', { on: e.target.checked }); });
$('harness').addEventListener('change', (e) => { this.setHarness(e.target.checked); track('harness', { on: this.inputs.harness }); });
$('btn-tack').addEventListener('click', () => this.tryTack());
$('btn-gybe').addEventListener('click', () => this.tryGybe());
$('btn-reset').addEventListener('click', () => { this.sim.reset(); this.resetInputs(); track('reset'); });
$('windset').addEventListener('input', (e) => {
this.sim.baseWind = +e.target.value; // slider is in m/s
this.#refreshWindReadout();
// one event on settle, not one per drag tick
trackDebounced('set_wind', { wind: Math.round(this.sim.baseWind) });
});
const closeWelcome = (how) => {
$('welcome-overlay').classList.add('off');
try { localStorage.setItem('ws_welcomeSeen', '1'); } catch (e) {} // don't show again next time
track('welcome_close', { how });
};
$('welcome-close').addEventListener('click', () => closeWelcome('button'));
// Tapping the dimmed backdrop (anywhere outside the card) also dismisses it.
$('welcome-overlay').addEventListener('click', (e) => {
if (e.target === $('welcome-overlay')) closeWelcome('backdrop');
});
// "How to play" (in settings) reopens the how-to on demand
$('info-toggle').addEventListener('click', () => {
$('welcome-overlay').classList.remove('off');
this.#sizeWelcomeCard();
$('settings-panel').classList.add('off');
$('settings-toggle').classList.remove('open');
track('how_to_play');
});
// "About" (in settings) — author credit popup
$('about-toggle').addEventListener('click', () => {
$('about-overlay').classList.remove('off');
$('settings-panel').classList.add('off');
$('settings-toggle').classList.remove('open');
track('about');
});
$('about-close').addEventListener('click', () => $('about-overlay').classList.add('off'));
// tap the dark backdrop (outside the card) closes it
$('about-overlay').addEventListener('click', (e) => {
if (e.target === $('about-overlay')) $('about-overlay').classList.add('off');
});
// Only the +/- button collapses/expands the sheet — tapping the header text
// (or anywhere else on it) must NOT toggle, so it can't be triggered by
// accident while reaching for a control.
$('panel').querySelector('.panel-header').addEventListener('click', (e) => {
if (!this.#isMobile()) return; // desktop panel doesn't collapse
if (!e.target.closest('#panel-toggle')) return;
this.setPanelCollapsed(!$('panel').classList.contains('collapsed'));
});
// Phone default: open the controls from the start (wind uses the shared
// 7 m/s default set in the sim).
if (this.#isMobile()) {
this.setPanelCollapsed(false);
}
// On phones the true-wind slider lives as a vertical fader on the sea's
// right edge (out of the sheet, so it fits + stays reachable when the sheet
// is collapsed). Desktop keeps it as the last row of the panel.
this.#placeWindFader();
this.#placeDemoTransport();
}
// Move #wind-ctl between the panel (desktop) and the open sea (phones). Keeps
// one input node — #windset's listeners stay wired wherever it lands — and
// re-homes it on rotate/resize via matchMedia.
#placeWindFader() {
const ctl = $('wind-ctl');
if (!ctl) return;
const mq = window.matchMedia('(max-width: 768px)');
const place = () => {
if (mq.matches) { $('app').appendChild(ctl); ctl.classList.add('wind-fader'); this.#sizeWindFader(); }
else { $('panel-body').appendChild(ctl); ctl.classList.remove('wind-fader'); ctl.style.bottom = ''; $('windset').style.height = ''; }
this.#applyFramingShiftX(); // recompute the horizontal framing for the new fader state
};
mq.addEventListener('change', place);
place();
}
// Make the phone wind fader sit flush above the control sheet: its top is
// fixed (below the demo transport row), its bottom tracks the sheet, and the
// vertical slider fills whatever height is left. Recomputed on resize + when
// the sheet opens/closes.
#sizeWindFader() {
const ctl = $('wind-ctl');
if (!ctl || !ctl.classList.contains('wind-fader')) return;
const panelTop = $('panel').getBoundingClientRect().top;
ctl.style.bottom = `${Math.round(innerHeight - panelTop + 6)}px`;
const label = ctl.querySelector('.ctl-label');
// clientHeight now reflects the top/bottom anchors; subtract padding (16),
// the flex gap (5) and the label so the slider fills the rest.
const avail = ctl.clientHeight - 16 - 5 - label.offsetHeight;
$('windset').style.height = `${Math.max(70, Math.round(avail))}px`;
}
// Home the pause (demo transport) button per viewport, re-homing on resize:
// · desktop → joins the top-right cluster, ahead of the DEMO/STOP button, so
// pause sits next to stop + settings (the HUD now lives down the left edge,
// so the cluster is free to hold it).
// · phones → its own holder, a row under the BALANCE meter, to keep the tight
// top-right corner uncluttered.
#placeDemoTransport() {
const pau = $('pause-toggle'), dt = $('demo-transport'), tb = $('top-buttons');
if (!pau || !dt || !tb) return;
const mq = window.matchMedia('(max-width: 768px)');
const place = () => { (mq.matches ? dt.appendChild(pau) : tb.prepend(pau)); };
mq.addEventListener('change', place);
place();
}
// ---------------- settings popover ----------------
#bindSettings() {
const panel = $('settings-panel');
const btn = $('settings-toggle');
const open = () => { panel.classList.remove('off'); btn.classList.add('open'); };
const close = () => { panel.classList.add('off'); btn.classList.remove('open'); };
btn.addEventListener('click', (e) => {
e.stopPropagation();
panel.classList.contains('off') ? open() : close();
});
$('settings-close').addEventListener('click', close);
// click the dim backdrop (outside the card) closes it — same as the About modal
panel.addEventListener('click', (e) => { if (e.target === panel) close(); });
$('units-seg').addEventListener('click', (e) => {
const b = e.target.closest('button'); if (!b) return;
this.setUnits(b.dataset.units);
track('set_units', { units: this.units });
});
$('windunits-seg').addEventListener('click', (e) => {
const b = e.target.closest('button'); if (!b) return;
this.setWindUnits(b.dataset.windunits);
track('set_wind_units', { units: this.windUnits });
});
$('sailsize-seg').addEventListener('click', (e) => {
const b = e.target.closest('button'); if (!b) return;
this.setSailArea(parseFloat(b.dataset.sailarea));
track('set_sail', { sail: this.inputs.sailArea });
});
$('camera-seg').addEventListener('click', (e) => {
const b = e.target.closest('button'); if (!b) return;
this.setCameraMode(b.dataset.camera);
track('set_camera', { mode: this.cameraMode });
});
$('democap-seg').addEventListener('click', (e) => {
const b = e.target.closest('button'); if (!b) return;
this.setShowCaptions(b.dataset.democap === 'on');
track('set_captions', { on: this.showCaptions });
});
$('foil-seg').addEventListener('click', (e) => {
const b = e.target.closest('button'); if (!b) return;
this.setFoil(b.dataset.foil === 'on');
track('set_foil', { on: this.inputs.foil });
});
const langSel = $('lang-select');
for (const { code, name } of LOCALES) {
const opt = document.createElement('option');
opt.value = code; opt.textContent = name;
langSel.appendChild(opt);
}
langSel.addEventListener('change', (e) => { setLang(e.target.value); track('change_language', { lang: e.target.value }); });
// initial state
this.setUnits(this.units);
this.setWindUnits(this.windUnits);
this.setSailArea(this.inputs.sailArea);
this.setCameraMode(this.cameraMode);
this.setShowCaptions(this.showCaptions);
// The "Kostia" easter-egg rig flies on a hydrofoil by default. Force it on
// for this session WITHOUT persisting, so it never leaks onto a normal visit.
if (this.world && this.world.kostia) this.setFoil(true, false);
else this.setFoil(this.inputs.foil);
this.#syncLangButtons();
this.#refreshWindReadout();
}
// Show/hide the demo narration banner. Off by default on phones; the player can
// flip it here. Persisted, reflected on the segment, and applied immediately.
setShowCaptions(on) {
this.showCaptions = !!on;
try { localStorage.setItem('ws_demoCaptions', this.showCaptions ? 'on' : 'off'); } catch { /* ignore */ }
const seg = $('democap-seg');
if (seg) for (const b of seg.children) b.classList.toggle('active', (b.dataset.democap === 'on') === this.showCaptions);
// apply now: hide a live caption, or bring the current one back
if (this.showCaptions && this.demo.running && this._capText) this.#showCaption(this._capText);
else if (!this.showCaptions) $('demo-caption').classList.add('off');
}
// Foil mode: swap the classic board for a hydrofoil. Persisted, reflected on
// the segment, fed to the sim via inputs.foil, and shown by the world (foil
// rig + hidden daggerboard). With a foil the daggerboard control is moot, so
// its row is hidden while foiling.
setFoil(on, persist = true) {
this.inputs.foil = !!on;
if (persist) {
try { localStorage.setItem('ws_foil', this.inputs.foil ? 'on' : 'off'); } catch { /* ignore */ }
}
const seg = $('foil-seg');
if (seg) for (const b of seg.children) b.classList.toggle('active', (b.dataset.foil === 'on') === this.inputs.foil);
if (this.world) this.world.setFoil(this.inputs.foil);
const dagRow = $('dagger-row');
if (dagRow) dagRow.style.display = this.inputs.foil ? 'none' : '';
}
setUnits(u) {
this.units = (u === 'kn') ? 'kn' : 'kmh';
try { localStorage.setItem('ws_units', this.units); } catch { /* ignore */ }
for (const b of $('units-seg').children) b.classList.toggle('active', b.dataset.units === this.units);
}
// Set the rig size (m²). Persisted, reflected on the segment, and fed to the
// sim via inputs.sailArea. Also used by the demo director per tour.
setSailArea(a) {
if (!SAIL_SIZES.includes(a)) a = SAIL_DEFAULT;
this.inputs.sailArea = a;
try { localStorage.setItem('ws_sailArea', String(a)); } catch { /* ignore */ }
const seg = $('sailsize-seg');
if (seg) for (const b of seg.children) b.classList.toggle('active', parseFloat(b.dataset.sailarea) === a);
}
// 'free' (default): the camera holds a fixed world angle and the player owns
// it via orbit. 'chase': the camera swings to stay behind the board.
setCameraMode(m) {
this.cameraMode = (m === 'chase') ? 'chase' : 'free';
try { localStorage.setItem('ws_cameraMode', this.cameraMode); } catch { /* ignore */ }
const seg = $('camera-seg');
if (seg) for (const b of seg.children) b.classList.toggle('active', b.dataset.camera === this.cameraMode);
if (this.world) this.world.setCameraMode(this.cameraMode);
}
setWindUnits(u) {
this.windUnits = (u === 'kn' || u === 'kmh') ? u : 'ms';
try { localStorage.setItem('ws_windUnits', this.windUnits); } catch { /* ignore */ }
for (const b of $('windunits-seg').children) b.classList.toggle('active', b.dataset.windunits === this.windUnits);
this.#refreshWindReadout();
}
// ---------------- pause (demo-only transport) ----------------
#bindPause() {
const pz = $('pause-toggle');
if (pz) pz.addEventListener('click', (e) => { e.stopPropagation(); this.setPaused(!this.paused); track('pause', { on: this.paused }); });
}
// Freeze / resume the whole simulation (main.js skips its update while paused).
// The button toggles between the ⏸ pause and ▶ play glyphs.
setPaused(on) {
this.paused = !!on;
const btn = $('pause-toggle');
if (btn) {
btn.classList.toggle('paused', this.paused);
const path = btn.querySelector('svg path');
if (path) path.setAttribute('d', this.paused
? 'M8 5v14l11-7z' // ▶ resume
: 'M6 5h4v14H6zM14 5h4v14h-4z'); // ⏸ pause
btn.title = this.paused ? t('resume.title') : t('pause.title');
}
// (no toast — the amber pill on the pause button is feedback enough)
}
// ---------------- guided demo ----------------
#bindDemo() {
const panel = $('demo-panel');
const btn = $('demo-toggle');
const open = () => {
panel.classList.remove('off'); btn.classList.add('open');
$('settings-panel').classList.add('off'); $('settings-toggle').classList.remove('open');
};
const close = () => { panel.classList.add('off'); btn.classList.remove('open'); };
this._closeDemoPanel = close;
btn.addEventListener('click', (e) => {
e.stopPropagation();
// once a tour is running the button IS the stop button — one tap ends it
if (this.demo.running) { this.demo.stop(); return; }
panel.classList.contains('off') ? open() : close();
});
$('demo-close').addEventListener('click', close);
// tap outside the popover closes it
addEventListener('click', (e) => {
if (!panel.classList.contains('off') && !panel.contains(e.target) && !btn.contains(e.target)) close();
});
for (const b of panel.querySelectorAll('.demo-mode')) {
b.addEventListener('click', () => { this.demo.start(b.dataset.mode); close(); });
}
// Touching a control during a demo does NOT stop it — it briefly hands the
// helm to the user (they steer off course, then the tour re-corrects). The
// wind slider and the header (collapse) are exempt: the wind is meant to be
// retuned freely and stays put, and collapsing isn't a control input.
$('panel').addEventListener('pointerdown', (e) => {
if (!this.demo.running) return;
if (e.target.closest('#windset') || e.target.closest('.panel-header')) return;
this.demo.nudge();
}, true);
}
// Drive a HORIZONTAL range input from captured pointer events so its drag can't
// be stolen by the mobile sheet's vertical pan. Maps the pointer's X straight to
// the value (respecting min/max/step) and fires a native 'input' event, so all
// existing listeners fire exactly as they do for a keyboard/native change. Only
// for horizontal sliders — the vertical wind fader keeps its native handling.
#makeSliderDraggable(el) {
if (!el) return;
const setFromX = (clientX) => {
const r = el.getBoundingClientRect();
if (r.width <= 0) return;
const min = +el.min || 0, max = +el.max || 100, step = +el.step || 1;
const frac = Math.max(0, Math.min(1, (clientX - r.left) / r.width));
let v = min + frac * (max - min);
v = Math.round(v / step) * step;
v = Math.max(min, Math.min(max, v));
if (+el.value !== v) {
el.value = v;
el.dispatchEvent(new Event('input', { bubbles: true }));
}
};
el.addEventListener('pointerdown', (e) => {
if (e.button > 0) return; // ignore right/middle mouse
e.preventDefault(); // claim the touch: no scroll, no native double-drag
try { el.setPointerCapture(e.pointerId); } catch { /* older browsers */ }
el.focus?.();
setFromX(e.clientX);
const move = (ev) => setFromX(ev.clientX);
const end = () => {
el.removeEventListener('pointermove', move);
el.removeEventListener('pointerup', end);
el.removeEventListener('pointercancel', end);
try { el.releasePointerCapture(e.pointerId); } catch { /* already released */ }
};
el.addEventListener('pointermove', move);
el.addEventListener('pointerup', end);
el.addEventListener('pointercancel', end);
});
}
// Reflect the demo's live state on the button and popover. While running, the
// ▶ DEMO pill turns into a ■ STOP button (icon + label + red styling) so one
// tap ends the tour — no reopening the popover.
#setDemoState(mode) {
// single choke point for demo lifecycle: fire start on entry, stop on exit
if (mode && mode !== this._demoMode) track('demo_start', { mode });
else if (!mode && this._demoMode) track('demo_stop', { mode: this._demoMode });
this._demoMode = mode || null;
const btn = $('demo-toggle');
btn.classList.toggle('running', !!mode);
// the pause button lives only inside a demo; leaving a demo also lifts any
// pause (its only un-pause control would otherwise vanish).
$('app').classList.toggle('demo-running', !!mode); // reveals the pause button wherever it's homed
if (!mode && this.paused) this.setPaused(false);
const path = btn.querySelector('svg path');
const label = btn.querySelector('.demo-btn-label');
if (mode) {
path.setAttribute('d', 'M6 6h12v12H6z'); // ■ stop
label.textContent = t('demo.stopbtn');
btn.title = 'stop demo / зупинити демо';
} else {
path.setAttribute('d', 'M8 5v14l11-7z'); // ▶ play
label.textContent = t('demo.btn');
btn.title = 'guided demo / демо-тур';
}
for (const b of $('demo-panel').querySelectorAll('.demo-mode')) {
b.classList.toggle('active', b.dataset.mode === mode);
}
}
// The demo narration reuses the SAME slot as every other transient message —
// low-centre on desktop, full-width just above the control sheet on phones.
#showCaption(text) {
this._capText = text; // remembered so re-enabling captions can restore it
const el = $('demo-caption');
if (text && this.showCaptions) {
$('demo-cap').textContent = text;
this.#positionAbovePanel(el);
el.classList.remove('off');
} else {
el.classList.add('off'); // no caption, or captions turned off
}
}
// Park a bottom overlay just above the (open) control sheet on phones; on
// desktop fall back to its CSS position. Shared by the toast and the caption.
#positionAbovePanel(el) {
if (this.#isMobile()) {
const panelTop = $('panel').getBoundingClientRect().top;
el.style.bottom = `${Math.round(innerHeight - panelTop + 4)}px`;
} else {
el.style.bottom = '';
}
}
// Forward the demo's route-preview points to the 3D world (or null to hide).
showRoutePreview(points) { if (this.world) this.world.setRoutePreview(points); }
// Turn the beginner autopilot on/off (used by the demo director).
setAutotrim(on) {
this.inputs.autotrim = !!on;
$('autotrim').checked = this.inputs.autotrim;
}
// Keep the wind slider + readout in step with a scripted wind change.
syncWindControl() {
$('windset').value = Math.round(this.sim.baseWind * 2) / 2; // m/s, snapped to the 0.5 step
this.#refreshWindReadout();
}
#isMobile() { return window.matchMedia('(max-width: 768px)').matches; }
// Single source of truth for the control sheet's open/closed state. Also lifts
// the 3D framing on mobile so the rider stays centred in the open state.
setPanelCollapsed(collapsed) {
const p = $('panel');
p.classList.toggle('collapsed', collapsed);
$('panel-toggle').textContent = collapsed ? '+' : '–';
this.#applyFramingLift();
this.#sizeWindFader(); // the fader tracks the sheet's top edge
// keep the demo caption sitting just above the sheet as it resizes
const cap = $('demo-caption');
if (!cap.classList.contains('off')) this.#positionAbovePanel(cap);
}
// Cap the how-to card to the *reliably measured* visible height, so its inner
// .w-scroll region (not the whole card) scrolls and the SAIL AWAY footer stays
// pinned to the card's bottom. Uses window.innerHeight, not 100dvh — some in-app
// WebViews over-measure dvh and would push the button off-screen. 28 = the
// overlay's 14px top+bottom padding.
#sizeWelcomeCard() {
const card = $('welcome-card');
if (card) card.style.maxHeight = `${Math.max(0, innerHeight - 28)}px`;
}
// Centre the rider in the clear band between the top overlays (HUD + meters)
// and the open control sheet: shift up by half the difference of the two
// insets, so it isn't pushed too high (which ignoring the top overlay did).
#applyFramingLift() {
if (!this.world) return;
this.#applyFramingShiftX();
const collapsed = $('panel').classList.contains('collapsed');
if (!this.#isMobile() || collapsed) { this.world.setFramingLift(0); return; }
const H = innerHeight;
const bottomInset = Math.max(0, H - $('panel').getBoundingClientRect().top);
const topInset = $('meters').getBoundingClientRect().bottom; // lowest top overlay edge
// Nudge the rider a touch DOWN so it sits nearer the vertical centre of the
// clear band, not high in it (mobile only).
this.world.setFramingLift((bottomInset - topInset) / H - 0.05);
}
// Nudge the rider LEFT so it's centred in the water beside the wind fader
// (mobile), instead of behind it. The shift = the fader-occupied width as a
// fraction of the viewport; on desktop (no fader) there's no shift.
#applyFramingShiftX() {
if (!this.world) return;
// The rider is nudged LEFT so it sits centred in the sea beside whatever
// occupies the right edge: on mobile the wind fader, on desktop the whole
// right sidebar. shift = occluded-right width / viewport width.
let rightEl = null;
if (this.#isMobile()) rightEl = document.querySelector('.wind-fader');
else rightEl = document.getElementById('sidebar');
if (!rightEl) { this.world.setFramingShiftX(0); return; }
const rightInset = innerWidth - rightEl.getBoundingClientRect().left;
// On mobile, ease the leftward shift back a touch so the rider sits nearer
// the centre of the sea (it was pushed a shade too far left before).
const easeRight = this.#isMobile() ? 0.05 : 0;
this.world.setFramingShiftX(rightInset / innerWidth - easeRight);
}
// Desktop: size the radar to an exact square matching the height of the HUD +
// meters column beside it, so it never sticks out below the meters (which
// would open a gap before the control panel). Measured from the two rows'
// own geometry so an oversized radar can't inflate what we read. Mobile keeps
// its fixed CSS size (inline styles cleared).
#sizeRadar() {
const c = $('compass');
if (!c) return;
if (this.#isMobile()) { c.style.width = ''; c.style.height = ''; return; }
const top = $('hud-top')?.getBoundingClientRect();
const met = $('meters')?.getBoundingClientRect();
if (!top || !met) return;
const contentH = met.bottom - top.top; // HUD row + gap + meters row
const size = Math.max(40, Math.round(contentH - 8)); // minus .compass-box padding (4px×2)
c.style.width = `${size}px`;
c.style.height = `${size}px`;
}
// Press-and-hold the crash popup to read it: holding freezes the recovery
// countdown; releasing recovers immediately and starts a fresh run.
#bindCrashHold() {
const ov = $('crash-overlay');
const hold = (e) => {
if (!this.sim.crashed) return;
this.sim.recoverHold = true;
e.preventDefault();
};
const release = () => {
if (!this.sim.recoverHold) return;
this.sim.recoverHold = false;
if (this.sim.crashed) this.sim.recover(); // release -> back on the board
};
ov.addEventListener('pointerdown', hold);
ov.addEventListener('pointerup', release);
ov.addEventListener('pointercancel', release);
// iOS starts a text selection / magnifier on long-press; suppress its default
ov.addEventListener('touchstart', (e) => { if (this.sim.crashed) e.preventDefault(); }, { passive: false });
ov.addEventListener('contextmenu', (e) => e.preventDefault());
}
// -------- interlocked setters (the "smart logic") --------
setRake(v) {
this.inputs.rake = v;
for (const b of $('rake-seg').children) {
b.classList.toggle('active', parseFloat(b.dataset.rake) === v);
}
}
setStance(s, userInitiated = false) {
const st = this.sim.lastState;
// interlock: can't run to the back straps with no speed — you'd sink the tail instantly
if (s === 'back' && st && st.v < 1.5 && !st.planing) {
this.flashMsg(t('flash.noBackStraps'));
this.#markBlocked('stance-seg', s);
return;
}
if (userInitiated) this.#takeManualControl(); // else the autopilot overrides it right back
this.inputs.stance = s;
for (const b of $('stance-seg').children) b.classList.toggle('active', b.dataset.stance === s);
// interlock: moving to the very front unhooks the harness (the lines can't reach)
if (s === 'front' && this.inputs.harness) {
this.setHarness(false);
this.flashMsg(t('flash.unhookFront'));
}
}
setLean(v) {
this.inputs.lean = Math.max(0, Math.min(100, Math.round(v)));
$('lean').value = this.inputs.lean;
// interlock: harness needs constant tension — dropping lean below 20 unhooks you
if (this.inputs.harness && this.inputs.lean < 20) {
this.setHarness(false);
this.flashMsg(t('flash.unhookUpright'));
}
}
setHarness(on) {
if (on) {
const st = this.sim.lastState;
// interlock: you can only hook in when the sail is actually pulling
if (!st || st.power01 < 0.25) {
this.flashMsg(t('flash.noPull'));
$('harness').checked = false;
return;
}
if (this.inputs.stance === 'front') {
this.flashMsg(t('flash.hookFromMast'));
$('harness').checked = false;
return;
}
if (this.inputs.lean < 30) this.setLean(40); // hooking in commits your weight
}
this.inputs.harness = on;
$('harness').checked = on;
}
tryTack() {
if (this.sim.maneuver) return;
// The sim now auto-steers to the entry angle, so we only block when it is
// truly impossible: no way on, or pointing downwind (gybe territory).
const res = this.sim.startTack();
if (!res.ok) {
if (res.reason === 'downwind') this.flashMsg(t('flash.tooDownwind'));
else if (res.reason === 'slow') this.flashMsg(t('flash.tackNeedSpeed'));
track('tack', { result: res.reason || 'blocked' });
return;
}
track('tack', { result: 'ok' });
// maneuver housekeeping: sheet out, unhook, step to neutral
if (this.inputs.harness) this.setHarness(false);
this.setStance('mid');
this.setLean(15);
this.setRake(0);
}
tryGybe() {
if (this.sim.maneuver) return;
const res = this.sim.startGybe();
if (!res.ok) {
if (res.reason === 'upwind') this.flashMsg(t('flash.tooUpwind'));
else if (res.reason === 'slow') this.flashMsg(t('flash.gybeNeedSpeed'));
track('gybe', { result: res.reason || 'blocked' });
return;
}
track('gybe', { result: 'ok' });
if (this.inputs.harness) this.setHarness(false);
this.setStance('mid');
this.setLean(15);
this.setRake(0);
}
resetInputs() {
this.setRake(0);
this.setStance('mid');
this.setLean(25);
this.inputs.sheetDeg = 55; $('sheet').value = 55;
this.setHarness(false);
}
#markBlocked(segId, key) {
for (const b of $(segId).children) {
if (b.dataset.stance === key || b.dataset.rake === key) {
b.classList.add('blocked');
setTimeout(() => b.classList.remove('blocked'), 900);
}
}
}
flashMsg(msg) { this.flash = { msg, until: performance.now() + 3300 }; this.#showToast(msg); }
// Hand control back to the player: any manual input on a control the autopilot
// manages switches the assist off (so the tap actually sticks) and says so.
// During a demo this instead opens a brief manual-override window — the tour
// keeps running and re-corrects afterwards, so we never kill the autopilot.
#takeManualControl() {
if (this.demo.running) { this.demo.nudge(); return; }
if (!this.inputs.autotrim) return;
this.inputs.autotrim = false;
$('autotrim').checked = false;
this.flashMsg(t('flash.manual'));
// grabbing a control is the main way people leave the beginner autopilot —
// count it too, or `assist {on:false}` only ever sees the explicit checkbox.
track('assist', { on: false });
}
// Transient toast explaining why an action was blocked (or a state change).
// On phones it floats just above the control sheet so it isn't hidden behind
// it; on desktop it sits low-centre. Fades after a moment or on the next tap.
#showToast(msg, dur = 3300) {
const el = $('toast');
el.textContent = msg;
this.#positionAbovePanel(el);
el.classList.add('on');
clearTimeout(this._toastTimer);
const hide = () => { el.classList.remove('on'); clearTimeout(this._toastTimer); };
this._toastTimer = setTimeout(hide, dur);
// dismiss on the next interaction — deferred so the triggering tap doesn't count
setTimeout(() => addEventListener('pointerdown', hide, { once: true }), 0);
}
// ---------------- keyboard ----------------
#bindKeys() {
addEventListener('keydown', (e) => {
if (e.code.startsWith('Arrow') || e.code === 'Space') e.preventDefault();
if (e.repeat) return;
this.keysHeld.add(e.code);
// during a demo a gameplay key opens a brief override instead of stopping
if (this.demo.running && GAME_KEYS.has(e.code)) this.demo.nudge();
switch (e.code) {
case 'Digit1': this.setStance('front', true); track('stance', { stance: 'front' }); break;
case 'Digit2': this.setStance('mid', true); track('stance', { stance: 'mid' }); break;
case 'Digit3': this.setStance('back', true); track('stance', { stance: 'back' }); break;
case 'KeyD': this.#takeManualControl(); $('dagger').checked = !$('dagger').checked; this.inputs.dagger = $('dagger').checked; track('dagger', { on: this.inputs.dagger }); break;
case 'KeyH': this.setHarness(!this.inputs.harness); track('harness', { on: this.inputs.harness }); break;
case 'KeyT': this.tryTack(); break;
case 'KeyG': this.tryGybe(); break;
case 'KeyR': this.sim.reset(); this.resetInputs(); track('reset'); break;
}
});
addEventListener('keyup', (e) => {
this.keysHeld.delete(e.code);
// arrow steering is momentary: snap rake back to neutral on release
if ((e.code === 'ArrowLeft' || e.code === 'ArrowRight')
&& !this.keysHeld.has('ArrowLeft') && !this.keysHeld.has('ArrowRight')) {
this.setRake(0);
}
});
}
/** continuous key handling + auto-trim; call every frame before sim.update */
tickInputs(dt, lastState) {
// guided demo drives the steering + wind while autotrim flies the rig
if (this.demo.running) this.demo.tick(dt, lastState);
const held = this.keysHeld;
// touching the sheet/lean keys takes over from the autopilot (arrows just steer)
if (this.inputs.autotrim
&& (held.has('KeyW') || held.has('KeyS') || held.has('KeyQ') || held.has('KeyE'))) {
this.#takeManualControl();
}
// during a demo, keep the override window alive while a steering/rig key is held
if (this.demo.running
&& (held.has('ArrowLeft') || held.has('ArrowRight') || held.has('KeyW')
|| held.has('KeyS') || held.has('KeyQ') || held.has('KeyE'))) {
this.demo.nudge();
}
if (held.has('ArrowLeft')) this.setRake(1); // forward → downwind
if (held.has('ArrowRight')) this.setRake(-1); // back → upwind
if (held.has('KeyW')) { this.inputs.sheetDeg = Math.max(0, this.inputs.sheetDeg - 35 * dt); $('sheet').value = this.inputs.sheetDeg; }
if (held.has('KeyS')) { this.inputs.sheetDeg = Math.min(90, this.inputs.sheetDeg + 35 * dt); $('sheet').value = this.inputs.sheetDeg; }
if (held.has('KeyQ')) this.setLean(this.inputs.lean - 55 * dt);
if (held.has('KeyE')) this.setLean(this.inputs.lean + 55 * dt);
if (this.inputs.autotrim && lastState && !lastState.crashed) {
// Full beginner autopilot: keep the rig, the rider's weight, the
// daggerboard and the stance all managed so a passive sailor cruises (and
// even planes) without ever being flung off. You just steer with the
// arrows; turn it off to practise real balance yourself. (During a demo
// override the autopilot stays on — only the director's steering pauses —
// so a stray manual input can steer off course but never capsizes.)
const clamp01 = (v, a, b) => Math.max(a, Math.min(b, v));
// 1) sheet -> the optimal boom angle for clean, maximum drive.
const target = lastState.sheetOpt ?? 45;
this.inputs.sheetDeg += (target - this.inputs.sheetDeg) * Math.min(dt * 3, 1);
$('sheet').value = this.inputs.sheetDeg;
// 2) lean -> hang exactly as hard as the sail pulls, a touch over, to sit
// in the safe band between catapult (sail wins) and back-fall (you win).
const boost = this.inputs.harness ? 1.38 : 1.0;
const wantLean = clamp01(((lastState.required01 ?? 0) + 0.12) / boost * 100, 0, 100);
this.inputs.lean += (wantLean - this.inputs.lean) * Math.min(dt * 5, 1);
this.inputs.lean = clamp01(this.inputs.lean, 0, 100);
$('lean').value = Math.round(this.inputs.lean);
// On a foil there's no daggerboard and you're flying, so treat "flying"
// like "planing" for the stance/board choices below.
const fast = lastState.planing || lastState.foiling;
// 3) daggerboard -> down for grip while displacing, retracted the instant
// you plane (a lowered board rails over and spins out at speed).
this.inputs.dagger = !fast;
$('dagger').checked = this.inputs.dagger;
// 4) stance -> into the back straps once planing/flying (less drag, no
// nose-dive), neutral otherwise.
this.inputs.stance = fast ? 'back' : 'mid';
for (const b of $('stance-seg').children) {
b.classList.toggle('active', b.dataset.stance === this.inputs.stance);
}
}
return this.inputs;
}
// ---------------- HUD ----------------
updateHUD(st) {
const unit = this.#unitLabel();
$('speed-val').textContent = this.#conv(st.speedKn).toFixed(1);
$('speed-unit').textContent = unit;
const skimming = st.planing || st.foiling;
const badge = $('planing-badge');
if (badge) {
badge.classList.toggle('on', skimming);
badge.textContent = st.foiling ? t('meter.foiling') : t('meter.planing');
}
$('pos-val').textContent = st.maneuver
? (st.maneuver.type === 'tack' ? t('man.tacking') : t('man.gybing'))
: t(st.pointOfSailKey);
$('tack-val').textContent = t(st.tackKey);
const windUnit = this.#windUnitLabel();
$('wind-val').textContent = this.#fmtWind(st.windKn);
$('wind-unit').textContent = windUnit;
// Always show the gust line (min +0) so the WIND box keeps a constant height
// and never jumps as gusts come and go.
$('gust-val').textContent = t('hud.gust', { n: this.#fmtWind(Math.max(0, st.gustKn)), unit: windUnit });
// sheet readout + optimal marker
$('sheet-val').textContent = `${Math.round(st.inputs.sheetDeg)}°`;
$('sheet-optimal').style.left = `${(st.sheetOpt / 90) * 100}%`;
$('lean-val').textContent = `${Math.round(st.inputs.lean)}%`;
// lean optimal marker: the balanced lean the autopilot targets, so with the
// assist on the slider sits right on the green line (harness lets you hang
// harder for the same hold, so the target drops).
const leanBoost = st.inputs.harness ? 1.38 : 1.0;
$('lean-optimal').style.left =
`${Math.max(0, Math.min(100, ((st.required01 || 0) + 0.12) / leanBoost * 100))}%`;
// power meter
$('power-fill').style.width = `${Math.min(100, st.power01 * 100)}%`;
const trimEl = $('trim-state');
// planing takes over the meter-title's right slot (see #planing-badge); hide
// the trim word while it shows so the two don't stack.
trimEl.textContent = skimming ? '' : ({ luff: t('trim.luff'), good: t('trim.good'), stall: t('trim.stall') }[st.trim] || '');
trimEl.className = skimming ? '' : st.trim;
// balance meter: band = required lean (with tolerance), diamond = effective
// lean. Both are clamped to the track, so a very high required lean (strong
// wind) pegs the band at the right edge instead of spilling out of the meter.
const req = Math.min(1.25, st.required01 || 0);
const band = $('required-band');
const bandW = (0.6 / 1.3) * 100; // fixed tolerance width
const bandLeft = Math.max(0, (req - 0.3) / 1.3) * 100;
band.style.left = `${Math.min(bandLeft, 100 - bandW)}%`; // keep the band inside the track
band.style.width = `${bandW}%`;
$('lean-marker').style.left = `${Math.min(1, Math.max(0, (st.eff01 || 0) / 1.3)) * 100}%`;
$('lean-marker').style.background = st.danger > 0.4 ? '#ff7043' : '#4fc3f7';
// hint bar: sim coaching warnings (flash/interlock messages go to the toast).
// Silenced during a demo — the narration caption owns that bottom slot.
const hintEl = $('hint-text');
if (st.warnings.length && !this.demo.running) {
hintEl.textContent = t(st.warnings[0]);
hintEl.classList.toggle('warn', st.danger > 0.25);
} else {
hintEl.textContent = '';
hintEl.classList.remove('warn');
}
// crash overlay
const ov = $('crash-overlay');
ov.classList.toggle('on', st.crashed);
if (st.crashed) {
$('crash-reason').textContent = t(st.crashReason);
$('crash-lesson').textContent = t(st.crashLesson);
// hold to keep reading; release to sail again (see #bindCrashHold)
$('crash-timer').textContent = this.sim.recoverHold
? t('crash.release')
: `${t('crash.timer', { n: Math.max(0, st.crashTimer).toFixed(0) })} · ${t('crash.hold')}`;
}
// action buttons enabled state
$('btn-tack').disabled = !!st.maneuver || st.crashed;
$('btn-gybe').disabled = !!st.maneuver || st.crashed;
this.#drawCompass(st);
}
#drawCompass(st) {
const ctx = this.compassCtx;
const S = 120, c = S / 2, R = 48;
ctx.clearRect(0, 0, S, S);
// ---- land / coast: a wavy shoreline low in the radar square ----
// The shore lies downwind — in this world-oriented radar that's the bottom.
// Draw it as a slim WAVY band (a real coast isn't a straight edge) kept in
// the lower strip so it never climbs onto the ring. It rides up a touch as
// the coast nears and sinks to the bottom edge as it recedes. Drawn first so
// the ring, wind arrow and board silhouette all overlay it.
const cd = this.world && this.world.coastDist;
if (cd) {
const f = Math.min(Math.max(
(cd - this.world.coastMin) / (this.world.coastMax - this.world.coastMin), 0), 1); // 0 near .. 1 far
const yBase = 110 + 8 * f; // near..far, always below the ring
const wave = (x) => yBase - 1.2 * Math.sin(x * 0.13 + 0.6) - 0.6 * Math.sin(x * 0.31 + 2.0);
const steps = 24;
ctx.beginPath();
ctx.moveTo(0, wave(0));
for (let i = 1; i <= steps; i++) ctx.lineTo((S * i) / steps, wave((S * i) / steps));
ctx.lineTo(S, S); ctx.lineTo(0, S); ctx.closePath();
const g = ctx.createLinearGradient(0, yBase - 2, 0, S);
g.addColorStop(0, 'rgba(150,124,92,0.55)'); // sandy brown shore
g.addColorStop(1, 'rgba(120,100,78,0.30)');
ctx.fillStyle = g; ctx.fill();
ctx.beginPath();
ctx.moveTo(0, wave(0));
for (let i = 1; i <= steps; i++) ctx.lineTo((S * i) / steps, wave((S * i) / steps));
ctx.strokeStyle = 'rgba(196,168,128,0.95)'; ctx.lineWidth = 1.6; ctx.stroke();
ctx.fillStyle = 'rgba(210,186,150,0.95)'; ctx.font = '9px monospace';
ctx.textAlign = 'center'; ctx.fillText('LAND', c, yBase - 4); ctx.textAlign = 'start';
}
// ring
ctx.strokeStyle = 'rgba(143,184,212,0.5)';
ctx.lineWidth = 1.5;
ctx.beginPath(); ctx.arc(c, c, R, 0, Math.PI * 2); ctx.stroke();
// screen mapping: world angle a (0 = +Z = up on compass, increasing toward +X = left)
const pt = (a, r) => [c - Math.sin(a) * r, c - Math.cos(a) * r];
// ---- camera / point-of-view cone ----
// Where you're looking from. The camera orbits the board (controls.target);
// its world azimuth is the XZ direction from the target out to the camera,
// which maps straight onto the radar's world angle (a = atan2(x, z)). We draw
// a translucent view cone fanning from the eye on the ring in toward the
// board, so it swings around the radar exactly as you orbit the view. Drawn
// before the board/rig so the silhouette stays crisp on top.
const cam = this.world && this.world.camera;
const tgt = this.world && this.world.controls && this.world.controls.target;
if (cam && tgt) {
const dx = cam.position.x - tgt.x, dz = cam.position.z - tgt.z;
if (dx * dx + dz * dz > 1e-4) {
const ca = Math.atan2(dx, dz); // camera azimuth, radar convention
const [ex, ey] = pt(ca, R + 6); // eye, just outside the ring
const half = 21 * DEG; // symbolic field-of-view spread
const [lx, ly] = pt(ca - half, R * 0.16); // cone base, near the board
const [rx, ry] = pt(ca + half, R * 0.16);
const g = ctx.createRadialGradient(ex, ey, 2, ex, ey, R + 6);
g.addColorStop(0, 'rgba(129,212,250,0.42)');
g.addColorStop(1, 'rgba(129,212,250,0.04)');
ctx.beginPath();
ctx.moveTo(ex, ey); ctx.lineTo(lx, ly); ctx.lineTo(rx, ry); ctx.closePath();
ctx.fillStyle = g; ctx.fill();
// little camera glyph at the eye, a lens dot facing the board
ctx.save();
ctx.translate(ex, ey);
ctx.rotate(Math.atan2(c - ex, ey - c)); // aim the body's short axis at centre
ctx.fillStyle = 'rgba(11,26,38,0.85)';
ctx.strokeStyle = '#81d4fa'; ctx.lineWidth = 1.2;
ctx.beginPath(); ctx.roundRect(-4, -3, 8, 6, 1.4); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#81d4fa';
ctx.beginPath(); ctx.arc(0, 0, 1.7, 0, Math.PI * 2); ctx.fill(); // lens
ctx.restore();
}
}
// no-go zone wedge around wind-from
const wa = st.windFromAngle;
ctx.fillStyle = 'rgba(255,112,67,0.18)';
ctx.beginPath();
ctx.moveTo(c, c);
const steps = 10;
for (let i = 0; i <= steps; i++) {
const a = wa - 45 * DEG + (90 * DEG * i) / steps;
const [x, y] = pt(a, R);
ctx.lineTo(x, y);
}
ctx.closePath(); ctx.fill();
// wind arrow (points DOWNWIND, from the edge toward centre)
const [wx, wy] = pt(wa, R + 8);
const [wx2, wy2] = pt(wa, R - 14);
ctx.strokeStyle = '#ffd54f'; ctx.fillStyle = '#ffd54f'; ctx.lineWidth = 2.5;
ctx.beginPath(); ctx.moveTo(wx, wy); ctx.lineTo(wx2, wy2); ctx.stroke();
const ah = Math.atan2(wx2 - wx, wy2 - wy);
ctx.beginPath();
ctx.moveTo(wx2, wy2);
ctx.lineTo(wx2 - Math.sin(ah - 0.4) * 8, wy2 - Math.cos(ah - 0.4) * 8);
ctx.lineTo(wx2 - Math.sin(ah + 0.4) * 8, wy2 - Math.cos(ah + 0.4) * 8);
ctx.closePath(); ctx.fill();
ctx.font = '9px monospace';
ctx.fillText('WIND', wx - 12 + (wx < c ? -14 : 6), wy + (wy < c ? -4 : 10));
// ---- board (top-down windsurfer silhouette) ----
const h = st.heading;
const side = st.beta >= 0 ? 1 : -1;
const sheet = (st.inputs.sheetDeg || 45) * DEG;
// Drawn in a frame rotated to the heading: nose points to -y (up), tail +y.
ctx.save();
ctx.translate(c, c);
ctx.rotate(-h);
ctx.beginPath();
ctx.moveTo(0, -26); // pointed nose
ctx.bezierCurveTo(6, -20, 8, -2, 6.5, 10); // right rail
ctx.quadraticCurveTo(5.5, 20, 0, 21); // rounded tail
ctx.quadraticCurveTo(-5.5, 20, -6.5, 10);
ctx.bezierCurveTo(-8, -2, -6, -20, 0, -26); // left rail back to nose
ctx.closePath();
ctx.fillStyle = 'rgba(79,195,247,0.28)';
ctx.fill();
ctx.strokeStyle = '#4fc3f7'; ctx.lineWidth = 1.6; ctx.stroke();
// centre stringer
ctx.beginPath(); ctx.moveTo(0, -22); ctx.lineTo(0, 18);
ctx.strokeStyle = 'rgba(79,195,247,0.5)'; ctx.lineWidth = 1; ctx.stroke();
// foot-strap ticks near the tail
ctx.strokeStyle = 'rgba(232,244,253,0.7)'; ctx.lineWidth = 1.3;
ctx.beginPath(); ctx.moveTo(-4.6, 10.5); ctx.lineTo(-1.6, 12.5); ctx.stroke();
ctx.beginPath(); ctx.moveTo(4.6, 10.5); ctx.lineTo(1.6, 12.5); ctx.stroke();
// fin off the tail
ctx.beginPath(); ctx.moveTo(0, 21); ctx.lineTo(0, 26);
ctx.strokeStyle = '#4fc3f7'; ctx.lineWidth = 1.4; ctx.stroke();
ctx.restore();
// ---- rig: mast stepped forward, sail bellied to leeward, tinted by trim ----
const [mx, my] = pt(h, 8); // mast base, forward of centre
const boomA = h + Math.PI + side * sheet; // clew swings aft + leeward
const clx = mx - Math.sin(boomA) * 30;
const cly = my - Math.cos(boomA) * 30;
const bellyA = boomA + side * 55 * DEG; // bow the cloth to leeward
const bx = mx - Math.sin(bellyA) * 20;
const by = my - Math.cos(bellyA) * 20;
ctx.beginPath();
ctx.moveTo(mx, my);
ctx.quadraticCurveTo(bx, by, clx, cly);
ctx.closePath();
ctx.fillStyle = st.trim === 'good' ? 'rgba(102,187,106,0.35)'
: st.trim === 'stall' ? 'rgba(255,112,67,0.35)'
: 'rgba(255,255,255,0.16)';
ctx.fill();
ctx.strokeStyle = 'rgba(255,255,255,0.9)'; ctx.lineWidth = 1.7;
ctx.beginPath(); ctx.moveTo(mx, my); ctx.quadraticCurveTo(bx, by, clx, cly); ctx.stroke();
// mast base dot
ctx.fillStyle = '#e8f4fd';
ctx.beginPath(); ctx.arc(mx, my, 2.2, 0, Math.PI * 2); ctx.fill();
}
}