Back to Arañas Pistoleras
SOURCE / PINNED RELEASE

Made of little things.

Arañas Pistoleras

Release
2578a037ceaf…
Author-recorded commit
e35cc0733a7b…
License
LICENSE
Author’s source reference
nostr://npub1eg5pfnws6tg6rtp33tugpppqtv5u05crmf983qp438cdycwzngys0gxzcs/wss%3A%2F%2Fgit.napplet.soy%2F/n-7bf0a402584

Archive hash verified: 52cdda8c063eaf88…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/fx.ts
// Everything that flies, splats, smokes and cracks. Purely for show: the rules
// never read anything from here.

import { drawTinyButterfly } from './butterfly.js';
import type { Kind, View } from './game.js';
import { drawHat, drawPistol, glow, type Palette } from './spider.js';

type Ctx = CanvasRenderingContext2D;
const TAU = Math.PI * 2;

type PartType = 'goo' | 'smoke' | 'spark' | 'leg' | 'gun' | 'hat' | 'ghost' | 'confetti' | 'ring' | 'heartHalf' | 'dust' | 'twinkle' | 'fly';

interface Part {
  type: PartType;
  x: number;
  y: number;
  vx: number;
  vy: number;
  rot: number;
  vr: number;
  age: number;
  life: number;
  size: number;
  color: string;
  color2: string;
  /** Where it lands, or Infinity. */
  floor: number;
  n: number;
  bounced: number;
  /** For a caught butterfly: where it started, where it is going, and what it is. */
  sx: number;
  sy: number;
  tx: number;
  ty: number;
  kind: Kind;
}

interface Word {
  x: number;
  y: number;
  text: string;
  age: number;
  life: number;
  size: number;
  color: string;
  tilt: number;
}

interface Crack {
  x: number;
  y: number;
  age: number;
  life: number;
  lines: number[][];
  rings: [number, number, number][];
}

interface Decal {
  x: number;
  y: number;
  age: number;
  life: number;
  color: string;
  blobs: [number, number, number][];
  drips: [number, number, number][];
}

const WORDS = ['¡PAF!', '¡ZAS!', '¡PUM!', '¡SPLAT!', '¡TOMA!', '¡PLAF!', '¡CHAO!'];

export class Fx {
  parts: Part[] = [];
  words: Word[] = [];
  cracks: Crack[] = [];
  decals: Decal[] = [];
  shake = 0;
  red = 0;
  white = 0;
  /** Set by the caller when the player asked for less motion. */
  calm = false;
  /** The visible area, so words stay inside it. */
  view: View = { w: 1265, h: 790 };
  private lastWord = -1;

  clear(): void {
    this.parts = [];
    this.words = [];
    this.cracks = [];
    this.decals = [];
    this.shake = this.red = this.white = 0;
  }

  private add(p: Partial<Part> & { type: PartType; x: number; y: number }): void {
    if (this.parts.length > 520) this.parts.shift();
    this.parts.push({ vx: 0, vy: 0, rot: 0, vr: 0, age: 0, life: 1, size: 10, color: '#fff', color2: '#fff', floor: Infinity, n: 0, bounced: 0, sx: 0, sy: 0, tx: 0, ty: 0, kind: 'widow', ...p });
  }

