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.

34.1 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>Infinite Zoom — Animation Handbook</title>
  <meta name="description" content="The view keeps zooming into pictures inside pictures. Best for page intros.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/infinite-zoom/">
  <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="Infinite Zoom — Animation Handbook">
  <meta property="og:description" content="The view keeps zooming into pictures inside pictures. Best for page intros.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/infinite-zoom/">
  <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="Infinite Zoom — Animation Handbook">
  <meta name="twitter:description" content="The view keeps zooming into pictures inside pictures. Best for page 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": "Infinite Zoom", "name": "Infinite Zoom", "description": "The view keeps zooming into pictures inside pictures. Best for page intros.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/infinite-zoom/", "inLanguage": "en", "articleSection": "3D & Advanced", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, 3D & Advanced, Infinite Zoom"}</script>
  <style>
    :root{--ui-accent:#3fd6c4;--edge:#3fd6c4;--view:#ffd84d}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{--bg:#0b0b0d;position:relative;overflow:hidden;background:var(--bg)}
    canvas{display:block;width:100%;height:100%}
    /* See inside and Compare: plain words over the picture, put in place by the script; the canvas draws everything else */
    .cap{display:none;position:absolute;left:0;top:0;padding:2px 8px;border-radius:999px;background:rgba(5,6,10,.92);color:#f4f4f2;font:600 12px/1.2 var(--hb-font);white-space:nowrap;pointer-events:none}
    .cap.vw{color:var(--view)}
    .stage[data-hb-mode="inside"] .cap.si,.stage[data-hb-mode="compare"] .cap.sc{display:block}
  </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="../holographic-card/" rel="prev" aria-label="Previous: Holographic Card"><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>Holographic Card</span></a><a href="../water-ripple/" rel="next" aria-label="Next: Water Ripple"><span><span class="hb-dir">Next: </span>Water Ripple</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">06.26 · 3D &amp; Advanced</p>
    <h1>Infinite Zoom</h1>
    <p class="hb-lede">The view keeps zooming into pictures inside pictures. Best for page 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 picture take over.</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" id="stage">
      <canvas id="cvs" role="img" aria-label="Framed pictures nested inside each other, each holding the next one, as the view zooms through them without end"></canvas>
      <p class="cap si vw" id="c-view" aria-hidden="true" data-hb-caption>Your view</p>
      <p class="cap si" id="c-top" aria-hidden="true" data-hb-caption>Reaching the window, it starts over one picture in</p>
      <p class="cap si" id="c-bot" aria-hidden="true" data-hb-caption>Each picture holds the next, two fifths the size</p>
      <p class="cap sc" id="c-left" aria-hidden="true" data-hb-caption>Off-center window</p>
      <p class="cap sc" id="c-right" aria-hidden="true" data-hb-caption>Centered window</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, your view (dashed) moves into a picture whose middle holds the next one; when it reaches that window, the drawing starts over one picture in, which looks exactly the same.</p>
      <p data-hb-for="compare" hidden>Off-center window: the next picture is still shrunk toward the middle, so it misses its frame and leaves a gap; Centered window: each picture fits the next exactly.</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="speed-lbl">Speed</p>
        <div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">
          <button type="button" data-secs="7.5" aria-pressed="false">Slow</button>
          <button type="button" data-secs="4.5" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-secs="2.5" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">How long each picture takes to fill the box.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="count-lbl">Number of scenes</p>
        <div class="seg" id="count-seg" role="group" aria-labelledby="count-lbl">
          <button type="button" data-n="1" aria-pressed="false">1</button>
          <button type="button" data-n="3" aria-pressed="false">3</button>
          <button type="button" data-n="5" class="on" aria-pressed="true">5</button>
        </div>
        <p class="hb-hint">One scene holds a copy of itself; more scenes take turns.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="dir-lbl">Direction</p>
        <div class="seg" id="dir-seg" role="group" aria-labelledby="dir-lbl">
          <button type="button" data-dir="1" class="on" aria-pressed="true">In</button>
          <button type="button" data-dir="-1" aria-pressed="false">Out</button>
        </div>
        <p class="hb-hint">In dives into each picture; out pulls back to the one around it.</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">
          <label class="hb-switch-row"><span>Shows the scene edges</span><input class="hb-switch" type="checkbox" role="switch" id="edges-tog" autocomplete="off"></label>
          <p class="hb-hint">Outlines and numbers each scene, so you can see where the next one waits.</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 an infinite zoom to [the section you want it in]. Give each picture, such as [a painting, a screen or a photo], a frame in its middle, the same shape as the view, that holds a smaller copy of the next picture, so the pictures nest inside each other. Move the view steadily into or out of the middle, growing or shrinking every picture by the same factor each second so the speed feels even. Each time a picture exactly fills the view, draw it as the outermost one, so the loop never jumps. Draw only the pictures big enough to see, and keep them sharp at full size. If the visitor has reduced motion turned on, show one still frame. 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>Page intros</li><li>Portfolios and galleries</li><li>Story and game sites</li><li>Music and art pages</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Long reading pages</li><li>Forms and checkout</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'),main=cvs.getContext('2d');let ctx=main; // ctx: what the helpers draw on (main, or a soft light)
const stage=document.getElementById('stage'),slowTog=document.getElementById('slow-tog');
const EDGE=getComputedStyle(document.documentElement).getPropertyValue('--edge').trim(),VIEW=getComputedStyle(document.documentElement).getPropertyValue('--view').trim();
// Phones are screens up to 600px wide, or up to 500px tall when held sideways: they draw 1.5 canvas pixels per screen pixel, not 2,
// and each picture's wall, sky or table in one flat color, without the soft light
const phone=matchMedia('(max-width:600px),(max-height:500px)');
const R=.4,TAU=Math.PI*2; // the frame in the middle of each picture holds the next picture at 40% of its size
let W=0,H=0,u=1,X=0,Y=0,w=0,h=0,dpr=1,kx=1,ky=1,raf=null,lastT=null; // kx, ky: canvas pixels per stage pixel
let SW=0,SH=0,cx=0,cy=0; // SW, SH: the stage's size (W, H: the size of the box being drawn, which a half of Compare changes for a while); cx, cy: the middle of the window
let p=0,at=0,clock=0; // p: how far the view has zoomed from picture `at` to the next one (0 to 1); clock: seconds played, for the stars
let SECS=4.5,COUNT=5,DIR=1,EDGES=false,mode='normal',paused=false;

// Drawing helpers, in stage pixels. u is 1% of the stage's shorter side; X, Y, w, h are the window in the middle, 40% of the stage.
// The box is the stage, or in Compare a half of it, with its window dx and dy boxes off the middle; what hangs on the window follows cx, cy.
function geom(bw,bh,dx,dy){W=bw;H=bh;u=Math.min(W,H)/100;w=W*R;h=H*R;X=W*(1-R)/2+W*dx;Y=H*(1-R)/2+H*dy;cx=W/2+W*dx;cy=H/2+H*dy;}
const grad=(g,c)=>{c.forEach((s,i)=>g.addColorStop(i/(c.length-1),s));return g;};
const lin=(x0,y0,x1,y1,...c)=>grad(ctx.createLinearGradient(x0,y0,x1,y1),c);
function box(x,y,bw,bh,f,r){ctx.fillStyle=f;ctx.beginPath();if(r&&ctx.roundRect)ctx.roundRect(x,y,bw,bh,r);else ctx.rect(x,y,bw,bh);ctx.fill();}
function oval(x,y,rx,ry,f,a){ctx.fillStyle=f;ctx.beginPath();ctx.ellipse(x,y,rx,ry,a||0,0,TAU);ctx.fill();}
function poly(f,...q){ctx.fillStyle=f;ctx.beginPath();for(let i=0;i<q.length;i+=2)ctx.lineTo(q[i],q[i+1]);ctx.fill();}
function line(c,lw,...q){ctx.strokeStyle=c;ctx.lineWidth=lw;ctx.lineJoin=ctx.lineCap='round';
  ctx.beginPath();for(let i=0;i<q.length;i+=2)ctx.lineTo(q[i],q[i+1]);ctx.stroke();}
const light=(x,y,r,c)=>box(x-r,y-r,2*r,2*r,grad(ctx.createRadialGradient(x,y,0,x,y,r),[c,c.replace(/[\d.]+\)$/,'0)')]));
// The frame m pixels around the window; hole() fills the window with a dark color, seen only in the smallest picture drawn
const frame=(m,f,r)=>box(X-m,Y-m,w+2*m,h+2*m,f,r),hole=()=>box(X,Y,w,h,'#0c0c10');

// Five pictures, each drawn over the whole stage around its window. 1: a painting in a gallery, under a picture light
function gallery(soft){
  if(soft){box(0,0,W,H,lin(0,0,0,H,'#1b5563','#0b2a33'));light(cx,Y,w*.8,'rgba(255,220,160,.3)');return;}
  box(0,H*.84,W,H*.16,'#08212a');box(0,H*.84,W,.7*u,'#2b6f7c');
  for(const s of [-1,1]){const x=cx+s*(w/2+24*u),y=cy-12*u;
    box(x-9*u,y+1.5*u,20*u,22*u,'rgba(0,0,0,.3)');box(x-10*u,y,20*u,22*u,'#3a2717');box(x-8.5*u,y+1.5*u,17*u,19*u,s<0?'#efe1c2':'#22304a');
    oval(x-s*2*u,y+10*u,5*u,5*u,s<0?'#d6623b':'#e8b44a');box(x-8.5*u,y+15*u,17*u,5.5*u,s<0?'#3c6e8f':'#7a4f9a');}
  box(X-3.3*u,Y-2.5*u,w+9*u,h+9*u,'rgba(0,0,0,.35)');
  frame(4.5*u,lin(X,Y,X+w,Y+h,'#fbe7a6','#c9962f','#8c5b17','#e9c568'));frame(1.3*u,'#5e3f12');hole();
  box(cx-w*.17,Y-7.1*u,w*.34,1.6*u,'#2b2418',.8*u);box(X+w-8*u,Y+h+9*u,8*u,3.6*u,'#efe3c6',.4*u);
  for(const x of [-.27,.27])box(cx+w*x-.8*u,H*.88,1.6*u,H*.12,'#06171d');box(cx-w*.3,H*.88,w*.6,2.4*u,'#06171d',u);
}
// 2: a drive-in movie at night: the screen on its posts, twinkling stars, the moon, hills and parked cars
const STARS=Array.from({length:44},(_,i)=>[(.5+i*.7548777)%1,(.5+i*.5698403)%1*.64,.25+i%3*.18,i]); // spread evenly, with no pattern
function drivein(soft){
  if(soft){box(0,0,W,H,lin(0,0,0,H*.86,'#070b24','#1f1850','#5b2c63'));light(W*.86,H*.18,15*u,'rgba(255,236,190,.3)');return;}
  for(const [sx,sy,r,i] of STARS){ctx.globalAlpha=.6+.4*Math.sin(clock*1.6+i*2.4);oval(sx*W,sy*H,r*u,r*u,'#fff');}ctx.globalAlpha=1;
  oval(W*.86,H*.18,4.6*u,4.6*u,'#fff2cf');poly('#1a1236',0,H*.86,0,H*.74,W*.18,H*.68,W*.36,H*.76,W*.62,H*.7,W*.82,H*.77,W,H*.71,W,H*.86);
  box(0,H*.86,W,H*.14,'#0d0a1d');for(const x of [-.32,.3])box(cx+w*x,Y+h,2.2*u,H,'#221b40');
  frame(3.4*u,'#2b2350');frame(2*u,'#dcd6ee');hole();
  poly('rgba(190,200,255,.07)',cx-1.5*u,H,cx+1.5*u,H,X+w,Y+h,X,Y+h);
  for(const c of [.1,.3,.7,.9]){const x=W*c,y=H*.94;
    box(x-9*u,y-4*u,18*u,5*u,'#231b42',2*u);box(x-5.5*u,y-7.5*u,11*u,4.4*u,'#231b42',2*u);box(x-4*u,y-6.6*u,8*u,2.4*u,'#3d3470',u);
    for(const s of [-1,1])oval(x+s*7*u,y-1.8*u,1.1*u,.7*u,'#ff4d6d');}
}
// 3: a living room in the evening: an old television on legs, a floor lamp and a plant
function livingroom(soft){
  if(soft){box(0,0,W,H,lin(0,0,0,H,'#8c4b2f','#55291b'));
    light(cx,cy,w*.95,'rgba(140,190,255,.22)');light(W*.12,H*.4,26*u,'rgba(255,200,120,.38)');return;}
  for(let x=2*u;x<W;x+=7*u)box(x,0,2.4*u,H*.86,'rgba(255,230,200,.05)');
  box(0,H*.86,W,H*.14,lin(0,H*.86,0,H,'#3b2216','#22130c'));oval(cx,H*.95,w*.7,3.4*u,'#7a2e38');
  box(W*.12-.4*u,H*.4,.8*u,H*.52,'#1d120d');poly('#f3c77e',W*.12-5*u,H*.4,W*.12+5*u,H*.4,W*.12+3*u,H*.4-7*u,W*.12-3*u,H*.4-7*u);
  for(const a of [-1.1,-.6,-.15,.3,.8])oval(W*.88+Math.sin(a)*6*u,H*.8-Math.cos(a)*7*u,2.2*u,6.5*u,'#3f7a4a',a);
  poly('#c26b3c',W*.88-4*u,H*.9,W*.88+4*u,H*.9,W*.88+5*u,H*.8,W*.88-5*u,H*.8);
  line('#2a1a12',1.2*u,X+4*u,Y+h+8*u,X+u,H*.92);line('#2a1a12',1.2*u,X+w-4*u,Y+h+8*u,X+w-u,H*.92);
  line('#c9c2b8',.5*u,cx-9*u,Y-17*u,cx,Y-4*u,cx+8*u,Y-18*u);
  box(X-5*u,Y-4*u,w+10*u,h+12*u,lin(0,Y,0,Y+h,'#a06c3f','#6d4425'),2.5*u);frame(1.6*u,'#141418',1.4*u);hole();
  for(const k of [1,2])oval(X+w-k*4.5*u,Y+h+4.8*u,1.3*u,1.3*u,'#e2cfa3');
  for(let k=0;k<4;k++)box(X+2*u,Y+h+3*u+k*.9*u,10*u,.4*u,'#4a2c18');
}
// 4: a desk at night: a laptop, a desk lamp, notes on the wall, a shelf of books and a mug
function desk(soft){
  if(soft){box(0,0,W,H,lin(0,0,0,H,'#1c2c4d','#111a30'));light(X-16*u,Y+h,34*u,'rgba(255,214,150,.3)');return;}
  for(const [dx,dy,c] of [[-31,-4,'#ffd65c'],[-22,3,'#ff8fab'],[-31,8,'#7fd6c2']])box(X+dx*u,Y+dy*u,7.5*u,7.5*u,c);
  box(X+w+8*u,Y+h*.35,30*u,1.2*u,'#3a4b70');
  ['#e46a5e','#f2c14e','#5aa9e6','#9b7fd1','#58c08f','#f08a4b'].forEach((c,k)=>{const bh=(9+k%3*2)*u;box(X+w+9*u+k*3.4*u,Y+h*.35-bh,2.8*u,bh,c);});
  const top=Y+h+.5*u;box(0,top,W,H-top,lin(0,top,0,H,'#5e4634','#3a2a1f'));
  line('#cfd5e2',.9*u,X-16*u,Y+h+4*u,X-21*u,Y+h*.35,X-12*u,Y+h*.1);oval(X-16*u,Y+h+4*u,4*u,1.2*u,'#cfd5e2');
  poly('#cfd5e2',X-12*u,Y+h*.1-u,X-7*u,Y+h*.1+6*u,X-15*u,Y+h*.1+7*u);
  frame(2.2*u,'#0e1118',1.8*u);hole();oval(cx,Y-1.1*u,.35*u,.35*u,'#3a4458');
  poly(lin(0,Y+h+2.2*u,0,Y+h+6*u,'#c9cfdb','#7c8496'),X-2.2*u,Y+h+2.2*u,X+w+2.2*u,Y+h+2.2*u,X+w+7*u,Y+h+6*u,X-7*u,Y+h+6*u);
  box(X+w+12*u,Y+h+3*u,6*u,7*u,'#e9e4da',.8*u);
  ctx.strokeStyle='#e9e4da';ctx.lineWidth=u;ctx.beginPath();ctx.arc(X+w+18*u,Y+h+6.5*u,2*u,-1.4,1.4);ctx.stroke(); // its handle
}
// 5: a photo on a wooden table, seen from above, beside other photos, a cup of coffee and a camera
function table(soft){
  if(soft){box(0,0,W,H,lin(0,0,W,H,'#7a4d2c','#4b2c17'));return;}
  for(let k=1;k<7;k++)box(0,H*k/7,W,.35*u,'rgba(30,15,5,.35)');
  for(const [px,py,a,c] of [[.1,.22,-.35,'#5aa9e6'],[.9,.8,.4,'#e9a23b'],[.88,.18,.25,'#58c08f']]){
    ctx.save();ctx.translate(W*px,H*py);ctx.rotate(a);box(-11*u,-12*u,22*u,26*u,'#f1ece2',.5*u);box(-9*u,-10*u,18*u,18*u,c);ctx.restore();}
  const cx=X-17*u,cy=Y+h*.95;oval(cx,cy,8*u,8*u,'#efe9df');oval(cx,cy,6.2*u,6.2*u,'#5a3420');box(cx+7.5*u,cy-1.2*u,4*u,2.4*u,'#efe9df',u);
  box(X+w+8*u,Y+h*.1,24*u,15*u,'#1d1d22',2*u);oval(X+w+20*u,Y+h*.1+7.5*u,5*u,5*u,'#3a3a44');oval(X+w+20*u,Y+h*.1+7.5*u,3*u,3*u,'#0d0d12');
  box(X-2.2*u,Y-1.6*u,w+6*u,h+13*u,'rgba(0,0,0,.35)',.6*u);box(X-3*u,Y-3*u,w+6*u,h+13*u,'#f5f1e8',.6*u);hole();
  const a=w*.55/(14*Math.PI),cap=[]; // the caption: seven joined loops of different heights on one baseline, like handwriting
  for(let t=0;t<=14*Math.PI;t+=.3)cap.push(X+w*.22+a*t-a*(1.9+.3*Math.sin(t*.53))*Math.sin(t),
    Y+h+7.5*u-a*(3.2+.7*Math.sin(t*.37+2))*((1+Math.cos(t))/2)**1.6);
  line('#3b4f8a',.45*u,...cap);
}
const SCENES=[gallery,drivein,livingroom,desk,table];
// Each picture's soft light (the shading of its wall, sky or table and the glow of its lamps) changes slowly across it, so it is drawn
// once per resize at a quarter of the stage's size and stretched to fit every frame: copying a picture costs far less than a gradient
const mk=()=>SCENES.map(()=>document.createElement('canvas')),SOFT=mk(),PAIR=[mk(),mk()],FLAT=['#134250','#1b1648','#743b26','#18253f','#643d22'];
function soften(set){
  set.forEach((c,i)=>{c.width=Math.ceil(W/4);c.height=Math.ceil(H/4);ctx=c.getContext('2d');ctx.scale(c.width/W,c.height/H);SCENES[i](true);});
  ctx=main;
}

// One picture of the zoom. Picture `at` fills the view at p = 0 and is drawn R^-p times its size; each picture inside it is drawn
// R times the size of the one around it, from the outside in, so each fills the window of the one before. At p = 1 the next
// picture fills the view at exactly its own size, which is where it starts as picture `at`: nothing jumps.
function render(){
  if(mode==='compare')return compare();
  // The pictures tile the canvas, but their edges are soft, so they go over a dark fill: no frame mixes in the one before it
  ctx.setTransform(1,0,0,1,0,0);ctx.fillStyle='#0c0c10';ctx.fillRect(0,0,cvs.width,cvs.height);
  if(mode==='inside')return inside();
  const seen=chain(1,p,0,0,SOFT,3);
  if(EDGES)edges(seen,0,0,false);
}
// The pictures from the outside in: picture j is drawn at base * R^(j - pp) about the middle of the box whose top left is (ox, oy),
// from the soft lights `soft`; one under `cut` pixels is left out, and no more than `max` are drawn. Each is clipped to its own
// rectangle, less the window the next fills. A chain that `max` cuts short fills its last window with the next picture's flat color,
// at that picture's own size about the middle of the box, so the window is not left dark
function chain(base,pp,ox,oy,soft,cut,max=9){
  const seen=[];for(let j=0;j<max;j++){const s=base*Math.pow(R,j-pp);if(s*Math.max(W,H)<cut)break;seen.push([s,(at+j)%COUNT]);}
  const fill=max<9&&seen.length===max;
  seen.forEach(([s,n],j)=>{
    ctx.setTransform(kx*s,0,0,ky*s,kx*W/2*(1-s)+kx*ox,ky*H/2*(1-s)+ky*oy);
    ctx.save();ctx.beginPath();ctx.rect(0,0,W,H);if(j<seen.length-1)ctx.rect(X,Y,w,h);ctx.clip('evenodd');
    if(phone.matches)box(0,0,W,H,FLAT[n]);else ctx.drawImage(soft[n],0,0,W,H);
    SCENES[n]();
    if(fill&&j===seen.length-1)box(W*(1-R)/2,H*(1-R)/2,W*R,H*R,FLAT[(n+1)%COUNT]);
    ctx.restore();
  });
  if(fill){const [s,n]=seen[max-1];seen.push([s*R,(n+1)%COUNT]);}
  return seen;
}
// Each picture's edge, dashed over a dark line, with its number in the chain (at its right corner in See inside, where "Your view" rides
// the left). save() and restore() undo the dash, the widths and the font afterwards, so the scenes' own lines are never drawn dashed
function edges(seen,ox,oy,right){
  ctx.save();ctx.setTransform(kx,0,0,ky,kx*ox,ky*oy);ctx.font='700 13px "Schibsted Grotesk",system-ui,sans-serif';ctx.textAlign='center';
  for(const [s,n] of seen){
    const x=W/2*(1-s),y=H/2*(1-s);
    ctx.setLineDash([]);ctx.lineWidth=4;ctx.strokeStyle='rgba(0,0,0,.45)';ctx.strokeRect(x,y,W*s,H*s);
    ctx.setLineDash([7,5]);ctx.lineWidth=2;ctx.strokeStyle=EDGE;ctx.strokeRect(x,y,W*s,H*s);
    if(H*s>60){const bx=right?x+W*s-30:x+6;box(bx,y+6,24,22,'rgba(8,10,14,.85)',5);ctx.fillStyle=EDGE;ctx.fillText(n+1,bx+12,y+22);}
  }
  ctx.restore();
}

// See inside: picture `at` held still at K of the box with the pictures it holds, and your view (the box) as a dashed outline that
// shrinks from its edge to its window as p grows (grows, zooming out). Where p wraps, the pictures are drawn one picture in (out) and
// the outline starts again: the swap Normal hides. Faint outlines mark where the view starts and ends. Under reduced motion, paused,
// it is posed half way, with an arrow as long as the move.
const reduceMQ=matchMedia('(prefers-reduced-motion:reduce)'),K=.8,still=()=>reduceMQ.matches&&paused;
const ring=(s,c,lw,dash)=>{ctx.setLineDash(dash);ctx.lineWidth=lw;ctx.strokeStyle=c;ctx.strokeRect(W/2*(1-s)-2,H/2*(1-s)-2,W*s+4,H*s+4);};
function inside(){
  const s=K*Math.pow(R,still()?.5:p),vx=Math.max(4,Math.min(W/2*(1-s)-2,SW-vw-4)),vy=Math.max(2,H/2*(1-s)-7-ch);
  put(cV,vx,vy); // "Your view" rides the outline's top left corner, above it
  const seen=chain(K,0,0,0,SOFT,3);
  // The edges, the faint outlines and the arrow are left out under the caption, so it covers none of them
  ctx.save();ctx.setTransform(kx,0,0,ky,0,0);ctx.beginPath();ctx.rect(0,0,W,H);ctx.rect(vx-3,vy-3,vw+6,ch+6);ctx.clip('evenodd');
  if(EDGES)edges(seen,0,0,true);
  ctx.lineCap='round';ring(K,'rgba(255,255,255,.5)',1.5,[3,4]);ring(K*R,'rgba(255,255,255,.5)',1.5,[3,4]);
  if(still()){
    const a=W/2*(1-K)-2,b=W/2*(1-K*R)-2,y=H/2,[f,t]=DIR>0?[a,b]:[b,a],d=t>f?-9:9;
    for(const [c,lw] of [['rgba(0,0,0,.6)',6],[VIEW,3]]){ctx.setLineDash([]);ctx.strokeStyle=c;ctx.lineWidth=lw;ctx.beginPath();ctx.moveTo(f,y);ctx.lineTo(t,y);ctx.moveTo(t+d,y-6);ctx.lineTo(t,y);ctx.lineTo(t+d,y+6);ctx.stroke();}
  }
  ctx.restore();
  ctx.save();ctx.setTransform(kx,0,0,ky,0,0);ring(s,'rgba(0,0,0,.6)',5,[]);ring(s,VIEW,2.5,[9,6]);ctx.restore();
}

// Compare: the chain twice, in two boxes side by side under their names (a band tall), each half clipped to its box. The globals the
// pictures read are set for each half and put back after. In the first half every window, frame and clip sits off the middle, while the
// next picture is still shrunk about the middle, as the page does it: it misses its window. Phones draw three pictures a frame in each half
// (together no more than Normal draws in the whole box), the last window filled with the next picture's flat color.
const OFF=[[.12,.1],[0,0]],keep=()=>[W,H,u,X,Y,w,h,cx,cy],back=g=>{[W,H,u,X,Y,w,h,cx,cy]=g;};
let band=28,pairKey='';
function compare(){
  ctx.setTransform(1,0,0,1,0,0);ctx.fillStyle='#0c0c10';ctx.fillRect(0,0,cvs.width,cvs.height);
  const bw=(SW-2)/2,bh=SH-band,g=keep();
  for(let i=0;i<2;i++){
    const ox=i*(bw+2);geom(bw,bh,...OFF[i]);
    ctx.save();ctx.setTransform(1,0,0,1,0,0);ctx.beginPath();ctx.rect(kx*ox,ky*band,kx*bw,ky*bh);ctx.clip();
    const seen=chain(1,p,ox,band,PAIR[i],3,phone.matches?3:9);
    if(EDGES)edges(seen,ox,band,false);
    ctx.restore();
  }
  back(g);ctx.fillStyle='#2a2b33';ctx.fillRect(kx*bw,0,kx*2,cvs.height);
}
// Each half's soft lights are drawn for its own box (and window) when Compare is shown, and again when that box changes
function softPair(){
  const key=SW+'x'+SH+'x'+band,g=keep();if(key===pairKey)return;pairKey=key;
  PAIR.forEach((set,i)=>{geom((SW-2)/2,SH-band,...OFF[i]);soften(set);});
  back(g);
}

// The captions: plain words, put in place from their measured sizes. Margin captions take shorter words where the long ones do not fit
const el=id=>document.getElementById(id),cV=el('c-view'),cT=el('c-top'),cB=el('c-bot'),cL=el('c-left'),cR=el('c-right');
const TOP=[['Reaching the window, it starts over one picture in','At the window it starts over','Restarts at the window'],['Reaching the edge, it starts over one picture out','At the edge it starts over','Restarts at the edge']];
const BOT=['Each picture holds the next, two fifths the size','Each holds the next, two fifths the size'];
const put=(e,x,y)=>{e.style.transform='translate('+Math.round(x)+'px,'+Math.round(y)+'px)';};
const fit=(e,words,room)=>{for(const t of words){e.textContent=t;if(e.offsetWidth<=room)break;}};
let ch=18,vw=60; // the height of a caption, the width of "Your view"
function layout(){
  if(mode==='inside'){
    ch=cV.offsetHeight;vw=cV.offsetWidth;
    // "Your view" can meet the top caption until its corner is a caption lower: the top caption stays right of where it gets to then
    const s0=1-2*(2*ch+14)/SH,xr=Math.max(SW*.1-2,SW/2*(1-s0)-2)+vw+8;
    fit(cT,TOP[DIR>0?0:1],SW-8-xr);fit(cB,BOT,SW-12);
    const c=(SW-cT.offsetWidth)/2;
    put(cT,c>=xr?c:SW-cT.offsetWidth-8,2);put(cB,(SW-cB.offsetWidth)/2,SH-ch-2);
  }else if(mode==='compare'){
    band=cL.offsetHeight+8;softPair();
    const bw=(SW-2)/2;put(cL,(bw-cL.offsetWidth)/2,4);put(cR,bw+2+(bw-cR.offsetWidth)/2,4);
  }
}

// The notes and the picture's name follow the mode, the direction and reduced motion (still: paused under reduced motion, when the
// views hold a pose). A note is written only when its words change, as its box is read out loud.
const notes=[...document.querySelectorAll('[data-hb-for]')],moving=notes.map(n=>n.textContent),NAME=cvs.getAttribute('aria-label');
const SAY=[[[moving[0],'Shown still: your view (dashed) is moving into a picture whose middle holds the next one; on reaching that window, the drawing starts over one picture in, which looks the same.'],
  ["Inside, your view (dashed) moves out from a picture's middle window toward its edge; when it reaches the edge, the drawing starts over one picture out, which looks exactly the same.",
   "Shown still: your view (dashed) is moving out from a picture's middle window; on reaching the edge, the drawing starts over one picture out, which looks the same."]],
  [moving[1],'Shown still: in Off-center window the next picture misses its frame and leaves a gap; in Centered window each picture fits the next exactly.']];
function describe(){
  const s=+still(),want=[SAY[0][DIR>0?0:1][s],SAY[1][s]];
  notes.forEach((n,i)=>{if(n.textContent!==want[i])n.textContent=want[i];});
  const name=mode==='inside'?'The nested pictures held still at a smaller size, with a dashed outline for your view moving '+(DIR>0?'into the middle picture and starting over one picture in':'out toward the edge and starting over one picture out')
    :mode==='compare'?'Two zooms side by side: on the left each window sits off the middle, so the next picture misses its frame; on the right each picture fits the next exactly':NAME;
  if(cvs.getAttribute('aria-label')!==name)cvs.setAttribute('aria-label',name);
}
function update(){describe();if(mode!=='normal')layout();render();}
// At the top level, before the shared script runs: a mode draws at once, playing or paused, and changes nothing the page keeps
document.addEventListener('hb:mode',e=>{mode=e.detail.mode;update();});

// loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. At most one frame per 16 ms
// zooms by the time since the last one (at most 50 ms, a third in slow motion); a first frame after a start only takes the time.
function loop(ts){
  raf=requestAnimationFrame(loop);
  if(lastT!==null&&ts-lastT<16)return;
  const dt=lastT===null?0:Math.min(ts-lastT,50)*(slowTog.checked?1/3:1);
  lastT=ts;clock+=dt/1000;p+=DIR*dt/1000/SECS;
  while(p>=1){p--;at=(at+1)%COUNT;} // the next picture now fills the view: it becomes picture `at`
  while(p<0){p++;at=(at+COUNT-1)%COUNT;} // zooming out, the picture around it does
  render();
}
function start(){if(raf)return;lastT=null;raf=requestAnimationFrame(loop);}

// At the top level, before the deferred shared script runs: it pauses on arrival under reduced motion, and its Play starts the
// loop. A paused picture stays as it is.
document.addEventListener('hb:pause',e=>{
  paused=e.detail.paused;
  if(paused){cancelAnimationFrame(raf);raf=null;}else start();
  if(mode!=='normal')update(); // reduced motion: the views hold a still pose while paused
});
// A tab that comes back after being hidden has no earlier frame to measure from, so its first frame zooms by nothing
document.addEventListener('visibilitychange',()=>{lastT=null;});

// Setting the canvas size wipes it, so a real change of the stage's size (or of the phone limit) draws the picture 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 sw=stage.clientWidth,sh=stage.clientHeight,r=Math.min(devicePixelRatio||1,phone.matches?1.5:2);
  if(sw===SW&&sh===SH&&r===dpr)return;
  SW=sw;SH=sh;dpr=r;geom(SW,SH,0,0);
  cvs.width=Math.round(W*dpr);cvs.height=Math.round(H*dpr);kx=cvs.width/W;ky=cvs.height/H;soften(SOFT);
  if(mode!=='normal')layout();
  render(); // the whole canvas, to the last pixel
}

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);
  }));
}
// Speed and Direction change the zoom from where it is; Number of scenes and the edges show at once, paused too
choices('speed-seg',d=>{SECS=+d.secs;});
choices('count-seg',d=>{COUNT=+d.n;at%=COUNT;render();});
choices('dir-seg',d=>{DIR=+d.dir;if(mode!=='normal')update();});
document.getElementById('edges-tog').addEventListener('change',e=>{EDGES=e.target.checked;render();});

// The captions' sizes depend on the font, so they are placed again once it has arrived
if(document.fonts)document.fonts.ready.then(()=>{if(mode!=='normal'){layout();render();}});
resize();
window.addEventListener('resize',resize);
// A window that crosses the phone size changes how the walls are drawn, even when the stage keeps its size and pixel ratio
const redo=()=>{SW=0;resize();};if(phone.addEventListener)phone.addEventListener('change',redo);else if(phone.addListener)phone.addListener(redo);
if(!matchMedia('(prefers-reduced-motion:reduce)').matches)start(); // under reduced motion the first picture stays still until Play
</script>
</body>
</html>