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/spider.ts
// How the spiders look. Everything is drawn with paths around the origin and
// placed by the caller; one unit is one world unit at scale 1.

import type { Kind } from './game.js';

export interface Palette {
  body: string;
  bodyHi: string;
  rim: string;
  leg: string;
  band: string | null;
  eye: string;
  eyeHi: string;
  mark: string;
  goo: string;
  gun: string;
  gunHi: string;
}

export const PALETTES: Record<Kind, Palette> = {
  widow: { body: '#15111d', bodyHi: '#3a3050', rim: '#8f7fc0', leg: '#15111d', band: null, eye: '#ff4338', eyeHi: '#ffd0a0', mark: '#ee2030', goo: '#8be04a', gun: '#59606e', gunHi: '#c9d2e0' },
  hat: { body: '#4b2e1f', bodyHi: '#93603a', rim: '#e0a56c', leg: '#3b2418', band: '#e8862e', eye: '#ffd23f', eyeHi: '#fff6c0', mark: '#f4a640', goo: '#b6e04a', gun: '#59606e', gunHi: '#c9d2e0' },
  mini: { body: '#24521f', bodyHi: '#6bb048', rim: '#c9ff8c', leg: '#1d431b', band: null, eye: '#e6ff5a', eyeHi: '#ffffff', mark: '#f0ff9a', goo: '#e8f04a', gun: '#59606e', gunHi: '#c9d2e0' },
  boss: { body: '#371545', bodyHi: '#7d359c', rim: '#e09cff', leg: '#2a1036', band: '#ffc933', eye: '#ff5ad8', eyeHi: '#ffe0fa', mark: '#ffc933', goo: '#d86bff', gun: '#b8860b', gunHi: '#ffe58a' },
};

const HURT: Palette = {
  body: '#ffffff', bodyHi: '#ffffff', rim: '#ffffff', leg: '#fff3d0', band: null, eye: '#ffffff', eyeHi: '#ffffff', mark: '#ffffff', goo: '#ffffff', gun: '#ffffff', gunHi: '#ffffff',
};

export interface SpiderPose {
  kind: Kind;
  x: number;
  y: number;
  scale: number;
  time: number;
  seed: number;
  /** On a thread: the legs dangle. Otherwise it stands. */
  hanging: boolean;
  /** Standing on the floor, so it casts a shadow there. */
  grounded: boolean;
  /** 0 still to 1 running. */
  walk: number;
  /** 0 pistols up to 1 pistols at you. */
  aim: number;
  /** 0 to 1: how close the shot is. */
  heat: number;
  /** 1 right at the shot, falling to 0. */
  flash: number;
  hurt: boolean;
  dizzy: boolean;
  /** Hat style (the level's index), or −1 for none. The queen wears a crown. */
  hat: number;
  lookX: number;
  lookY: number;
  squashX: number;
  squashY: number;
  /** World y where its thread is fixed; no thread when undefined. */
  threadTop?: number;
}

const TAU = Math.PI * 2;

/** A hairy outline: an ellipse whose edge zigzags. */
function furPath(ctx: CanvasRenderingContext2D, cx: number, cy: number, rx: number, ry: number, spikes: number, depth: number, turn: number): void {
  ctx.beginPath();
  const n = spikes * 2;
  for (let i = 0; i <= n; i++) {
    const a = (i / n) * TAU + turn;
    const k = i % 2 ? 1 + depth : 1;
    const px = cx + Math.cos(a) * rx * k;
    const py = cy + Math.sin(a) * ry * k;
    if (i === 0) ctx.moveTo(px, py);
    else ctx.lineTo(px, py);
  }
  ctx.closePath();
}

interface P2 {
  x: number;
  y: number;
}

/** The knee of a two-bone leg. `prefer` picks which of the two solutions. */
function knee(hip: P2, foot: P2, a: number, b: number, prefer: (p: P2) => number): P2 {
  const dx = foot.x - hip.x;
  const dy = foot.y - hip.y;
  const d = Math.max(Math.abs(a - b) + 1, Math.min(a + b - 1, Math.hypot(dx, dy)));
  const base = Math.atan2(dy, dx);
  const inner = Math.acos(Math.max(-1, Math.min(1, (a * a + d * d - b * b) / (2 * a * d))));
  const k1 = { x: hip.x + Math.cos(base + inner) * a, y: hip.y + Math.sin(base + inner) * a };
  const k2 = { x: hip.x + Math.cos(base - inner) * a, y: hip.y + Math.sin(base - inner) * a };
  return prefer(k1) > prefer(k2) ? k1 : k2;
}