  update(dt: number): void {
    this.shake = Math.max(0, this.shake - dt * 3.2);
    this.red = Math.max(0, this.red - dt * 1.7);
    this.white = Math.max(0, this.white - dt * 9);
    for (let i = this.parts.length - 1; i >= 0; i--) {
      const p = this.parts[i];
      p.age += dt;
      if (p.age >= p.life) {
        this.parts.splice(i, 1);
        continue;
      }
      switch (p.type) {
        case 'goo':
        case 'leg':
        case 'gun':
        case 'hat':
        case 'confetti':
        case 'heartHalf':
          p.vy += (p.type === 'confetti' ? 380 : 1500) * dt;
          if (p.type === 'confetti') {
            p.vx *= Math.pow(0.3, dt);
            p.vy = Math.min(p.vy, 210);
          }
          break;
        case 'smoke':
          p.vy -= 26 * dt;
          p.vx *= Math.pow(0.25, dt);
          p.size += 34 * dt;
          break;
        case 'spark':
          p.vy += 500 * dt;
          break;
        case 'ghost':
          p.vx = Math.sin(p.age * 4 + p.n) * 34;
          break;
        case 'fly': {
          // A loop upwards first, then straight into the jar.
          const k = p.age / p.life;
          const e = k * k * (3 - 2 * k);
          p.x = p.sx + (p.tx - p.sx) * e + Math.sin(k * Math.PI) * p.n * 90;
          p.y = p.sy + (p.ty - p.sy) * e - Math.sin(k * Math.PI) * 70;
          break;
        }
        default:
          break;
      }
      p.x += p.vx * dt;
      p.y += p.vy * dt;
      p.rot += p.vr * dt;
      if (p.y > p.floor && p.vy > 0) {
        p.y = p.floor;
        if (p.type === 'goo') {
          // A drop on the floor: leave a small stain and go.
          this.stain(p.x, p.y, p.size * 1.4, p.color, 0.35);
          this.parts.splice(i, 1);
        } else {
          p.vy *= -0.42;
          p.vx *= 0.6;
          p.vr *= 0.5;
          p.bounced++;
          if (Math.abs(p.vy) < 80) {
            p.vy = 0;
            p.vr = 0;
            p.floor = Infinity;
            p.vx = 0;
            // Lie there for a moment, then fade.
            p.life = Math.min(p.life, p.age + 0.9);
          }
        }
      }
    }
    for (let i = this.words.length - 1; i >= 0; i--) {
      const w = this.words[i];
      w.age += dt;
      w.y -= 36 * dt;
      if (w.age >= w.life) this.words.splice(i, 1);
    }
    for (let i = this.cracks.length - 1; i >= 0; i--) {
      this.cracks[i].age += dt;
      if (this.cracks[i].age >= this.cracks[i].life) this.cracks.splice(i, 1);
    }
    for (let i = this.decals.length - 1; i >= 0; i--) {
      this.decals[i].age += dt;
      if (this.decals[i].age >= this.decals[i].life) this.decals.splice(i, 1);
    }
  }

  // ------------------------------------------------------------ emitters

  /** The click itself: a comic burst. */
  impact(x: number, y: number, hit: boolean): void {
    this.add({ type: 'ring', x, y, life: 0.28, size: hit ? 70 : 34, color: hit ? '#fff3b0' : 'rgba(255,255,255,0.7)' });
    if (!hit) {
      for (let i = 0; i < 5; i++) {
        const a = Math.random() * TAU;
        this.add({ type: 'dust', x, y, vx: Math.cos(a) * 60, vy: Math.sin(a) * 60 - 20, life: 0.45, size: 6 + Math.random() * 5, color: 'rgba(225,225,235,0.6)' });
      }
      return;
    }
    for (let i = 0; i < 9; i++) {
      const a = Math.random() * TAU;
      const s = 260 + Math.random() * 380;
      this.add({ type: 'spark', x, y, vx: Math.cos(a) * s, vy: Math.sin(a) * s, life: 0.3 + Math.random() * 0.2, size: 3.5, color: '#fff0a0' });
    }
    this.shake = Math.max(this.shake, 0.25);
  }

  word(x: number, y: number, size = 58, text?: string, color = '#ffe14a'): void {
    let i = Math.floor(Math.random() * WORDS.length);
    if (i === this.lastWord) i = (i + 1) % WORDS.length;
    this.lastWord = i;
    const label = text ?? WORDS[i];
    // Kept inside the picture and below the HUD.
    const half = label.length * size * 0.27;
    const cx = Math.max(half + 10, Math.min(this.view.w - half - 10, x));
    const cy = Math.max(size * 0.7 + 90, Math.min(this.view.h - size, y));
    this.words.push({ x: cx, y: cy, text: label, age: 0, life: 0.85, size, color, tilt: (Math.random() - 0.5) * 0.4 });
  }

  private stain(x: number, y: number, r: number, color: string, life = 1): void {
    if (this.decals.length > 16) this.decals.shift();
    const blobs: [number, number, number][] = [];
    const n = r > 30 ? 9 : 3;
    for (let i = 0; i < n; i++) {
      const a = Math.random() * TAU;
      const d = Math.random() * r * 0.9;
      blobs.push([Math.cos(a) * d, Math.sin(a) * d * (r > 30 ? 1 : 0.4), r * (0.25 + Math.random() * 0.5)]);
    }
    const drips: [number, number, number][] = [];
    if (r > 30) for (let i = 0; i < 4; i++) drips.push([(Math.random() - 0.5) * r * 1.4, r * (0.5 + Math.random() * 1.3), 4 + Math.random() * 5]);
    this.decals.push({ x, y, age: 0, life: 7 * life, color, blobs, drips });
  }

