Wave creates longer path

by Chantal Roth

HTML

<div class="container">
  <h3>GR "Solid Model" Density Demo</h3>
  <p>
    <strong>Concept:</strong> A transverse wave creates a longer path. If the medium is elastic (resists stretching), the particles must pull together horizontally to accommodate the wave. <br>
    <em>Result: Wave Energy = Increased Density = Increased Refractive Index.</em>
  </p>
  
  <div class="controls">
    <label>Wave Amplitude (Energy): 
      <input type="range" id="ampRange" min="0" max="40" value="0">
    </label>
    <span id="ampVal">0</span>
  </div>

  <canvas id="simCanvas" width="600" height="300"></canvas>
  
  <div class="legend">
    <span style="color:#444">Low Density (Space)</span>
    <span style="background: linear-gradient(90deg, #888, #ff0000); width: 100px; display:inline-block; height: 10px; margin: 0 10px; border-radius:5px;"></span>
    <span style="color:#ff0000">High Density (Matter)</span>
  </div>
</div>

CSS

body {
  font-family: sans-serif;
  background: #f4f4f4;
  display: flex;
  justify-content: center;
  align-items: center;
  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;
  max-width: 650px;
}

canvas {
  background: #111;
  border-radius: 4px;
  margin-top: 20px;
  box-shadow: inset 0 0 20px #000;
}

.controls {
  margin: 15px 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
}

p {
  font-size: 14px;
  line-height: 1.4;
  color: #555;
}

.legend {
  margin-top: 10px;
  font-size: 12px;
  font-weight: bold;
}

JavaScript

const canvas = document.getElementById('simCanvas');
const ctx = canvas.getContext('2d');
const ampSlider = document.getElementById('ampRange');
const ampVal = document.getElementById('ampVal');

// Simulation Constants
const numParticles = 80;
const restSpacing = 7; // The physical length of the "string" between atoms
const frequency = 0.15;
const speed = 0.1;

let time = 0;

function draw() {
  // Clear screen
  ctx.fillStyle = '#111';
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  
  const amplitude = parseFloat(ampSlider.value);
  ampVal.textContent = amplitude;

  // Arrays to store calculated positions
  let particles = [];
  
  // 1. CALCULATE POSITIONS
  // We start from the left and calculate x based on the fixed segment length (restSpacing).
  // This simulates the "Rubber Band" constraint: The string doesn't stretch infinitely.
  
  let currentX = 50; // Start position
  
  for (let i = 0; i < numParticles; i++) {
    // Calculate the Transverse Displacement (Y)
    // Using a Gaussian envelope to create a localized "Packet" (Particle)
    let center = numParticles / 2;
    let envelope = Math.exp(-Math.pow((i - center), 2) / (2 * 100)); // Gaussian width
    let yOffset = amplitude * Math.sin((i * frequency) - time) * envelope;
    
    let prevParticle = particles[i-1];
    
    let x, y;
    
    if (i === 0) {
      x = currentX;
      y = canvas.height / 3 + yOffset;
    } else {
      // GEOMETRIC CONSTRAINT (The Physics of the Paper)
      // We know the Y difference. We know the hypotenuse is fixed (restSpacing).
      // We must solve for dx:  dx^2 + dy^2 = restSpacing^2
      
      let targetY = canvas.height / 3 + yOffset;
      let dy = targetY - prevParticle.y;
      
      // Prevent breaking if amplitude is too high for the spacing
      let dx = Math.sqrt(Math.max(0, restSpacing*restSpacing - dy*dy));
      
      x = prevParticle.x + dx;
      y = targetY;
    }
    
   ...