const STAND_FEET: P2[] = [
  { x: 64, y: 96 },
  { x: 116, y: 90 },
  { x: 154, y: 80 },
  { x: 176, y: 66 },
];
const HANG_FEET: P2[] = [
  { x: 74, y: 118 },
  { x: 150, y: 84 },
  { x: 168, y: -6 },
  { x: 124, y: -104 },
];

function legPoints(pose: SpiderPose, side: number, i: number): [P2, P2, P2] {
  const hip = { x: side * 30, y: 4 + i * 6 };
  const base = pose.hanging ? HANG_FEET[i] : STAND_FEET[i];
  const phase = pose.time * (pose.hanging ? 2.1 : 13) + i * 1.7 + (side > 0 ? 0 : Math.PI) + pose.seed * 9;
  let fx = base.x;
  let fy = base.y;
  if (pose.hanging) {
    fx += Math.sin(phase) * 7;
    fy += Math.cos(phase * 0.8) * 7;
    // A scared wriggle while it is stunned.
    if (pose.dizzy) fx += Math.sin(pose.time * 22 + i) * 6;
  } else {
    const w = pose.walk;
    fx += Math.cos(phase) * 16 * w;
    fy -= Math.max(0, Math.sin(phase)) * 16 * w;
    fy += Math.sin(pose.time * 2 + i) * 1.5;
  }
  const foot = { x: side * fx, y: fy };
  const k = knee(hip, foot, 78, 104, pose.hanging ? (p) => Math.abs(p.x) * 0.6 - p.y : (p) => -p.y);
  return [hip, k, foot];
}

function drawLeg(ctx: CanvasRenderingContext2D, pal: Palette, pts: [P2, P2, P2]): void {
  const [h, k, f] = pts;
  ctx.lineCap = 'round';
  ctx.lineJoin = 'round';
  // The rim first, so the leg reads on a dark wall.
  ctx.strokeStyle = pal.rim;
  ctx.globalAlpha = 0.55;
  ctx.lineWidth = 19;
  ctx.beginPath();
  ctx.moveTo(h.x, h.y);
  ctx.lineTo(k.x, k.y);
  ctx.stroke();
  ctx.lineWidth = 14;
  ctx.beginPath();
  ctx.moveTo(k.x, k.y);
  ctx.lineTo(f.x, f.y);
  ctx.stroke();
  ctx.globalAlpha = 1;
  ctx.strokeStyle = pal.leg;
  ctx.lineWidth = 15;
  ctx.beginPath();
  ctx.moveTo(h.x, h.y);
  ctx.lineTo(k.x, k.y);
  ctx.stroke();
  ctx.lineWidth = 10;
  ctx.beginPath();
  ctx.moveTo(k.x, k.y);
  ctx.lineTo(f.x, f.y);
  ctx.stroke();
  if (pal.band) {
    ctx.strokeStyle = pal.band;
    ctx.lineWidth = 10;
    ctx.setLineDash([9, 22]);
    ctx.beginPath();
    ctx.moveTo(k.x, k.y);
    ctx.lineTo(f.x, f.y);
    ctx.stroke();
    ctx.setLineDash([]);
  }
  // The knee joint.
  ctx.fillStyle = pal.bodyHi;
  ctx.beginPath();
  ctx.arc(k.x, k.y, 6, 0, TAU);
  ctx.fill();
}

