Trees and Earthquake

by Chantal Roth

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.9/dat.gui.min.js"></script>
<script id="MathJax-script" async
  src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>

<canvas id="c"></canvas>

<div id="hud">
  <div class="title">Waves -> “Particles” via Threshold Detection</div>
  <div class="subtitle">
    A continuous wave (storm / quake) creates discrete events when resonant stress crosses a threshold.
    Click a tree/house to inspect its local wave + stress.
    <div class="tip">Tip: If you see too few events, increase <b>Event sensitivity</b> or lower <b>Failure threshold</b>.</div>
  </div>

  <div id="formula">
    $$\textbf{Wave:}\quad u_{tt}=c^2\nabla^2 u-\gamma u_t + S(\mathbf{x},t)$$
    $$\textbf{Detector (resonant):}\quad x''+2\zeta\omega_0 x'+\omega_0^2 x=\kappa\,u(t)$$
    $$\textbf{Event (“particle”):}\quad |x(t)|>x_{\rm fail}\ \Rightarrow\ \text{collapse}$$
  </div>

  <div class="legend">
    <span class="chip wave"></span> wave field &nbsp;&nbsp;
    <span class="chip stress"></span> detector stress &nbsp;&nbsp;
    <span class="chip event"></span> discrete event
  </div>

  <canvas id="plot" width="440" height="160"></canvas>
</div>

CSS

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: radial-gradient(1100px 800px at 28% 18%, #071a22 0%, #050913 55%, #02030a 100%);
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
}

#c {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
}

#hud {
  position: fixed;
  left: 14px;
  top: 14px;
  width: min(540px, calc(100vw - 28px));
  color: rgba(240, 246, 255, 0.95);
  background: rgba(6, 12, 22, 0.58);
  border: 1px solid rgba(120, 220, 255, 0.18);
  box-shadow: 0 12px 40px rgba(0,0,0,0.48);
  backdrop-filter: blur(10px);
  border-radius: 14px;
  padding: 12px 14px;
  user-select: none;
}

.title {
  font-weight: 800;
  letter-spacing: 0.2px;
  font-size: 16px;
  margin-bottom: 2px;
}

.subtitle {
  font-size: 12px;
  line-height: 1.35;
  color: rgba(220, 235, 255, 0.85);
  margin-bottom: 10px;
}

.subtitle .tip{
  margin-top: 6px;
  font-size: 12px;
  color: rgba(235, 255, 240, 0.85);
  opacity: 0.95;
}

#formula {
  font-size: 12px;
  color: rgba(230, 240, 255, 0.90);
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid rgba(140, 220, 255, 0.14);
  margin-bottom: 10px;
}

.legend {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  font-size: 12px;
  color: rgba(220, 235, 255, 0.85);
  margin: 6px 0 8px 0;
}

.chip {
  display: inline-block;
  width: 14px;
  height: 10px;
  border-radius: 3px;
  border: 1px solid rgba(255,255,255,0.25);
  margin-right: 4px;
}
.chip.wave  { background: linear-gradient(90deg, rgba(60,120,255,0.80), rgba(70,220,160,0.75), rgba(255,235,120,0.80)); }
.chip.stress{ background: linear-gradient(90deg, rgba(255,200,90,0.90), rgba(255,70,70,0.85)); }
.chip.event { background: rgba(255,255,255,0.95); }

#plot {
  width: 100%;
  height: 160px;
  border-radius: 12px;
 ...

JavaScript

(() => {
  // ============================================================
  // Waves -> discrete events demo (threshold detection)
  // Improvements:
  // - Much more frequent events by default
  // - Clear "Event sensitivity" + "Failure threshold" controls
  // - Smaller/fewer detectors by default
  // - More 3D-ish trees/houses + better collapse visuals
  // - Dramatic aurora palette: blue -> green -> yellow
  // ============================================================

  const canvas = document.getElementById('c');
  const ctx = canvas.getContext('2d', { alpha: true });
  const plotCanvas = document.getElementById('plot');
  const pctx = plotCanvas.getContext('2d');

  // RoundRect fallback (older canvases)
  if (!CanvasRenderingContext2D.prototype.roundRect) {
    CanvasRenderingContext2D.prototype.roundRect = function(x, y, w, h, r) {
      r = Math.min(r, w/2, h/2);
      this.beginPath();
      this.moveTo(x + r, y);
      this.arcTo(x + w, y, x + w, y + h, r);
      this.arcTo(x + w, y + h, x, y + h, r);
      this.arcTo(x, y + h, x, y, r);
      this.arcTo(x, y, x + w, y, r);
      this.closePath();
      return this;
    };
  }

  function resize() {
    const dpr = Math.max(1, Math.min(2, window.devicePixelRatio || 1));
    canvas.width = Math.floor(innerWidth * dpr);
    canvas.height = Math.floor(innerHeight * dpr);
    canvas.style.width = innerWidth + "px";
    canvas.style.height = innerHeight + "px";
    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
  }
  window.addEventListener('resize', resize);
  resize();

  const TAU = Math.PI * 2;
  const clamp = (x, a, b) => Math.max(a, Math.min(b, x));
  const lerp = (a, b, t) => a + (b - a) * t;

  function mulberry32(seed) {
    let t = seed >>> 0;
    return function() {
      t += 0x6D2B79F5;
      let x = Math.imul(t ^ (t >>> 15), 1 | t);
      x ^= x + Math.imul(x ^ (x >>> 7), 61 | x);
      return ((x ^ (x >>> 14)) >>> 0) / 4294967296;
    };
 ...