@kashvi公開版 1

Little mischief

公開版 1部品 · スライダー

Kashvi部品 · スライダー見られた数 11コピーされた数 0いいね 0

元の作品:住人のいる呼び鈴(@oasis)

MIT · コードの1行目に作者の名前が残ります

この作品について

機械がコードを読んで出しています。印を押すと、その行が見られます。

シェーダー
2 本
外から読む物
なし
大きさ
68.5 KB3 ファイル3,174 行

キー操作と読み上げコードに書いてある物です。

役目
status
フォーカスの枠
:focus-visible
知らせ
polite

言語の割合

  • JavaScript 90.4%
  • GLSL 6.3%
  • CSS 2.7%
  • HTML 0.6%
<!-- UI by @kashvi — ludion.ai/p/button-resident-01c5 (MIT) -->
<!-- Remix of UI by @oasis — ludion.ai/p/button-resident (MIT) -->
<div data-ld-part="button-resident-01c5" lang="ja">
  <span class="base" aria-hidden="true"></span>
  <canvas class="scene" aria-hidden="true"></canvas>
  <button class="hit" type="button" aria-label="Slide quickly to help the kids escape"></button>
  <span class="vh" role="status" aria-live="polite" data-status></span>
  <span class="probe" aria-hidden="true"></span>