/** A revolver around its grip. `f` is 0 from the side (barrel up) to 1 straight at you. */
export function drawPistol(ctx: CanvasRenderingContext2D, f: number, gun: string, gunHi: string, size = 1): void {
  ctx.save();
  ctx.scale(size, size);
  // From the side, pressed flat as it turns towards the viewer.
  const flat = 1 - 0.88 * f;
  ctx.save();
  ctx.scale(1, flat);
  // Barrel.
  ctx.fillStyle = gun;
  ctx.beginPath();
  ctx.roundRect(-6, -78, 12, 52, 3);
  ctx.fill();
  ctx.fillStyle = gunHi;
  ctx.fillRect(-6, -78, 4, 52);
  // Front sight.
  ctx.fillStyle = gun;
  ctx.fillRect(-9, -76, 4, 7);
  // Cylinder.
  ctx.fillStyle = '#2c313b';
  ctx.beginPath();
  ctx.roundRect(-11, -30, 22, 24, 5);
  ctx.fill();
  ctx.fillStyle = gunHi;
  ctx.globalAlpha = 0.5;
  ctx.fillRect(-7, -28, 3, 20);
  ctx.fillRect(1, -28, 3, 20);
  ctx.globalAlpha = 1;
  // Frame and hammer.
  ctx.fillStyle = gun;
  ctx.beginPath();
  ctx.roundRect(-9, -8, 18, 12, 3);
  ctx.fill();
  ctx.fillRect(6, -14, 6, 8);
  ctx.restore();
  // Grip.
  ctx.fillStyle = '#6a3d1e';
  ctx.beginPath();
  ctx.moveTo(-8, 0);
  ctx.lineTo(9, 0);
  ctx.lineTo(13, 26 * (0.45 + 0.55 * flat) + 4);
  ctx.lineTo(-5, 26 * (0.45 + 0.55 * flat) + 6);
  ctx.closePath();
  ctx.fill();
  ctx.fillStyle = '#a5672f';
  ctx.fillRect(-4, 3, 4, 14 * (0.45 + 0.55 * flat));
  // From the front: the cylinder's face and the hole of the barrel.
  if (f > 0.3) {
    const k = (f - 0.3) / 0.7;
    ctx.globalAlpha = Math.min(1, k * 1.6);
    const cy = -18 * (1 - k) - 10;
    ctx.fillStyle = '#2c313b';
    ctx.beginPath();
    ctx.arc(0, cy + 6, 19 * (0.5 + 0.5 * k), 0, TAU);
    ctx.fill();
    ctx.fillStyle = gun;
    ctx.beginPath();
    ctx.arc(0, cy - 6, 15 * (0.5 + 0.5 * k), 0, TAU);
    ctx.fill();
    ctx.strokeStyle = gunHi;
    ctx.lineWidth = 2.5;
    ctx.beginPath();
    ctx.arc(0, cy - 6, 13 * (0.5 + 0.5 * k), Math.PI * 0.9, Math.PI * 1.7);
    ctx.stroke();
    ctx.fillStyle = '#05060a';
    ctx.beginPath();
    ctx.arc(0, cy - 6, 8.5 * (0.5 + 0.5 * k), 0, TAU);
    ctx.fill();
    ctx.globalAlpha = 1;
  }
  ctx.restore();
}

/** Where a hand is, for arm `row` (0 the lower pair, 1 the queen's upper pair). */
function hand(pose: SpiderPose, side: number, row: number): P2 {
  const idle = row === 0 ? { x: 80, y: 34 } : { x: 112, y: -8 };
  const aimed = row === 0 ? { x: 46, y: 70 } : { x: 92, y: 44 };
  const a = pose.aim;
  const bob = Math.sin(pose.time * 2.4 + row + pose.seed * 5) * 2.5;
  return { x: side * (idle.x + (aimed.x - idle.x) * a), y: idle.y + (aimed.y - idle.y) * a + bob };
}

/** Where the muzzles are in the spider's own units: for the heat glow and the flash. */
function muzzles(pose: SpiderPose): P2[] {
  const rows = pose.kind === 'boss' ? 2 : 1;
  const out: P2[] = [];
  const size = pose.kind === 'boss' ? 1.05 : 1.15;
  for (let row = 0; row < rows; row++) {
    for (const side of [-1, 1]) {
      const h = hand(pose, side, row);
      const up = 78 * (1 - 0.88 * pose.aim) + (pose.aim > 0.3 ? 16 * ((pose.aim - 0.3) / 0.7) : 0);
      out.push({ x: h.x, y: h.y - up * size * (1 - pose.aim * 0.55) });
    }
  }
  return out;
}

