Skip to content

176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.

33.4 KB
Raw Blame
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Light Rays — Animation Handbook</title>
  <meta name="description" content="Soft beams of light fall from above and slowly sweep across. Best for hero intros.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/light-rays/">
  <link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
  <link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
  <link rel="apple-touch-icon" href="../../../apple-touch-icon.png">
  <meta property="og:type" content="article">
  <meta property="og:site_name" content="Animation Handbook">
  <meta property="og:title" content="Light Rays — Animation Handbook">
  <meta property="og:description" content="Soft beams of light fall from above and slowly sweep across. Best for hero intros.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/light-rays/">
  <meta property="og:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
  <meta name="twitter:card" content="summary_large_image">
  <meta name="twitter:title" content="Light Rays — Animation Handbook">
  <meta name="twitter:description" content="Soft beams of light fall from above and slowly sweep across. Best for hero intros.">
  <meta name="twitter:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
  <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Light Rays", "name": "Light Rays", "description": "Soft beams of light fall from above and slowly sweep across. Best for hero intros.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/light-rays/", "inLanguage": "en", "articleSection": "Ambient & Background", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Ambient & Background, Light Rays"}</script>
  <style>
    :root{--ui-accent:#ffce5a}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    /* A dark room, a little lighter where the light comes in (--sx, set by the script) */
    .stage{position:relative;overflow:hidden;--sx:6%;--r0:#2a1c0f;--r1:#120c08;--r2:#070505;background:radial-gradient(ellipse 90% 75% at var(--sx) 0%,var(--r0),var(--r1) 55%,var(--r2))}
    .stage[data-tone="cool"]{--r0:#14223a;--r1:#09101c;--r2:#04060a}
    canvas{position:absolute;inset:0;display:block;width:100%;height:100%}
    .fg{position:absolute;left:50%;bottom:clamp(18px,9%,40px);transform:translateX(-50%);width:min(86%,440px);text-align:center;z-index:1;pointer-events:none}
    .fg::before{content:"";position:absolute;inset:-24px -32px;z-index:-1;background:radial-gradient(closest-side,rgba(7,5,5,.6),rgba(7,5,5,0))} /* keeps the text readable where beams cross under it */
    .fg-eyebrow{font-size:11px;font-weight:600;letter-spacing:2px;text-transform:uppercase;color:rgba(255,255,255,.9);margin-bottom:6px}
    .fg-title{font-size:clamp(18px,3.2vw,28px);font-weight:800;line-height:1.2;color:#fff;text-wrap:balance}
    /* See inside and Compare: the key and the half names are plain words over the picture, each on its own dark backing */
    .key,.half{display:none}
    .tag{position:absolute;padding:4px 10px;border-radius:999px;background:rgba(5,6,10,.9);color:#f4f4f2;font:600 13px/1.2 var(--hb-font);white-space:nowrap}
    .key{position:absolute;bottom:10px;flex-direction:column;gap:4px}
    .kr{position:static;display:flex;align-items:center;gap:7px;padding-left:7px}
    .kr[hidden]{display:none}
    .kr svg{flex:none;width:22px;height:16px;fill:none;stroke:#fff;stroke-width:1.6;stroke-linecap:round;stroke-linejoin:round}
    .half{top:8px;left:10px}
    .half.two{left:calc(50% + 10px)}
    .stage[data-hb-mode="inside"] .fg,.stage[data-hb-mode="compare"] .fg{display:none}
    .stage[data-hb-mode="inside"] .key{display:flex;left:10px;align-items:flex-start}
    .stage[data-hb-mode="inside"][data-from="left"] .key{left:auto;right:10px;align-items:flex-end}
    .stage[data-hb-mode="compare"] .half{display:block}
    /* Compare: a divider, and a lighter patch of room at each half's own source, below the names */
    .stage[data-hb-mode="compare"]{background:radial-gradient(ellipse 90% 75% at var(--sx) 0%,var(--r0),var(--r1) 55%,var(--r2)) 0 var(--nm)/50% calc(100% - var(--nm)) no-repeat,radial-gradient(ellipse 90% 75% at var(--sx) 0%,var(--r0),var(--r1) 55%,var(--r2)) 100% var(--nm)/50% calc(100% - var(--nm)) no-repeat,var(--r2)}
    .stage[data-hb-mode="compare"]::after{content:"";position:absolute;left:50%;top:0;bottom:0;width:2px;margin-left:-1px;background:rgba(244,244,242,.3)}
    @media(max-width:600px){.tag{font-size:12px;padding:3px 8px}.key{gap:3px}.kr{padding-left:6px}.half.two{left:calc(50% + 6px)}}
  </style>
  <link rel="stylesheet" href="../../../assets/css/demo-page.css?v=11">
  <script src="../../../assets/js/demo-page.js?v=11" defer></script>
</head>
<body class="hb" data-hb-kind="loop">
<nav class="hb-bar" aria-label="Animation Handbook">
  <a class="hb-home" href="../../../"><svg class="hb-mark" viewBox="0 0 32 32" aria-hidden="true"><g class="g"><circle cx="11.16" cy="20.84" r="6.1"/><circle cx="15.12" cy="16.88" r="6.1"/><circle cx="18.51" cy="13.49" r="6.1"/></g><circle class="b" cx="21.2" cy="10.8" r="6.6"/></svg><span>Animation Handbook</span></a>
  <span class="hb-pager"><a href="../topographic-lines/" rel="prev" aria-label="Previous: Topographic Lines"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg><span><span class="hb-dir">Previous: </span>Topographic Lines</span></a><a href="../game-of-life/" rel="next" aria-label="Next: Game of Life"><span><span class="hb-dir">Next: </span>Game of Life</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg></a></span>
  <a class="hb-star" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener" aria-label="Star on GitHub"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg><span>Star on GitHub</span></a>
</nav>
<main class="hb-page">
  <header class="hb-head">
    <p class="hb-cat">07.22 · Ambient &amp; Background</p>
    <h1>Light Rays</h1>
    <p class="hb-lede">Soft beams of light fall from above and slowly sweep across. Best for hero intros.</p>
  </header>

  <section class="hb-step hb-watch" aria-labelledby="hb-watch-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">1</span>
      <div><h2 id="hb-watch-title">Watch it</h2><p>It moves by itself. Pause it to look closely, or turn on slow motion to see each part of the movement.</p></div>
    </div>
    <div class="seg hb-modes" role="group" aria-label="What the box shows" data-hb-modes>
      <button type="button" data-hb-mode="normal" class="on" aria-pressed="true">Normal</button>
      <button type="button" data-hb-mode="inside" aria-pressed="false">See inside</button>
      <button type="button" data-hb-mode="compare" aria-pressed="false">Compare</button>
    </div>
    <div class="stage" data-tone="warm">
      <canvas id="cvs" aria-hidden="true"></canvas>
      <div class="fg">
        <p class="fg-eyebrow">Light rays</p>
        <p class="fg-title">A soft, slow start for your page</p>
      </div>
      <div class="key" aria-hidden="true">
        <p class="tag kr" data-hb-caption><svg viewBox="0 0 22 16"><path d="M11 3 16.5 13h-11z"/></svg>All beams start from one point</p>
        <p class="tag kr" data-hb-caption><svg viewBox="0 0 22 16"><path d="M1 11.2h20M3 11.2C8 11.2 8.5 4.8 11 4.8S14 11.2 19 11.2"/></svg>Clear edges, bright middle</p>
        <p class="tag kr" data-hb-caption><svg viewBox="0 0 22 16"><path d="M3 14C3 8 7 4 13 3"/><circle cx="9" cy="5.6" r="1.8" fill="#fff"/></svg><span>Beams swing every <b id="secs">12 to 22</b> seconds</span></p>
        <p class="tag kr" id="kdust" data-hb-caption><svg viewBox="0 0 22 16"><circle cx="11" cy="8" r="6"/></svg>Dust glints only inside a beam</p>
      </div>
      <p class="tag half" aria-hidden="true" data-hb-caption>Hard edges</p>
      <p class="tag half two" aria-hidden="true" data-hb-caption>Soft edges</p>
    </div>
    <div class="hb-player">
      <button class="hb-play" type="button" id="btn-pause" data-hb-pause data-state="playing"><svg class="hb-ic hb-i-pause" viewBox="0 0 24 24" aria-hidden="true"><rect x="6" y="4" width="4" height="16" rx="1"/><rect x="14" y="4" width="4" height="16" rx="1"/></svg><svg class="hb-ic hb-i-play" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 3l14 9-14 9z"/></svg><span class="hb-pause-label">Pause</span></button>
      <label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo autocomplete="off"><span>Slow motion</span></label>
    </div>
    <div class="hb-mode-notes" aria-live="polite">
      <p data-hb-for="inside" hidden>Inside, each beam is a slice of light from one point just above the box: clear at its edges, brightest in the middle. Each one swings and brightens on its own slow beat.</p>
      <p data-hb-for="compare" hidden>Hard edges fills each beam with one flat color, so the light looks cut out of paper. Soft edges fades each beam to clear at both sides, so it reads as light in the air.</p>
    </div>
  </section>

  <section class="hb-step hb-try" aria-labelledby="hb-try-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">2</span>
      <div><h2 id="hb-try-title">Try it</h2><p>Change a setting and see the difference as it moves.</p></div>
    </div>
    <div class="hb-settings">
      <div class="hb-setting">
        <p class="hb-setting-name" id="tone-lbl">Light color</p>
        <div class="seg" id="tone-seg" role="group" aria-labelledby="tone-lbl">
          <button type="button" data-tone="warm" class="on" aria-pressed="true">Warm</button>
          <button type="button" data-tone="cool" aria-pressed="false">Cool</button>
        </div>
        <p class="hb-hint">Warm is late sunshine; cool is moonlight.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="speed-lbl">Speed</p>
        <div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">
          <button type="button" data-spd="0.5" aria-pressed="false">Slow</button>
          <button type="button" data-spd="1" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-spd="2" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">How fast the beams sway and the dust drifts.</p>
      </div>
      <div class="hb-setting">
        <label class="hb-switch-row"><span>Floating dust</span><input class="hb-switch" type="checkbox" role="switch" id="dust-tog" checked autocomplete="off"></label>
        <p class="hb-hint">Specks of dust drift and glint where the light falls.</p>
      </div>
    </div>
    <details class="hb-options">
      <summary><span class="hb-more-label">More options</span><span class="hb-less">Fewer options</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></summary>
      <div class="hb-settings">
        <div class="hb-setting">
          <p class="hb-setting-name" id="count-lbl">Number of rays</p>
          <div class="seg" id="count-seg" role="group" aria-labelledby="count-lbl">
            <button type="button" data-n="5" aria-pressed="false">Few</button>
            <button type="button" data-n="8" class="on" aria-pressed="true">Some</button>
            <button type="button" data-n="12" aria-pressed="false">Many</button>
          </div>
          <p class="hb-hint">More rays overlap into a fuller, brighter light.</p>
        </div>
        <div class="hb-setting">
          <p class="hb-setting-name" id="from-lbl">Light comes from</p>
          <div class="seg" id="from-seg" role="group" aria-labelledby="from-lbl">
            <button type="button" data-from="left" class="on" aria-pressed="true">Top left</button>
            <button type="button" data-from="top" aria-pressed="false">Top</button>
            <button type="button" data-from="right" aria-pressed="false">Top right</button>
          </div>
          <p class="hb-hint">Where the beams start.</p>
        </div>
      </div>
    </details>
  </section>

  <section class="hb-step hb-prompt-step" aria-labelledby="hb-prompt-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">3</span>
      <div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div>
    </div>
    <div class="hb-card">
      <p class="hb-prompt">Add soft light rays to [the dark section you want them in]. From one point just above the top edge, draw a fan of long, narrow beams on a canvas, made soft by gradients rather than blur filters: clear at the edges, brightest near the source and fading out farther away. Let each beam sway and brighten and dim on its own slow cycle, paced by elapsed time, so the light seems to sweep across the scene, and add overlapping beams together. When the settings include dust, float tiny specks that glint only where a beam crosses them, with fewer specks on phones. Keep the content above the light. If the visitor has reduced motion turned on, show the rays still. Match the settings listed below.</p>
      <div class="hb-chips-box" hidden>
        <p class="hb-chips-title">Your settings</p>
        <ul class="hb-chips"></ul>
      </div>
      <div class="hb-copy-row">
        <button class="hb-copy" type="button"><svg class="hb-ic hb-i-copy" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg><svg class="hb-ic hb-i-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg><span class="hb-copy-label" aria-live="polite">Copy prompt</span></button>
        <p class="hb-hint">Replace the words in brackets with your own element. Your settings are added at the end.</p>
      </div>
    </div>
    <div class="hb-star-row"><p>Free and open source. If this helped, a star on GitHub helps other people find it.</p><a class="hb-star-btn" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg>Star on GitHub</a></div>
  </section>

  <section class="hb-about" aria-labelledby="hb-what-title">
    <div class="hb-what">
      <h2 id="hb-what-title">What it is</h2>
      <div class="hb-what-text"><p>The full explanation is in <a href="README.md">README.md</a>.</p></div>
    </div>
    <div class="hb-fit">
      <h2>Good for</h2>
      <ul class="hb-tags hb-good"><li>Hero sections</li><li>Page intros</li><li>Nature and wellness sites</li><li>Product launches</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Light pages</li><li>Busy layouts</li></ul>
    </div>
  </section>

  <section class="hb-related" aria-labelledby="hb-related-title" hidden>
    <h2 id="hb-related-title">Similar animations</h2>
    <div class="hb-rel-list"></div>
  </section>
</main>
<footer class="hb-foot"><span>Animation Handbook</span><a href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener">Source on GitHub</a><a href="#top">Back to top ↑</a></footer>
<script>
  const cvs=document.getElementById('cvs'),ctx=cvs.getContext('2d');
  const stage=document.querySelector('.stage');
  const slowTog=document.getElementById('slow-tog');
  const TAU=Math.PI*2;
  const CONIC=!!ctx.createConicGradient; // browsers before about 2023 have none: there each beam is a narrow wedge of one flat, faint color
  const LIGHT={warm:'255,212,150',cool:'168,204,255'},SPECK={warm:'255,236,205',cool:'220,234,255'};
  const PHONE_MOTES=40,MOTES=80; // phones and short screens float half as many specks of dust
  const phone=matchMedia('(max-width:600px),(max-height:500px)');
  let W=0,H=0,raf=null,lastT=null;
  let TONE='warm',SPD=1,DUST=true,COUNT=8,FROM='left';
  let clock=0; // the light's own time in seconds: it moves only while the loop runs, so Play carries on with no jump
  let rays=[],motes=[];
  // A scene is one box of light: its source, fan direction, reach and fade and glow gradients. Normal draws one; Compare two half-width ones
  const full={},half={},NM=38; // NM: the strip above Compare's halves that holds their names
  const key=document.querySelector('.key'),secs=document.getElementById('secs'),kdust=document.getElementById('kdust');
  if(!CONIC)key.children[1].querySelector('path').setAttribute('d','M1 11.2h20M3 11.2V4.8h16v6.4'); // flat beams: the key shows the flat-topped bump
  const notes={inside:document.querySelector('[data-hb-for="inside"]'),compare:document.querySelector('[data-hb-for="compare"]')};
  const MOVING=notes.inside.textContent,reduced=matchMedia('(prefers-reduced-motion: reduce)');
  const STILL='Inside, shown still: each beam is a slice of light from one point just above the box, clear at its edges. Each arc shows how far its beam swings.';
  const DASH=[7,5],DASH2=[3,4];
  let mode='normal',paused=false,bump=null;
  const clearOfDots=m=>{for(const r of rays)if(Math.hypot(r.dx-m.x,r.dy-m.y)<20)return false;return true;};

  // The beams: each has its place in the fan (c), a width and a strength, and its own slow sway and slow brightening
  function build(){const spread=.55;
    rays=Array.from({length:COUNT},(_,i)=>({
      c:-spread+(i+.5+(Math.random()-.5)*.6)*2*spread/COUNT,w:.1+Math.random()*.16,a:.14+Math.random()*.16,
      s:.025+Math.random()*.04,f:TAU/(12+Math.random()*10),p:Math.random()*TAU,
      g:TAU/(6+Math.random()*6),q:Math.random()*TAU,now:0,al:0,d:0,lo:0,hi:0,dx:0,dy:0
    }));
    if(W)plan();
  }
  // Specks of dust: each drifts up and sideways with a gentle wobble and twinkles on its own rhythm
  function scatter(){
    motes=Array.from({length:phone.matches?PHONE_MOTES:MOTES},()=>({x:Math.random()*W,y:Math.random()*H,r:.7+Math.random()*1.3,
      vx:(Math.random()-.5)*6,vy:-(2+Math.random()*5),w:TAU/(5+Math.random()*6),tw:TAU/(1.5+Math.random()*3),p:Math.random()*TAU,l:0}));
  }
  // Where the light starts (a little above the top edge) and the direction its fan points; the room is lighter there too
  function aimScene(o,w,h){
    const fx=FROM==='left'?.06:FROM==='right'?.94:.5,tx=FROM==='left'?.64:FROM==='right'?.36:.5;
    o.w=w;o.h=h;o.sx=w*fx;o.sy=-h*.14;o.base=Math.atan2(h-o.sy,w*tx-o.sx);o.reach=Math.hypot(w,h)*1.05;
    o.fade=ctx.createRadialGradient(o.sx,o.sy,0,o.sx,o.sy,o.reach);
    o.fade.addColorStop(0,'#000');o.fade.addColorStop(.5,'rgba(0,0,0,.75)');o.fade.addColorStop(1,'rgba(0,0,0,0)');
    o.glow=ctx.createRadialGradient(o.sx,o.sy,0,o.sx,o.sy,Math.min(w,h)*.8);
    o.glow.addColorStop(0,`rgba(${LIGHT[TONE]},.3)`);o.glow.addColorStop(1,`rgba(${LIGHT[TONE]},0)`);
  }
  function aim(){
    aimScene(full,W,H);aimScene(half,W/2,H-NM); // Compare's scenes are the same numbers for a box half as wide
    stage.style.setProperty('--sx',(FROM==='left'?6:FROM==='right'?94:50)+'%');stage.dataset.from=FROM;plan();
  }
  // How brightly the beams light one spot of a scene: every beam that covers it adds its strength, most at its middle line
  function lightAt(o,x,y){
    const d=Math.hypot(x-o.sx,y-o.sy)/o.reach,ang=Math.atan2(y-o.sy,x-o.sx);
    if(d>=1)return 0;
    let sum=0;
    for(const r of rays){const u=(ang-r.now)*2/r.w;if(u>-1&&u<1){const b=1-u*u;sum+=r.al*b*b;}}
    return sum*(1-d);
  }
  function pose(o,t,sweep){
    for(const r of rays){
      r.now=o.base+r.c+r.s*Math.sin(r.f*t+r.p)+sweep;
      r.al=r.a*(.6+.4*Math.sin(r.g*t+r.q));
    }
  }
  // One scene of light. Each beam is a soft wedge: a conic gradient around the source, clear at both edges and brightest in the middle,
  // drawn with 'lighter' so overlaps add up; 'destination-in' with a radial gradient then fades them with distance. Hard edges fills each wedge flat instead.
  function paint(o,hard,sweep){
    const rgb=LIGHT[TONE],R=o.reach*1.3;
    ctx.globalCompositeOperation='lighter';
    if(CONIC){ // a faint haze over the whole fan, so the beams shine through lit air instead of a black room
      const hz=ctx.createConicGradient(o.base-.75+sweep,o.sx,o.sy),h=1.5/TAU;
      hz.addColorStop(0,`rgba(${rgb},0)`);hz.addColorStop(h*.3,`rgba(${rgb},.07)`);hz.addColorStop(h*.7,`rgba(${rgb},.07)`);hz.addColorStop(h,`rgba(${rgb},0)`);
      ctx.fillStyle=hz;ctx.fillRect(0,0,o.w,o.h);
    }
    for(const r of rays){
      const e=hard?r.w/2:CONIC?r.w:r.w/4; // the triangle filled: wider than a soft wedge, or the middle half of one in a flat color
      if(CONIC&&!hard){const g=ctx.createConicGradient(r.now-r.w/2,o.sx,o.sy),k=r.w/TAU;
        g.addColorStop(0,`rgba(${rgb},0)`);g.addColorStop(k*.2,`rgba(${rgb},${r.al*.3})`);g.addColorStop(k*.5,`rgba(${rgb},${r.al})`);
        g.addColorStop(k*.8,`rgba(${rgb},${r.al*.3})`);g.addColorStop(k,`rgba(${rgb},0)`);ctx.fillStyle=g;}
      else ctx.fillStyle=`rgba(${rgb},${r.al*(hard?.45:.6)})`;
      ctx.beginPath();ctx.moveTo(o.sx,o.sy);ctx.lineTo(o.sx+Math.cos(r.now-e)*R,o.sy+Math.sin(r.now-e)*R);
      ctx.lineTo(o.sx+Math.cos(r.now+e)*R,o.sy+Math.sin(r.now+e)*R);ctx.fill();
    }
    ctx.globalCompositeOperation='destination-in';ctx.fillStyle=o.fade;ctx.fillRect(0,0,o.w,o.h);
    ctx.globalCompositeOperation='lighter';ctx.fillStyle=o.glow;ctx.fillRect(0,0,o.w,o.h);
    ctx.globalCompositeOperation='source-over';
  }
  // The dust, on top, each speck as bright as the light where it is. A Compare half shows every second speck, squeezed into the half
  function dust(o){
    if(!DUST)return;
    const kx=o.w/W,ky=o.h/H,step=o===half?2:1;
    ctx.fillStyle=`rgb(${SPECK[TONE]})`;
    for(let i=0;i<motes.length;i+=step){
      const m=motes[i],x=m.x*kx,y=m.y*ky;
      ctx.globalAlpha=Math.min(1,.06+4*lightAt(o,x,y)*(.65+.35*Math.sin(m.tw*clock+m.p)));
      ctx.beginPath();ctx.arc(x,y,m.r,0,TAU);ctx.fill();
    }
    ctx.globalAlpha=1;
  }
  // The whole picture at the light's time: one scene, or (Compare) two half-width scenes, each drawn whole in its own clip so one half's fade never erases the other
  function draw(){
    const sweep=.06*Math.sin(clock*.11);
    ctx.clearRect(0,0,W,H);
    if(mode==='compare'){
      pose(half,clock,sweep);
      for(let i=0;i<2;i++){
        ctx.save();ctx.translate(i*half.w,NM);ctx.beginPath();ctx.rect(0,0,half.w,half.h);ctx.clip();
        paint(half,i===0,sweep);dust(half);ctx.restore();
      }
      return;
    }
    pose(full,clock,sweep);paint(full,false,sweep);dust(full);
    if(mode==='inside')marks();
  }
  // See inside, over the page's picture: a dashed line down each beam's middle (back to the one point it starts from), the beam nearest the middle
  // with dashed edges and a bump across it, an arc for each beam's whole swing with a dot where it is now, and rings on the brightest specks
  function marks(){
    const o=full,R=o.reach,b=bump&&rays[bump.i];
    const line=a=>{const c=Math.cos(a),s=Math.sin(a);if(s<=0)return;ctx.moveTo(o.sx-c*o.sy/s,0);ctx.lineTo(o.sx+c*R,o.sy+s*R);}; // a beam that points up never shows
    ctx.save();ctx.lineCap='round';ctx.strokeStyle='#fff';ctx.fillStyle='#fff';ctx.setLineDash(DASH);ctx.lineWidth=1.5;
    for(const r of rays){ctx.globalAlpha=.3+Math.min(.6,2.2*r.al);ctx.beginPath();line(r.now);ctx.stroke();}
    let bx=-99,bumpD=()=>99;
    if(b){
      const hw=CONIC?b.w/2:b.w/4,[px,sn,cs,chord]=bumpPose(b.now,bump.y,hw);bx=px;
      ctx.globalAlpha=.8;ctx.lineWidth=1;ctx.setLineDash(DASH2);ctx.beginPath();line(b.now-hw);line(b.now+hw);ctx.stroke();
      ctx.save();ctx.translate(bx,bump.y);ctx.rotate(b.now+Math.PI/2);ctx.setLineDash([]);
      const h=bumpH(chord),T=bumpTail(chord),e=chord/2,k=chord*.3;
      bumpD=(qx,qy)=>bumpDist(qx-bx,qy-bump.y,sn,cs,chord);
      ctx.beginPath(); // a low bell: flat at both edges and at the top, on a baseline a little longer than the beam
      ctx.moveTo(-T,0);ctx.lineTo(-e,0);
      if(CONIC){ctx.bezierCurveTo(-e+k,0,-e+k*1.1,h,0,h);ctx.bezierCurveTo(e-k*1.1,h,e-k,0,e,0);} // soft beams: smooth; flat ones: a flat-topped box
      else{ctx.lineTo(-e,h);ctx.lineTo(e,h);ctx.lineTo(e,0);}
      ctx.lineTo(T,0);ctx.closePath();
      ctx.globalAlpha=1;ctx.lineWidth=2;ctx.fillStyle='rgba(255,255,255,.22)';ctx.fill();ctx.stroke();ctx.restore();
    }
    ctx.setLineDash([]);ctx.globalAlpha=.65;ctx.lineWidth=1.5;ctx.beginPath();
    for(const r of rays)if(r.d>0){ctx.moveTo(o.sx+r.d*Math.cos(r.lo),o.sy+r.d*Math.sin(r.lo));ctx.arc(o.sx,o.sy,r.d,r.lo,r.hi);}
    ctx.stroke();
    ctx.globalAlpha=1;ctx.lineWidth=2;ctx.strokeStyle='rgba(5,6,10,.85)';ctx.beginPath();
    for(const r of rays){r.dx=r.dy=-99;if(r.d>0){r.dx=o.sx+r.d*Math.cos(r.now);r.dy=o.sy+r.d*Math.sin(r.now);ctx.moveTo(r.dx+4.5,r.dy);ctx.arc(r.dx,r.dy,4.5,0,TAU);}}
    ctx.fill();ctx.stroke();
    ctx.beginPath();ctx.moveTo(o.sx,2);ctx.lineTo(o.sx-6.5,12.5);ctx.lineTo(o.sx+6.5,12.5);ctx.closePath();ctx.fill();ctx.stroke();
    if(DUST){ // the brightest specks first, none within a ring's width of one already picked
      ctx.strokeStyle='#fff';ctx.lineWidth=1.5;ctx.globalAlpha=.9;ctx.beginPath();
      for(const m of motes)m.l=m.x<12||m.x>W-12||m.y<12||m.y>H-12||near(m.x,m.y,14)||bumpD(m.x,m.y)<20||!clearOfDots(m)?0:lightAt(o,m.x,m.y);
      for(let k=0;k<(phone.matches?4:6);k++){
        let pick=null;
        for(const m of motes)if(m.l>(pick?pick.l:.04))pick=m;
        if(!pick)break;
        ctx.moveTo(pick.x+9,pick.y);ctx.arc(pick.x,pick.y,9,0,TAU);
        for(const m of motes)if(Math.hypot(m.x-pick.x,m.y-pick.y)<24)m.l=0; // no other ring within a ring's width
      }
      ctx.stroke();
    }
    ctx.restore();
  }
  // Where See inside puts what stays put, once and not every frame: the bump (on a middle beam, where it is wide enough to read, wherever it swings) and
  // each beam's arc: a distance from the source at which the whole swing is inside the box, clear of the key, the bump and every other arc
  let keyR=null;
  // The bump is a low bell across its beam on a baseline a little longer than the beam, as the key draws it
  const bumpTail=c=>c/2+Math.max(5,c*.14),bumpH=c=>Math.min(24,c*.4);
  const bumpPose=(a,y,hw)=>{const sn=Math.sin(a),cs=Math.cos(a),d=(y-full.sy)/sn;return[full.sx+d*cs,sn,cs,2*d*Math.sin(hw)];}; // its center across, the beam's sine and cosine, its width, for a beam at angle a
  const bumpDist=(dx,dy,sn,cs,c)=>{const lx=cs*dy-sn*dx,ly=-cs*dx-sn*dy;return Math.hypot(Math.max(Math.abs(lx)-bumpTail(c),0),Math.max(-ly,ly-bumpH(c),0));}; // how far a point is from the bump drawn so
  const bumpCorners=(bx,y,sn,cs,c)=>[-1,1].flatMap(u=>[0,1].map(v=>{const T=u*bumpTail(c),h=v*bumpH(c);return[bx-T*sn-h*cs,y+T*cs-h*sn];})); // the corners of the box the bump is drawn in
  const near=(x,y,m)=>keyR&&x>keyR.x-m&&x<keyR.x+keyR.w+m&&y>keyR.y-m;
  const room=(x,y)=>x>8&&x<W-8&&y>8&&y<H-8&&!near(x,y,8); // inside the box, clear of the key
  function plan(){
    const o=full,kw=key.offsetWidth,placed=[[o.sx,7]];
    keyR=kw?{x:FROM==='left'?W-10-kw:10,y:H-10-key.offsetHeight,w:kw}:null;
    bump=null;if(!rays.length)return;
    const mid=rays.map((_,i)=>i).sort((i,j)=>Math.abs(rays[i].c)-Math.abs(rays[j].c)).slice(0,3);
    let best=null,score=-1e9; // among the three beams nearest the middle and six heights: the place where the bump is nearest 52 wide, whole inside the box and clear of the key at every angle of its swing
    for(const bi of mid)for(const f of [.58,.5,.42,.34,.26,.2]){
      const r0=rays[bi],a0=o.base+r0.c,sp0=r0.s+.06,y=H*f,P=Array.from({length:17},(_,k)=>bumpPose(a0+sp0*(k-8)/8,y,CONIC?r0.w/2:r0.w/4));
      const fits=P.every(([bx,sn,cs,c])=>sn>.3&&bumpCorners(bx,y,sn,cs,c).every(([qx,qy])=>room(qx,qy)));
      const sc=-Math.abs(P[8][3]-52)-f;
      if(fits&&sc>score){best={i:bi,y,P};score=sc;}
      else if(!best&&f===.2&&bi===mid[0])best={i:bi,y,P}; // no place is clear: the highest one on the middle beam
    }
    bump=best;
    const free=(x,y)=>room(x,y)&&bump.P.every(([bx,sn,cs,c])=>bumpDist(x-bx,y-bump.y,sn,cs,c)>12);
    rays.forEach((r,i)=>{
      const sp=r.s+.06,Q=[],pref=i%2?.7:.3;let dlo=0,dhi=1e9;
      r.lo=o.base+r.c-sp;r.hi=o.base+r.c+sp;r.d=0;
      for(let k=0;k<9;k++){ // the distances at which every angle of the swing is inside the box
        const q=r.lo+(r.hi-r.lo)*k/8,c=Math.cos(q),s=Math.sin(q);Q.push([c,s]);
        if(s<=0)return;
        dlo=Math.max(dlo,(8-o.sy)/s);dhi=Math.min(dhi,(H-8-o.sy)/s,c>0?(W-8-o.sx)/c:c<0?(8-o.sx)/c:1e9);
      }
      const ds=Array.from({length:12},(_,k)=>(k+.5)/12).sort((u,v)=>Math.abs(u-pref)-Math.abs(v-pref)); // alternate beams prefer near and far
      for(const u of ds){
        const d=dlo+(dhi-dlo)*u,P=Q.map(([c,s])=>[o.sx+d*c,o.sy+d*s]);
        if(dhi>=dlo&&P.every(([x,y])=>free(x,y)&&placed.every(([px,py])=>Math.hypot(px-x,py-y)>14))){r.d=d;placed.push(...P);break;}
      }
    });
  }
  // The dust moves on by s seconds of the light's own time, coming back in at the bottom when it leaves at the top
  function drift(s){
    for(const m of motes){
      m.x+=(m.vx+5*Math.sin(m.w*clock+m.p))*s;m.y+=m.vy*s;
      if(m.y<-4){m.y=H+4;m.x=Math.random()*W;}
      if(m.x<-4)m.x=W+4;else if(m.x>W+4)m.x=-4;
    }
  }
  // loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. At most one picture per
  // 16 ms. The light's time grows by the time since the last picture (never more than 50 ms, so a background tab does not
  // leap) times Speed, or a third of that in slow motion; the first frame after a start has nothing to measure from and adds none.
  function loop(ts){
    raf=requestAnimationFrame(loop);
    if(lastT!==null&&ts-lastT<16)return;
    const dt=lastT===null?0:Math.min(ts-lastT,50),s=dt/1000*SPD*(slowTog.checked?1/3:1);
    lastT=ts;
    clock+=s;drift(s);draw();
  }
  function start(){if(raf)return;lastT=null;raf=requestAnimationFrame(loop);}

  // Registered at the top level, before the shared script (deferred) can run: under reduced motion it pauses the
  // loop on arrival, which this listener must catch. The rays on screen stay as they are while paused.
  document.addEventListener('hb:pause',e=>{
    paused=e.detail.paused;words();
    if(paused){cancelAnimationFrame(raf);raf=null;}
    else start();
  });
  // The mode switch draws the mode at once. Every mode is drawn from the clock alone, so Normal's picture comes back exactly
  document.addEventListener('hb:mode',e=>{mode=e.detail.mode;relayout();});
  // A tab that comes back after being hidden has no earlier frame to measure from, so its first frame adds no time
  document.addEventListener('visibilitychange',()=>{lastT=null;});

  // Setting the canvas size wipes it, so a real change of the stage's size aims the light and draws it again at once,
  // playing or paused. A resize that leaves the stage as it was (a phone's address bar sliding in or out) does nothing.
  function resize(){
    const w=stage.clientWidth,h=stage.clientHeight;
    if(w===W&&h===H)return;
    W=w;H=h;const dpr=Math.min(window.devicePixelRatio||1,2);
    cvs.width=Math.round(W*dpr);cvs.height=Math.round(H*dpr);ctx.setTransform(dpr,0,0,dpr,0,0);
    aim();scatter();draw();
  }

  function choices(id,pick){
    const buttons=document.querySelectorAll('#'+id+' button');
    buttons.forEach(b=>b.addEventListener('click',()=>{
      if(b.classList.contains('on'))return; // pressing the choice already made does nothing
      buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
      pick(b.dataset);
    }));
  }
  // A setting that changes the picture draws it at once, so the change shows while the loop is paused. Speed changes no
  // picture: the beams carry on from where they are, at the new pace.
  choices('tone-seg',d=>{TONE=d.tone;stage.dataset.tone=TONE;aim();draw();});
  choices('speed-seg',d=>{SPD=+d.spd;keySecs();});
  document.getElementById('dust-tog').addEventListener('change',e=>{DUST=e.target.checked;kdust.hidden=!DUST;relayout();});
  slowTog.addEventListener('change',keySecs);
  choices('count-seg',d=>{COUNT=+d.n;build();draw();});
  choices('from-seg',d=>{FROM=d.from;aim();draw();});

  // See inside's key says how long a beam takes to swing: 12 to 22 seconds at Normal speed, longer at Slow and in slow motion
  function keySecs(){
    const k=(slowTog.checked?3:1)/SPD,t=Math.round(12*k)+' to '+Math.round(22*k);
    if(secs.textContent!==t)secs.textContent=t;relayout();
  }
  // The See inside words say "shown still" only while reduced motion has the loop paused; Compare's say so where soft beams cannot be drawn
  function words(){const t=reduced.matches&&paused?STILL:MOVING;if(notes.inside.textContent!==t)notes.inside.textContent=t;}
  function relayout(){if(W){plan();draw();}}
  if(!CONIC)notes.compare.textContent='This browser cannot draw soft beams, so Hard edges and Soft edges both look flat here. In a current browser, Soft edges fades each beam to clear at both sides.';
  reduced.addEventListener('change',words);
  if(document.fonts)document.fonts.ready.then(()=>{if(mode==='inside')relayout();});
  stage.style.setProperty('--nm',NM+'px');
  build();resize();
  window.addEventListener('resize',resize);
  // A window that crosses the phone size changes how many specks of dust it shows, even when the stage keeps its size
  phone.addEventListener('change',()=>{if((phone.matches?PHONE_MOTES:MOTES)!==motes.length){scatter();draw();}});
  start();
</script>
</body>
</html>