  /** A spider bursts: goo, eight legs, its pistols, maybe a hat, and a little ghost. */
  burst(x: number, y: number, scale: number, pal: Palette, floor: number, opts: { hat?: number; pistols?: number; gold?: boolean; big?: boolean } = {}): void {
    const big = opts.big ? 2.2 : 1;
    this.stain(x, y, 58 * scale, pal.goo);
    const drops = Math.round((opts.big ? 46 : 16) * (this.calm ? 0.5 : 1));
    for (let i = 0; i < drops; i++) {
      const a = Math.random() * TAU;
      const s = (160 + Math.random() * 560) * Math.sqrt(scale) * (opts.big ? 1.3 : 1);
      this.add({ type: 'goo', x: x + Math.cos(a) * 20 * scale, y: y + Math.sin(a) * 20 * scale, vx: Math.cos(a) * s, vy: Math.sin(a) * s - 260, life: 1.6, size: (5 + Math.random() * 9) * Math.sqrt(scale), color: pal.goo, floor: floor + 40 + Math.random() * 150 });
    }
    for (let i = 0; i < 8; i++) {
      const side = i < 4 ? -1 : 1;
      const a = -Math.PI / 2 + side * (0.4 + (i % 4) * 0.45);
      const s = 420 + Math.random() * 300;
      this.add({ type: 'leg', x: x + side * 40 * scale, y, vx: Math.cos(a) * s * big * 0.7, vy: Math.sin(a) * s - 150, rot: Math.random() * TAU, vr: (Math.random() - 0.5) * 22, life: 2.2, size: 62 * scale, color: pal.leg, color2: pal.rim, floor: floor + 50 + Math.random() * 130 });
    }
    const guns = opts.pistols ?? 2;
    for (let i = 0; i < guns; i++) {
      const side = i % 2 ? 1 : -1;
      this.add({ type: 'gun', x: x + side * 60 * scale, y: y + 30 * scale, vx: side * (130 + Math.random() * 200), vy: -520 - Math.random() * 240, rot: 0, vr: side * (9 + Math.random() * 8), life: 3, size: scale * 1.1, color: pal.gun, color2: pal.gunHi, floor: floor + 70 + Math.random() * 110 });
    }
    if (opts.hat !== undefined && opts.hat >= 0) this.hat(x, y - 60 * scale, scale, opts.hat, floor);
    for (let i = 0; i < 7; i++) {
      const a = Math.random() * TAU;
      this.add({ type: 'smoke', x: x + Math.cos(a) * 30 * scale, y: y + Math.sin(a) * 30 * scale, vx: Math.cos(a) * 80, vy: Math.sin(a) * 60 - 30, life: 0.7 + Math.random() * 0.4, size: 26 * scale, color: '235,235,245' });
    }
    this.add({ type: 'ring', x, y, life: 0.4, size: 150 * scale, color: pal.goo });
    this.add({ type: 'ghost', x, y: y - 20 * scale, vy: -84, life: 1.9, size: 30 * Math.sqrt(scale), n: Math.random() * 6 });
    this.shake = Math.max(this.shake, opts.big ? 1 : 0.45);
  }

  /** A butterfly is caught: glitter where it was, and it flies off to the jar in the HUD. */
  collect(x: number, y: number, scale: number, kind: Kind, glitter: string, tx: number, ty: number, big = false): void {
    const count = Math.round((big ? 40 : 14) * (this.calm ? 0.5 : 1));
    for (let i = 0; i < count; i++) {
      const a = Math.random() * TAU;
      const sp = (80 + Math.random() * 300) * (big ? 1.6 : 1);
      this.add({ type: 'twinkle', x, y, vx: Math.cos(a) * sp, vy: Math.sin(a) * sp, life: 0.5 + Math.random() * 0.6, size: 9 + Math.random() * 12, color: i % 3 ? glitter : '#ffffff' });
    }
    this.add({ type: 'ring', x, y, life: 0.4, size: 150 * scale, color: glitter });
    const flock = big ? 9 : 1;
    for (let i = 0; i < flock; i++) {
      this.add({ type: 'fly', x, y, sx: x + (big ? (Math.random() - 0.5) * 200 : 0), sy: y + (big ? (Math.random() - 0.5) * 140 : 0), tx, ty, kind: big && i ? (['widow', 'hat', 'mini'] as Kind[])[i % 3] : kind, life: 0.75 + i * 0.09, size: scale * (big && i ? 0.5 : 1), n: Math.random() < 0.5 ? -1 : 1 });
    }
    this.shake = Math.max(this.shake, big ? 0.6 : 0.15);
  }

