Files
uconsole_asw_game/data/commander/js/plot/spectrum.js
T
db9979andClaude Opus 5.5 4dd23bc242 Web client: CIC redesign with ES modules and bundled fonts
- Remote Crew browser client rebuilt as a dark combat-information-centre layout
  for large desktop monitors: slim status bar (stations, mission, clocks, UTC,
  link state), pulsing alert band, the instrument as centrepiece and
  collapsible docks (contacts, station, contact detail, log drawer; keys , . L).
  Four columns from 2400 px, fluid type up to 4K; guide, lookout and contact
  library open as overlays (Esc closes).
- app.js split into native ES modules under data/commander/js; the transport
  (net/*) no longer touches the DOM and talks to the views through synchronous
  topics (core/events.js, app/bindings.js); redraws share one frame scheduler.
- style.css replaced by a token-based design system in data/commander/css.
- Inter 4.1 and JetBrains Mono 2.304 bundled unmodified (OFL-1.1) with
  provenance in THIRD_PARTY_NOTICES.md; CSP allows font-src 'self'.
- Server serves the css/js/fonts trees from one asset manifest
  (src/commander/assets.py); packaging, capture tool, help, manual and docs
  updated; tests share tests/commander_web.py and import modules directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 12:02:01 +02:00

54 lines
2.6 KiB
JavaScript

import { palette } from "../core/palette.js";
import { plotAxes } from "./axes.js";
import { registerAnimatedPlot } from "./clock.js";
import { drawPeakLabels } from "./peaks.js";
import { drawEmpty, visualContext } from "../views/visual-common.js";
// Spectra ease towards each publication instead of jumping to it.
const SPECTRUM_SMOOTHING_S = .15;
export const spectrumStates = new Map();
export function spectrum(id, values, markers = [], frequencies = null, xmax = 80, xmin = 0, overlay = null) {
const spec = {values, markers, frequencies, xmax, xmin, overlay};
const draw = (wallNow) => drawSpectrum(id, spec, wallNow);
registerAnimatedPlot(id, draw);
return draw(performance.now());
}
function easedSpectrum(id, values, wallNow) {
const maximum = Math.max(1e-6, ...values.map((value) => Math.abs(value)));
let state = spectrumStates.get(id);
if (!state || state.values.length !== values.length) {
state = {values: Float64Array.from(values), maximum, wall: wallNow};
spectrumStates.set(id, state);
return state;
}
const alpha = 1 - Math.exp(-Math.max(0, wallNow - state.wall) / 1000 / SPECTRUM_SMOOTHING_S);
for (let index = 0; index < values.length; index++) state.values[index] += (values[index] - state.values[index]) * alpha;
state.maximum += (maximum - state.maximum) * alpha;
state.wall = wallNow;
return state;
}
function drawSpectrum(id, spec, wallNow) {
const {markers, frequencies, xmax, xmin} = spec;
let plot = visualContext(id);
if (!plot) return;
if (!spec.values.length) { spectrumStates.delete(id); drawEmpty(plot); return; }
const eased = easedSpectrum(id, spec.values, wallNow);
const values = eased.values;
let maximum = eased.maximum;
for (const value of values) maximum = Math.max(maximum, Math.abs(value));
plot = plotAxes(plot, xmax - xmin, maximum, " Hz", "rel.", xmin, true);
plot.context.strokeStyle = palette().accent;
plot.context.beginPath();
values.forEach((value, index) => {
const x = frequencies ? (frequencies[index] - xmin) / (xmax - xmin) * plot.width : values.length === 1 ? 0 : index / (values.length - 1) * plot.width;
const y = plot.height - Math.max(0, value) / maximum * (plot.height - 12);
index ? plot.context.lineTo(x, y) : plot.context.moveTo(x, y);
});
plot.context.stroke();
drawPeakLabels(plot, values, frequencies, xmin, xmax, maximum, markers);
plot.context.fillStyle = palette().amber;
for (const marker of markers.slice(0, 20)) plot.context.fillText(marker.text, Math.max(2, Math.min(plot.width - 50, marker.x * plot.width)), 14);
spec.overlay?.(plot);
return plot;
}