function drawEye(ctx: CanvasRenderingContext2D, pal: Palette, x: number, y: number, r: number, pose: SpiderPose, blink: number): void {
  ctx.save();
  ctx.translate(x, y);
  ctx.scale(1, blink);
  ctx.fillStyle = '#0a0710';
  ctx.beginPath();
  ctx.arc(0, 0, r + 2.2, 0, TAU);
  ctx.fill();
  const g = ctx.createRadialGradient(-r * 0.3, -r * 0.3, r * 0.1, 0, 0, r);
  g.addColorStop(0, pal.eyeHi);
  g.addColorStop(0.55, pal.eye);
  g.addColorStop(1, pal.eye);
  ctx.fillStyle = g;
  ctx.beginPath();
  ctx.arc(0, 0, r, 0, TAU);
  ctx.fill();
  if (pose.dizzy) {
    ctx.strokeStyle = '#0a0710';
    ctx.lineWidth = Math.max(1.4, r * 0.18);
    ctx.beginPath();
    for (let i = 0; i <= 24; i++) {
      const a = (i / 24) * TAU * 1.6 + pose.time * 9;
      const rr = (i / 24) * r * 0.8;
      if (i === 0) ctx.moveTo(Math.cos(a) * rr, Math.sin(a) * rr);
      else ctx.lineTo(Math.cos(a) * rr, Math.sin(a) * rr);
    }
    ctx.stroke();
  } else {
    // A slit pupil that follows the pointer and narrows while it aims.
    const px = pose.lookX * r * 0.38;
    const py = pose.lookY * r * 0.32;
    ctx.fillStyle = '#0a0710';
    ctx.beginPath();
    ctx.ellipse(px, py, r * (0.36 - 0.14 * pose.aim), r * 0.66, 0, 0, TAU);
    ctx.fill();
    ctx.fillStyle = 'rgba(255,255,255,0.9)';
    ctx.beginPath();
    ctx.arc(px - r * 0.3, py - r * 0.36, r * 0.17, 0, TAU);
    ctx.fill();
  }
  ctx.restore();
}