  /** A butterfly got away with a heart: a soft blush instead of broken glass. */
  flee(x: number, y: number, glitter: string): void {
    this.red = 0.55;
    for (let i = 0; i < 16; i++) {
      const a = Math.random() * TAU;
      const sp = 120 + Math.random() * 360;
      this.add({ type: 'twinkle', x, y, vx: Math.cos(a) * sp, vy: Math.sin(a) * sp - 80, life: 0.6 + Math.random() * 0.5, size: 10 + Math.random() * 12, color: i % 2 ? glitter : '#ffffff' });
    }
    this.add({ type: 'ring', x, y, life: 0.5, size: 220, color: '#ffffff' });
  }

  hat(x: number, y: number, scale: number, style: number, floor: number): void {
    const side = Math.random() < 0.5 ? -1 : 1;
    this.add({ type: 'hat', x, y, vx: side * (120 + Math.random() * 160), vy: -640, rot: 0, vr: side * 7, life: 3.2, size: scale, n: style, floor: floor + 60 + Math.random() * 100 });
  }

  /** Smoke curling from a barrel after the shot. */
  gunSmoke(x: number, y: number): void {
    for (let i = 0; i < 5; i++) {
      this.add({ type: 'smoke', x: x + (Math.random() - 0.5) * 16, y, vx: (Math.random() - 0.5) * 50, vy: -50 - Math.random() * 50, life: 0.9 + Math.random() * 0.5, size: 12 + Math.random() * 8, color: '220,220,230' });
    }
  }

  /** The player was hit: the glass in front of the scene cracks. */
  crack(view: View): void {
    const x = view.w * (0.22 + Math.random() * 0.56);
    const y = view.h * (0.25 + Math.random() * 0.45);
    const lines: number[][] = [];
    const n = 9 + Math.floor(Math.random() * 4);
    for (let i = 0; i < n; i++) {
      const a = (i / n) * TAU + Math.random() * 0.4;
      const len = 80 + Math.random() * 170;
      const pts = [0, 0];
      let px = 0;
      let py = 0;
      const steps = 3 + Math.floor(Math.random() * 2);
      for (let s = 1; s <= steps; s++) {
        const j = a + (Math.random() - 0.5) * 0.5;
        px += (Math.cos(j) * len) / steps;
        py += (Math.sin(j) * len) / steps;
        pts.push(px, py);
      }
      lines.push(pts);
    }
    const rings: [number, number, number][] = [];
    for (let i = 0; i < 7; i++) rings.push([26 + Math.random() * 90, Math.random() * TAU, 0.3 + Math.random() * 0.6]);
    if (this.cracks.length > 4) this.cracks.shift();
    this.cracks.push({ x, y, age: 0, life: 4.5, lines, rings });
    this.red = 1;
    this.white = 1;
    this.shake = 1.3;
    for (let i = 0; i < 12; i++) {
      const a = Math.random() * TAU;
      const s = 200 + Math.random() * 500;
      this.add({ type: 'spark', x, y, vx: Math.cos(a) * s, vy: Math.sin(a) * s, life: 0.5, size: 4, color: '#e8f4ff' });
    }
  }

  heartBreak(x: number, y: number, size: number): void {
    for (const side of [-1, 1]) {
      this.add({ type: 'heartHalf', x, y, vx: side * 130, vy: -330, rot: 0, vr: side * 5, life: 1.3, size, n: side });
    }
  }

  sparkle(x: number, y: number, color: string, count = 10): void {
    for (let i = 0; i < count; i++) {
      const a = Math.random() * TAU;
      const s = 60 + Math.random() * 220;
      this.add({ type: 'twinkle', x, y, vx: Math.cos(a) * s, vy: Math.sin(a) * s, life: 0.5 + Math.random() * 0.5, size: 8 + Math.random() * 10, color });
    }
  }

