Lab · 2026-11-20

ASCII 광안대교 드론쇼

광안리 해변에서 바라본 광안대교가 지어지고, 드론 800대가 갈매기를 그린 뒤 BUSAN 글자로 바뀌고 흩어지면 불꽃놀이와 다리 아래 나이아가라 불꽃이 쏟아지는 애니메이션이에요. 그림 없이 | / \ # 같은 글자만으로 그렸어요. 영상은 @ai.asap.kr 릴스에 있어요.

ASCII 광안대교 드론쇼 릴스 표지
HTML 파일 받기브라우저에서 바로 보기

HTML 파일 한 개, 993줄.

광고

코드

클로드가 짠 코드에서 밝기·글자 변환·드론·불꽃·색감 부분을 손봐 녹화했어요.

광고
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>gwangan_bridge.js</title>
<style>
body{margin:0;height:100vh;background:#000;display:grid;place-items:center;overflow:hidden}
canvas{width:min(100vw,56.25vh)}
</style>
</head>
<body>
<canvas id="view" width="1080" height="1920"></canvas>
<script>
(() => {
// gwangan_bridge.js : Gwangalli night photo, painted then printed as ASCII
const SOURCE = document.currentScript.text.replace(/^\n+/, '').trimEnd().split('\n');

// ---- layout ----
const W = 1080, H = 1920, SCENE_H = 1290, CODE_TOP = 1300;
const COLS = 120, ROWS = 86, CELL_W = 9, CELL_H = 15, N = COLS * ROWS;
const LW = 360, LH = 430, K = 1 / 3;   // low-res photo, 1/3 of the scene
const LOOP = 34, HORIZON = 800, SEA_END = 1130, DRONES = 800;
const CODE_ROWS = 18, LINE_H = 30;
const RAMP = " .':;~^+=*xvo%&#W@";   // soft symbol ramp: cosy, not noisy
const GOLD = '255,196,92', SILVER = '214,224,240', WHITE = '255,255,255';
// night-pastel palette: one tone, different hues (tone-and-tone)
const COOL_LED = [243, 230, 207], SKY_LED = [242, 172, 128];   // cables ivory, deck apricot
const GOLD_LED = [255, 190, 95];
// fireworks: one pastel colour per shell, gold only now and then (tone-and-tone)
const SHELL_COLORS = ['240,150,170', '245,178,130', '190,170,240', '150,215,200', '232,232,240'];
let shellNo = 0;
const nextShellColor = () => (++shellNo % 5 === 0 ? GOLD : SHELL_COLORS[shellNo % SHELL_COLORS.length]);

const clamp = (v, a = 0, b = 1) => Math.min(b, Math.max(a, v));
const lerp = (a, b, p) => a + (b - a) * p;
const ease = p => p < 0.5 ? 2 * p * p : 1 - (2 - 2 * p) ** 2 / 2;
const hash = n => { const s = Math.sin(n * 127.1 + 311.7) * 43758.5453; return s - Math.floor(s); };
const rgba = (rgb, a) => 'rgba(' + rgb + ',' + a + ')';
const mix = (a, b, t) => a.map((v, i) => Math.round(lerp(v, b[i], t))).join(',');
const NUMS = {};
const nums = rgb => NUMS[rgb] || (NUMS[rgb] = rgb.split(',').map(Number));

function makeCanvas(w, h, cpu = true) {
  const c = document.createElement('canvas');
  c.width = w; c.height = h;
  return [c, c.getContext('2d', { willReadFrequently: cpu })];
}
const sceneScale = c => c.setTransform(K, 0, 0, K, 0, 0);
const pixelScale = c => c.setTransform(1, 0, 0, 1, 0, 0);
function glow(c, x, y, r, rgb, a) {
  const g = c.createRadialGradient(x, y, 0, x, y, r);
  g.addColorStop(0, rgba(rgb, a));
  g.addColorStop(1, rgba(rgb, 0));
  c.fillStyle = g;
  c.beginPath(); c.arc(x, y, r, 0, 7); c.fill();
}

const view = document.getElementById('view'), out = view.getContext('2d');
const [photo, P] = makeCanvas(LW, LH);     // the painted night photo
const [front, F] = makeCanvas(LW, LH);     // city, drones, fireworks
const [bridge, B] = makeCanvas(LW, LH);    // Gwangan Bridge
const [sky, S] = makeCanvas(LW, LH);       // static sky
const [city, C] = makeCanvas(LW, LH);      // static Marine City
const [mirror, M] = makeCanvas(LW, 267);   // what the sea reflects
const [water, R] = makeCanvas(LW, 110);    // reflection strip
const [waves, V] = makeCanvas(LW + 120, 110); // broken ripple mask
const MK = makeCanvas(540, 300)[1];
const [letters, A] = makeCanvas(W, SCENE_H, false);
const colI = new Float32Array(COLS), colR = new Float32Array(COLS);
const colG = new Float32Array(COLS), colB = new Float32Array(COLS);

// ---- bridge geometry (scene coordinates) ----
const deckTop = x => 770 + 110 * (x - 250) / 830;
const deckBottom = x => deckTop(x) + 4 + 18 * (x - 250) / 830;
const waterAt = x => Math.max(806, deckTop(x) + 43 + 0.25 * (x - 380));
const spacing = x => 6 + 12 * (x - 250) / 830;   // perspective gap
const FRONT = { x: 720, base: 960, top: 300, w: 18, gap: 50 };
const BACK = { x: 380, base: 830, top: 540, w: 7, gap: 22 };
const pillarX = (tw, side) => tw.x + side * (tw.gap + tw.w) / 2;
const strands = [-1, 1].map((side, n) => ({
  front: [pillarX(FRONT, side), FRONT.top],
  back: [pillarX(BACK, side), BACK.top],
  mid: [552 + side * 12, 1140],
  right: [860, 760], rightEnd: [1080, deckTop(1080) + n * 4],
  left: [330, 740], leftEnd: [250, deckTop(250) + n]
}));
const bez = (p0, c, p2, t, k) =>
  (1 - t) ** 2 * p0[k] + 2 * t * (1 - t) * c[k] + t * t * p2[k];

// ---- backdrop ----
function buildSky() {
  sceneScale(S);
  let g = S.createLinearGradient(0, 0, 0, HORIZON);
  g.addColorStop(0, '#030305'); g.addColorStop(1, '#0f0c10');
  S.fillStyle = g; S.fillRect(0, 0, W, HORIZON);
  // thin amber haze where city light hits the air
  g = S.createLinearGradient(0, 720, 0, HORIZON);
  g.addColorStop(0, 'rgba(176,110,150,0)');
  g.addColorStop(1, 'rgba(176,110,150,0.14)');   // plum haze
  S.fillStyle = g; S.fillRect(0, 720, W, 80);
}
const stars = Array.from({ length: 46 }, () => ({
  x: (Math.random() * COLS | 0) * 9 + 4.5, y: (Math.random() * 42 | 0) * 15 + 7.5, r: 1.3 + Math.random() * 2,
  a: 0.3 + Math.random() * 0.7, twinkle: Math.random() < 0.3, ph: Math.random() * 6
}));
function drawStars(c, T) {
  for (const s of stars) {
    const tw = 0.4 + 0.6 * Math.abs(Math.sin(T * 1.6 + s.ph));
    c.fillStyle = rgba(s.ph > 3 ? '255,244,226' : '205,218,246', s.twinkle ? s.a * tw : s.a);
    c.beginPath(); c.arc(s.x, s.y, s.r, 0, 7); c.fill();
  }
}
const towers = [];
function buildMarineCity() {
  sceneScale(C);
  for (const x of [6, 32, 58, 86, 112, 140, 166, 194]) {
    const w = 18 + Math.random() * 12, top = 430 + Math.random() * 190;
    towers.push({ x: x + w / 2, top });
    const g = C.createLinearGradient(0, top, 0, HORIZON);
    g.addColorStop(0, '#0a1024'); g.addColorStop(1, '#05081a');   // faces stay dark: only windows print
    C.fillStyle = g; C.beginPath();
    C.moveTo(x, HORIZON); C.lineTo(x, top + 10);
    C.quadraticCurveTo(x + w / 2, top - 6, x + w, top + 10);
    C.lineTo(x + w, HORIZON); C.fill();
    // tight grid of small windows, about 65% lit
    for (let y = top + 16; y < HORIZON - 4; y += 8)
      for (let wx = x + 3; wx < x + w - 4; wx += 6) if (Math.random() < 0.65) {
        C.fillStyle = ['#f5c66e', '#f3e6cf', '#9fd3c7', '#e9a3a8', '#f5c66e'][Math.random() * 5 | 0];
        C.globalAlpha = 0.5 + Math.random() * 0.5;
        C.fillRect(wx, y, 3, 4);
      }
    C.globalAlpha = 1;
  }
}
function buildRippleMask() {
  for (let j = 0; j < 110; j++)
    for (let x = 0; x < LW + 120;) {
      const keep = 2 + Math.random() * (8 - j * 0.04);
      const cut = 1.5 + Math.random() * (2 + j * 0.05);
      V.fillStyle = rgba('0,0,0', 0.7 + Math.random() * 0.3);
      V.fillRect(x + keep, j, cut, 1);
      x += keep + cut;
    }
}

// hangers: vertical lines from each cable down to the deck
const hangers = [];
function planHangers() {
  const xs = [];
  for (let x = 1076; x > 254; x -= spacing(x)) xs.push(x);
  for (const s of strands) {
    const segs = [[s.front, s.mid, s.back, 1], [s.front, s.right, s.rightEnd, 0],
      [s.back, s.left, s.leftEnd, 0]];
    for (const [p0, c, p2, both] of segs) for (let i = 0; i < 200; i++) {
      const t0 = i / 200, t1 = (i + 1) / 200;
      const xa = bez(p0, c, p2, t0, 0), xb = bez(p0, c, p2, t1, 0);
      for (const x of xs) {
        const f = (x - xa) / (xb - xa);
        if (!(f >= 0 && f < 1)) continue;
        const t = lerp(t0, t1, f), y = bez(p0, c, p2, t, 1);
        if (y > deckTop(x) - 4) continue;
        const grow = both ? 1 - Math.abs(1 - 2 * t) : t;
        hangers.push({ x, y1: y, y2: deckTop(x), at: 1.5 + 1.5 * grow });
      }
    }
  }
}

// draw part of a quadratic curve (de Casteljau split), return its tip
function quadPart(c, p0, cp, p2, t) {
  const q = [lerp(p0[0], cp[0], t), lerp(p0[1], cp[1], t)];
  const r = [lerp(cp[0], p2[0], t), lerp(cp[1], p2[1], t)];
  const tip = [lerp(q[0], r[0], t), lerp(q[1], r[1], t)];
  c.moveTo(p0[0], p0[1]); c.quadraticCurveTo(q[0], q[1], tip[0], tip[1]);
  return tip;
}

function drawTower(tw, rise, led) {
  if (rise <= 0) return;
  const top = tw.base - (tw.base - tw.top) * rise, half = tw.gap / 2;
  // floodlit from below: bright at the foot, dark at the top
  const g = B.createLinearGradient(0, tw.base, 0, tw.top);
  g.addColorStop(0, 'rgba(236,160,168,0.95)');   // towers: rose floodlight
  g.addColorStop(0.55, 'rgba(122,78,92,0.9)');
  g.addColorStop(1, 'rgba(28,24,26,1)');
  B.fillStyle = g;
  B.fillRect(tw.x - half - tw.w, top, tw.w, tw.base - top);
  B.fillRect(tw.x + half, top, tw.w, tw.base - top);
  // three cross beams: one near the top, two hugging the deck
  const beams = [tw.top + (tw.base - tw.top) * 0.05, deckTop(tw.x), deckBottom(tw.x)];
  for (const y of beams) if (y > top)
    B.fillRect(tw.x - half - tw.w, y - tw.w * 0.35, tw.gap + tw.w * 2, tw.w * 0.7);
  // white LED strips on the pillar edges
  B.globalCompositeOperation = 'lighter';
  B.strokeStyle = led(COOL_LED, 0.9); B.lineWidth = tw.w > 10 ? 1.8 : 1;
  B.shadowBlur = 3; B.shadowColor = B.strokeStyle; B.beginPath();
  for (const x of [tw.x - half - tw.w, tw.x - half, tw.x + half, tw.x + half + tw.w]) {
    B.moveTo(x, tw.base); B.lineTo(x, top);
  }
  B.stroke(); B.shadowBlur = 0;
  if (rise < 1) glow(B, tw.x, top, 34, WHITE, 1);
  B.globalCompositeOperation = 'source-over';
}

function ledStrip(head, edge, width, color) {
  B.strokeStyle = color; B.lineWidth = width;
  B.shadowBlur = 4; B.shadowColor = color;
  B.beginPath(); B.moveTo(1080, edge(1080)); B.lineTo(head, edge(head)); B.stroke();
  B.shadowBlur = 0;
}

const cars = Array.from({ length: 46 }, (_, i) => ({
  u: Math.random(), v: 0.05 + Math.random() * 0.04, dir: i % 2 ? 1 : -1 }));
function drawCars(T, head, dim) {
  for (const car of cars) {
    const u = ((car.u + car.dir * car.v * T) % 1 + 1) % 1, x = 250 + 830 * u;
    if (x < head) continue;
    const y = lerp(deckTop(x), deckBottom(x), car.dir > 0 ? 0.3 : 0.65);
    const r = 0.8 + 2.2 * u;   // closer cars look bigger
    B.fillStyle = car.dir > 0 ? rgba('255,250,235', dim) : rgba('255,60,45', dim);
    B.fillRect(x - r, y - r * 0.6, r * 2, r * 1.2);
  }
}

// Gwangan Bridge: towers rise, cables sweep out, deck LEDs light up
function drawBridge(T) {
  pixelScale(B); B.clearRect(0, 0, LW, LH); sceneScale(B);
  const rise = ease(clamp(T / 1.5)), cable = clamp((T - 1.5) / 1.5);
  const deck = clamp(T - 3);
  const gold = 0;   // the bridge keeps its pastel lights through the fireworks
  // dimmed during the drone show, easing down and back up (no hard switch)
  const dim = 1 - 0.4 * ease(clamp((T - 4) / 1.2)) * (1 - ease(clamp((T - 19) / 1.2)));
  const led = (rgb, a) => rgba(mix(rgb, GOLD_LED, gold), a * dim);
  drawTower(BACK, rise, led); drawTower(FRONT, rise, led);
  // thin dark hangers drop from the cables
  B.strokeStyle = rgba('183,168,232', 0.32 * dim);   // hangers: lavender
  B.lineWidth = 1; B.beginPath();
  for (const h of hangers) {
    const p = clamp((T - h.at) / 0.25);
    if (p > 0) { B.moveTo(h.x, h.y1); B.lineTo(h.x, lerp(h.y1, h.y2, p)); }
  }
  B.stroke();
  // deck: dark body, X truss between the roads, LED strips on both edges
  if (deck > 0) {
    const head = 1080 - 830 * deck;
    B.fillStyle = '#100e12'; B.beginPath();
    B.moveTo(head, deckTop(head)); B.lineTo(1080, deckTop(1080));
    B.lineTo(1080, deckBottom(1080)); B.lineTo(head, deckBottom(head)); B.fill();
    B.strokeStyle = led(SKY_LED, 0.45); B.lineWidth = 1; B.beginPath();
    for (let x = 1080, nx; (nx = x - spacing(x) * 1.6) > head; x = nx) {
      B.moveTo(x, deckTop(x)); B.lineTo(nx, deckBottom(nx));
      B.moveTo(x, deckBottom(x)); B.lineTo(nx, deckTop(nx));
    }
    B.stroke();
    B.globalCompositeOperation = 'lighter';
    ledStrip(head, deckTop, 3, led(COOL_LED, 1));
    ledStrip(head, deckBottom, 2.2, led(SKY_LED, 0.9));
    if (deck < 1) glow(B, head, deckTop(head) + 4, 28, WHITE, 1);
    drawCars(T, head, dim);
  }
  // main cables: smooth hanging curves drawn outward from the tower tops
  if (cable > 0) {
    B.globalCompositeOperation = 'lighter';
    B.strokeStyle = led(COOL_LED, 0.95); B.lineWidth = 2;
    B.shadowBlur = 4; B.shadowColor = B.strokeStyle;
    const tips = [];
    B.beginPath();
    for (const s of strands) tips.push(
      quadPart(B, s.front, s.mid, s.back, cable / 2),
      quadPart(B, s.back, s.mid, s.front, cable / 2),
      quadPart(B, s.front, s.right, s.rightEnd, cable),
      quadPart(B, s.back, s.left, s.leftEnd, cable));
    B.stroke(); B.shadowBlur = 0;
    if (cable < 1) for (const t of tips) glow(B, t[0], t[1], 18, WHITE, 1);
  }
  // red aviation lights blink on the tower tops
  B.globalCompositeOperation = 'lighter';
  if (rise >= 1 && (T * 1.1) % 1 < 0.45)
    for (const tw of [FRONT, BACK]) for (const side of [-1, 1])
      glow(B, pillarX(tw, side), tw.top - 6, tw.w + 4, '255,40,30', 1);
  B.globalCompositeOperation = 'source-over';
}

// ---- drone pictures: fill a silhouette, one drone per grid point ----
function shapeFromMask(paint, colorOf) {
  pixelScale(MK); MK.clearRect(0, 0, 540, 300);
  MK.setTransform(0.5, 0, 0, 0.5, 0, 0); MK.fillStyle = '#fff'; paint(MK);
  const d = MK.getImageData(0, 0, 540, 300).data;
  const inside = (x, y) => {
    x = Math.floor(x / 2); y = Math.floor(y / 2);
    return x >= 0 && y >= 0 && x < 540 && y < 300 && d[(y * 540 + x) * 4 + 3] > 127;
  };
  // outline pixels, thinned to one drone every gap px
  const border = [];
  for (let y = 1; y < 600; y += 2) for (let x = 1; x < 1080; x += 2)
    if (inside(x, y) && !(inside(x - 2, y) && inside(x + 2, y) &&
        inside(x, y - 2) && inside(x, y + 2))) border.push([x, y]);
  const thin = gap => {
    const grid = new Map(), keep = [];
    for (const p of border) {
      const gx = Math.floor(p[0] / gap), gy = Math.floor(p[1] / gap);
      let ok = true;
      for (let a = -1; a <= 1; a++) for (let b = -1; b <= 1; b++)
        for (const q of grid.get((gx + a) * 999 + gy + b) || [])
          if ((q[0] - p[0]) ** 2 + (q[1] - p[1]) ** 2 < gap * gap) ok = false;
      if (!ok) continue;
      keep.push(p);
      const key = gx * 999 + gy;
      (grid.get(key) || grid.set(key, []).get(key)).push(p);
    }
    return keep;
  };
  let gap = 9, edge = thin(gap);
  while (edge.length > DRONES * 0.45) edge = thin(gap *= 1.12);
  // inner grid of 18x15 (cell aligned), squeezed until it fits 800
  const need = DRONES - edge.length, m = gap * 0.55;
  const fill = k => {
    const pts = [];
    for (let y = 7.5; y < 600; y += 15 * k) for (let x = 4.5; x < 1080; x += 18 * k)
      if (inside(x, y) && inside(x - m, y) && inside(x + m, y) &&
          inside(x, y - m) && inside(x, y + m)) pts.push([x, y]);
    return pts;
  };
  let lo = 0.25, hi = 3;
  for (let i = 0; i < 22; i++) {
    const k = (lo + hi) / 2;
    if (fill(k).length >= need) lo = k; else hi = k;
  }
  const grid = fill(lo);
  const inner = Array.from({ length: need },
    (_, i) => grid[Math.floor(i * grid.length / need)] || edge[i % edge.length]);
  return edge.map(p => ({ x: p[0], y: p[1], rgb: colorOf(p[0], p[1], true) }))
    .concat(inner.map(p => ({ x: p[0], y: p[1], rgb: colorOf(p[0], p[1], false) })));
}

// drone pictures are dotted LINE drawings, like the real Gwangalli show:
// stroke the drawing on a mask, then space drones evenly along the strokes
function shapeFromStroke(paint, colorOf) {
  pixelScale(MK); MK.clearRect(0, 0, 540, 300);
  MK.setTransform(0.5, 0, 0, 0.5, 0, 0);
  MK.strokeStyle = '#fff'; MK.lineWidth = 5; MK.lineJoin = 'round'; MK.lineCap = 'round';
  paint(MK);
  const d = MK.getImageData(0, 0, 540, 300).data, pts = [];
  for (let y = 0; y < 300; y++) for (let x = 0; x < 540; x++)
    if (d[(y * 540 + x) * 4 + 3] > 127) pts.push([x * 2 + 1, y * 2 + 1]);
  const thin = gap => {
    const grid = new Map(), keep = [];
    for (const p of pts) {
      const gx = Math.floor(p[0] / gap), gy = Math.floor(p[1] / gap);
      let ok = true;
      for (let a = -1; a <= 1 && ok; a++) for (let b = -1; b <= 1 && ok; b++)
        for (const q of grid.get((gx + a) * 999 + gy + b) || [])
          if ((q[0] - p[0]) ** 2 + (q[1] - p[1]) ** 2 < gap * gap) { ok = false; break; }
      if (!ok) continue;
      keep.push(p);
      const key = gx * 999 + gy;
      (grid.get(key) || grid.set(key, []).get(key)).push(p);
    }
    return keep;
  };
  let lo = 4, hi = 40;
  for (let i = 0; i < 14; i++) { const g = (lo + hi) / 2; if (thin(g).length >= DRONES) lo = g; else hi = g; }
  const keep = thin(lo);
  return Array.from({ length: DRONES }, (_, i) => {
    const p = keep[Math.floor(i * keep.length / DRONES)];
    return { x: p[0], y: p[1], rgb: colorOf(p[0], p[1]) };
  });
}
const mixRgb = (a, b, f) => Math.round(a[0] + (b[0] - a[0]) * f) + ',' +
  Math.round(a[1] + (b[1] - a[1]) * f) + ',' + Math.round(a[2] + (b[2] - a[2]) * f);

// Busan's city bird: slender gull outline with feather lines
function gullOutline(c) {
  for (const side of [1, -1]) {
    c.save(); c.translate(540, 0); c.scale(side, 1); c.translate(-540, 0);
    c.beginPath(); c.moveTo(556, 226);
    c.bezierCurveTo(600, 196, 660, 150, 720, 146);
    c.bezierCurveTo(800, 142, 900, 196, 1010, 262);
    c.lineTo(986, 268); c.lineTo(958, 258); c.lineTo(948, 270);
    c.lineTo(920, 250); c.lineTo(908, 262); c.lineTo(880, 240);
    c.bezierCurveTo(820, 206, 770, 186, 724, 188);
    c.bezierCurveTo(670, 192, 610, 230, 560, 262);
    c.stroke();
    // flight feathers: three soft lines inside the wing
    for (const k of [0.3, 0.55, 0.8]) {
      c.beginPath(); c.moveTo(640 + k * 40, 196 + k * 4);
      c.quadraticCurveTo(760, 170 + k * 14, 860 + k * 70, 214 + k * 34); c.stroke();
    }
    c.restore();
  }
  c.beginPath(); c.ellipse(540, 250, 20, 46, 0, 0, 7); c.stroke();
  c.beginPath(); c.arc(540, 198, 17, 0, 7); c.stroke();
  c.beginPath(); c.moveTo(533, 212); c.lineTo(540, 238); c.lineTo(547, 212); c.stroke();
  c.beginPath(); c.moveTo(526, 290); c.lineTo(540, 326); c.lineTo(554, 290); c.stroke();
}
function seagullShape() {
  return shapeFromStroke(gullOutline,
    x => mixRgb([246, 238, 222], [160, 206, 242], clamp((Math.abs(x - 540) - 160) / 320)));   // ivory gull, pale-sky wing tips
}

// BUSAN: outlined letters, the way drone shows write words
function busanShape() {
  return shapeFromStroke(c => {
    c.font = '900 300px "Arial Black", "Helvetica Neue", Arial, sans-serif';
    c.letterSpacing = '30px';
    const m = c.measureText('BUSAN');
    const w = m.actualBoundingBoxLeft + m.actualBoundingBoxRight;
    const h = m.actualBoundingBoxAscent + m.actualBoundingBoxDescent;
    c.translate(70, 80); c.scale(940 / w, 220 / h);
    c.lineWidth = 5 * w / 940;
    c.strokeText('BUSAN', m.actualBoundingBoxLeft, m.actualBoundingBoxAscent);
  }, x => { const f = clamp((x - 70) / 940);   // rose -> apricot -> gold across the word
    return f < 0.5 ? mixRgb([236, 160, 172], [242, 178, 132], f * 2) : mixRgb([242, 178, 132], [245, 204, 116], f * 2 - 1); });
}

// nearest-first matching: each drone grabs the closest free spot
function matchNearest(from, to) {
  const used = new Uint8Array(to.length), map = new Int32Array(from.length);
  const order = from.map((p, i) => i)
    .sort((a, b) => Math.abs(from[b].x - 540) - Math.abs(from[a].x - 540));
  for (const i of order) {
    let best = 0, bestD = Infinity;
    for (let j = 0; j < to.length; j++) {
      if (used[j]) continue;
      const d = (to[j].x - from[i].x) ** 2 + (to[j].y - from[i].y) ** 2;
      if (d < bestD) { bestD = d; best = j; }
    }
    used[best] = 1; map[i] = best;
  }
  return map;
}

const SHAPES = {}, drones = [];
function setupDrones() {
  SHAPES.seagull = seagullShape();
  SHAPES.busan = busanShape();
  const toBusan = matchNearest(SHAPES.seagull, SHAPES.busan);
  const pads = [130, 320, 540, 760, 950];   // take-off spots on the sand
  for (let i = 0; i < DRONES; i++) {
    const gx = SHAPES.seagull[i].x;
    const pad = pads.reduce((a, b) => Math.abs(b - gx) < Math.abs(a - gx) ? b : a);
    drones.push({
      i, b: toBusan[i],
      sx: clamp(gx + (Math.random() - 0.5) * 260, 40, 1040), sy: 1150 + Math.random() * 110,
      launch: 4 + Math.random() * 1.1, off: 20.4 + Math.random() * 1.6,
      bend: (Math.random() - 0.5) * 90, drift: (Math.random() - 0.5) * 140
    });
  }
}

// where spot j of a picture is at time T (flapping wings)
function shapePoint(name, j, T) {
  const p = SHAPES[name][j];
  if (name === 'seagull') {
    const f = clamp((Math.abs(p.x - 540) - 40) / 420);
    return { x: p.x, y: p.y - 55 * Math.sin(T * Math.PI * 2 / 0.8) * f ** 1.5, rgb: p.rgb };
  }
  return p;
}

// a drone wakes on the sand, lifts off and heads straight to its seagull spot
function launchDrones(d, T) {
  if (T < d.launch) return null;
  const p = clamp((T - d.launch) / (6.3 - d.launch));
  const to = shapePoint('seagull', d.i, T);
  return {
    x: lerp(d.sx, to.x, ease(p)), y: lerp(d.sy, to.y, 1 - (1 - p) ** 2.2),
    rgb: to.rgb, r: p < 1 ? 2 : 4, a: clamp((T - d.launch) / 0.2)
  };
}

// fly from one picture to the next on a gently bent path (1.4 s)
function morph(d, fromName, fi, t0, toName, ti, T) {
  const a = shapePoint(fromName, fi, t0), b = shapePoint(toName, ti, T);
  const e = ease(clamp((T - t0) / 1.4));
  const dx = b.x - a.x, dy = b.y - a.y, len = Math.hypot(dx, dy) || 1;
  const bow = d.bend * Math.sin(Math.PI * e) * Math.min(1, len / 200);
  return {
    x: lerp(a.x, b.x, e) - dy / len * bow, y: lerp(a.y, b.y, e) + dx / len * bow,
    rgb: e < 0.5 ? a.rgb : b.rgb, r: 4, a: 1, moving: e > 0 && e < 1
  };
}

function dronePos(d, T) {
  if (T < 4 || T >= d.off) return null;
  if (T < 13) return launchDrones(d, T);                       // lift off, then the seagull
  if (T < 20) return morph(d, 'seagull', d.i, 13, 'busan', d.b, T);
  // 20 s: one white flash as BUSAN, then drift up and switch off one by one
  const p = shapePoint('busan', d.b, T), u = Math.max(0, T - 20.3);
  return {
    x: p.x + d.drift * u, y: p.y - 70 * u - 50 * u * u, rgb: WHITE,
    // one soft white pulse: the glow swells, the glyphs stay
    r: 4 + 0.9 * Math.sin(Math.PI * clamp((T - 20) / 0.6)),
    a: Math.min(1, (d.off - T) / 0.25), moving: u > 0
  };
}

const sprites = {};
function droneSprite(rgb, r) {
  const key = rgb + '|' + r;
  if (sprites[key]) return sprites[key];
  const [s, c] = makeCanvas(16, 16), core = 1.1 / r;
  const g = c.createRadialGradient(8, 8, 0, 8, 8, 8);
  g.addColorStop(0, 'rgba(255,255,255,1)');
  g.addColorStop(core * 0.5, rgba(rgb, 1));
  g.addColorStop(core, rgba(rgb, 0.9));
  g.addColorStop(Math.min(0.99, core * 1.35), rgba(rgb, 0.12));
  g.addColorStop(1, rgba(rgb, 0));
  c.fillStyle = g; c.fillRect(0, 0, 16, 16);
  return (sprites[key] = s);
}
function drawDrone(c, p) {
  c.globalAlpha = p.a;
  c.drawImage(droneSprite(p.rgb, p.r), p.x - p.r, p.y - p.r, p.r * 2, p.r * 2);
}

// 800 drones: bright dot + small halo, added with 'lighter'
const droneDots = [];   // drones printed as crisp dot glyphs after sampling
function droneFormation(c, T) {
  const low = [], tails = new Path2D();
  droneDots.length = 0;
  c.globalCompositeOperation = 'lighter';
  for (const d of drones) {
    const p = dronePos(d, T);
    if (!p) continue;
    if (p.moving) {
      const q = dronePos(d, T - 0.1);
      if (q) { tails.moveTo(q.x, q.y); tails.lineTo(p.x, p.y); }
    }
    if (p.y > 790) { low.push(p); continue; }
    droneDots.push({ x: p.x, y: p.y, rgb: p.rgb, a: p.a, r: p.r, s: d.i, T });
    drawDrone(c, { x: p.x, y: p.y, rgb: p.rgb, r: 3.2, a: p.a * 0.9 });
    const k = clamp(p.x / CELL_W | 0, 0, COLS - 1), w = 0.012 * p.a, n = nums(p.rgb);
    colI[k] += w; colR[k] += w * n[0]; colG[k] += w * n[1]; colB[k] += w * n[2];
  }
  c.globalAlpha = 1; c.strokeStyle = 'rgba(245,235,220,0.3)'; c.lineWidth = 1.2;
  c.stroke(tails);
  c.globalCompositeOperation = 'source-over';
  return low;
}

// ---- fireworks ----
let shells = [];
function planFireworks() {
  shells = []; shellNo = 0;
  const add = (L, type, x, big, rgb) => {
    type = type || ['peony', 'willow', 'crackle'][Math.random() * 3 | 0];
    const willow = type === 'willow', n = big ? 110 : 70 + (Math.random() * 21 | 0);
    shells.push({
      L, type, n, x: x || 260 + Math.random() * 780,
      yb: big ? 110 + Math.random() * 60 : 90 + Math.random() * 330,
      R: big ? 240 : 120 + Math.random() * 70,
      rgb: rgb || nextShellColor(),
      k: willow ? 1.6 : 2.4, g: willow ? 120 : 55,
      life: big ? 3.2 : willow ? 2.6 : 2, trail: willow ? 0.75 : 0.32,
      dir: Float32Array.from({ length: n }, (_, i) => i / n * 6.283 + Math.random() * 0.08),
      spd: Float32Array.from({ length: n }, () => 0.6 + Math.random() * 0.4)
    });
  };
  for (let t = 20; t < 23.9; t += 0.5) add(t + Math.random() * 0.2);   // 2 per second
  for (let t = 24; t < 28.9; t += 0.25) add(t + Math.random() * 0.1);  // 4 per second
  [[250, '240,150,170'], [540, GOLD], [830, '232,232,240']]       // finale: rose, gold, silver
    .forEach(([x, rgb]) => add(28.1, 'willow', x, true, rgb));
}
// spark position with air drag (k) and gravity (g), closed form
function sparkAt(s, i, a) {
  const e = (1 - Math.exp(-s.k * a)) / s.k, v = s.R * s.k * s.spd[i];
  return [s.x + Math.cos(s.dir[i]) * v * e,
    s.yb + Math.sin(s.dir[i]) * v * e + s.g * (a - e) / s.k];
}
function launchRocket(c, s, p) {
  // rocket climbs from a barge by the deck, dragging a spark tail
  const y0 = deckTop(Math.max(260, s.x)) - 4;
  const yAt = q => lerp(y0, s.yb, 1 - (1 - clamp(q)) ** 2);
  c.strokeStyle = 'rgba(255,214,160,0.75)'; c.lineWidth = 2;
  c.beginPath(); c.moveTo(s.x, yAt(p - 0.2)); c.lineTo(s.x, yAt(p)); c.stroke();
  glow(c, s.x, yAt(p), 7, '255,232,190', 1);
}
// burst = radial spokes: each spark draws the path it just travelled
const fwDots = [];   // firework sparks printed as crisp glyphs after sampling
function drawBurst(c, s, a) {
  const fade = (1 - a / s.life) ** 1.3, blink = Math.floor(a * 14);
  for (let i = 0; i < s.n; i++) {
    if (a > s.life * 0.55 && hash(i * 7.3 + blink) < 0.35) continue;
    const col = a < 0.1 ? WHITE : s.rgb;
    for (let j = 0; j < 4; j++) {
      const aj = a - s.trail * j / 4;
      if (aj < 0) break;
      const p = sparkAt(s, i, aj);
      fwDots.push({ x: p[0], y: p[1], rgb: col, a: fade * [1, 0.7, 0.45, 0.25][j], ch: ['*', '+', '.', "'"][j], pri: 3 - j });
    }
  }
  c.globalAlpha = 0.45;
  for (let seg = 0; seg < 4; seg++) {
    const a1 = Math.max(0, a - s.trail * seg / 4), a0 = Math.max(0, a - s.trail * (seg + 1) / 4);
    if (a1 <= a0) break;
    c.beginPath();
    for (let i = 0; i < s.n; i++) {
      if (a > s.life * 0.55 && hash(i * 7.3 + blink) < 0.35) continue;  // twinkle
      const p0 = sparkAt(s, i, a0), p1 = sparkAt(s, i, a1);
      c.moveTo(p0[0], p0[1]); c.lineTo(p1[0], p1[1]);
    }
    c.strokeStyle = rgba(a < 0.1 ? WHITE : s.rgb, fade * [1, 0.55, 0.3, 0.14][seg]);
    c.lineWidth = [2.4, 1.8, 1.3, 1][seg];
    c.stroke();
  }
  c.globalAlpha = 1;
  if (s.type === 'crackle' && a > 1.1) {   // tiny white pops near the end
    c.fillStyle = 'rgba(255,255,255,0.9)';
    for (let i = 0; i < s.n; i++) if (hash(i * 3.1 + Math.floor(a * 22)) < 0.3) {
      const p = sparkAt(s, i, a);
      c.fillRect(p[0] + (hash(i + a) - 0.5) * 20, p[1] + (hash(i - a) - 0.5) * 20, 3, 3);
    }
  }
  const k0 = clamp((s.x - s.R) / CELL_W | 0, 0, COLS - 1);
  const k1 = clamp((s.x + s.R) / CELL_W | 0, 0, COLS - 1), n = nums(s.rgb);
  for (let k = k0; k <= k1; k++) {
    const w = 0.08 * fade;
    colI[k] += w; colR[k] += w * n[0]; colG[k] += w * n[1]; colB[k] += w * n[2];
  }
}
function fireworksShow(c, T) {
  c.globalCompositeOperation = 'lighter'; c.lineCap = 'round';
  let fired = 0;
  for (const s of shells) {
    if (T < s.L) continue;
    const age = T - s.L - 0.9;
    if (age < 0) { launchRocket(c, s, (T - s.L) / 0.9); continue; }
    fired++;
    if (age < s.life) drawBurst(c, s, age);
  }
  c.globalCompositeOperation = 'source-over';
  return fired;
}
// each burst brightens sky and sea just a little
function burstFlash(c, T) {
  for (const s of shells) {
    const a = T - s.L - 0.9;
    if (a >= 0 && a < 0.3) glow(c, s.x, s.yb, 420, s.rgb, 0.025 * (1 - a / 0.3));
  }
}

// finale: gold and silver niagara pouring from the lower deck into the sea
function niagaraFinale(c, T) {
  if (T < 24 || T > 29.6) return;
  const fade = T > 29 ? 1 - (T - 29) / 0.6 : 1;
  c.lineCap = 'butt';
  ['232,232,240', '245,190,150'].forEach((rgb, odd) => {   // niagara: silver ivory + pale apricot
    const curtain = new Path2D(), sparks = new Path2D(), echo = new Path2D();
    let n = 0;
    for (let x = 1078; x > 252; x -= spacing(x) * 0.6, n++) {
      if (n % 2 !== odd) continue;
      const on = clamp((T - 24 - 2.5 * (1078 - x) / 830) / 0.5);
      if (on <= 0) break;
      const y0 = deckBottom(x), y1 = waterAt(x), drop = (y1 - y0) * on;
      curtain.moveTo(x, y0); curtain.lineTo(x + 1, y0 + drop);
      for (let k = 0; k < 3; k++) {
        const ph = (T * 1.4 + hash(x + k * 17)) % 1, y = y0 + (y1 - y0) * ph * ph;
        if (y < y0 + drop) { sparks.moveTo(x, y); sparks.lineTo(x, Math.min(y + 12, y1)); }
      }
      if (on >= 1) {   // its reflection on the water
        const w = Math.sin(T * 6 + x) * 2;
        echo.moveTo(x + w, y1 + 2);
        echo.lineTo(x - w, Math.min(SEA_END, y1 + (y1 - y0) * 0.7));
      }
    }
    c.lineWidth = 1.2; c.strokeStyle = rgba(rgb, 0.5 * fade); c.stroke(curtain);
    c.lineWidth = 2.2; c.strokeStyle = rgba(rgb, fade); c.stroke(sparks);
    c.setLineDash([5, 4]); c.lineDashOffset = -T * 40;
    c.lineWidth = 1.2; c.strokeStyle = rgba(rgb, 0.25 * fade); c.stroke(echo);
    c.setLineDash([]);
  });
}

function drawSea(c) {
  const g = c.createLinearGradient(0, HORIZON, 0, SEA_END);
  g.addColorStop(0, '#0b0a10'); g.addColorStop(1, '#040305');
  c.fillStyle = g; c.fillRect(0, HORIZON, W, SEA_END - HORIZON);
}
function reflectInSea(T) {
  pixelScale(M); M.fillStyle = '#000'; M.fillRect(0, 0, LW, 267);
  M.drawImage(front, 0, 0); M.drawImage(bridge, 0, 0);
  R.globalCompositeOperation = 'source-over'; R.clearRect(0, 0, LW, 110);
  // flipped, stretched 1.6x, each row swaying sideways
  for (let j = 0; j < 110; j++) {
    const amp = (2 + 4 * j / 110) / 3;
    const dx = Math.sin(j * 0.8 + T * 3) * amp + Math.sin(j * 0.31 - T * 1.3) * amp * 0.5;
    R.drawImage(mirror, 0, 266 - j / 1.6, LW, 1, dx, j, LW, 1);
  }
  // multiply by itself: only real lights survive
  R.globalCompositeOperation = 'multiply'; R.drawImage(water, 0, 0);
  R.globalCompositeOperation = 'source-over';
  // light pillars under drones and fireworks high in the sky
  for (let k = 0; k < COLS; k++) if (colI[k] > 0.02) {
    const n = colI[k];
    R.fillStyle = rgba((colR[k] / n | 0) + ',' + (colG[k] / n | 0) + ',' + (colB[k] / n | 0),
      Math.min(0.6, n));
    R.fillRect(k * 3, 3, 3, 107);
  }
  // cut every row with broken ripples
  R.globalCompositeOperation = 'destination-out';
  for (let j = 0; j < 110; j++)
    R.drawImage(waves, 0, j, LW + 120, 1, -((T * (8 + (j % 5) * 4)) % 120), j, LW + 120, 1);
  pixelScale(P); P.globalCompositeOperation = 'lighter'; P.globalAlpha = 0.5;
  P.drawImage(water, 0, 267);
  P.globalAlpha = 1; P.globalCompositeOperation = 'source-over';
}
function drawBeach(c, T) {
  const g = c.createLinearGradient(0, SEA_END, 0, SCENE_H);
  g.addColorStop(0, '#0a0f1f'); g.addColorStop(1, '#030406');
  c.fillStyle = g; c.fillRect(0, SEA_END, W, SCENE_H - SEA_END);
  // three foam lines washing up and sliding back
  c.globalCompositeOperation = 'lighter'; c.lineCap = 'round';
  for (let k = 0; k < 3; k++) {
    const base = 1140 + k * 36 + Math.sin(T * 0.45 + k * 1.9) * 16;
    c.beginPath();
    for (let x = 0; x <= W; x += 24) {
      const y = base + Math.sin(x * 0.011 + T * 0.6 + k * 2) * 7 +
        Math.sin(x * 0.029 - T * 0.4 + k) * 3;
      if (x) c.lineTo(x, y); else c.moveTo(x, y);
    }
    c.setLineDash([140 - k * 30, 30 + k * 20]); c.lineDashOffset = -T * (14 + k * 6);
    c.strokeStyle = rgba('214,232,240', 0.8 - k * 0.22); c.lineWidth = 3 - k * 0.7;
    c.shadowBlur = 3; c.shadowColor = 'rgba(200,225,235,0.8)'; c.stroke();   // surf: pale aqua
  }
  c.setLineDash([]); c.shadowBlur = 0; c.globalCompositeOperation = 'source-over';
}

// stage 1: paint the photo on the small canvas
function paintPhoto(T) {
  pixelScale(F); F.clearRect(0, 0, LW, LH); F.drawImage(city, 0, 0);
  sceneScale(F);
  towers.forEach((t, i) => {
    if ((T * 0.9 + i * 0.37) % 1 < 0.4) glow(F, t.x, t.top - 4, 8, '255,40,30', 1);
  });
  colI.fill(0); colR.fill(0); colG.fill(0); colB.fill(0); fwDots.length = 0;
  const low = droneFormation(F, T), fired = fireworksShow(F, T);
  drawBridge(T);
  pixelScale(P); P.drawImage(sky, 0, 0);
  sceneScale(P); drawStars(P, T);
  pixelScale(P); P.drawImage(front, 0, 0);
  sceneScale(P); drawSea(P);
  reflectInSea(T);
  pixelScale(P); P.drawImage(bridge, 0, 0);
  sceneScale(P); drawBeach(P, T);
  P.globalCompositeOperation = 'lighter';
  for (const d of low) drawDrone(P, d);
  P.globalAlpha = 1;
  niagaraFinale(P, T); burstFlash(P, T);
  P.globalCompositeOperation = 'source-over';
  return fired;
}

// stage 2: sample the photo into 120 x 86 cells and print letters
const lum = new Float32Array(N), hot = new Int32Array(N);
const gx = new Float32Array(N), gy = new Float32Array(N);
const power = new Float32Array(N), coherent = new Float32Array(N);
const angle = new Float32Array(N), hist = new Uint32Array(64), colorCache = [];
function lineGlyph(e) {
  const x = Math.cos(e), y = Math.sin(e);
  if (Math.abs(y) < 0.38) return '-';
  if (Math.abs(x) < 0.38) return '|';
  return x * y < 0 ? '/' : '\\';
}
function sampleToAscii() {
  const px = P.getImageData(0, 0, LW, LH).data;
  // mean and brightest pixel of each 3x5 block (= one 9x15 cell)
  for (let cy = 0; cy < ROWS; cy++) for (let cx = 0; cx < COLS; cx++) {
    let sum = 0, max = -1, at = 0;
    for (let y = 0; y < 5; y++) {
      let i = ((cy * 5 + y) * LW + cx * 3) * 4;
      for (let x = 0; x < 3; x++, i += 4) {
        const l = px[i] * 0.299 + px[i + 1] * 0.587 + px[i + 2] * 0.114;
        sum += l; if (l > max) { max = l; at = i; }
      }
    }
    const k = cy * COLS + cx;
    lum[k] = Math.min(1, Math.pow((0.5 * sum / 15 + 0.5 * max) / 255, 0.9)); hot[k] = at;
  }
  // Sobel filter on the cell brightness grid
  const L = (x, y) => lum[clamp(y, 0, ROWS - 1) * COLS + clamp(x, 0, COLS - 1)];
  for (let y = 0; y < ROWS; y++) for (let x = 0; x < COLS; x++) {
    const k = y * COLS + x;
    gx[k] = L(x + 1, y - 1) + 2 * L(x + 1, y) + L(x + 1, y + 1)
      - L(x - 1, y - 1) - 2 * L(x - 1, y) - L(x - 1, y + 1);
    gy[k] = L(x - 1, y + 1) + 2 * L(x, y + 1) + L(x + 1, y + 1)
      - L(x - 1, y - 1) - 2 * L(x, y - 1) - L(x + 1, y - 1);
  }
  // structure tensor over 3x3 cells: edge strength and direction
  hist.fill(0);
  let lit = 0;
  for (let y = 0; y < ROWS; y++) for (let x = 0; x < COLS; x++) {
    let xx = 0, yy = 0, xy = 0;
    for (let b = -1; b <= 1; b++) for (let a = -1; a <= 1; a++) {
      const j = clamp(y + b, 0, ROWS - 1) * COLS + clamp(x + a, 0, COLS - 1);
      xx += gx[j] * gx[j]; yy += gy[j] * gy[j]; xy += gx[j] * gy[j];
    }
    const k = y * COLS + x, s = Math.sqrt(xx + yy);
    power[k] = s;
    coherent[k] = s > 0 ? Math.hypot(xx - yy, 2 * xy) / (xx + yy) : 0;
    angle[k] = 0.5 * Math.atan2(2 * xy, xx - yy) + Math.PI / 2;  // edge runs across the gradient
    if (lum[k] >= 0.07) { hist[Math.min(63, s * 4 | 0)]++; lit++; }
  }
  let cut = 63, acc = 0;
  while (cut > 0 && (acc += hist[cut]) < lit * 0.2) cut--;
  const edgeMin = Math.max(0.5, cut / 4);   // top 20% gradients become line glyphs
  pixelScale(A); A.clearRect(0, 0, W, SCENE_H);
  A.font = 'bold 13px Menlo, Consolas, "Courier New", monospace';
  A.textBaseline = 'top';
  let count = 0;
  // cells taken by a drone (and its halo) print the drone, not the sampled glow
  const dotAt = new Map(), halo = new Set();
  for (const p of droneDots) {
    const cx = clamp(Math.round(p.x / CELL_W - 0.5), 0, COLS - 1), cy = clamp(Math.round(p.y / CELL_H - 0.5), 0, ROWS - 1);
    const base = 0.78 + 0.22 * hash(p.s * 1.7), tw = 0.88 + 0.12 * Math.sin(p.T * 5 + p.s);
    const wave = 0.85 + 0.15 * Math.sin(p.x * 0.012 - p.T * 4);
    const spark = hash(p.s * 3.3 + Math.floor(p.T * 9)) < 0.015;
    const v = p.r > 5 || spark ? 1 : p.a * base * tw * wave;
    dotAt.set(cy * COLS + cx, { rgb: spark ? WHITE : p.rgb, a: v,
      ch: v > 0.97 ? '*' : v > 0.5 ? '\u2022' : 'o', pri: 9 });
    for (let b = -1; b <= 1; b++) for (let a = -1; a <= 1; a++) halo.add(clamp(cy + b, 0, ROWS - 1) * COLS + clamp(cx + a, 0, COLS - 1));
  }
  for (const p of fwDots) {
    if (p.y > 790 || p.a < 0.08) continue;
    const cx = Math.round(p.x / CELL_W - 0.5), cy = Math.round(p.y / CELL_H - 0.5);
    if (cx < 0 || cy < 0 || cx >= COLS || cy >= ROWS) continue;
    const k = cy * COLS + cx, q = dotAt.get(k);
    if (!q || q.pri < p.pri) dotAt.set(k, p);
  }
  for (let k = 0; k < N; k++) {
    const b = lum[k];
    if (dotAt.has(k)) continue;
    const bloom = halo.has(k) && power[k] < edgeMin * 1.5;
    if (bloom && b < 0.16) continue;
    if (b < 0.1) {             // empty night: faint, slowly twinkling dust above the sea
      const h = (k * 2654435761 >>> 0) % 1000, yy = k / COLS | 0;
      if (h < 26 && yy < 52 && !dotAt.has(k)) {
        const tw = 0.5 + 0.5 * Math.sin(curT * (1 + h % 7 * 0.3) + h);
        A.fillStyle = 'rgba(225,218,205,' + (0.16 + 0.3 * tw).toFixed(2) + ')';
        A.fillText(h < 4 ? '+' : h < 12 ? "'" : '.', (k % COLS) * CELL_W + 0.5, yy * CELL_H + 1);
      }
      continue;
    }
    let ch = RAMP[1 + Math.min(RAMP.length - 2,
      Math.floor(((b - 0.1) / 0.9) ** 0.8 * (RAMP.length - 1)))];
    if (power[k] >= edgeMin && coherent[k] > 0.45) ch = lineGlyph(angle[k]);
    if (bloom) ch = b > 0.35 ? ':' : '.';
    // a lone bright cell in the dark (a star, a far lamp) prints as a point, not a letter
    { const x = k % COLS, y = k / COLS | 0; let near = 0;
      for (let b2 = -1; b2 <= 1; b2++) for (let a2 = -1; a2 <= 1; a2++)
        if ((a2 || b2) && lum[clamp(y + b2, 0, ROWS - 1) * COLS + clamp(x + a2, 0, COLS - 1)] >= 0.1) near++;
      if (near <= 1) ch = b > 0.6 ? '*' : b > 0.3 ? '+' : '.'; }
    // colour: hue of the brightest pixel, max channel = 1, times brightness
    const i = hot[k], r = px[i], g = px[i + 1], bl = px[i + 2];
    const f = (150 + 105 * Math.sqrt(b)) / Math.max(r, g, bl, 1);
    let r2 = r * f, g2 = g * f, b2 = bl * f;
    const gy = (r2 + g2 + b2) / 3;   // push away from grey: richer colour
    const sat = v => Math.max(0, Math.min(255, gy + (v - gy) * 1.35));
    r2 = sat(r2); g2 = sat(g2); b2 = sat(b2);
    const key = (r2 >> 3) << 10 | (g2 >> 3) << 5 | (b2 >> 3);
    A.fillStyle = colorCache[key] || (colorCache[key] = 'rgb(' + ((key >> 10) << 3) + ',' +
      ((key >> 5 & 31) << 3) + ',' + ((key & 31) << 3) + ')');
    A.fillText(ch, (k % COLS) * CELL_W + 0.5, (k / COLS | 0) * CELL_H + 1);
    count++;
  }
  // drones: one bright dot glyph each, brightest ones as a star
  for (const [k, p] of dotAt) {
    const n = nums(p.rgb), a = p.a;
    const g2 = p.pri === 9 ? 0.55 + 0.45 * a : a;   // drones never go muddy; sparks fade out
    A.fillStyle = 'rgb(' + (n[0] * g2 | 0) + ',' + (n[1] * g2 | 0) + ',' + (n[2] * g2 | 0) + ')';
    A.fillText(p.ch, (k % COLS) * CELL_W + 0.5, (k / COLS | 0) * CELL_H + 1);
    count++;
  }
  return count;
}

// ---- info line and live code editor ----
const shapeName = T => T < 4 || T >= 20 ? 'off' : T < 6 ? 'launch'
  : T < 13 ? 'seagull' : 'BUSAN';
function drawInfo(T, fired, count) {
  out.fillStyle = 'rgba(0,0,0,0.8)'; out.fillRect(0, 1232, 660, 58);
  out.font = '17px Menlo, Consolas, monospace';
  out.textAlign = 'left'; out.textBaseline = 'alphabetic'; out.fillStyle = '#80858f';
  out.fillText('@ai.asap.kr', 24, 1254);
  out.fillText('drones ' + DRONES + '   shape ' + shapeName(T) + '   fireworks ' + fired +
    '   letters ' + count.toLocaleString('en-US'), 24, 1279);
}
const CODE_FONT = '23px Menlo, Consolas, "Courier New", monospace';
const KEYWORDS = new Set(['const', 'let', 'function', 'return', 'for', 'of', 'if',
  'else', 'continue', 'break', 'new', 'true', 'false']);
const TOKEN = /(\/\/.*$)|('(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*")|(\b\d+(?:\.\d+)?\b)|([A-Za-z_$][\w$]*)/g;
function colorize(line) {
  const parts = [];
  let last = 0, m;
  TOKEN.lastIndex = 0;
  while ((m = TOKEN.exec(line))) {
    if (m.index > last) parts.push([line.slice(last, m.index), '#c8ccd4']);
    const color = m[1] ? '#5c6370' : m[2] ? '#c3e88d' : m[3] ? '#f78c6c'
      : KEYWORDS.has(m[4]) ? '#c792ea' : line[TOKEN.lastIndex] === '(' ? '#82aaff' : '#c8ccd4';
    parts.push([m[0], color]); last = TOKEN.lastIndex;
  }
  if (last < line.length) parts.push([line.slice(last), '#c8ccd4']);
  return parts;
}
const CODE = SOURCE.map(colorize);
// which function is on screen when each scene starts
const SCENE_CODE = [[0, 'drawBridge'], [4, 'launchDrones'], [6, 'seagullShape'],
  [13, 'busanShape'], [20, 'fireworksShow'], [24, 'niagaraFinale']]
  .map(([t, name]) => [t, Math.max(CODE_ROWS,
    SOURCE.findIndex(l => l.includes('function ' + name + '(')))]);
out.font = CODE_FONT;
const CHAR_W = out.measureText('M').width;
function drawEditor(T) {
  out.fillStyle = '#000'; out.fillRect(0, SCENE_H, W, H - SCENE_H);
  out.fillStyle = '#2b2e35'; out.fillRect(0, CODE_TOP, W, 2);
  out.font = '21px Menlo, Consolas, monospace'; out.textBaseline = 'middle';
  out.textAlign = 'left'; out.fillStyle = '#c8ccd4';
  out.fillText('gwangan_bridge.js', 28, CODE_TOP + 34);
  out.textAlign = 'right'; out.fillStyle = '#8b9099';
  out.fillText('running · 30 fps', W - 28, CODE_TOP + 34);
  const status = out.measureText('running · 30 fps').width;
  out.fillStyle = '#ff9a3c';
  out.beginPath(); out.arc(W - 46 - status, CODE_TOP + 34, 6, 0, 7); out.fill();
  // newest line: jump to this scene's function, then 4 whole lines per second
  // one steady pace from start to end: 4 whole lines per second, no jumps
  const pos = CODE_ROWS + T * 4;
  const newest = Math.floor(pos);
  const slide = 1 - ease(clamp((pos - newest) / 0.45));
  const top = CODE_TOP + 64;
  out.save(); out.beginPath(); out.rect(0, top, W, H - top); out.clip();
  out.font = CODE_FONT;
  for (let r = -1; r < CODE_ROWS; r++) {
    const n = newest - CODE_ROWS + 1 + r, y = top + 4 + (r + slide) * LINE_H;
    const idx = (n % CODE.length + CODE.length) % CODE.length, mid = y + LINE_H / 2;
    if (n === newest) {
      out.fillStyle = rgba('255,255,255', 0.09 * (1 - (pos - newest) * 0.5));
      out.fillRect(0, y, W, LINE_H);
    }
    out.textAlign = 'right'; out.fillStyle = '#555a63'; out.fillText(idx + 1, 74, mid);
    out.textAlign = 'left';
    let x = 96;
    for (const [text, color] of CODE[idx]) {
      out.fillStyle = color; out.fillText(text, x, mid);
      x += text.length * CHAR_W;
      if (x > W) break;
    }
  }
  out.restore();
}

// ---- main loop ----
let started = performance.now(), lastPaint = 0, cycle = -1;
let curT = 0;
// a short line at the very end, typed out letter by letter
function drawMessage(t) {
  const msg = 'G W A N G A L L I', st = 25.8;
  if (t < st) return;
  const n = Math.min(msg.length, Math.floor((t - st) / 0.14) + 1);
  const fade = t > 29.6 ? Math.max(0, 1 - (t - 29.6) / 0.4) : 1;
  out.save();
  out.font = 'bold 30px Menlo, Consolas, monospace'; out.textAlign = 'center'; out.textBaseline = 'top';
  out.shadowColor = 'rgba(255,200,110,0.9)'; out.shadowBlur = 12;
  out.fillStyle = 'rgba(255,214,140,' + fade.toFixed(2) + ')';
  out.fillText(msg.slice(0, n).padEnd(msg.length, ' '), W / 2, 34);
  out.restore();
}
function frame(now) {
  requestAnimationFrame(frame);
  if (now - lastPaint < 31) return;   // about 30 fps
  lastPaint = now;
  const time = (now - started) / 1000, T = time % LOOP;
  if (Math.floor(time / LOOP) !== cycle) { cycle = Math.floor(time / LOOP); planFireworks(); }
  curT = T;
  const fired = paintPhoto(T), count = sampleToAscii();
  const sky = out.createLinearGradient(0, 0, 0, SCENE_H);   // deep navy night, not flat black
  sky.addColorStop(0, '#050506'); sky.addColorStop(0.62, '#0d0b10'); sky.addColorStop(1, '#0a0809');
  out.fillStyle = sky; out.fillRect(0, 0, W, SCENE_H);
  out.drawImage(letters, 0, 0);
  // soft bloom: the same letters again, blurred and added on top
  out.save(); out.globalCompositeOperation = 'lighter';
  out.globalAlpha = 0.35; out.filter = 'blur(4px)';
  out.drawImage(letters, 0, 0); out.restore();
  drawInfo(T, fired, count);
  drawEditor(T);
}
buildSky(); buildMarineCity(); buildRippleMask(); planHangers(); setupDrones();
F.imageSmoothingQuality = P.imageSmoothingQuality = 'high';
requestAnimationFrame(frame);
})();
</script>
</body>
</html>
광고