/** Hats sit on the origin, which is the top of the head. */
export function drawHat(ctx: CanvasRenderingContext2D, style: number, time: number): void {
  const swing = Math.sin(time * 3) * 0.25;
  switch (style) {
    case 0: {
      // Mortarboard.
      ctx.fillStyle = '#1b1d2b';
      ctx.beginPath();
      ctx.roundRect(-30, -16, 60, 22, 6);
      ctx.fill();
      ctx.fillStyle = '#2b2e44';
      ctx.beginPath();
      ctx.moveTo(-62, -22);
      ctx.lineTo(0, -40);
      ctx.lineTo(62, -22);
      ctx.lineTo(0, -6);
      ctx.closePath();
      ctx.fill();
      ctx.strokeStyle = '#ffd23f';
      ctx.lineWidth = 3.5;
      ctx.beginPath();
      ctx.moveTo(0, -23);
      ctx.lineTo(44, -14);
      ctx.lineTo(44 + swing * 20, 10);
      ctx.stroke();
      ctx.fillStyle = '#ffd23f';
      ctx.beginPath();
      ctx.arc(44 + swing * 20, 14, 6, 0, TAU);
      ctx.fill();
      break;
    }
    case 1: {
      // Chef's hat.
      ctx.fillStyle = '#f4f1e8';
      for (const [x, y, r] of [[-26, -44, 24], [0, -56, 28], [26, -44, 24]]) {
        ctx.beginPath();
        ctx.arc(x, y, r, 0, TAU);
        ctx.fill();
      }
      ctx.beginPath();
      ctx.roundRect(-34, -40, 68, 44, 6);
      ctx.fill();
      ctx.fillStyle = '#cfc9ba';
      ctx.fillRect(-34, -8, 68, 5);
      ctx.fillRect(-14, -36, 3, 22);
      ctx.fillRect(10, -36, 3, 22);
      break;
    }
    case 2: {
      // Nightcap with a pompom.
      const tipX = 64;
      const tipY = -18 + swing * 16;
      ctx.fillStyle = '#3f6fd1';
      ctx.beginPath();
      ctx.moveTo(-40, 4);
      ctx.quadraticCurveTo(-30, -66, 16, -60);
      ctx.quadraticCurveTo(52, -56, tipX, tipY);
      ctx.quadraticCurveTo(36, -34, 40, 4);
      ctx.closePath();
      ctx.fill();
      ctx.strokeStyle = '#ffffff';
      ctx.globalAlpha = 0.75;
      ctx.lineWidth = 5;
      ctx.beginPath();
      ctx.moveTo(-28, -22);
      ctx.quadraticCurveTo(0, -30, 34, -22);
      ctx.moveTo(-16, -44);
      ctx.quadraticCurveTo(8, -50, 38, -40);
      ctx.stroke();
      ctx.globalAlpha = 1;
      ctx.fillStyle = '#ffffff';
      ctx.beginPath();
      ctx.roundRect(-44, -4, 88, 14, 7);
      ctx.fill();
      ctx.beginPath();
      ctx.arc(tipX, tipY, 12, 0, TAU);
      ctx.fill();
      break;
    }
    case 3: {
      // Miner's helmet with a lamp.
      ctx.fillStyle = '#f2b824';
      ctx.beginPath();
      ctx.arc(0, 0, 44, Math.PI, 0);
      ctx.closePath();
      ctx.fill();
      ctx.fillStyle = '#c98d12';
      ctx.beginPath();
      ctx.roundRect(-54, -6, 108, 12, 6);
      ctx.fill();
      ctx.fillRect(-4, -44, 8, 40);
      ctx.fillStyle = '#3a3f4b';
      ctx.beginPath();
      ctx.arc(0, -20, 13, 0, TAU);
      ctx.fill();
      ctx.fillStyle = '#fff6b0';
      ctx.beginPath();
      ctx.arc(0, -20, 9, 0, TAU);
      ctx.fill();
      break;
    }
    case 4: {
      // Witch's hat.
      ctx.fillStyle = '#2a1b3d';
      ctx.beginPath();
      ctx.ellipse(0, 0, 66, 15, 0, 0, TAU);
      ctx.fill();
      ctx.beginPath();
      ctx.moveTo(-36, -2);
      ctx.quadraticCurveTo(-20, -60, 8, -84);
      ctx.quadraticCurveTo(26, -98, 44 + swing * 10, -92);
      ctx.quadraticCurveTo(26, -76, 36, -2);
      ctx.closePath();
      ctx.fill();
      ctx.fillStyle = '#7a3fb0';
      ctx.beginPath();
      ctx.moveTo(-36, -4);
      ctx.quadraticCurveTo(0, -14, 36, -4);
      ctx.lineTo(34, -18);
      ctx.quadraticCurveTo(0, -27, -31, -18);
      ctx.closePath();
      ctx.fill();
      ctx.fillStyle = '#ffd23f';
      ctx.fillRect(-9, -24, 18, 15);
      ctx.fillStyle = '#2a1b3d';
      ctx.fillRect(-4, -20, 8, 7);
      break;
    }
    default: {
      // The queen's crown.
      ctx.fillStyle = '#ffc933';
      ctx.beginPath();
      ctx.moveTo(-46, 6);
      ctx.lineTo(-54, -44);
      ctx.lineTo(-28, -20);
      ctx.lineTo(-14, -56);
      ctx.lineTo(0, -22);
      ctx.lineTo(14, -56);
      ctx.lineTo(28, -20);
      ctx.lineTo(54, -44);
      ctx.lineTo(46, 6);
      ctx.closePath();
      ctx.fill();
      ctx.fillStyle = '#e09a12';
      ctx.fillRect(-46, -4, 92, 10);
      const gems: [number, number, string][] = [[-54, -46, '#ff4a6a'], [-14, -58, '#5ad8ff'], [14, -58, '#7dff6a'], [54, -46, '#ff4a6a'], [0, -1, '#d86bff']];
      for (const [x, y, c] of gems) {
        ctx.fillStyle = c;
        ctx.beginPath();
        ctx.arc(x, y, 6.5, 0, TAU);
        ctx.fill();
      }
    }
  }
}