  confetti(view: View, count = 70): void {
    const colors = ['#ffe14a', '#ff5ad8', '#7dff6a', '#5ad8ff', '#ff7a4a', '#ffffff'];
    for (let i = 0; i < count; i++) {
      this.add({ type: 'confetti', x: Math.random() * view.w, y: -20 - Math.random() * view.h * 0.4, vx: (Math.random() - 0.5) * 260, vy: 120 + Math.random() * 160, rot: Math.random() * TAU, vr: (Math.random() - 0.5) * 12, life: 5, size: 9 + Math.random() * 9, color: colors[i % colors.length], n: Math.random() * 6 });
    }
  }

  // -------------------------------------------------------------- drawing

  /** Stains on the scene, behind the spiders. */
  drawBack(ctx: Ctx): void {
    for (const d of this.decals) {
      const k = d.age / d.life;
      ctx.globalAlpha = Math.min(1, (1 - k) * 2.2) * 0.85;
      ctx.fillStyle = d.color;
      const grow = Math.min(1, d.age * 9);
      for (const [bx, by, br] of d.blobs) {
        ctx.beginPath();
        ctx.arc(d.x + bx * grow, d.y + by * grow, br * grow, 0, TAU);
        ctx.fill();
      }
      for (const [dx, len, w] of d.drips) {
        const l = len * Math.min(1, d.age * 0.7);
        ctx.beginPath();
        ctx.roundRect(d.x + dx - w / 2, d.y, w, l, w / 2);
        ctx.fill();
        ctx.beginPath();
        ctx.arc(d.x + dx, d.y + l, w * 0.8, 0, TAU);
        ctx.fill();
      }
    }
    ctx.globalAlpha = 1;
  }

  draw(ctx: Ctx): void {
    for (const p of this.parts) {
      const k = p.age / p.life;
      switch (p.type) {
        case 'goo':
          ctx.fillStyle = p.color;
          ctx.beginPath();
          ctx.ellipse(p.x, p.y, p.size * 0.8, p.size * 1.15, Math.atan2(p.vy, p.vx) + Math.PI / 2, 0, TAU);
          ctx.fill();
          break;
        case 'smoke':
          ctx.fillStyle = `rgba(${p.color},${(1 - k) * 0.42})`;
          ctx.beginPath();
          ctx.arc(p.x, p.y, p.size, 0, TAU);
          ctx.fill();
          break;
        case 'dust':
          ctx.globalAlpha = 1 - k;
          ctx.fillStyle = p.color;
          ctx.beginPath();
          ctx.arc(p.x, p.y, p.size * (0.6 + k), 0, TAU);
          ctx.fill();
          ctx.globalAlpha = 1;
          break;
        case 'leg':
          ctx.save();
          ctx.translate(p.x, p.y);
          ctx.rotate(p.rot);
          ctx.globalAlpha = Math.min(1, (1 - k) * 4);
          ctx.lineCap = 'round';
          ctx.lineJoin = 'round';
          for (const [w, c] of [[14, p.color2], [10, p.color]] as [number, string][]) {
            ctx.strokeStyle = c;
            ctx.lineWidth = (w * p.size) / 62;
            ctx.beginPath();
            ctx.moveTo(-p.size * 0.5, p.size * 0.15);
            ctx.lineTo(0, -p.size * 0.3);
            ctx.lineTo(p.size * 0.55, p.size * 0.25);
            ctx.stroke();
          }
          ctx.restore();
          break;
        case 'gun':
          ctx.save();
          ctx.translate(p.x, p.y);
          ctx.rotate(p.rot);
          ctx.globalAlpha = Math.min(1, (1 - k) * 4);
          drawPistol(ctx, 0, p.color, p.color2, p.size);
          ctx.restore();
          break;
        case 'hat':
          ctx.save();
          ctx.translate(p.x, p.y);
          ctx.rotate(p.rot);
          ctx.scale(p.size, p.size);
          ctx.globalAlpha = Math.min(1, (1 - k) * 4);
          drawHat(ctx, p.n, 0);
          ctx.restore();
          break;
        case 'ghost':
          drawGhost(ctx, p.x, p.y, p.size, Math.min(1, (1 - k) * 2) * Math.min(1, p.age * 5), p.age);
          break;
        case 'confetti':
          ctx.save();
          ctx.translate(p.x + Math.sin(p.age * 3 + p.n) * 20, p.y);
          ctx.rotate(p.rot);
          ctx.globalAlpha = Math.min(1, (1 - k) * 3);
          ctx.fillStyle = p.color;
          ctx.fillRect(-p.size / 2, (-p.size / 4) * Math.cos(p.age * 9 + p.n), p.size, (p.size / 2) * Math.cos(p.age * 9 + p.n));
          ctx.restore();
          break;
        case 'ring':
          ctx.strokeStyle = p.color;
          ctx.globalAlpha = 1 - k;
          ctx.lineWidth = 7 * (1 - k) + 1;
          ctx.beginPath();
          ctx.arc(p.x, p.y, p.size * (0.3 + 0.7 * Math.sqrt(k)), 0, TAU);
          ctx.stroke();
          ctx.globalAlpha = 1;
          break;
        case 'fly':
          drawTinyButterfly(ctx, p.kind, p.x, p.y, p.size * (1 - 0.8 * k) * 0.8, p.age, Math.min(1, (1 - k) * 5));
          break;
        case 'heartHalf':
          ctx.save();
          ctx.translate(p.x, p.y);
          ctx.rotate(p.rot);
          ctx.globalAlpha = 1 - k;
          ctx.beginPath();
          ctx.rect(p.n < 0 ? -p.size * 2 : 0, -p.size * 2, p.size * 2, p.size * 4);
          ctx.clip();
          heartPath(ctx, 0, 0, p.size);
          ctx.fillStyle = '#ff4a6a';
          ctx.fill();
          ctx.restore();
          break;
        default:
          break;
      }
    }
    ctx.globalAlpha = 1;
  }

