Refraction (Snell's Law)

by Chantal Roth

HTML

<!-- MathJax for formulas -->
<script>
  window.MathJax = { tex: { inlineMath: [['$', '$'], ['\\(', '\\)']] }, svg: {fontCache: 'global'} };
</script>
<script defer src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>

<div id="ui">
  <div class="row">
    <label>Mode:</label>
    <select id="mode">
      <option value="step">Step (sudden change)</option>
      <option value="gradient">Gradient (graded index)</option>
    </select>
  </div>

  <div class="row">
    <label>Incident angle (°):</label>
    <input id="angle" type="range" min="0" max="80" step="1" value="20">
    <span id="angleVal">20°</span>
  </div>

  <div class="row step-only">
    <label>n₁ (top):</label>
    <input id="n1" type="range" min="1.00" max="4" step="0.01" value="1.00">
    <span id="n1Val">1.00</span>
  </div>
  <div class="row step-only">
    <label>n₂ (bottom):</label>
    <input id="n2" type="range" min="1.00" max="10" step="0.01" value="1.50">
    <span id="n2Val">1.50</span>
  </div>

<div class="row grad-only">
  <label>n at top:</label>
  <input id="ngTop" type="range" min="1.00" max="4.00" step="0.01" value="1.00">
  <span id="ngTopVal">1.00</span>
</div>
<div class="row grad-only">
  <label>n at bottom:</label>
  <input id="ngBottom" type="range" min="1.00" max="10.00" step="0.01" value="1.80">
  <span id="ngBottomVal">1.80</span>
</div>



  <div class="row">
    <label>Vacuum wavelength λ₀ (px):</label>
    <input id="lambda0" type="range" min="10" max="60" step="1" value="28">
    <span id="lambda0Val">28</span>
  </div>
  <div class="row">
    <label>Rays:</label>
    <input id="rays" type="range" min="0" max="120" step="1" value="70">
    <span id="raysVal">70</span>
  </div>

<div class="row">
  <button id="infoBtn">Info</button>
  <button id="pauseBtn">Pause</button>
  <button id="resetPhaseBtn">Reset phase</button>
  <span id="note" class="note"></span>
</div>
</div>
<canvas...

CSS

html, body {
  margin: 0;
  height: 100%;
  background: #111;
  color: #eee;
  font: 14px/1.3 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}
#ui {
  position: fixed;
  top: 8px; left: 8px;
  background: rgba(0,0,0,0.55);
  padding: 8px 10px;
  border-radius: 8px;
  backdrop-filter: blur(4px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.35);
  max-width: 520px;
  z-index: 10;           /* <-- put UI above the canvas */
}
#c { position: absolute; inset: 0; z-index: 0; }  /* <-- ensure canvas stays behind */

#ui .row {
  display: flex; align-items: center; gap: 10px;
  margin: 6px 0;
  flex-wrap: wrap;
}
#ui label { min-width: 150px; color: #ddd; }
#ui input[type="range"] { width: 200px; }
#ui select { background: #1e1e1e; color: #eee; border: 1px solid #444; border-radius: 4px; padding: 2px 6px; }
#ui button {
  background: #2a2a2a; color: #fff; border: 1px solid #555; border-radius: 4px; padding: 4px 10px; cursor: pointer;
}
#ui button:hover { background: #333; }


.note { margin-left: 10px; color: #f1d06d; }
.grad-only { display: none; }
.step-only { display: flex; }
/* Info modal */
.modal.hidden { display: none; }
.modal {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(4px);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px;
}
.modal-content{
  max-width: 840px; width: 100%;
  background: rgba(18,18,20,0.92);
  color: #f0f0f0; border: 1px solid #444; border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.5);
  padding: 18px 22px 22px 22px;
  overflow: auto; max-height: calc(100vh - 80px);
}
.modal-content h2 { margin: 6px 0 12px; }
.modal-content p, .modal-content li { line-height: 1.45; }
.modal-content code { background:#222; padding:0 4px; border-radius:4px; }
.modal-content hr { border: none; border-top: 1px solid #444; margin: 12px 0; }
.close{
  float: right; border: 1px solid #666; border-radius:...

JavaScript

// Snell + wavefronts: rays + isophase construction from optical path length (OPL)
// Assumptions:
//  - optical phase φ = k0 * OPL - ω t ; k0 = 2π / λ0 , ω/k0 = c0
//  - wavefronts are φ = 2π m ⇒ OPL = c0 t + m λ0
//  - OPL increments as ∫ n ds ; local wavelength λ(x) = λ0 / n(x) ; speed c(x) = c0 / n(x)

const canvas = document.getElementById('c');
const ctx = canvas.getContext('2d');
let W = 0, H = 0;

function getNRange() {
 // const nTop = //params.ngTop, nBot = params.ngBottom;
 const nTop = 10, nBot =1;
  if (params.mode === 'step') {
    //const nTop = params.n1, nBot = params.n2;
    
    return { nMin: Math.min(nTop, nBot), nMax: Math.max(nTop, nBot) };
  } else {
   
    return { nMin: Math.min(nTop, nBot), nMax: Math.max(nTop, nBot) };
  }
}

function resize() {
  W = canvas.width = window.innerWidth;
  H = canvas.height = window.innerHeight;
  invalidateBackground();
  recomputeRays();
}
window.addEventListener('resize', resize);

// UI elements
const q = id => document.getElementById(id);
const el = {
  mode: q('mode'),
  angle: q('angle'), angleVal: q('angleVal'),
  n1: q('n1'), n1Val: q('n1Val'),
  n2: q('n2'), n2Val: q('n2Val'),
  ngTop: q('ngTop'), ngTopVal: q('ngTopVal'),
  ngBottom: q('ngBottom'), ngBottomVal: q('ngBottomVal'),
  lambda0: q('lambda0'), lambda0Val: q('lambda0Val'),
  rays: q('rays'), raysVal: q('raysVal'),
  showRays: q('showRays'),
  showSpeedKey: q('showSpeedKey'),
  pauseBtn: q('pauseBtn'),
  resetPhaseBtn: q('resetPhaseBtn'),
  note: q('note'),
    infoBtn: q('infoBtn'),
  infoPanel: q('infoPanel'),
  infoClose: q('infoClose'),
};



// Parameters (animation units are pixels + seconds)
const params = {
  mode: 'step',
  angleDeg: 20,
  n1: 1.00,
  n2: 1.50,
  ngTop: 1.00,
  ngBottom: 1.80,   // <-- replaces ngDelta
  lambda0: 28,
  rays: 20,
  showRays: true,
  showSpeedKey: true
};

// Animation clocks
const c0 = 180; // "vacuum" phase speed in px/s...