function drawMark(ctx: CanvasRenderingContext2D, kind: Kind, pal: Palette): void {
  ctx.fillStyle = pal.mark;
  ctx.strokeStyle = pal.mark;
  const cy = -66;
  if (kind === 'widow' || kind === 'boss') {
    ctx.beginPath();
    ctx.moveTo(-17, cy - 24);
    ctx.lineTo(17, cy - 24);
    ctx.lineTo(0, cy);
    ctx.closePath();
    ctx.fill();
    ctx.beginPath();
    ctx.moveTo(-15, cy + 20);
    ctx.lineTo(15, cy + 20);
    ctx.lineTo(0, cy);
    ctx.closePath();
    ctx.fill();
    if (kind === 'boss') {
      for (const x of [-36, 36]) {
        ctx.beginPath();
        ctx.arc(x, cy - 6, 6, 0, TAU);
        ctx.fill();
      }
    }
  } else if (kind === 'hat') {
    ctx.lineWidth = 7;
    ctx.lineCap = 'round';
    for (let i = 0; i < 3; i++) {
      ctx.beginPath();
      ctx.moveTo(-26 + i * 3, cy - 26 + i * 15);
      ctx.lineTo(0, cy - 14 + i * 15);
      ctx.lineTo(26 - i * 3, cy - 26 + i * 15);
      ctx.stroke();
    }
  } else {
    for (const [x, y, r] of [[-20, cy - 14, 8], [18, cy - 18, 7], [0, cy + 6, 9], [-26, cy + 12, 5], [28, cy + 8, 5]]) {
      ctx.beginPath();
      ctx.arc(x, y, r, 0, TAU);
      ctx.fill();
    }
  }
}