  /** Bright bits, drawn with additive blending after the darkness. */
  drawGlow(ctx: Ctx, sprite: CanvasImageSource): void {
    for (const p of this.parts) {
      const k = p.age / p.life;
      if (p.type === 'spark') {
        ctx.strokeStyle = p.color;
        ctx.globalAlpha = 1 - k;
        ctx.lineWidth = p.size;
        ctx.lineCap = 'round';
        ctx.beginPath();
        ctx.moveTo(p.x, p.y);
        ctx.lineTo(p.x - p.vx * 0.035, p.y - p.vy * 0.035);
        ctx.stroke();
      } else if (p.type === 'twinkle') {
        ctx.globalAlpha = (1 - k) * 0.9;
        glow(ctx, sprite, p.x, p.y, p.size * (1.4 - k), p.color);
      }
    }
    ctx.globalAlpha = 1;
  }

  drawWords(ctx: Ctx, font: string): void {
    for (const w of this.words) {
      const k = w.age / w.life;
      const pop = k < 0.18 ? 0.4 + (k / 0.18) * 0.85 : 1.25 - Math.min(0.25, (k - 0.18) * 1.6);
      ctx.save();
      ctx.translate(w.x, w.y);
      ctx.rotate(w.tilt);
      ctx.scale(pop, pop);
      ctx.globalAlpha = k > 0.7 ? (1 - k) / 0.3 : 1;
      ctx.font = `${w.size}px ${font}`;
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      ctx.lineJoin = 'round';
      ctx.strokeStyle = '#1a0f24';
      ctx.lineWidth = w.size * 0.2;
      ctx.strokeText(w.text, 0, 0);
      ctx.fillStyle = w.color;
      ctx.fillText(w.text, 0, 0);
      ctx.restore();
    }
  }

