GRIN Optics

by Chantal Roth

HTML

<div class="container">
  <div class="controls">
    <label>
      Gradient Strength (Focus Power):
      <input type="range" id="gradientInput" min="0" max="100" value="20">
    </label>
    <label>
      Number of Rays:
      <input type="range" id="rayInput" min="3" max="21" step="2" value="7">
    </label>
  </div>
  <canvas id="simCanvas" width="800" height="400"></canvas>
  <p class="caption">
    <strong>Visualizing GRIN Optics:</strong> The <em>Green Line</em> represents the refractive index ($n$).
    Notice how increasing the gradient strength curves the profile and pulls the focal point closer.
  </p>
</div>

CSS

body {
  font-family: 'Segoe UI', sans-serif;
  background: #f4f4f4;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
}

.container {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
  text-align: center;
}

canvas {
  background: #fff;
  border: 1px solid #ddd;
  margin-top: 15px;
}

.controls {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-bottom: 10px;
}

label {
  display: flex;
  flex-direction: column;
  font-size: 14px;
  color: #333;
}

input[type=range] {
  margin-top: 5px;
  cursor: pointer;
}

.caption {
  font-size: 14px;
  color: #666;
  max-width: 600px;
  margin: 10px auto 0;
  line-height: 1.5;
}

JavaScript

const canvas = document.getElementById('simCanvas');
const ctx = canvas.getContext('2d');
const gradientInput = document.getElementById('gradientInput');
const rayInput = document.getElementById('rayInput');

// Configuration
const width = canvas.width;
const height = canvas.height;
const centerY = height / 2;
const lensStart = 200;
const lensWidth = 300;
const lensEnd = lensStart + lensWidth;

// Simulation Loop
function draw() {
  ctx.clearRect(0, 0, width, height);
  
  const strength = parseInt(gradientInput.value);
  const rayCount = parseInt(rayInput.value);
  
  // 'g' is the gradient parameter.
  const g = 0.0000 + (strength / 100) * 0.015; 

  drawGrid();
  drawLens(strength);
  drawIndexProfile(g);
  drawRays(g, rayCount);
  drawAxis();
}

function drawAxis() {
  ctx.beginPath();
  ctx.setLineDash([10, 5]);
  ctx.strokeStyle = '#999'; // Lighter grey for less distraction
  ctx.lineWidth = 1;
  ctx.moveTo(0, centerY);
  ctx.lineTo(width, centerY);
  ctx.stroke();
  ctx.setLineDash([]);
}

function drawLens(strength) {
  const grad = ctx.createLinearGradient(0, centerY - 150, 0, centerY + 150);
  const opacity = 0.2 + (strength/200);
  
  grad.addColorStop(0, `rgba(173, 216, 230, ${opacity * 0.5})`); 
  grad.addColorStop(0.5, `rgba(30, 144, 255, ${opacity})`);      
  grad.addColorStop(1, `rgba(173, 216, 230, ${opacity * 0.5})`); 

  ctx.fillStyle = grad;
  ctx.fillRect(lensStart, centerY - 150, lensWidth, 300);
}

// --- UPDATED FUNCTION ---
function drawIndexProfile(g) {
  ctx.beginPath();
  ctx.strokeStyle = '#008000'; // Standard green
  ctx.lineWidth = 3;

  // CHANGED: Reduced from 100,000 to 25,000 to make the curve wider/smoother
  const curveAmp = 25000; 
  
  // Starting X position for the peak of the curve
  const curveStartX = 150; 

  for (let y = centerY - 150; y <= centerY + 150; y += 5) {
    let r = y - centerY;
    
    // Calculate x-position based on refractive index
...