export function drawSpider(ctx: CanvasRenderingContext2D, pose: SpiderPose): void {
  const pal = pose.hurt ? HURT : PALETTES[pose.kind];
  const boss = pose.kind === 'boss';
  if (pose.threadTop !== undefined) {
    ctx.strokeStyle = 'rgba(235,240,255,0.75)';
    ctx.lineWidth = Math.max(1.5, (boss ? 4.5 : 2.6) * Math.sqrt(pose.scale));
    ctx.beginPath();
    ctx.moveTo(pose.x, pose.threadTop);
    ctx.lineTo(pose.x, pose.y - 100 * pose.scale * pose.squashY);
    ctx.stroke();
  }
  ctx.save();
  // A tremble just before the shot.
  const shake = pose.heat > 0.72 ? (pose.heat - 0.72) * 9 : 0;
  ctx.translate(pose.x + Math.sin(pose.time * 61) * shake, pose.y + Math.cos(pose.time * 53) * shake);
  ctx.scale(pose.scale * pose.squashX, pose.scale * pose.squashY);
  const breathe = 1 + Math.sin(pose.time * 2.6 + pose.seed * 7) * 0.015;

  if (pose.grounded) {
    ctx.fillStyle = 'rgba(0,0,0,0.3)';
    ctx.beginPath();
    ctx.ellipse(0, 94, 150, 16, 0, 0, TAU);
    ctx.fill();
  }

  // Hind legs, then the abdomen, then the front legs over it.
  for (const i of [3, 2]) for (const side of [-1, 1]) drawLeg(ctx, pal, legPoints(pose, side, i));

  furPath(ctx, 0, -62, 66 * breathe, 60 * breathe, 22, 0.085, pose.seed);
  const ag = ctx.createRadialGradient(-22, -92, 6, 0, -62, 80);
  ag.addColorStop(0, pal.bodyHi);
  ag.addColorStop(1, pal.body);
  ctx.fillStyle = ag;
  ctx.fill();
  ctx.strokeStyle = pal.rim;
  ctx.globalAlpha = 0.7;
  ctx.lineWidth = 3;
  ctx.stroke();
  ctx.globalAlpha = 1;
  drawMark(ctx, pose.kind, pal);

  for (const i of [1, 0]) for (const side of [-1, 1]) drawLeg(ctx, pal, legPoints(pose, side, i));

  // Head.
  furPath(ctx, 0, 14, 54, 48, 18, 0.08, pose.seed * 3);
  const hg = ctx.createRadialGradient(-16, -12, 5, 0, 14, 62);
  hg.addColorStop(0, pal.bodyHi);
  hg.addColorStop(1, pal.body);
  ctx.fillStyle = hg;
  ctx.fill();
  ctx.strokeStyle = pal.rim;
  ctx.globalAlpha = 0.7;
  ctx.lineWidth = 3;
  ctx.stroke();
  ctx.globalAlpha = 1;

  // Jaws and fangs.
  for (const side of [-1, 1]) {
    ctx.fillStyle = pal.bodyHi;
    ctx.beginPath();
    ctx.ellipse(side * 13, 46, 12, 14, 0, 0, TAU);
    ctx.fill();
    ctx.fillStyle = pose.hurt ? '#ffffff' : '#fff8e6';
    ctx.beginPath();
    ctx.moveTo(side * 6, 52);
    ctx.lineTo(side * 20, 52);
    ctx.quadraticCurveTo(side * 16, 68, side * 8, 80);
    ctx.quadraticCurveTo(side * 9, 64, side * 6, 52);
    ctx.fill();
  }
  // A drop of venom now and then.
  const drip = (pose.time * 0.45 + pose.seed * 3) % 1.6;
  if (drip < 1 && !pose.hurt) {
    ctx.fillStyle = pal.goo;
    ctx.globalAlpha = 1 - drip;
    ctx.beginPath();
    ctx.ellipse(8, 82 + drip * drip * 70, 3.6, 5.5, 0, 0, TAU);
    ctx.fill();
    ctx.globalAlpha = 1;
  }

  // Eight eyes.
  const blinkPhase = (pose.time * 0.31 + pose.seed * 5) % 1;
  const blink = blinkPhase > 0.965 && pose.aim < 0.2 ? 0.12 : 1;
  for (const side of [-1, 1]) {
    drawEye(ctx, pal, side * 32, -20, 5.5, pose, blink);
    drawEye(ctx, pal, side * 12, -17, 6.5, pose, blink);
    drawEye(ctx, pal, side * 43, -2, 8, pose, blink);
    drawEye(ctx, pal, side * 20, 8, 14.5, pose, blink);
  }
  // Angry lids come down as it aims.
  if (pose.aim > 0.05 && !pose.dizzy) {
    ctx.fillStyle = pal.body;
    for (const side of [-1, 1]) {
      ctx.beginPath();
      ctx.moveTo(side * 2, -9);
      ctx.lineTo(side * 38, -12);
      ctx.lineTo(side * 38, -12 + 6 * pose.aim);
      ctx.lineTo(side * 2, -9 + 17 * pose.aim);
      ctx.closePath();
      ctx.fill();
    }
  }

  // Arms and pistols; the queen has four.
  const rows = boss ? 2 : 1;
  for (let row = rows - 1; row >= 0; row--) {
    for (const side of [-1, 1]) {
      const h = hand(pose, side, row);
      const shoulder = { x: side * (40 + row * 6), y: 26 - row * 24 };
      const elbow = { x: (shoulder.x + h.x) / 2 + side * 14, y: Math.max(shoulder.y, h.y) + 14 };
      ctx.lineCap = 'round';
      ctx.lineJoin = 'round';
      ctx.strokeStyle = pal.rim;
      ctx.globalAlpha = 0.55;
      ctx.lineWidth = 16;
      ctx.beginPath();
      ctx.moveTo(shoulder.x, shoulder.y);
      ctx.quadraticCurveTo(elbow.x, elbow.y, h.x, h.y + 10);
      ctx.stroke();
      ctx.globalAlpha = 1;
      ctx.strokeStyle = pal.leg;
      ctx.lineWidth = 12;
      ctx.stroke();
      ctx.save();
      ctx.translate(h.x, h.y);
      // Held a little outwards, turning in as it aims; kicked up by the shot.
      ctx.rotate(side * (0.3 * (1 - pose.aim) - 0.5 * pose.flash));
      ctx.scale(side, 1);
      drawPistol(ctx, pose.aim, pal.gun, pal.gunHi, boss ? 1.05 : 1.15);
      ctx.restore();
      // The claw holding the grip.
      ctx.fillStyle = pal.leg;
      ctx.beginPath();
      ctx.arc(h.x, h.y + 12, 9, 0, TAU);
      ctx.fill();
    }
  }

  if (pose.hat >= 0 || boss) {
    ctx.save();
    ctx.translate(0, -30);
    ctx.rotate(Math.sin(pose.time * 1.9 + pose.seed) * 0.04);
    drawHat(ctx, boss ? 9 : pose.hat, pose.time + pose.seed * 4);
    ctx.restore();
  }
  ctx.restore();
}