  /** Cracked glass and the flashes, over everything but the HUD. */
  drawScreen(ctx: Ctx, view: View): void {
    for (const c of this.cracks) {
      const k = c.age / c.life;
      const a = k > 0.6 ? (1 - k) / 0.4 : 1;
      const grow = Math.min(1, c.age * 14);
      ctx.save();
      ctx.translate(c.x, c.y);
      ctx.globalAlpha = a;
      ctx.lineCap = 'round';
      ctx.lineJoin = 'round';
      for (const [w, col] of [[5, 'rgba(10,14,30,0.5)'], [2.2, 'rgba(235,245,255,0.95)']] as [number, string][]) {
        ctx.strokeStyle = col;
        ctx.lineWidth = w;
        for (const pts of c.lines) {
          ctx.beginPath();
          ctx.moveTo(0, 0);
          for (let i = 2; i < pts.length; i += 2) ctx.lineTo(pts[i] * grow, pts[i + 1] * grow);
          ctx.stroke();
        }
        for (const [r, a0, len] of c.rings) {
          ctx.beginPath();
          ctx.arc(0, 0, r * grow, a0, a0 + len);
          ctx.stroke();
        }
      }
      // The hole.
      ctx.fillStyle = '#05060a';
      ctx.beginPath();
      ctx.arc(0, 0, 13, 0, TAU);
      ctx.fill();
      ctx.strokeStyle = 'rgba(235,245,255,0.9)';
      ctx.lineWidth = 3;
      ctx.stroke();
      ctx.restore();
    }
    if (this.red > 0) {
      const g = ctx.createRadialGradient(view.w / 2, view.h / 2, Math.min(view.w, view.h) * 0.25, view.w / 2, view.h / 2, Math.hypot(view.w, view.h) * 0.6);
      g.addColorStop(0, 'rgba(255,30,40,0)');
      g.addColorStop(1, `rgba(255,30,40,${0.6 * this.red})`);
      ctx.fillStyle = g;
      ctx.fillRect(0, 0, view.w, view.h);
    }
    if (this.white > 0 && !this.calm) {
      ctx.fillStyle = `rgba(255,250,235,${0.4 * this.white})`;
      ctx.fillRect(0, 0, view.w, view.h);
    }
  }

  /** How far to push the picture this frame. */
  offset(t: number): { x: number; y: number } {
    if (this.calm || this.shake <= 0) return { x: 0, y: 0 };
    const m = Math.min(1.2, this.shake) * 13;
    return { x: Math.sin(t * 71) * m, y: Math.cos(t * 59) * m };
  }
}

export function heartPath(ctx: Ctx, x: number, y: number, s: number): void {
  ctx.beginPath();
  ctx.moveTo(x, y + s * 0.95);
  ctx.bezierCurveTo(x - s * 1.5, y - s * 0.1, x - s * 0.8, y - s * 1.25, x, y - s * 0.45);
  ctx.bezierCurveTo(x + s * 0.8, y - s * 1.25, x + s * 1.5, y - s * 0.1, x, y + s * 0.95);
  ctx.closePath();
}

/** The little soul of a spider, off to spider heaven. */
export function drawGhost(ctx: Ctx, x: number, y: number, s: number, alpha: number, t: number): void {
  ctx.save();
  ctx.translate(x, y);
  ctx.globalAlpha = alpha * 0.85;
  ctx.strokeStyle = '#eef2ff';
  ctx.lineWidth = s * 0.14;
  ctx.lineCap = 'round';
  for (let i = 0; i < 4; i++) {
    for (const side of [-1, 1]) {
      const wob = Math.sin(t * 7 + i) * s * 0.12;
      ctx.beginPath();
      ctx.moveTo(side * s * 0.7, -s * 0.2 + i * s * 0.3);
      ctx.quadraticCurveTo(side * s * 1.4, -s * 0.5 + i * s * 0.4 + wob, side * s * 1.7, i * s * 0.5 + wob);
      ctx.stroke();
    }
  }
  ctx.fillStyle = '#eef2ff';
  ctx.beginPath();
  ctx.arc(0, 0, s, 0, TAU);
  ctx.fill();
  ctx.strokeStyle = '#3a3050';
  ctx.lineWidth = s * 0.13;
  for (const side of [-1, 1]) {
    ctx.beginPath();
    ctx.moveTo(side * s * 0.4 - s * 0.16, -s * 0.3);
    ctx.lineTo(side * s * 0.4 + s * 0.16, s * 0.02);
    ctx.moveTo(side * s * 0.4 + s * 0.16, -s * 0.3);
    ctx.lineTo(side * s * 0.4 - s * 0.16, s * 0.02);
    ctx.stroke();
  }
  ctx.beginPath();
  ctx.arc(0, s * 0.5, s * 0.16, 0, TAU);
  ctx.stroke();
  ctx.strokeStyle = '#ffe58a';
  ctx.lineWidth = s * 0.14;
  ctx.beginPath();
  ctx.ellipse(0, -s * 1.35, s * 0.6, s * 0.2, 0, 0, TAU);
  ctx.stroke();
  ctx.restore();
}