Relativistic Doppler Symmetry Demo

by Chantal Roth

HTML

<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" async></script>

<header>
  <h1>Relativistic Doppler & Time Dilation</h1>
  <p class="subtitle">
    Two clocks exchange signals in an absolute frame. 
    The emitter sends exactly 5 signals at regular internal intervals.
  </p>
  <div class="key-insight">
    <strong>Observation:</strong> Despite an absolute medium, the math works out symmetrically. 
    Neither clock can determine who is "really" moving.
  </div>

  <div class="legend-bar">
    <span class="legend-item"><i style="background: var(--accent-red); box-shadow: var(--glow-red);"></i>Clock A</span>
    <span class="legend-item"><i style="background: var(--accent-blue); box-shadow: var(--glow-blue);"></i>Clock B</span>
    <span class="legend-item"><i style="border: 1px solid #666;"></i>Wavefront</span>
    <span class="legend-item"><i style="background: var(--accent-green); box-shadow: 0 0 5px var(--accent-green);"></i>Detection Event</span>
  </div>
</header>

<div id="control-deck">
  <div class="btn-group">
    <button id="runButton">▶️ Start / Pause</button>
    <button id="restartButton">🔄 Reset</button>
  </div>

  <div class="control-group" style="border-left: 2px solid #555;">
    <label title="Speed of wavefront propagation in the medium (affects time dilation strength)">
       <span>Wave Speed (c)</span>
      <input id="speedOfWaveField" type="range" min="5" max="100" value="20">
      <span id="speedOfWaveVal" class="val-display">20</span>
    </label>
  </div>

  <div class="control-group" style="border-left: 2px solid var(--accent-red);">
    <label title="Clock A's velocity relative to the medium (positive = rightward).">
      <span style="color: var(--accent-red); font-weight: bold;">Clock A Velocity</span>
      <input id="speedFieldA" type="range" min="-50" max="50" value="0">
      <span id="speedFieldAVal" class="val-display">0</span>
    </label>
  </div>

  <div...

CSS

:root {
    --bg-space: #0a0a12;
    --bg-panel: #141420;
    --text-primary: #e0e0e0;
    --text-secondary: #a0a0a0;
    --accent-red: #ff3333;
    --accent-blue: #3388ff;
    --accent-green: #00ff99;
    --glow-red: 0 0 10px rgba(255, 51, 51, 0.7);
    --glow-blue: 0 0 10px rgba(51, 136, 255, 0.7);
  }

  body {
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    margin: 0;
    background: var(--bg-space);
    color: var(--text-primary);
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
  }

  /* Header and Intro */
  header {
    text-align: center;
    padding: 1rem;
    max-width: 900px;
    border-bottom: 1px solid #333;
  }

  h1 { margin: 0.5rem 0; letter-spacing: 1px; }
  .subtitle { color: var(--text-secondary); margin-bottom: 1rem; }
  .key-insight {
    background: rgba(255, 255, 255, 0.05);
    padding: 0.8rem;
    border-radius: 6px;
    border-left: 4px solid var(--accent-green);
    margin: 1rem auto;
    display: inline-block;
  }

  /* Legend integrated into header area */
  .legend-bar {
    display: flex;
    gap: 1.5rem;
    justify-content: center;
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin-top: 0.5rem;
  }
  .legend-item i { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 5px; }

  /* Control Deck */
  #control-deck {
    background: var(--bg-panel);
    padding: 1rem;
    border-radius: 8px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.3);
    margin: 1rem 0;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 1rem;
    border: 1px solid #333;
  }

  .control-group {
    display: flex;
    align-items: center;
    background: rgba(0,0,0,0.2);
    padding: 0.5rem 0.8rem;
    border-radius: 4px;
  }

  .control-group label {
    display: flex;
    flex-direction: column;
    font-size: 0.8rem;
    text-align: center;
  ...

JavaScript

(() => {
const fmt = (v, d=2) => Number.parseFloat(v).toFixed(d);
const $   = id => document.getElementById(id);

const CANVAS_HEIGHT = 300;
let CANVAS_WIDTH = 800;
const CLOCK_RADIUS  = 15;

const MAX_NR_CLICKS = 5;
const BASE_INTERVAL_NS = 1_000_000_000;

/* ---------- state set by controls ---------- */
let speedOfWave       = 20;
let sourceA_velocity  = 0;
let sourceB_velocity  = -16;
let animationSpeed    = 20_000_000;
let whichIsEmitter    = 1;

/* ---------- DOM Elements ---------- */
const runBtn       = $('runButton');
const restartBtn   = $('restartButton');
const infoBtn      = $('infoButton');
const speedWaveInp       = $('speedOfWaveField');
const speedOfWaveVal     = $('speedOfWaveVal');
const speedAInp          = $('speedFieldA');
const speedFieldAVal     = $('speedFieldAVal');
const speedBInp          = $('speedFieldB');
const speedFieldBVal     = $('speedFieldBVal');
const animSpeedInp       = $('animationSpeedField');
const animationSpeedVal  = $('animationSpeedVal');
const emitterSel         = $('whichIsEmitterField');

const canvas1 = $('canvas1');
const canvas2 = $('canvas2');
const ctx1 = canvas1.getContext('2d');
const ctx2 = canvas2.getContext('2d');
const infoModal    = $('infoModal');
const closeInfoBtn = $('closeInfo');

infoBtn.addEventListener('click', () => { infoModal.style.display = 'flex'; });
closeInfoBtn.addEventListener('click', () => { infoModal.style.display = 'none'; });

function resizeCanvases() {
    const wrapper = $('scen1-wrapper');
    // Safety check: if clientWidth is 0 (layout not ready), fallback to 800
    const containerWidth = wrapper && wrapper.clientWidth > 0 ? wrapper.clientWidth : 800;
    
    CANVAS_WIDTH = containerWidth;
    canvas1.width = CANVAS_WIDTH;
    canvas1.height = CANVAS_HEIGHT;
    canvas2.width = CANVAS_WIDTH;
    canvas2.height = CANVAS_HEIGHT;
}
window.addEventListener('resize', resizeCanvases);

/* ---------- Classes ----------...