</div>
<style>
/* UI by @kashvi — ludion.ai/p/button-resident-01c5 (MIT) */
/* Remix of UI by @oasis — ludion.ai/p/button-resident (MIT) */
[data-ld-part="button-resident-01c5"] {
  --_bg: var(--ld-bg, #ece4d6);
  --_surface: var(--ld-surface, #7d9a5b);
  --_fg: var(--ld-fg, #1d2140);
  --_muted: var(--ld-muted, #8f95bd);
  --_accent: var(--ld-accent, #f0a43a);
  --_radius: var(--ld-radius, 9999px);
  --_font: var(--ld-font, system-ui, sans-serif);
  --_space: var(--ld-space, 8px);
  --_shadow: var(--ld-shadow, 0 18px 40px rgb(29 33 64 / .22), 0 3px 8px rgb(29 33 64 / .12));
  --_motion: var(--ld-motion, 220ms);

  position: relative;
  display: block;
  box-sizing: border-box;
  width: min(100%, calc((100vh - 48px) * 1.8));
  aspect-ratio: 1.8;
  font-family: var(--_font);
  color: var(--_fg);
  -webkit-tap-highlight-color: transparent;
}

[data-ld-part="button-resident-01c5"] .base {
  position: absolute;
  left: 7%;
  top: 20%;
  width: 86%;
  height: 60%;
  border-radius: var(--_radius);
  background: var(--_bg);
  box-shadow: var(--_shadow);
}

[data-ld-part="button-resident-01c5"] .scene {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

[data-ld-part="button-resident-01c5"] .hit {
  position: absolute;
  left: 7%;
  top: 20%;
  width: 86%;
  height: 60%;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: var(--_radius);
  background: transparent;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

[data-ld-part="button-resident-01c5"] .hit:focus {
  outline: none;
}

[data-ld-part="button-resident-01c5"] .hit:focus-visible {
  outline: 2px solid var(--_accent);
  outline-offset: calc(var(--_space) * .5);
}

[data-ld-part="button-resident-01c5"] .vh {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

[data-ld-part="button-resident-01c5"] .probe {
  display: none;
}
</style>
<script type="module">
/* UI by @kashvi — ludion.ai/p/button-resident-01c5 (MIT) */
/* Remix of UI by @oasis — ludion.ai/p/button-resident (MIT) */
function mount(root) {
  const doc = root.ownerDocument;
  const win = doc.defaultView;
  const canvas = root.querySelector('.scene');
  const base = root.querySelector('.base');
  const hit = root.querySelector('.hit');
  const status = root.querySelector('[data-status]');
  const probe = root.querySelector('.probe');

  const reduceQuery = win.matchMedia('(prefers-reduced-motion: reduce)');
  let reduce = reduceQuery.matches;

  /* ---------- Color palette ---------- */

  const swatch = doc.createElement('canvas');
  swatch.width = swatch.height = 1;
  const swatchCtx = swatch.getContext('2d', { willReadFrequently: true });

  function readToken(name) {
    probe.style.color = `var(--_${name})`;
    const css = win.getComputedStyle(probe).color;

    swatchCtx.clearRect(0, 0, 1, 1);
    swatchCtx.fillStyle = css;
    swatchCtx.fillRect(0, 0, 1, 1);

    const d = swatchCtx.getImageData(0, 0, 1, 1).data;

    return [d[0] / 255, d[1] / 255, d[2] / 255];
  }

  const toLinear = (v) => (
    v <= 0.04045
      ? v / 12.92
      : Math.pow((v + 0.055) / 1.055, 2.4)
  );

  const toGamma = (v) => (
    v <= 0.0031308
      ? v * 12.92
      : 1.055 * Math.pow(v, 1 / 2.4) - 0.055
  );

  const clamp01 = (v) => Math.min(1, Math.max(0, v));

  function toLab(c) {
    const r = toLinear(c[0]);
    const g = toLinear(c[1]);
    const b = toLinear(c[2]);

    const l = Math.cbrt(
      0.4122214708 * r +
      0.5363325363 * g +
      0.0514459929 * b
    );

    const m = Math.cbrt(
      0.2119034982 * r +
      0.6806995451 * g +
      0.1073969566 * b
    );

    const s = Math.cbrt(
      0.0883024619 * r +
      0.2817188376 * g +
      0.6299787005 * b
    );

    return [
      0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s,
      1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s,
      0.0259040371 * l + 0.7827717662 * m - 0.8086755480 * s
    ];
  }

  function fromLab(o) {
    const l = Math.pow(
      o[0] + 0.3963377774 * o[1] + 0.2158037573 * o[2],
      3
    );

    const m = Math.pow(
      o[0] - 0.1055613458 * o[1] - 0.0638541728 * o[2],
      3
    );

    const s = Math.pow(
      o[0] - 0.0894841775 * o[1] - 1.2914855480 * o[2],
      3
    );

    return [
      clamp01(toGamma(
        4.0767416621 * l -
        3.3077115913 * m +
        0.2309699292 * s
      )),

      clamp01(toGamma(
        -1.2684380046 * l +
        2.6097574011 * m -
        0.3413193965 * s
      )),

      clamp01(toGamma(
        -0.0041960863 * l -
        0.7034186147 * m +
        1.7076147010 * s
      ))
    ];
  }

  function mix(a, b, t) {
    const x = toLab(a);
    const y = toLab(b);

    return fromLab([
      x[0] + (y[0] - x[0]) * t,
      x[1] + (y[1] - x[1]) * t,
      x[2] + (y[2] - x[2]) * t
    ]);
  }

  function light(c, dL) {
    const x = toLab(c);
    return fromLab([x[0] + dL, x[1], x[2]]);
  }

  const pack = (c) => (
    (
      (255 << 24) |
      (Math.round(c[2] * 255) << 16) |
      (Math.round(c[1] * 255) << 8) |
      Math.round(c[0] * 255)
    ) >>> 0
  );

  const unpack = (v) => [
    v & 255,
    (v >>> 8) & 255,
    (v >>> 16) & 255
  ];

  let RGB = {};
  let P = {};
  let tokens = {};

  function buildPalette() {
    const bg = readToken('bg');
    const sf = readToken('surface');
    const fg = readToken('fg');
    const mu = readToken('muted');
    const ac = readToken('accent');

    tokens = { bg, sf, fg, mu, ac };

    const C = {};

    C.ink = light(fg, -0.02);
    C.skyTop = mix(fg, mu, 0.26);
    C.skyMid = mix(fg, mu, 0.56);
    C.skyLow = mix(mu, ac, 0.3);
    C.horizon = light(mix(ac, bg, 0.42), 0.02);
    C.cloudTop = mix(C.skyMid, bg, 0.24);
    C.cloudLit = mix(C.skyLow, ac, 0.5);
    C.star = light(mix(bg, mu, 0.1), 0.05);
    C.moon = light(mix(bg, ac, 0.16), 0.06);
    C.smoke = mix(C.skyMid, bg, 0.32);
    C.hillFar = mix(fg, mu, 0.42);
    C.hillFarRim = mix(C.hillFar, C.horizon, 0.38);
    C.hillNear = mix(fg, sf, 0.32);
    C.townLight = light(mix(ac, bg, 0.2), 0.05);
    C.leafDark = mix(sf, fg, 0.64);
    C.leafMid = mix(sf, fg, 0.44);
    C.leafLight = mix(sf, mu, 0.3);
    C.trunk = mix(ac, fg, 0.72);
    C.trunkDark = mix(ac, fg, 0.86);
    C.grass = mix(sf, fg, 0.5);
    C.grassDark = mix(sf, fg, 0.68);
    C.grassLight = mix(sf, mu, 0.38);
    C.fence = light(mix(bg, mu, 0.35), -0.14);
    C.fenceDark = light(C.fence, -0.12);
    C.stone = light(mix(bg, mu, 0.45), -0.16);
    C.stoneLight = mix(C.stone, bg, 0.32);
    C.stoneDark = light(C.stone, -0.12);
    C.mailbox = mix(mu, fg, 0.38);
    C.mailboxLight = mix(C.mailbox, bg, 0.28);
    C.flag = mix(ac, fg, 0.12);
    C.roof = mix(fg, mu, 0.22);
    C.roofLight = mix(C.roof, mu, 0.36);
    C.roofDark = light(C.roof, -0.06);
    C.trim = light(bg, -0.08);
    C.trimDark = light(bg, -0.24);
    C.brick = mix(ac, fg, 0.56);
    C.brickDark = light(C.brick, -0.08);
    C.wall = light(mix(bg, mu, 0.3), -0.18);
    C.wallLine = light(C.wall, -0.07);
    C.wallDark = light(C.wall, -0.12);
    C.door = mix(ac, fg, 0.58);
    C.doorLight = mix(C.door, ac, 0.26);
    C.doorDark = light(C.door, -0.07);
    C.knob = light(ac, 0.08);
    C.glow = light(mix(ac, bg, 0.45), 0.04);
    C.glowDeep = mix(ac, bg, 0.12);
    C.curtain = mix(bg, ac, 0.32);
    C.curtainDark = light(C.curtain, -0.1);
    C.floorIn = mix(ac, fg, 0.42);
    C.pot = mix(ac, fg, 0.42);
    C.potLight = mix(C.pot, ac, 0.32);
    C.lamp = light(mix(ac, bg, 0.3), 0.1);
    C.lampLight = light(mix(ac, bg, 0.12), 0.06);
    C.flowerA = light(mix(ac, bg, 0.12), 0.02);
    C.flowerB = mix(mu, bg, 0.38);
    C.flowerC = light(bg, 0.02);
    C.flowerD = mix(ac, mu, 0.48);
    C.cat = light(mix(fg, mu, 0.1), -0.03);
    C.catLight = mix(C.cat, mu, 0.42);
    C.catEye = light(mix(ac, sf, 0.32), 0.1);
    C.hair = mix(fg, ac, 0.24);
    C.skin = mix(bg, ac, 0.3);
    C.shirt = light(mix(sf, bg, 0.3), 0.03);
    C.shirtDark = light(C.shirt, -0.1);
    C.pants = mix(fg, mu, 0.36);
    C.shoe = fg;
    C.bubble = light(bg, 0.05);
    C.bellLit = light(ac, 0.12);
    C.mat = mix(ac, fg, 0.46);
    C.matDark = light(C.mat, -0.08);
    C.firefly = light(mix(ac, sf, 0.35), 0.12);
    C.moth = light(mix(bg, mu, 0.2), 0.02);
    C.bird = mix(fg, mu, 0.45);
    C.birdBreast = light(mix(ac, fg, 0.18), 0.02);
    C.reflect = light(mix(bg, mu, 0.2), 0.08);

    RGB = C;
    P = {};

    for (const k of Object.keys(C)) {
      P[k] = pack(C[k]);
    }
  }

  /* ---------- Pixel scene ---------- */

  const SW = 200;
  const SH = 70;
  const M = 10;
  const TW = SW + 2 * M;
  const TH = SH + 2 * M;

  const baseLayer = new Uint32Array(TW * TH);
  const pix = new Uint32Array(TW * TH);
  const at = (x, y) => (y + M) * TW + (x + M);
  const pixBytes = new Uint8Array(pix.buffer);

  let target = pix;
  let clipBox = null;

  const BAYER = [
    0, 8, 2, 10,
    12, 4, 14, 6,
    3, 11, 1, 9,
    15, 7, 13, 5
  ];

  const bay = (x, y) => (
    (BAYER[(y & 3) * 4 + (x & 3)] + 0.5) / 16
  );

  function put(x, y, c) {
    x |= 0;
    y |= 0;

    if (
      x < -M ||
      y < -M ||
      x >= SW + M ||
      y >= SH + M
    ) return;

    if (
      clipBox &&
      (
        x < clipBox[0] ||
        y < clipBox[1] ||
        x > clipBox[2] ||
        y > clipBox[3]
      )
    ) return;

    target[at(x, y)] = c;
  }

  function fill(x, y, w, h, c) {
    for (let j = 0; j < h; j++) {
      for (let i = 0; i < w; i++) {
        put(x + i, y + j, c);
      }
    }
  }

  function tintPixel(x, y, rgb, t) {
    if (
      x < -M ||
      y < -M ||
      x >= SW + M ||
      y >= SH + M ||
      t <= 0
    ) return;

    const i = at(x, y);
    const c = unpack(target[i]);

    const r = c[0] + (rgb[0] * 255 - c[0]) * t;
    const g = c[1] + (rgb[1] * 255 - c[1]) * t;
    const b = c[2] + (rgb[2] * 255 - c[2]) * t;

    target[i] = (
      (255 << 24) |
      (Math.round(b) << 16) |
      (Math.round(g) << 8) |
      Math.round(r)
    ) >>> 0;
  }

  function glow(cx, cy, r, strength, rgb, yMin = -M, steps = 4) {
    const x0 = Math.max(-M, Math.floor(cx - r));
    const x1 = Math.min(SW + M - 1, Math.ceil(cx + r));
    const y0 = Math.max(yMin, Math.floor(cy - r));
    const y1 = Math.min(SH + M - 1, Math.ceil(cy + r));

    for (let y = y0; y <= y1; y++) {
      for (let x = x0; x <= x1; x++) {
        const d = Math.hypot(
          x + 0.5 - cx,
          (y + 0.5 - cy) * 1.15
        ) / r;

        if (d >= 1) continue;

        const a = strength * (1 - d) * (1 - d);
        const level = Math.floor(a * steps + bay(x, y)) / steps;

        if (level > 0) {
          tintPixel(x, y, rgb, Math.min(0.85, level));
        }
      }
    }
  }

  function makeRandom(seed) {
    let s = seed >>> 0 || 1;

    return () => {
      s ^= s << 13;
      s >>>= 0;
      s ^= s >>> 17;
      s >>>= 0;
      s ^= s << 5;
      s >>>= 0;

      return s / 4294967296;
    };
  }

  const farHillY = (x) => Math.round(
    39 +
    2.5 * Math.sin(x * 0.05 + 0.6) +
    1.5 * Math.sin(x * 0.13 + 2)
  );

  const nearHillY = (x) => Math.round(
    46 +
    1.6 * Math.sin(x * 0.08 + 1.3) +
    1.2 * Math.sin(x * 0.21) -
    1.5 * Math.abs(Math.sin(x * 0.52))
  );

  const roofLeft = (y) => Math.round(91 + (26 - y) * 1.4);

  let stars = [];
  let townLights = [];
  let fireflies = [];
  let boxFlowers = [];

  function drawBase() {
    target = baseLayer;
    clipBox = null;

    const rand = makeRandom(20261112);

    const stops = [
      RGB.skyTop,
      RGB.skyMid,
      RGB.skyLow,
      RGB.horizon
    ];

    const bands = [];

    for (let i = 0; i < 3; i++) {
      bands.push(
        pack(stops[i]),
        pack(mix(stops[i], stops[i + 1], 0.5))
      );
    }

    bands.push(pack(stops[3]));

    for (let y = -M; y < SH + M; y++) {
      const t = Math.min(1, Math.max(0, y / 44)) * (bands.length - 1);
      const i = Math.min(bands.length - 2, Math.floor(t));
      const f = t - i;

      for (let x = -M; x < SW + M; x++) {
        put(
          x,
          y,
          bay(x, y) < f ? bands[i + 1] : bands[i]
        );
      }
    }

    const streak = (x0, x1, y, c) => {
      for (let x = x0; x <= x1; x++) {
        const edge = Math.min(x - x0, x1 - x);

        if (edge < 3 && rand() < 0.45) continue;

        put(x, y, c);
      }
    };

    streak(36, 58, 17, P.cloudTop);
    streak(29, 70, 18, P.cloudLit);
    streak(42, 63, 19, P.cloudLit);
    streak(118, 138, 8, P.cloudTop);
    streak(112, 150, 9, P.cloudLit);
    streak(122, 142, 10, P.cloudLit);

    glow(56.5, 9.5, 9, 0.35, RGB.moon, 0, 3);

    for (let dy = -6; dy <= 6; dy++) {
      for (let dx = -6; dx <= 6; dx++) {
        const inA = dx * dx + dy * dy <= 4.6 * 4.6;

        const inB =
          (dx - 2.2) * (dx - 2.2) +
          (dy + 1.2) * (dy + 1.2) <= 4.1 * 4.1;

        if (inA && !inB) {
          put(56 + dx, 9 + dy, P.moon);
        }
      }
    }

    for (let x = -M; x < SW + M; x++) {
      const hy = farHillY(x);

      for (let y = hy; y < SH + M; y++) {
        put(x, y, P.hillFar);
      }

      put(x, hy, P.hillFarRim);
    }

    townLights = [];

    for (const x of [6, 13, 31, 38, 44, 63, 70, 84]) {
      const y = farHillY(x) + 2 + (x % 3);

      if (y < nearHillY(x) - 1) {
        townLights.push([x, y, rand() * 6]);
      }
    }

    for (let x = -M; x < SW + M; x++) {
      for (let y = nearHillY(x); y < SH + M; y++) {
        put(x, y, P.hillNear);
      }
    }

    const blobs = [
      [16, 25, 9],
      [8, 29, 7],
      [25, 30, 7],
      [17, 17, 6.5],
      [10, 21, 5]
    ];

    for (let y = 6; y < 40; y++) {
      for (let x = 0; x < 36; x++) {
        let best = 9;
        let lit = 0;

        for (const [cx, cy, r] of blobs) {
          const d = Math.hypot(x - cx, y - cy) / r;

          if (d < best) {
            best = d;
            lit = ((x - cx) * -0.6 + (y - cy) * -0.8) / r;
          }
        }

        if (best > 1) continue;
        if (best > 0.86 && rand() < 0.4) continue;

        let c = P.leafDark;

        if (lit > 0.2 && bay(x, y) < (lit - 0.2) * 2) {
          c = P.leafMid;
        }

        if (lit > 0.6 && bay(x, y) < 0.5) {
          c = P.leafLight;
        }

        if (rand() < 0.04) {
          c = P.trunkDark;
        }

        put(x, y, c);
      }
    }

    for (let y = 55; y < SH + M; y++) {
      for (let x = -M; x < SW + M; x++) {
        const f = (y - 62) / 7;

        put(
          x,
          y,
          f > 0 && bay(x, y) < f
            ? P.grassDark
            : P.grass
        );
      }
    }

    for (const ry of [47, 51]) {
      fill(-M, ry, 93 + M, 1, P.fence);
      fill(-M, ry + 1, 93 + M, 1, P.fenceDark);
    }

    for (let x = -11; x < 92; x += 4) {
      fill(x, 45, 2, 11, P.fence);
      fill(x + 1, 45, 1, 11, P.fenceDark);
      put(x, 44, P.fence);
    }

    fill(15, 33, 4, 23, P.trunk);
    fill(18, 33, 1, 23, P.trunkDark);
    put(14, 54, P.trunk);
    put(19, 55, P.trunkDark);
    put(16, 40, P.trunkDark);
    put(16, 47, P.trunkDark);

    for (let x = -M; x < SW + M; x++) {
      if (rand() < 0.4) put(x, 54, P.grassLight);
      if (rand() < 0.2) put(x, 55, P.grassLight);
    }

    for (let i = 0; i < 260; i++) {
      const x = Math.floor(rand() * (SW + 2 * M)) - M;
      const y = 56 + Math.floor(rand() * (14 + M));

      put(
        x,
        y,
        rand() < 0.5 ? P.grassLight : P.grassDark
      );
    }

    const flowerSet = [
      P.flowerA,
      P.flowerB,
      P.flowerC,
      P.flowerD
    ];

    for (let i = 0; i < 34; i++) {
      const x = Math.floor(rand() * 94);
      const y = 57 + Math.floor(rand() * 12);

      put(x, y + 1, P.leafMid);
      put(x, y, flowerSet[i % 4]);
    }

    fill(73, 42, 2, 15, P.trunk);
    fill(74, 42, 1, 15, P.trunkDark);
    fill(70, 37, 8, 1, P.mailboxLight);
    fill(69, 38, 10, 4, P.mailbox);
    fill(69, 38, 1, 4, P.mailboxLight);
    fill(69, 42, 10, 1, P.stoneDark);
    fill(70, 40, 2, 1, P.ink);
    fill(79, 37, 1, 6, P.ink);
    fill(80, 37, 2, 2, P.flag);

    for (const t of [0, 0.2, 0.39, 0.57, 0.73, 0.87]) {
      const u = 1 - t;

      const cx =
        u * u * 58 +
        2 * u * t * 80 +
        t * t * 105;

      const cy =
        u * u * 68 +
        2 * u * t * 63 +
        t * t * 62;

      const rx = 4.6 - t * 1.6;
      const ry = 1.7 - t * 0.5;

      for (let y = Math.floor(cy - ry); y <= Math.ceil(cy + ry); y++) {
        for (let x = Math.floor(cx - rx); x <= Math.ceil(cx + rx); x++) {
          const d =
            ((x - cx) / rx) ** 2 +
            ((y - cy) / ry) ** 2;

          if (d > 1) continue;

          let c = P.stone;

          if (y < cy - ry * 0.3) c = P.stoneLight;
          if (y > cy + ry * 0.4) c = P.stoneDark;

          put(x, y, c);
        }
      }
    }

    fill(176, 6, 6, 8, P.brick);

    for (let y = 6; y < 14; y++) {
      for (let x = 176; x < 182; x++) {
        const row = y - 6;

        if (
          row % 3 === 2 ||
          (x + (Math.floor(row / 3) % 2) * 3) % 6 === 0
        ) {
          put(x, y, P.brickDark);
        }
      }
    }

    fill(175, 5, 8, 1, P.stoneDark);
    fill(176, 4, 6, 1, P.ink);

    for (let y = 14; y <= 26; y++) {
      const xl = roofLeft(y);

      for (let x = xl; x < SW + M; x++) {
        if (y === 14) {
          put(x, y, P.roofDark);
          continue;
        }

        const r = (y - 15) % 3;
        const course = Math.floor((y - 15) / 3);

        let c = r === 0 ? P.roofLight : P.roof;

        if (r !== 0 && (x + course * 3) % 6 === 0) {
          c = P.roofDark;
        }

        put(x, y, c);
      }

      put(xl, y, P.trimDark);
      put(xl + 1, y, P.trim);
    }

    fill(91, 27, SW + M - 91, 1, P.trim);
    fill(96, 28, SW + M - 96, 1, P.wallDark);

    for (let y = 29; y <= 53; y++) {
      for (let x = 96; x < SW + M; x++) {
        let c = (y - 29) % 3 === 2 ? P.wallLine : P.wall;

        if (y > 47 && bay(x, y) < (y - 47) / 9) {
          c = P.wallDark;
        }

        put(x, y, c);
      }
    }

    fill(96, 28, 2, 26, P.trim);
    fill(97, 28, 1, 26, P.trimDark);

    for (let y = 54; y <= 56; y++) {
      for (let x = 96; x < SW + M; x++) {
        put(
          x,
          y,
          y === 55 || (x + (y === 56 ? 3 : 0)) % 6 === 0
            ? P.stoneDark
            : P.stone
        );
      }
    }

    fill(189, 27, 2, 30, P.trimDark);
    fill(189, 27, 1, 30, P.trim);
    fill(188, 34, 4, 1, P.ink);
    fill(188, 45, 4, 1, P.ink);
    fill(189, 56, 4, 1, P.trimDark);

    fill(110, 33, 18, 2, P.trim);
    fill(110, 33, 2, 24, P.trim);
    fill(126, 33, 2, 24, P.trim);
    fill(127, 35, 1, 22, P.trimDark);
    fill(116, 30, 6, 2, P.trim);
    fill(116, 31, 6, 1, P.trimDark);
    fill(118, 30, 2, 1, P.doorDark);

    fill(129, 43, 3, 4, P.trim);
    fill(131, 43, 1, 4, P.trimDark);

    fill(105, 40, 3, 1, P.ink);
    fill(101, 38, 5, 1, P.ink);
    fill(101, 39, 1, 4, P.ink);
    fill(105, 39, 1, 4, P.ink);
    fill(102, 39, 3, 4, P.lamp);
    fill(101, 43, 5, 1, P.ink);
    put(103, 44, P.ink);
    put(103, 37, P.ink);

    fill(143, 31, 34, 2, P.trim);
    fill(143, 31, 2, 18, P.trim);
    fill(175, 31, 2, 18, P.trim);
    fill(176, 33, 1, 14, P.trimDark);
    fill(141, 47, 38, 1, P.trim);
    fill(141, 48, 38, 1, P.trimDark);
    fill(143, 49, 34, 4, P.door);
    fill(143, 49, 34, 1, P.doorLight);
    fill(143, 51, 34, 1, P.doorDark);

    for (let x = 140; x < SW + M; x++) {
      const top = 53 - Math.round(Math.abs(Math.sin(x * 0.55)) * 1.6);

      for (let y = top; y <= 57; y++) {
        let c = P.leafDark;

        if (y <= top + 1 && bay(x, y) < 0.6) {
          c = P.leafMid;
        }

        put(x, y, c);
      }

      if (rand() < 0.12) {
        put(
          x,
          top + 1 + Math.floor(rand() * 3),
          rand() < 0.5 ? P.flowerC : P.flowerB
        );
      }
    }

    fill(104, 61, 30, 2, P.stoneLight);
    fill(104, 63, 30, 2, P.stoneDark);
    fill(107, 57, 24, 2, P.stoneLight);
    fill(107, 59, 24, 2, P.stone);
    fill(114, 57, 10, 2, P.mat);
    fill(114, 58, 10, 1, P.matDark);

    for (let x = 114; x < 124; x += 2) {
      put(x, 59, P.matDark);
    }

    fill(97, 51, 9, 1, P.potLight);
    fill(98, 52, 7, 3, P.pot);
    fill(99, 55, 5, 2, P.pot);
    fill(104, 52, 1, 3, P.trunkDark);

    const leaf = (x0, y0, x1, y1) => {
      const n = Math.max(
        Math.abs(x1 - x0),
        Math.abs(y1 - y0)
      );

      for (let i = 0; i <= n; i++) {
        const x = Math.round(x0 + ((x1 - x0) * i) / n);
        const y = Math.round(y0 + ((y1 - y0) * i) / n);

        put(x, y, i > n - 2 ? P.leafLight : P.leafMid);

        if (i > 1 && i < n - 1) {
          put(x + 1, y, P.leafDark);
        }
      }
    };

    leaf(101, 50, 96, 45);
    leaf(101, 50, 106, 45);
    leaf(101, 50, 100, 44);
    leaf(101, 50, 103, 46);
    leaf(101, 50, 98, 48);
    leaf(101, 50, 105, 48);

    stars = [];

    const skySet = new Set(bands);

    while (stars.length < 18) {
      const x = Math.floor(rand() * SW);
      const y = 5 + Math.floor(rand() * 25);

      if (!skySet.has(baseLayer[at(x, y)])) continue;
      if (Math.hypot(x - 56, y - 9) < 9) continue;

      stars.push([
        x,
        y,
        rand() * 6.28,
        0.6 + rand() * 1.4,
        rand() < 0.25
      ]);
    }

    fireflies = [];

    for (let i = 0; i < 7; i++) {
      fireflies.push([
        8 + rand() * 84,
        34 + rand() * 28,
        rand() * 6.28,
        0.6 + rand() * 0.8
      ]);
    }

    boxFlowers = [];

    for (let x = 144; x < 176; x++) {
      const h = 1 + Math.floor(rand() * 3);

      for (let k = 0; k < h; k++) {
        boxFlowers.push([
          x,
          48 - k,
          k === h - 1 && rand() < 0.55 ? 'bloom' : 'leaf',
          rand()
        ]);
      }
    }
  }

  /* ---------- Resident ---------- */

  function person(cx, fy, f, tint) {
    const col = (c) => (tint ? tint(c) : c);

    const H = col(P.hair);
    const K = col(P.skin);
    const E = col(P.ink);
    const T = col(P.shirt);
    const Td = col(P.shirtDark);
    const L = col(P.pants);
    const B = col(P.shoe);

    let lo = 0;
    let ro = 0;

    if (f.step === 1) lo = -1;
    if (f.step === 2) ro = 1;

    fill(cx - 2 + lo, fy - 3, 2, 3, L);
    fill(cx + 1 + ro, fy - 3, 2, 3, L);
    fill(cx - 2, fy - 3, 5, 1, L);
    fill(cx - 2 + lo, fy, 2, 1, B);
    fill(cx + 1 + ro, fy, 2, 1, B);
    fill(cx - 2, fy - 8, 5, 5, T);
    fill(cx + 2, fy - 8, 1, 5, Td);

    put(cx, fy - 8, K);

    const arm = (s, kind) => {
      const x = cx + 3 * s;
      const sleeve = s > 0 ? Td : T;

      if (kind === 'up1') {
        put(x, fy - 8, sleeve);
        put(x + s, fy - 9, sleeve);
        put(x + s, fy - 10, K);
        put(x + s, fy - 11, K);
      } else if (kind === 'up2') {
        put(x, fy - 8, sleeve);
        put(x + s, fy - 9, sleeve);
        put(x + 2 * s, fy - 10, K);
        put(x + 2 * s, fy - 11, K);
      } else if (kind === 'hip') {
        put(x, fy - 8, sleeve);
        put(x + s, fy - 7, sleeve);
        put(x + s, fy - 6, sleeve);
        put(x, fy - 5, K);
      } else {
        fill(x, fy - 8, 1, 3, sleeve);
        put(x, fy - 5, K);
      }
    };

    arm(-1, f.armL || 'down');
    arm(1, f.armR || 'down');

    const m = f.face === 'right' ? -1 : 1;
    const hp = (dx, dy, c) => put(cx + dx * m, fy + dy, c);

    fill(cx - 1, fy - 13, 3, 1, H);
    fill(cx - 2, fy - 12, 5, 1, H);

    if (f.face === 'front') {
      hp(-2, -11, H);
      hp(-1, -11, K);
      hp(0, -11, K);
      hp(1, -11, K);
      hp(2, -11, H);

      for (let dx = -2; dx <= 2; dx++) {
        hp(dx, -10, K);
      }

      hp(-1, -10, E);
      hp(1, -10, E);
      hp(-1, -9, K);
      hp(0, -9, K);
      hp(1, -9, K);
    } else {
      hp(-2, -11, K);
      hp(-1, -11, K);
      hp(0, -11, H);
      hp(1, -11, H);
      hp(2, -11, H);

      for (let dx = -2; dx <= 1; dx++) {
        hp(dx, -10, K);
      }

      hp(-1, -10, E);
      hp(2, -10, H);
      hp(-2, -9, K);
      hp(-1, -9, K);
      hp(0, -9, K);
    }
  }

  /* ---------- NEW: Two kids ---------- */

  function drawKid(cx, fy, type, step, fleeing = false, ringing = false) {
    const hair = type === 0 ? P.hair : P.trunkDark;
    const shirt = type === 0 ? P.flowerD : P.shirt;
    const shirtDark = type === 0 ? P.brick : P.shirtDark;
    const pants = type === 0 ? P.doorDark : P.pants;
    const skin = P.skin;

    const leftLeg = step === 1 ? -1 : 0;
    const rightLeg = step === 2 ? 1 : 0;

    fill(cx - 1 + leftLeg, fy - 4, 1, 4, pants);
    fill(cx + 1 + rightLeg, fy - 4, 1, 4, pants);

    put(cx - 1 + leftLeg, fy, P.shoe);
    put(cx + 1 + rightLeg, fy, P.shoe);

    fill(cx - 2, fy - 9, 5, 5, shirt);
    fill(cx + 2, fy - 9, 1, 5, shirtDark);

    fill(cx - 2, fy - 15, 5, 2, hair);
    fill(cx - 2, fy - 13, 5, 4, skin);

    put(cx - 2, fy - 12, hair);
    put(cx + 2, fy - 13, hair);

    put(
      fleeing ? cx - 1 : cx + 1,
      fy - 11,
      P.ink
    );

    put(cx, fy - 10, skin);

    if (ringing) {
      put(cx + 3, fy - 8, shirtDark);
      put(cx + 4, fy - 9, skin);
      put(cx + 4, fy - 10, skin);
      put(cx + 4, fy - 11, skin);
      put(cx + 4, fy - 12, skin);
    } else if (fleeing) {
      put(cx - 3, fy - 8, shirtDark);
      put(cx - 4, fy - 9, skin);
      put(cx + 3, fy - 7, shirt);
      put(cx + 4, fy - 6, skin);
    } else {
      put(cx - 3, fy - 7, shirtDark);
      put(cx - 4, fy - 6, skin);
      put(cx + 3, fy - 7, shirt);
      put(cx + 4, fy - 8, skin);
    }
  }

  /* ---------- Cat ---------- */

  function cat(now, perk, hop) {
    const y0 = hop ? -2 : 0;
    const C = P.cat;
    const Cl = P.catLight;

    fill(136, 53 + y0, 6, 1, Cl);
    fill(135, 54 + y0, 8, 3, C);
    fill(134, 57 + y0, 9, 1, C);
    fill(133, 57 + y0, 2, 1, Cl);

    fill(132, 52 + y0, 5, 3, C);
    fill(133, 51 + y0, 3, 1, Cl);
    put(132, 51 + y0, C);
    put(135, 51 + y0, C);

    if (perk) {
      put(132, 50 + y0, C);
      put(135, 50 + y0, C);
    }

    const blink = !reduce && now % 4.3 < 0.14;

    if (!blink) {
      put(133, 53 + y0, P.catEye);
      put(135, 53 + y0, P.catEye);
    }

    const flick = !reduce && Math.floor(now * 1.3) % 5 === 0;

    const tail = flick
      ? [[143, 56], [144, 55], [145, 54], [145, 53]]
      : [[143, 56], [144, 55], [144, 54], [143, 53]];

    for (const [x, y] of tail) {
      put(x, y + y0, C);
    }
  }

  const GLYPHS = {
    '?': ['###', '..#', '.##', '...', '.#.'],
    '!': ['.#.', '.#.', '.#.', '...', '.#.'],
    note: ['.##', '.#.', '.#.', '##.', '##.'],
    '!?': ['#.###', '#...#', '#..#.', '.....', '#..#.']
  };

  function bubble(cx, bottom, kind) {
    const g = GLYPHS[kind];
    const gw = g[0].length;
    const bw = gw + 4;
    const bh = 9;

    const x0 = cx - Math.floor(bw / 2);
    const y0 = bottom - bh - 1;

    for (let y = 0; y < bh; y++) {
      for (let x = 0; x < bw; x++) {
        const corner =
          (x === 0 || x === bw - 1) &&
          (y === 0 || y === bh - 1);

        if (corner) continue;

        const edge =
          x === 0 ||
          y === 0 ||
          x === bw - 1 ||
          y === bh - 1;

        put(
          x0 + x,
          y0 + y,
          edge ? P.ink : P.bubble
        );
      }
    }

    put(cx, y0 + bh - 1, P.bubble);
    put(cx, y0 + bh, P.ink);
    put(cx - 1, y0 + bh, P.ink);

    for (let y = 0; y < 5; y++) {
      for (let x = 0; x < gw; x++) {
        if (g[y][x] === '#') {
          put(x0 + 2 + x, y0 + 2 + y, P.ink);
        }
      }
    }
  }

  /* ---------- Resident movement and game state ---------- */

  const S = {
    phase: 'in',
    t0: 0,

    ix: 163,
    itx: 163,
    iwait: 1.5,
    iface: 'front',
    istep: 0,

    walkClock: 0,
    pending: false,
    presses: [],

    perk: -9,
    hop: -9,
    birdMode: 'perch',
    birdT: 0,

    shake: -9,
    bell: -9,
    outFeet: 56,

    flags: {},

    /* Kids remain hidden until the glass is pressed. */
    kids: {
      mode: 'hidden',
      x1: -10,
      x2: -27,
      target1: 126,
      target2: 140,
      step: 0,
      slidePower: 0,
      escapeSpeed: 0,
      doneAt: 0
    }
  };

  const irand = makeRandom(7);

  function say(text) {
    status.textContent = text;
  }

  function startToDoor(now) {
    S.phase = 'toDoor';
    S.t0 = now;
    S.flags = {};
    S.pending = false;
  }

  function onBell(now) {
    S.bell = now;
    S.perk = now;

    if (S.birdMode === 'perch') {
      S.birdMode = 'away';
      S.birdT = now;
    }

    S.presses = S.presses.filter((t) => now - t < 3);
    S.presses.push(now);

    const count = S.presses.length;
    const e = now - S.t0;

    if (S.phase === 'in') {
      startToDoor(now);
    } else if (S.phase === 'door') {
      if (count >= 4 && e > 0.3 && e < 4.2) {
        S.outFeet = doorFrame(e).fy;
        S.phase = 'annoyed';
        S.t0 = now;
        S.flags = {};

        say('住人が少し困っています');
      } else if (e < 1.35) {
        S.t0 = now - 1.35;
      } else if (e >= 2.4 && e < 4.2) {
        S.t0 = now - 2.0;
      } else if (e >= 4.2) {
        S.pending = true;
      }
    }
  }

  function doorFrame(e) {
    const walk = (k) => (
      reduce
        ? 0
        : Math.floor(k / 0.12) % 2
          ? 1
          : 2
    );

    if (e < 0.35) {
      return {
        fy: 56,
        face: 'front',
        bubble: e > 0.12 ? '?' : null
      };
    }

    if (e < 0.85) {
      return {
        fy: 56,
        face: 'left',
        bubble: '?'
      };
    }

    if (e < 1.35) {
      return {
        fy: 56,
        face: 'right',
        bubble: '?'
      };
    }

    if (e < 1.6) {
      return {
        fy: 56 + Math.round(((e - 1.35) / 0.25) * 2),
        face: 'front',
        step: walk(e)
      };
    }

    if (e < 2.0) {
      return {
        fy: 58,
        face: 'front',
        bubble: '!'
      };
    }

    if (e < 3.8) {
      const up = reduce
        ? 'up1'
        : Math.floor((e - 2) / 0.2) % 2
          ? 'up1'
          : 'up2';

      return {
        fy: 58,
        face: 'front',
        armR: up,
        bubble: e > 2.5 ? 'note' : null
      };
    }

    if (e < 4.2) {
      return {
        fy: 58 - Math.round(((e - 3.8) / 0.4) * 2),
        face: 'left',
        step: walk(e)
      };
    }

    return null;
  }

  /* ---------- Kids movement ---------- */

  function updateKids(now, dt) {
    const kids = S.kids;

    /* Kids only approach after the user presses the glass. */
    if (kids.mode === 'approach') {
      const speed = reduce ? 44 : 30;

      kids.x1 += Math.min(
        kids.target1 - kids.x1,
        speed * dt
      );

      kids.x2 += Math.min(
        kids.target2 - kids.x2,
        speed * dt
      );

      kids.step = reduce
        ? 0
        : Math.floor(now * 8) % 2
          ? 1
          : 2;

      if (
        kids.x1 >= kids.target1 &&
        kids.x2 >= kids.target2
      ) {
        kids.x1 = kids.target1;
        kids.x2 = kids.target2;

        kids.mode = 'waiting';
        kids.slidePower = 0;
        kids.escapeSpeed = 0;

        say('The kids rang the bell! Get ready to slide!');

        onBell(now);
      }
    }

    /* Smooth acceleration when the man comes outside. */
    else if (kids.mode === 'escape') {
      const targetSpeed = 18 + 82 * kids.slidePower;

      const easing = 1 - Math.exp(-4 * dt);

      kids.escapeSpeed +=
        (targetSpeed - kids.escapeSpeed) * easing;

      kids.x1 -= kids.escapeSpeed * dt;
      kids.x2 -= kids.escapeSpeed * 1.04 * dt;

      kids.step = reduce
        ? 0
        : Math.floor(now * 15) % 2
          ? 1
          : 2;

      if (kids.x1 < -14 && kids.x2 < -14) {
        kids.mode = 'done';
        kids.doneAt = now;

        say('Both kids escaped! Nice work!');
      }
    }
  }

  function update(now, dt) {
    const e = now - S.t0;

    if (S.phase === 'in') {
      if (S.pending && e > 0.4) {
        startToDoor(now);
      } else if (S.iwait > 0) {
        S.iwait -= dt;
        S.iface = 'front';
        S.istep = 0;
      } else if (reduce) {
        S.ix = S.itx;
        S.iwait = 3 + irand() * 3;
        S.itx = 150 + Math.round(irand() * 18);
      } else {
        const d = S.itx - S.ix;
        const v = Math.sign(d) * Math.min(Math.abs(d), 7 * dt);

        S.ix += v;
        S.iface = d < 0 ? 'left' : 'right';

        S.walkClock += dt;

        S.istep = Math.floor(S.walkClock / 0.16) % 2
          ? 1
          : 2;

        if (Math.abs(S.itx - S.ix) < 0.3) {
          S.ix = S.itx;
          S.iwait = 1.5 + irand() * 3;
          S.itx = 150 + Math.round(irand() * 18);
        }
      }
    }

    else if (S.phase === 'toDoor') {
      S.ix -= (reduce ? 60 : 18) * dt;
      S.iface = 'left';

      S.walkClock += dt;

      S.istep = reduce
        ? 0
        : Math.floor(S.walkClock / 0.12) % 2
          ? 1
          : 2;

      if (e >= 0.9 && !S.flags.latch) {
        S.flags.latch = true;
        sfx('latch');
      }

      if (e >= 1.2) {
        S.phase = 'door';
        S.t0 = now;
        S.flags = {};

        say('The man has opened the door!');
      }
    }

    else if (S.phase === 'door') {

      /* Kids begin escaping as soon as the resident appears. */
      if (S.kids.mode === 'waiting') {
        S.kids.mode = 'escape';
        S.kids.escapeSpeed = 0;

        say('The man is coming out! Slide quickly to help the kids!');
      }

      if (e >= 2.0 && !S.flags.wave) {
        S.flags.wave = true;
        say('住人が手を振っています');
      }

      if (e >= 4.45 && !S.flags.close) {
        S.flags.close = true;
        sfx('close');
      }

      if (e >= 4.6) {
        S.phase = 'in';
        S.t0 = now;
        S.ix = 146;
        S.itx = 154;
        S.iwait = 0;

        say('住人は家に戻りました');
      }
    }

    else if (S.phase === 'annoyed') {
      if (e >= 1.45 && !S.flags.slam) {
        S.flags.slam = true;
        S.shake = now;
        S.hop = now;
        sfx('slam');
      }

      if (e >= 2.0) {
        S.phase = 'in';
        S.t0 = now;
        S.ix = 146;
        S.itx = 160;
        S.iwait = 1.2;
        S.presses = [];
        S.pending = false;

        say('住人は家に戻りました');
      }
    }

    updateKids(now, dt);
  }

  /* ---------- Scene drawing ---------- */

  function drawScene(now) {
    pix.set(baseLayer);

    target = pix;
    clipBox = null;

    const t = reduce ? 0 : now;

    for (const [x, y, ph, sp, big] of stars) {
      const b = reduce
        ? 0.7
        : 0.5 + 0.5 * Math.sin(t * sp + ph);

      if (b < 0.3) continue;

      put(x, y, P.star);

      if (big && b > 0.82) {
        tintPixel(x - 1, y, RGB.star, 0.5);
        tintPixel(x + 1, y, RGB.star, 0.5);
        tintPixel(x, y - 1, RGB.star, 0.5);
        tintPixel(x, y + 1, RGB.star, 0.5);
      }
    }

    for (const [x, y, ph] of townLights) {
      if (reduce || Math.sin(t * 0.7 + ph) > -0.6) {
        put(x, y, P.townLight);
      }
    }

    for (let i = 0; i < 4; i++) {
      const a = reduce
        ? 0.2 + i * 0.18
        : (t * 0.11 + i / 4) % 1;

      const x = 178 + a * 22 + Math.sin(t * 0.8 + i) * 1.2;
      const y = 3.5 - a * 5;
      const w = a < 0.4 ? 2 : 3;

      for (let j = 0; j < w; j++) {
        for (let k = 0; k < (a < 0.4 ? 1 : 2); k++) {
          const px0 = Math.round(x + j);
          const py0 = Math.round(y + k);

          if (bay(px0, py0) < (1 - a) * 0.85) {
            put(px0, py0, P.smoke);
          }
        }
      }
    }

    const e = now - S.t0;

    /* Window */
    clipBox = [145, 33, 174, 46];

    for (let y = 33; y <= 46; y++) {
      for (let x = 145; x <= 174; x++) {
        const d = Math.hypot(
          (x - 152) / 14,
          (y - 37) / 12
        );

        put(
          x,
          y,
          bay(x, y) < d * 0.9
            ? P.glowDeep
            : P.glow
        );
      }
    }

    fill(152, 33, 1, 3, P.ink);
    fill(150, 36, 5, 2, P.doorDark);
    fill(151, 38, 3, 1, P.lamp);
    fill(164, 36, 6, 5, P.trimDark);
    fill(165, 37, 4, 3, P.leafMid);
    fill(165, 37, 4, 1, P.flowerB);
    put(167, 38, P.flowerA);

    let inside = null;

    if (S.phase === 'in' || S.phase === 'toDoor') {
      inside = {
        x: Math.round(S.ix),
        face: S.iface,
        step: S.istep
      };
    }

    if (inside) {
      const dim = (c) => {
        const a = unpack(c);

        return pack(
          mix(
            [
              a[0] / 255,
              a[1] / 255,
              a[2] / 255
            ],
            RGB.glowDeep,
            0.22
          )
        );
      };

      person(inside.x, 50, inside, dim);
    }

    for (const [x0, dir] of [[145, 1], [171, -1]]) {
      for (let y = 33; y <= 46; y++) {
        const w = y < 41
          ? 4
          : 4 + Math.min(2, Math.floor((y - 41) / 2));

        for (let k = 0; k < w; k++) {
          const x = dir > 0
            ? x0 + k
            : x0 + 3 - k;

          put(
            x,
            y,
            k % 2 === 0
              ? P.curtainDark
              : P.curtain
          );
        }
      }

      fill(x0, 41, 4, 1, P.doorDark);
    }

    fill(145, 33, 30, 1, P.trimDark);
    fill(159, 33, 2, 14, P.trim);

    for (let y = 33; y <= 46; y++) {
      for (let x = 145; x <= 174; x++) {
        const k = x - 145 - (y - 33);
        const k2 = x - 162 - (y - 33);

        if (k === 4 || k === 5 || k2 === 3) {
          tintPixel(x, y, RGB.reflect, 0.32);
        }
      }
    }

    clipBox = null;

    for (const [x, y, kind, r] of boxFlowers) {
      if (kind === 'leaf') {
        put(x, y, r < 0.5 ? P.leafMid : P.leafDark);
      } else {
        put(
          x,
          y,
          [
            P.flowerA,
            P.flowerD,
            P.flowerC,
            P.flowerB
          ][Math.floor(r * 4)]
        );
      }
    }

    /* Door */
    let open = 0;
    let fig = null;

    if (S.phase === 'toDoor') {
      open = Math.max(
        0,
        Math.min(1, (e - 0.9) / 0.3)
      );
    }

    if (S.phase === 'door') {
      open = e < 4.2
        ? 1
        : Math.max(0, 1 - (e - 4.2) / 0.3);

      fig = doorFrame(e);
    }

    if (S.phase === 'annoyed') {
      open = e < 1.45
        ? 1
        : Math.max(0, 1 - (e - 1.45) / 0.08);

      if (e < 1.2) {
        fig = {
          fy: S.outFeet,
          face: 'front',
          armL: 'hip',
          armR: 'hip',
          bubble: '!?' 
        };
      } else if (e < 1.45) {
        fig = {
          fy: 56,
          face: 'left'
        };
      }
    }

    if (reduce && open > 0 && open < 1) {
      open = open < 0.5 ? 0 : 1;
    }

    if (open <= 0.001) {
      fill(112, 35, 14, 22, P.door);
      fill(112, 35, 1, 22, P.doorLight);
      fill(125, 35, 1, 22, P.doorDark);

      for (const [py, ph] of [[41, 6], [49, 6]]) {
        fill(114, py, 10, 1, P.doorDark);
        fill(114, py, 1, ph, P.doorDark);
        fill(114, py + ph - 1, 10, 1, P.doorLight);
        fill(123, py, 1, ph, P.doorLight);
      }

      fill(115, 37, 8, 3, P.glow);
      fill(118, 37, 1, 3, P.door);
      put(123, 47, P.knob);
      put(123, 48, P.doorDark);
    } else {
      for (let y = 35; y <= 56; y++) {
        for (let x = 112; x <= 125; x++) {
          let c = y < 37 ? P.glowDeep : P.glow;

          if (y >= 52) {
            c = y === 52 ? P.doorLight : P.floorIn;
          }

          put(x, y, c);
        }
      }

      fill(119, 39, 4, 4, P.trimDark);
      fill(120, 40, 2, 2, P.flowerD);

      const w = Math.max(
        2,
        Math.round(14 * (1 - 0.82 * open))
      );

      fill(112, 35, w, 22, P.doorDark);
      fill(112 + w - 1, 35, 1, 22, P.doorLight);

      if (w > 5) {
        fill(114, 41, w - 4, 1, P.door);
      }
    }

    /* Door light */
    if (open > 0) {
      for (let y = 57; y <= 67; y++) {
        const spread = (y - 56) * 0.9;
        const a = open * 0.7 * (1 - (y - 56) / 12);

        for (
          let x = Math.floor(112 - spread);
          x <= Math.ceil(125 + spread);
          x++
        ) {
          const level = Math.floor(a * 4 + bay(x, y)) / 4;

          if (level > 0) {
            tintPixel(
              x,
              y,
              RGB.glow,
              Math.min(0.7, level)
            );
          }
        }
      }
    }

    if (fig) {
      person(119, fig.fy, fig);
    }

    /* Bird */
    {
      const PX = 74;
      const PY = 36;

      let bx = PX;
      let by = PY;
      let fly = false;
      let face = 1;

      const be = now - S.birdT;

      if (S.birdMode === 'away') {
        if (reduce) {
          bx = -9;
        } else {
          const a = Math.min(1, be / 1.3);

          bx = PX - a * 64;
          by = PY - a * 30 + Math.sin(a * 18) * 1.2;

          fly = true;
          face = -1;
        }

        if (be > 7) {
          S.birdMode = 'back';
          S.birdT = now;
        }
      } else if (S.birdMode === 'back') {
        const a = reduce
          ? 1
          : Math.min(1, be / 1.6);

        bx = PX + (1 - a) * 46;
        by =
          PY -
          (1 - a) * 26 +
          Math.sin(a * 16) * 1.2 * (1 - a);

        fly = a < 1;
        face = -1;

        if (a >= 1) {
          S.birdMode = 'perch';
          S.birdT = now;
        }
      }

      bx = Math.round(bx);
      by = Math.round(by);

      if (bx > -4) {
        const peck =
          !fly &&
          !reduce &&
          Math.floor(now * 1.1) % 6 === 0;

        const hop2 =
          !fly &&
          !reduce &&
          Math.floor(now * 0.7) % 9 === 4;

        const fx = (dx) => bx + dx * face;
        const yy = by - (hop2 ? 1 : 0);

        put(fx(-1), yy - 1, P.bird);
        put(fx(0), yy - 1, P.bird);
        put(fx(1), yy - 1, P.birdBreast);

        if (peck) {
          put(fx(2), yy - 1, P.bird);
          put(fx(3), yy - 1, P.knob);
        } else {
          put(fx(1), yy - 2, P.bird);
          put(fx(2), yy - 2, P.knob);
        }

        if (fly) {
          const up = Math.floor(now * 14) % 2 === 0;

          put(fx(0), yy + (up ? -2 : 0), P.bird);
          put(fx(-1), yy + (up ? -3 : 1), P.bird);
        } else {
          put(fx(0), yy, P.ink);
        }
      }
    }

    /* Cat */
    const perk =
      now - S.perk < 1.6 ||
      S.phase === 'door' ||
      S.phase === 'annoyed';

    const hop = !reduce && now - S.hop < 0.35;

    cat(now, perk, hop);

    /* NEW: kids are drawn only during an active round */
    if (
      S.kids.mode === 'approach' ||
      S.kids.mode === 'waiting' ||
      S.kids.mode === 'escape'
    ) {
      const running = S.kids.mode === 'escape';

      const kidStep = reduce
        ? 0
        : running
          ? Math.floor(now * 15) % 2 ? 1 : 2
          : Math.floor(now * 8) % 2 ? 1 : 2;

      drawKid(
        Math.round(S.kids.x1),
        59,
        0,
        kidStep,
        running,
        S.kids.mode === 'waiting'
      );

      drawKid(
        Math.round(S.kids.x2),
        66,
        1,
        kidStep,
        running,
        false
      );
    }

    /* Doorbell */
    const bellA = Math.max(
      0,
      1 - (now - S.bell) / 1.4
    );

    put(
      130,
      44,
      bellA > 0.05 ? P.bellLit : P.trimDark
    );

    put(
      130,
      45,
      bellA > 0.05 ? P.bellLit : P.trimDark
    );

    /* Lighting */
    const flicker = reduce
      ? 1
      : 0.94 +
        0.06 *
        Math.sin(t * 11.3) *
        Math.sin(t * 4.1);

    glow(
      103.5,
      40.5,
      17,
      0.62 * flicker,
      RGB.lampLight,
      0,
      4
    );

    glow(
      159.5,
      49,
      15,
      0.36,
      RGB.glow,
      47,
      3
    );

    if (bellA > 0) {
      glow(
        130.5,
        44.5,
        6,
        bellA * 0.8,
        RGB.bellLit,
        0,
        3
      );
    }

    if (open > 0) {
      glow(
        118.5,
        46,
        14,
        0.3 * open,
        RGB.glow,
        0,
        3
      );
    }

    /* Fireflies */
    for (const [bx, by, ph, sp] of fireflies) {
      const x = Math.round(
        bx + Math.sin(t * 0.4 * sp + ph) * 6
      );

      const y = Math.round(
        by + Math.cos(t * 0.33 * sp + ph * 1.3) * 4
      );

      const on =
        reduce ||
        Math.sin(t * 1.7 * sp + ph * 3) > 0.15;

      if (!on) continue;

      put(x, y, P.firefly);

      for (const [dx, dy] of [
        [1, 0],
        [-1, 0],
        [0, 1],
        [0, -1]
      ]) {
        tintPixel(
          x + dx,
          y + dy,
          RGB.firefly,
          0.35
        );
      }
    }

    /* Moths */
    for (let i = 0; i < 2; i++) {
      const a = t * (4.5 + i * 1.7) + i * 2.4;

      const x = Math.round(
        103.5 + Math.cos(a) * (4 + i)
      );

      const y = Math.round(
        40.5 +
        Math.sin(a) * 2.6 +
        Math.sin(t * 13 + i) * 0.6
      );

      put(x, y, P.moth);
    }

    if (fig && fig.bubble) {
      bubble(
        fig.fy > 56 ? 121 : 120,
        fig.fy - 14,
        fig.bubble
      );
    }

    /* Door shake */
    if (!reduce && now - S.shake < 0.22) {
      const dx =
        Math.floor((now - S.shake) / 0.055) % 2
          ? 1
          : -1;

      for (let y = 0; y < TH; y++) {
        const row = y * TW;

        if (dx > 0) {
          pix.copyWithin(
            row + 1,
            row,
            row + TW - 1
          );
        } else {
          pix.copyWithin(
            row,
            row + 1,
            row + TW
          );
        }
      }
    }
  }

  /* ---------- Sound ---------- */

  let actx = null;
  let master = null;
  let noise = null;

  function audio() {
    if (!actx) {
      const AC = win.AudioContext || win.webkitAudioContext;

      if (!AC) return null;

      actx = new AC();

      master = actx.createGain();
      master.gain.value = 0.6;
      master.connect(actx.destination);

      noise = actx.createBuffer(
        1,
        Math.floor(actx.sampleRate * 0.5),
        actx.sampleRate
      );

      const d = noise.getChannelData(0);
      const r = makeRandom(99);

      for (let i = 0; i < d.length; i++) {
        d[i] = r() * 2 - 1;
      }
    }

    if (actx.state === 'suspended') {
      actx.resume();
    }

    return actx;
  }

  function chime(freq, vol, dur) {
    const ac = audio();

    if (!ac) return;

    const t0 = ac.currentTime + 0.025;

    for (const [m, a] of [
      [1, 1],
      [2, 0.22],
      [3, 0.06],
      [4.16, 0.05],
      [1.003, 0.4]
    ]) {
      const o = ac.createOscillator();

      o.type = 'sine';
      o.frequency.value = freq * m;

      const g = ac.createGain();

      const end = t0 + dur / Math.sqrt(Math.max(1, m));

      g.gain.setValueAtTime(0.0001, t0);
      g.gain.exponentialRampToValueAtTime(
        vol * a,
        t0 + 0.006
      );

      g.gain.exponentialRampToValueAtTime(
        0.0001,
        end
      );

      o.connect(g).connect(master);

      o.start(t0);
      o.stop(end + 0.05);
    }
  }

  function thump(vol, cutoff, dur, delay = 0) {
    const ac = audio();

    if (!ac) return;

    const t0 = ac.currentTime + 0.025 + delay;

    const s = ac.createBufferSource();
    s.buffer = noise;

    const f = ac.createBiquadFilter();
    f.type = 'lowpass';
    f.frequency.value = cutoff;

    const g = ac.createGain();

    g.gain.setValueAtTime(0.0001, t0);
    g.gain.exponentialRampToValueAtTime(vol, t0 + 0.004);
    g.gain.exponentialRampToValueAtTime(0.0001, t0 + dur);

    s.connect(f).connect(g).connect(master);

    s.start(t0);
    s.stop(t0 + dur + 0.05);
  }

  function tink(freq, vol, dur, delay = 0) {
    const ac = audio();

    if (!ac) return;

    const t0 = ac.currentTime + 0.025 + delay;

    const o = ac.createOscillator();
    o.type = 'triangle';
    o.frequency.value = freq;

    const g = ac.createGain();

    g.gain.setValueAtTime(0.0001, t0);
    g.gain.exponentialRampToValueAtTime(vol, t0 + 0.002);
    g.gain.exponentialRampToValueAtTime(0.0001, t0 + dur);

    o.connect(g).connect(master);

    o.start(t0);
    o.stop(t0 + dur + 0.05);
  }

  function sfx(kind) {
    if (!actx) return;

    if (kind === 'press') {
      thump(0.1, 420, 0.07);
      tink(2300, 0.025, 0.05);
      chime(659.25, 0.16, 1.4);
    } else if (kind === 'release') {
      thump(0.05, 380, 0.05);
      chime(523.25, 0.17, 2.1);
    } else if (kind === 'latch') {
      tink(1800, 0.04, 0.03);
      thump(0.05, 1400, 0.04, 0.03);
    } else if (kind === 'close') {
      thump(0.16, 320, 0.12);
      tink(1600, 0.03, 0.03, 0.01);
    } else if (kind === 'slam') {
      thump(0.32, 260, 0.2);
      tink(1500, 0.05, 0.04, 0.01);
    }
  }

  /* ---------- Original WebGL glass ---------- */

  const VS = 'attribute vec2 a;void main(){gl_Position=vec4(a,0.,1.);}';

  const FS = `
precision highp float;
uniform vec2 uRes;
uniform float uDpr;
uniform vec4 uSock;
uniform float uSR;
uniform float uBez;
uniform vec4 uGl;
uniform float uGR;
uniform float uMag;
uniform float uPress;
uniform vec4 uRip[4];
uniform sampler2D uTex;
uniform vec2 uTexN;
uniform float uMargin;
uniform vec3 uBg;
uniform vec3 uTint;
uniform vec3 uWarm;

const vec3 LIT = vec3(-0.42, -0.72, 0.55);

float box(vec2 p, vec2 b, float r) {
  r = min(r, min(b.x, b.y));
  vec2 q = abs(p) - b + r;
  return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r;
}

vec3 sceneAt(vec2 p) {
  vec2 wh = uSock.zw - uBez;
  vec2 uv = (p - (uSock.xy - wh)) / (2.0 * wh);
  vec2 texel = vec2(uMargin) + uv * (uTexN - 2.0 * uMargin);
  texel = clamp(floor(texel), vec2(0.0), uTexN - 1.0) + 0.5;
  return texture2D(uTex, texel / uTexN).rgb;
}

vec3 under(vec2 p) {
  vec2 c = uSock.xy;
  vec2 wh = uSock.zw - uBez;
  float wr = max(uSR - uBez, 0.0);
  float dW = box(p - c, wh, wr);

  if (dW <= 0.0) {
    vec3 s = sceneAt(p);
    float ao = mix(0.58, 1.0, smoothstep(0.0, uBez * 0.9, -dW));
    float dTop = box(p - c - vec2(0.0, uBez * 0.55), wh, wr);
    float sh = smoothstep(-uBez * 0.6, uBez * 0.2, dTop) * 0.32;
    return s * ao * (1.0 - sh);
  }

  float t = clamp(dW / uBez, 0.0, 1.0);
  float e = 0.5;

  vec2 g = vec2(
    box(p + vec2(e, 0.0) - c, wh, wr) -
    box(p - vec2(e, 0.0) - c, wh, wr),

    box(p + vec2(0.0, e) - c, wh, wr) -
    box(p - vec2(0.0, e) - c, wh, wr)
  );

  g = g / max(length(g), 1e-5);

  float dh = 3.14159 * cos(3.14159 * t) * 0.6;

  vec3 n = normalize(vec3(-g * dh, 1.0));
  vec3 L = normalize(LIT);
  vec3 h = normalize(L + vec3(0.0, 0.0, 1.0));

  float dif = dot(n, L);
  float sp = pow(max(dot(n, h), 0.0), 36.0);

  vec3 col = uBg * (0.8 + 0.3 * dif) + sp * 0.22;
  col *= mix(0.42, 1.0, smoothstep(0.0, 0.22, t));

  return col;
}

void main() {
  vec2 p = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y) / uDpr;

  float dS = box(p - uSock.xy, uSock.zw, uSR);
  float aS = clamp(0.5 - dS * uDpr, 0.0, 1.0);

  if (aS <= 0.0) {
    gl_FragColor = vec4(0.0);
    return;
  }

  vec3 col = under(p);

  float lift = 1.0 - 0.7 * uPress;

  float dSh = box(
    p - uGl.xy - vec2(1.5, 5.0) * lift,
    uGl.zw,
    uGR
  );

  col *= 1.0 - 0.34 * (
    1.0 - smoothstep(-8.0, 10.0 * lift + 2.0, dSh)
  );

  float dG = box(p - uGl.xy, uGl.zw, uGR);

  if (dG < 1.5) {
    float e = max(-dG, 0.0);
    float B = min(uGl.z, uGl.w) * 0.42;

    float x = clamp(e / B, 0.0, 1.0);
    float s = 1.0 - x;
    float slope = s / sqrt(max(1.0 - s * s, 0.03));

    float k = 0.5;

    vec2 g = vec2(
      box(p + vec2(k, 0.0) - uGl.xy, uGl.zw, uGR) -
      box(p - vec2(k, 0.0) - uGl.xy, uGl.zw, uGR),

      box(p + vec2(0.0, k) - uGl.xy, uGl.zw, uGR) -
      box(p - vec2(0.0, k) - uGl.xy, uGl.zw, uGR)
    );

    g = g / max(length(g), 1e-5);

    vec2 gh = -g * slope * 0.9;

    for (int i = 0; i < 4; i++) {
      vec4 r = uRip[i];

      if (r.w > 0.001) {
        vec2 d = p - r.xy;
        float rr = length(d);

        float front = r.z * 170.0;

        float env =
          r.w *
          exp(-r.z * 2.8) *
          exp(-pow((rr - front) / 22.0, 2.0));

        gh +=
          (d / max(rr, 1.0)) *
          env *
          cos(0.24 * (rr - front)) *
          0.42;
      }
    }

    vec3 n = normalize(vec3(-gh, 1.0));

    vec2 off = n.xy * B * 0.6;
    vec2 q = uGl.xy + (p - uGl.xy) / uMag;

    vec3 cg;

    cg.r = sceneAt(q + off * 1.04).r;
    cg.g = sceneAt(q + off).g;
    cg.b = sceneAt(q + off * 0.96).b;

    float fres = pow(1.0 - n.z, 2.0);

    cg = mix(cg, uTint, 0.05 + 0.38 * fres);

    vec3 L = normalize(LIT);
    vec3 h = normalize(L + vec3(0.0, 0.0, 1.0));

    float nh = max(dot(n, h), 0.0);

    cg += pow(nh, 90.0) * 0.8 + pow(nh, 14.0) * 0.08;

    float facing = dot(g, normalize(LIT.xy));
    float rim = 1.0 - smoothstep(0.4, 2.2, e);

    cg += rim * (0.16 + 0.55 * max(facing, 0.0));
    cg *= 1.0 - rim * 0.35 * max(-facing, 0.0);

    float caus =
      smoothstep(B * 0.08, B * 0.3, e) *
      (1.0 - smoothstep(B * 0.3, B * 0.75, e)) *
      max(-facing, 0.0);

    cg += caus * uWarm * 0.22;

    float aG = clamp(0.5 - dG * uDpr, 0.0, 1.0);

    col = mix(col, cg, aG);
  }

  gl_FragColor = vec4(col * aS, aS);
}`;

  let gl = null;
  let prog = null;
  let tex = null;
  let U = {};
  let ctx2d = null;
  let fallbackCanvas = null;

  function initGL() {
    gl = canvas.getContext(
      'webgl',
      {
        alpha: true,
        premultipliedAlpha: true,
        antialias: false
      }
    );

    if (!gl) return false;

    const sh = (type, src) => {
      const s = gl.createShader(type);

      gl.shaderSource(s, src);
      gl.compileShader(s);

      if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) {
        throw new Error(gl.getShaderInfoLog(s));
      }

      return s;
    };

    prog = gl.createProgram();

    gl.attachShader(prog, sh(gl.VERTEX_SHADER, VS));
    gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, FS));

    gl.linkProgram(prog);

    if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {
      throw new Error(gl.getProgramInfoLog(prog));
    }

    gl.useProgram(prog);

    const buf = gl.createBuffer();

    gl.bindBuffer(gl.ARRAY_BUFFER, buf);

    gl.bufferData(
      gl.ARRAY_BUFFER,
      new Float32Array([-1, -1, 3, -1, -1, 3]),
      gl.STATIC_DRAW
    );

    const loc = gl.getAttribLocation(prog, 'a');

    gl.enableVertexAttribArray(loc);

    gl.vertexAttribPointer(
      loc,
      2,
      gl.FLOAT,
      false,
      0,
      0
    );

    tex = gl.createTexture();

    gl.bindTexture(gl.TEXTURE_2D, tex);

    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);

    gl.texImage2D(
      gl.TEXTURE_2D,
      0,
      gl.RGBA,
      TW,
      TH,
      0,
      gl.RGBA,
      gl.UNSIGNED_BYTE,
      pixBytes
    );

    for (const n of [
      'uRes',
      'uDpr',
      'uSock',
      'uSR',
      'uBez',
      'uGl',
      'uGR',
      'uMag',
      'uPress',
      'uRip',
      'uTex',
      'uTexN',
      'uMargin',
      'uBg',
      'uTint',
      'uWarm'
    ]) {
      U[n] = gl.getUniformLocation(prog, n);
    }

    gl.uniform1i(U.uTex, 0);
    gl.uniform2f(U.uTexN, TW, TH);
    gl.uniform1f(U.uMargin, M);

    return true;
  }

  function setColorUniforms() {
    if (!gl) return;

    gl.uniform3fv(U.uBg, tokens.bg);

    gl.uniform3fv(
      U.uTint,
      light(mix(tokens.bg, tokens.mu, 0.12), 0.06)
    );

    gl.uniform3fv(U.uWarm, RGB.lampLight);
  }

  /* ---------- Original press spring and ripples ---------- */

  let cssW = 0;
  let cssH = 0;
  let dpr = 1;

  let motionSec = 0.22;
  let radiusPx = 9999;

  const spring = { x: 0, v: 0, target: 0 };
  const squash = { x: 0, v: 0 };

  let ripples = [];
  let pressed = false;
  let lastPoint = null;

  /* Used only to measure sliding speed. */
  let lastSlideTime = 0;

  function readGeometry() {
    const r = canvas.getBoundingClientRect();

    cssW = r.width;
    cssH = r.height;

    dpr = Math.min(2, win.devicePixelRatio || 1);

    const w = Math.max(1, Math.round(cssW * dpr));
    const h = Math.max(1, Math.round(cssH * dpr));

    if (canvas.width !== w || canvas.height !== h) {
      canvas.width = w;
      canvas.height = h;
    }

    radiusPx =
      parseFloat(win.getComputedStyle(base).borderTopLeftRadius) || 0;

    const m = win
      .getComputedStyle(root)
      .getPropertyValue('--_motion')
      .trim();

    const v = parseFloat(m);

    if (Number.isFinite(v)) {
      motionSec = Math.max(
        0.06,
        m.endsWith('ms') ? v / 1000 : v
      );
    }
  }

  function geometry() {
    const sx = cssW / 2;
    const sy = cssH / 2;

    const shx = cssW * 0.43;
    const shy = cssH * 0.3;

    const sr = Math.min(radiusPx, shy);
    const bez = shy * 0.15;
    const gap = shy * 0.025;

    const p = spring.x;
    const sq = squash.x;

    const scale = 1 - 0.03 * p;

    return {
      sock: [sx, sy, shx, shy],
      sr,
      bez,

      glass: [
        sx,
        sy + 0.8 * p,
        (shx - bez - gap) * scale * (1 + sq),
        (shy - bez - gap) * scale * (1 - sq)
      ],

      gr: Math.max(sr - bez - gap, 0) * scale,
      mag: 1.03 + 0.05 * p
    };
  }

  function stepPhysics(dt) {
    if (reduce) {
      spring.x = spring.target;
      spring.v = 0;

      squash.x = squash.v = 0;

      ripples = [];

      return false;
    }

    const w = (2 * Math.PI) / (motionSec * 2.2);

    const n = 3;
    const h = dt / n;

    for (let i = 0; i < n; i++) {
      spring.v += (
        w * w * (spring.target - spring.x) -
        2 * 0.5 * w * spring.v
      ) * h;

      spring.x += spring.v * h;

      const w2 = w * 1.7;

      squash.v += (
        -w2 * w2 * squash.x -
        2 * 0.22 * w2 * squash.v
      ) * h;

      squash.x += squash.v * h;
    }

    squash.x = Math.max(
      -0.07,
      Math.min(0.07, squash.x)
    );

    for (const r of ripples) {
      r.age += dt;
    }

    ripples = ripples.filter((r) => r.age < 1.6);

    return (
      Math.abs(spring.x - spring.target) > 0.0005 ||
      Math.abs(spring.v) > 0.001 ||
      Math.abs(squash.x) > 0.0002 ||
      Math.abs(squash.v) > 0.001 ||
      ripples.length > 0
    );
  }

  function pointIn(ev) {
    const r = canvas.getBoundingClientRect();

    return [
      ev.clientX - r.left,
      ev.clientY - r.top
    ];
  }

  function pressStart(point) {
    if (pressed) return;

    pressed = true;

    /* NEW: start a round only when the user presses the glass. */
    if (
      S.phase === 'in' &&
      (
        S.kids.mode === 'hidden' ||
        S.kids.mode === 'done'
      )
    ) {
      S.kids.mode = 'approach';

      S.kids.x1 = -10;
      S.kids.x2 = -27;

      S.kids.slidePower = 0;
      S.kids.escapeSpeed = 0;

      say('Two kids are coming to ring the bell!');
    }

    audio();

    const now = performance.now() / 1000;

    spring.target = 1;
    squash.v += 0.9;

    lastPoint = point || [
      cssW / 2,
      cssH / 2
    ];

    lastSlideTime = now;

    if (!reduce) {
      ripples.push({
        x: lastPoint[0],
        y: lastPoint[1],
        age: 0,
        amp: 1
      });
    }

    if (ripples.length > 4) {
      ripples.shift();
    }

    sfx('press');

    wake();
  }

  function pressEnd() {
    if (!pressed) return;

    pressed = false;

    spring.target = 0;
    squash.v -= 1.3;

    const pt = lastPoint || [
      cssW / 2,
      cssH / 2
    ];

    if (!reduce) {
      ripples.push({
        x: pt[0],
        y: pt[1],
        age: 0,
        amp: 0.55
      });
    }

    if (ripples.length > 4) {
      ripples.shift();
    }

    sfx('release');
    wake();
  }

  /* ---------- Original rendering loop ---------- */

  let raf = 0;
  let last = 0;
  let sceneClock = 0;
  let glassMoving = true;
  let dirty = true;
  let inView = true;

  const FRAME = 1 / 12;

  function render(now) {
    const g = geometry();

    if (gl) {
      gl.viewport(
        0,
        0,
        canvas.width,
        canvas.height
      );

      gl.uniform2f(
        U.uRes,
        canvas.width,
        canvas.height
      );

      gl.uniform1f(
        U.uDpr,
        canvas.width / Math.max(1, cssW)
      );

      gl.uniform4fv(U.uSock, g.sock);
      gl.uniform1f(U.uSR, g.sr);
      gl.uniform1f(U.uBez, g.bez);

      gl.uniform4fv(U.uGl, g.glass);
      gl.uniform1f(U.uGR, g.gr);
      gl.uniform1f(U.uMag, g.mag);

      gl.uniform1f(
        U.uPress,
        Math.max(0, Math.min(1.2, spring.x))
      );

      const rip = new Float32Array(16);

      ripples.slice(-4).forEach((r, i) => {
        rip.set(
          [r.x, r.y, r.age, r.amp],
          i * 4
        );
      });

      gl.uniform4fv(U.uRip, rip);

      gl.clearColor(0, 0, 0, 0);
      gl.clear(gl.COLOR_BUFFER_BIT);

      gl.drawArrays(gl.TRIANGLES, 0, 3);
    } else if (ctx2d) {
      const c = fallbackCanvas.getContext('2d');

      c.putImageData(
        new ImageData(
          new Uint8ClampedArray(pix.buffer.slice(0)),
          TW,
          TH
        ),
        0,
        0
      );

      const k = canvas.width / Math.max(1, cssW);

      ctx2d.setTransform(1, 0, 0, 1, 0, 0);

      ctx2d.clearRect(
        0,
        0,
        canvas.width,
        canvas.height
      );

      ctx2d.setTransform(k, 0, 0, k, 0, 0);

      const [sx, sy, hx, hy] = g.sock;

      const wx = hx - g.bez;
      const wy = hy - g.bez;

      ctx2d.save();
      ctx2d.beginPath();

      ctx2d.roundRect(
        sx - wx,
        sy - wy,
        wx * 2,
        wy * 2,
        Math.max(0, g.sr - g.bez)
      );

      ctx2d.clip();

      ctx2d.imageSmoothingEnabled = false;

      ctx2d.drawImage(
        fallbackCanvas,
        M,
        M,
        SW,
        SH,
        sx - wx,
        sy - wy,
        wx * 2,
        wy * 2
      );

      ctx2d.restore();
    }
  }

  function frame(ms) {
    raf = 0;

    const now = ms / 1000;

    const dt = Math.min(
      1 / 20,
      last ? now - last : 1 / 60
    );

    last = now;

    glassMoving = stepPhysics(dt);
    sceneClock += dt;

    if (sceneClock >= FRAME || dirty) {
      sceneClock =
        sceneClock >= FRAME
          ? sceneClock % FRAME
          : sceneClock;

      update(now, Math.max(dt, FRAME));
      drawScene(now);

      if (gl) {
        gl.bindTexture(gl.TEXTURE_2D, tex);

        gl.texSubImage2D(
          gl.TEXTURE_2D,
          0,
          0,
          0,
          TW,
          TH,
          gl.RGBA,
          gl.UNSIGNED_BYTE,
          pixBytes
        );
      }

      dirty = true;
    }

    if (dirty || glassMoving) {
      render(now);
      dirty = false;
    }

    if (running()) {
      raf = win.requestAnimationFrame(frame);
    }
  }

  function running() {
    return (
      inView &&
      doc.visibilityState !== 'hidden'
    );
  }

  function wake() {
    dirty = true;

    if (!raf && running()) {
      last = 0;
      raf = win.requestAnimationFrame(frame);
    }
  }

  /* ---------- Initialize ---------- */

  buildPalette();
  drawBase();
  readGeometry();
  drawScene(performance.now() / 1000);

  let glOk = false;

  try {
    glOk = initGL();
  } catch (err) {
    glOk = false;
    gl = null;
  }

  if (!glOk) {
    gl = null;

    ctx2d = canvas.getContext('2d');

    fallbackCanvas = doc.createElement('canvas');
    fallbackCanvas.width = TW;
    fallbackCanvas.height = TH;
  }

  setColorUniforms();

  /* ---------- Pointer and keyboard ---------- */

  const onPointerDown = (ev) => {
    if (ev.button !== 0) return;

    ev.preventDefault();

    try {
      hit.setPointerCapture(ev.pointerId);
    } catch (err) {
      /* Continue if pointer capture is unavailable. */
    }

    pressStart(pointIn(ev));
  };

  /* NEW: capture drag speed while preserving original glass movement. */
  const onPointerMove = (ev) => {
    if (!pressed) return;

    const point = pointIn(ev);
    const now = performance.now() / 1000;

    if (
      lastPoint &&
      (
        S.kids.mode === 'waiting' ||
        S.kids.mode === 'escape'
      )
    ) {
      const dx = point[0] - lastPoint[0];
      const dy = point[1] - lastPoint[1];

      const distance = Math.hypot(dx, dy);

      const elapsed = Math.max(
        0.008,
        now - lastSlideTime
      );

      const speed =
        distance /
        elapsed /
        Math.max(1, cssW);

      const power = clamp01(speed / 2.2);

      S.kids.slidePower = Math.max(
        S.kids.slidePower,
        power
      );
    }

    lastPoint = point;
    lastSlideTime = now;

    wake();
  };

  const onPointerUp = () => pressEnd();

  let keyDown = false;

  const onKeyDown = (ev) => {
    if (ev.key !== ' ' && ev.key !== 'Enter') return;

    ev.preventDefault();

    if (ev.repeat || keyDown) return;

    keyDown = true;
    pressStart(null);
  };

  const onKeyUp = (ev) => {
    if (ev.key !== ' ' && ev.key !== 'Enter') return;

    ev.preventDefault();

    if (!keyDown) return;

    keyDown = false;
    pressEnd();
  };

  const onBlur = () => {
    keyDown = false;
    pressEnd();
  };

  const onClick = (ev) => {
    if (ev.detail !== 0 || pressed || keyDown) return;

    pressStart(null);

    win.setTimeout(pressEnd, 140);
  };

  hit.addEventListener('pointerdown', onPointerDown);
  hit.addEventListener('pointermove', onPointerMove);
  hit.addEventListener('pointerup', onPointerUp);
  hit.addEventListener('pointercancel', onPointerUp);
  hit.addEventListener('lostpointercapture', onPointerUp);
  hit.addEventListener('keydown', onKeyDown);
  hit.addEventListener('keyup', onKeyUp);
  hit.addEventListener('blur', onBlur);
  hit.addEventListener('click', onClick);

  hit.addEventListener(
    'contextmenu',
    (ev) => ev.preventDefault()
  );

  /* ---------- Reduced motion ---------- */

  const onReduce = () => {
    reduce = reduceQuery.matches;
    wake();
  };

  reduceQuery.addEventListener('change', onReduce);

  /* ---------- Visibility ---------- */

  const onVisibility = () => {
    if (running()) {
      wake();
    } else if (raf) {
      win.cancelAnimationFrame(raf);
      raf = 0;
    }
  };

  doc.addEventListener(
    'visibilitychange',
    onVisibility
  );

  /* ---------- Resize and intersection ---------- */

  const io = new win.IntersectionObserver((entries) => {
    inView = entries.some((en) => en.isIntersecting);
    onVisibility();
  });

  io.observe(root);

  const ro = new win.ResizeObserver(() => {
    readGeometry();
    wake();
  });

  ro.observe(root);

  /* ---------- WebGL context ---------- */

  const onLost = (ev) => {
    ev.preventDefault();

    if (raf) {
      win.cancelAnimationFrame(raf);
    }

    raf = 0;
  };

  const onRestored = () => {
    try {
      initGL();
      setColorUniforms();
    } catch (err) {
      gl = null;
    }

    wake();
  };

  canvas.addEventListener('webglcontextlost', onLost);
  canvas.addEventListener('webglcontextrestored', onRestored);

  wake();

  /* ---------- Cleanup ---------- */

  return () => {
    if (raf) {
      win.cancelAnimationFrame(raf);
    }

    raf = 0;

    io.disconnect();
    ro.disconnect();

    doc.removeEventListener('visibilitychange', onVisibility);
    reduceQuery.removeEventListener('change', onReduce);

    hit.removeEventListener('pointerdown', onPointerDown);
    hit.removeEventListener('pointermove', onPointerMove);
    hit.removeEventListener('pointerup', onPointerUp);
    hit.removeEventListener('pointercancel', onPointerUp);
    hit.removeEventListener('lostpointercapture', onPointerUp);
    hit.removeEventListener('keydown', onKeyDown);
    hit.removeEventListener('keyup', onKeyUp);
    hit.removeEventListener('blur', onBlur);
    hit.removeEventListener('click', onClick);

    canvas.removeEventListener('webglcontextlost', onLost);
    canvas.removeEventListener('webglcontextrestored', onRestored);

    if (actx) {
      actx.close();
    }

    if (gl) {
      const lose = gl.getExtension('WEBGL_lose_context');

      if (lose) {
        lose.loseContext();
      }
    }
  };
}
mount(document.querySelector('[data-ld-part="button-resident-01c5"]'));
</script>

ファイルか分け目を選ぶと、ここに本文が出ます。

ほかの作品