/** The light a spider gives off: eyes, hot muzzles and the flash. Drawn after
 *  the darkness with additive blending, so it shows in the darkest corner. */
export function drawSpiderGlow(ctx: CanvasRenderingContext2D, pose: SpiderPose, sprite: CanvasImageSource): void {
  const pal = PALETTES[pose.kind];
  ctx.save();
  ctx.translate(pose.x, pose.y);
  ctx.scale(pose.scale * pose.squashX, pose.scale * pose.squashY);
  const eyes: [number, number, number][] = [[20, 8, 34], [43, -2, 18], [12, -17, 14], [32, -20, 12]];
  ctx.globalAlpha = (pose.dizzy ? 0.1 : 0.2 + 0.22 * pose.aim) * (pose.kind === 'hat' || pose.kind === 'mini' ? 0.5 : 1);
  for (const side of [-1, 1]) {
    for (const [x, y, r] of eyes) tint(ctx, sprite, side * x, y, r * 1.5, pal.eye);
  }
  if (pose.hat === 3 && pose.kind === 'hat') {
    ctx.globalAlpha = 0.45;
    tint(ctx, sprite, 0, -50, 44, '#fff0a0');
  }
  const ms = muzzles(pose);
  if (pose.heat > 0.02) {
    const pulse = pose.heat > 0.6 ? 0.75 + 0.25 * Math.sin(pose.time * 26) : 1;
    ctx.globalAlpha = Math.min(1, (0.25 + 0.75 * pose.heat) * pulse);
    for (const m of ms) tint(ctx, sprite, m.x, m.y, 26 + 46 * pose.heat, '#ff3020');
  }
  if (pose.flash > 0.02) {
    ctx.globalAlpha = Math.min(1, pose.flash * 1.4);
    for (const m of ms) {
      tint(ctx, sprite, m.x, m.y, 150 * pose.flash + 40, '#ffd070');
      ctx.fillStyle = '#fff6d0';
      ctx.beginPath();
      const spikes = 7;
      for (let i = 0; i < spikes * 2; i++) {
        const a = (i / (spikes * 2)) * TAU + pose.seed * 6;
        const r = (i % 2 ? 22 : 74) * pose.flash;
        const px = m.x + Math.cos(a) * r;
        const py = m.y + Math.sin(a) * r;
        if (i === 0) ctx.moveTo(px, py);
        else ctx.lineTo(px, py);
      }
      ctx.closePath();
      ctx.fill();
    }
  }
  ctx.restore();
}

/** Glow sprites, one per colour, made on first use. */
const tinted = new Map<string, HTMLCanvasElement>();
function tint(ctx: CanvasRenderingContext2D, sprite: CanvasImageSource, x: number, y: number, r: number, color: string): void {
  let c = tinted.get(color);
  if (!c) {
    c = document.createElement('canvas');
    c.width = c.height = 64;
    const g = c.getContext('2d') as CanvasRenderingContext2D;
    g.drawImage(sprite, 0, 0, 64, 64);
    g.globalCompositeOperation = 'source-in';
    g.fillStyle = color;
    g.fillRect(0, 0, 64, 64);
    tinted.set(color, c);
  }
  ctx.drawImage(c, x - r, y - r, r * 2, r * 2);
}

/** A soft round glow in white, to be tinted. */
export function makeGlowSprite(): HTMLCanvasElement {
  const c = document.createElement('canvas');
  c.width = c.height = 64;
  const g = c.getContext('2d') as CanvasRenderingContext2D;
  const grad = g.createRadialGradient(32, 32, 0, 32, 32, 32);
  grad.addColorStop(0, 'rgba(255,255,255,1)');
  grad.addColorStop(0.35, 'rgba(255,255,255,0.45)');
  grad.addColorStop(1, 'rgba(255,255,255,0)');
  g.fillStyle = grad;
  g.fillRect(0, 0, 64, 64);
  return c;
}

export function glow(ctx: CanvasRenderingContext2D, sprite: CanvasImageSource, x: number, y: number, r: number, color: string): void {
  tint(ctx, sprite, x, y, r, color);
}