ASCII 남산타워
남산 위 N서울타워가 한 칸씩 지어지고, 조명이 파랑·분홍·금색·초록·무지개로 바뀌는 사이 불꽃놀이가 터지는 애니메이션이에요. 그림 없이 | / \ # 같은 글자만으로 그렸어요. 영상은 @ai.asap.kr 릴스에 있어요.

HTML 파일 한 개, 1,113줄.
광고
코드
클로드가 짠 코드를 그대로 녹화했어요.
광고
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Namsan N Seoul Tower · ASCII</title>
<style>
html, body { margin: 0; height: 100%; background: #000; }
body { display: flex; align-items: center; justify-content: center; overflow: hidden; }
canvas { display: block; width: min(100vw, 56.25vh); height: min(177.78vw, 100vh); background: #000; }
</style>
</head>
<body>
<canvas id="stage" width="1080" height="1920"></canvas>
<script>
(() => {
// ============================================================
// namsan_tower.js : ASCII N Seoul Tower on Namsan, 30s loop
// ============================================================
// ---------- canvas and character grid ----------
const W = 1080, H = 1920; // portrait 9:16
const SCENE_H = 1290; // scene area height
const EDITOR_TOP = 1300; // code editor starts here
const CELL_W = 12, CELL_H = 20; // one character per cell
const COLS = 90, ROWS = 64;
const N = COLS * ROWS;
const LOOP = 30; // seconds per cycle
const FPS = 30;
const canvas = document.getElementById('stage');
const ctx = canvas.getContext('2d');
const scene = document.createElement('canvas');
scene.width = W; scene.height = SCENE_H;
const sctx = scene.getContext('2d');
const MONO = 'Menlo, Consolas, "DejaVu Sans Mono", "Courier New", monospace';
const FONT = 'bold 19px ' + MONO;
const SOURCE = (document.currentScript || document.scripts[document.scripts.length - 1]).textContent;
// ---------- deterministic randomness ----------
function makeRandom(seed) {
return () => {
seed = (seed + 0x6D2B79F5) | 0;
let x = Math.imul(seed ^ (seed >>> 15), 1 | seed);
x = (x + Math.imul(x ^ (x >>> 7), 61 | x)) ^ x;
return ((x ^ (x >>> 14)) >>> 0) / 4294967296;
};
}
const rand = makeRandom(20261119);
function hash(a, b) {
let h = Math.imul(a | 0, 374761393) + Math.imul(b | 0, 668265263);
h = Math.imul(h ^ (h >>> 13), 1274126177);
return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
}
// small color helpers
const mix = (a, b, k) => [a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k, a[2] + (b[2] - a[2]) * k];
const scale = (a, k) => [a[0] * k, a[1] * k, a[2] * k];
const WHITE = [255, 255, 255];
const RED = [255, 40, 36];
const BLACK = [0, 0, 0];
// ---------- cell buffers ----------
const SKY = 0, FAR = 1, HILL = 2, TOWER = 3, MAST = 4, CITY = 5;
const kind = new Uint8Array(N); // what occupies each cell
const baseCh = new Array(N).fill(' '); // static picture
const baseCol = new Float32Array(N * 3);
const ch = new Array(N).fill(' '); // this frame
const col = new Float32Array(N * 3);
const wide = new Uint8Array(N); // hangul sign glyphs span two cells
const mark = new Uint32Array(N); // one firework glyph per cell per frame
let stamp = 0;
const cellIndex = (c, r) => (c < 0 || r < 0 || c >= COLS || r >= ROWS) ? -1 : r * COLS + c;
const cellAtPx = (x, y) => cellIndex(Math.floor(x / CELL_W), Math.floor(y / CELL_H));
const isOpenSky = i => i >= 0 && kind[i] <= FAR; // fireworks only light open sky
function setBase(i, chr, rgb, k) {
baseCh[i] = chr;
baseCol[i * 3] = rgb[0]; baseCol[i * 3 + 1] = rgb[1]; baseCol[i * 3 + 2] = rgb[2];
if (k !== undefined) kind[i] = k;
}
function put(i, chr, rgb) {
ch[i] = chr;
col[i * 3] = rgb[0]; col[i * 3 + 1] = rgb[1]; col[i * 3 + 2] = rgb[2];
}
function tint(i, rgb, k) {
col[i * 3] += rgb[0] * k; col[i * 3 + 1] += rgb[1] * k; col[i * 3 + 2] += rgb[2] * k;
}
// ---------- layer 0: night sky, navy above, orange city haze below ----------
function paintSky() {
for (let r = 0; r < ROWS; r++) {
const k = Math.min(1, (r * CELL_H + 10) / 900);
const tone = mix([16, 24, 66], [112, 64, 36], k * k);
for (let c = 0; c < COLS; c++) {
const i = cellIndex(c, r);
kind[i] = SKY;
if (hash(c, r) < 0.05 + 0.45 * k * k * k) {
const chr = hash(r + 11, c) < 0.2 + 0.5 * k ? ':' : '.';
setBase(i, chr, scale(tone, 0.55 + 0.45 * hash(c + 3, r)));
}
}
}
}
// ---------- layer 1: faint far ridge on the horizon ----------
const farRidgeY = x => 840 + 16 * Math.sin(x * 0.010 + 1.3) + 9 * Math.sin(x * 0.026 + 0.4) + 4 * Math.sin(x * 0.07);
function paintFarRidge() {
for (let c = 0; c < COLS; c++) {
const top = Math.floor(farRidgeY(c * CELL_W + 6) / CELL_H);
for (let r = top; r < ROWS; r++) {
const i = cellIndex(c, r);
kind[i] = FAR;
if (r === top) setBase(i, hash(c, 91) < 0.75 ? '-' : '.', [52, 56, 84]);
else setBase(i, hash(c, r + 50) < 0.3 ? '.' : ' ', [34, 36, 58]);
}
}
}
// ---------- layer 2: Namsan, a broad forested dome ----------
const PEAK_X = 540, PEAK_Y = 760, EDGE_Y = 980;
function hillY(x) {
const u = Math.min(1, Math.abs(x - PEAK_X) / 540);
const dome = PEAK_Y + (EDGE_Y - PEAK_Y) * (1 - Math.cos(u * Math.PI / 2));
// tree crowns roughen the ridge, but never at the very top
return dome - 5 * Math.abs(Math.sin(x * 0.045)) * Math.min(1, Math.max(0, u * 5 - 0.5));
}
const crestRow = [];
const hillTint = new Float32Array(N); // how much tower light reaches each tree
let hillLit = [];
let lamps = [];
function paintHill() {
for (let c = -1; c <= COLS; c++) crestRow[c] = Math.floor(hillY(c * CELL_W + 6) / CELL_H);
// overlapping round tree clumps, painted back to front
const clumps = [];
for (let r = 36; r < ROWS + 2; r += 1.5)
for (let c = -4; c < COLS + 4; c += 3 + rand() * 1.6)
clumps.push({ c: c + rand(), r: r + rand() * 0.7, rx: 2.2 + rand() * 2.0, ry: 1.3 + rand() * 0.9 });
for (let c = 0; c < COLS; c++) {
const top = crestRow[c];
for (let r = top; r < ROWS; r++) {
const i = cellIndex(c, r);
kind[i] = HILL;
hillTint[i] = Math.max(0, 1 - Math.hypot(c - 45, (r - 38) * 1.6) / 16);
if (r === top) {
// ridge outline follows the slope
const slope = crestRow[c + 1] - crestRow[c - 1];
setBase(i, slope > 0 ? '\\' : slope < 0 ? '/' : '^', [72, 150, 82]);
continue;
}
let v = null; // position inside the front-most clump: -1 top .. 1 bottom
for (const k of clumps) {
const ex = (c - k.c) / k.rx, ey = (r - k.r) / k.ry;
if (ex * ex + ey * ey <= 1) v = ey;
}
const d = 0.88 * (0.85 + 0.15 * hash(c, r)); // far away: a touch darker than the city
if (v === null) setBase(i, hash(c, r) < 0.4 ? '.' : ' ', scale([20, 52, 30], d));
else if (v < -0.45) setBase(i, '^', scale([76, 168, 86], d)); // sunlit crown
else if (v < 0.05) setBase(i, 'A', scale([52, 128, 64], d));
else if (v < 0.5) setBase(i, 'n', scale([34, 92, 48], d)); // shaded underside
else setBase(i, '.', scale([22, 58, 32], d));
}
}
// street lamps along the ridge road
for (const c of [6, 12, 18, 24, 29, 34, 56, 61, 66, 72, 78, 84]) {
const i = cellIndex(c, crestRow[c] + 2 + (c % 2));
if (i < 0) continue;
setBase(i, '.', [255, 205, 90]);
lamps.push(i);
}
}
// ---------- layer 3: N Seoul Tower (cols 41..49, rows 4..37) ----------
const TOWER_BASE = 38, TIP_ROW = 4;
const TOWER_SPAN = TOWER_BASE - TIP_ROW;
const towerCells = [];
function addTower(c, r, chr, part, shade) {
const i = cellIndex(c, r);
if (kind[i] === HILL) return; // trees hide the foot of the tower
towerCells.push({ c, r, i, ch: chr, part, shade, born: 0, flash: 0 });
}
function buildTower() {
// antenna: red-white lattice, 3 cells wide at the bottom, 1 at the top
for (let r = 5; r <= 9; r++) addTower(45, r, r % 2 ? 'x' : '|', 'antenna', 1);
for (let r = 10; r <= 13; r++) {
const even = r % 2 === 0;
addTower(44, r, even ? '/' : '\\', 'antenna', 1);
addTower(45, r, 'x', 'antenna', 0.85);
addTower(46, r, even ? '\\' : '/', 'antenna', 0.6);
}
// short neck between antenna and drum
addTower(44, 14, '|', 'neck', 1); addTower(45, 14, '#', 'neck', 0.8); addTower(46, 14, '|', 'neck', 0.5);
// observatory drum: rounded caps and six window bands
for (let c = 42; c <= 48; c++) addTower(c, 15, '_', 'drum', 0.9);
const bandShade = [0.72, 1.0, 0.84, 0.95, 0.68, 0.88];
for (let r = 16; r <= 21; r++) {
const cap = r === 16 || r === 21;
addTower(41, r, cap ? '(' : '|', 'drum', 0.9);
addTower(42, r, r % 2 ? '@' : '|', 'glass', 1); // vertical glass highlight
for (let c = 43; c <= 48; c++) addTower(c, r, r % 2 ? '=' : '#', 'drum', bandShade[r - 16] * (1 - (c - 43) * 0.09));
addTower(49, r, cap ? ')' : '|', 'drum', 0.45);
}
addTower(42, 22, '\\', 'drum', 0.8);
for (let c = 43; c <= 47; c++) addTower(c, 22, '_', 'drum', 0.7);
addTower(48, 22, '/', 'drum', 0.5);
// shaft: lit from the left, seam down the middle, ring deck at 30%
for (let r = 23; r < TOWER_BASE; r++) {
if (r === 27) {
addTower(43, r, '(', 'deck', 0.9);
for (let c = 44; c <= 46; c++) addTower(c, r, '=', 'deck', 1 - (c - 44) * 0.25);
addTower(47, r, ')', 'deck', 0.45);
continue;
}
if (r === 28) {
addTower(43, r, '\\', 'deck', 0.8);
for (let c = 44; c <= 46; c++) addTower(c, r, '-', 'deck', 0.9 - (c - 44) * 0.25);
addTower(47, r, '/', 'deck', 0.4);
continue;
}
addTower(44, r, '@#8'[r % 3], 'shaft', 1);
addTower(45, r, [25, 31, 34].includes(r) ? '=' : '|', 'shaft', 0.7); // seam + small windows
addTower(46, r, ':-.'[r % 3], 'shaft', 0.42);
}
for (const t of towerCells) kind[t.i] = TOWER;
kind[cellIndex(45, TIP_ROW)] = TOWER; // aviation light on the tip
// construction order: bottom row first, one cell at a time
const order = towerCells.slice().sort((a, b) => b.r - a.r || a.c - b.c);
order.forEach((t, k) => { t.born = 0.15 + (k / order.length) * 3.6; });
}
// ---------- small red-white transmitter mast left of the tower ----------
const mastCells = [];
function buildMast() {
for (let r = 21; r < TOWER_BASE; r++) {
const w = 1 + Math.round(3 * (r - 21) / 17); // trapezoid: 1 cell up top, 4 at the base
const left = 37 - Math.floor((w - 1) / 2);
for (let k = 0; k < w; k++) {
const c = left + k;
const i = cellIndex(c, r);
if (kind[i] === HILL) continue;
const chr = w === 1 ? (r % 2 ? 'x' : '|')
: k === 0 ? '/' : k === w - 1 ? '\\' : ((r + c) % 2 ? '\\' : '/');
mastCells.push({ i, ch: chr, red: Math.floor((r - 21) / 2) % 2 === 0, shade: k === 0 ? 1 : 0.7 });
kind[i] = MAST;
}
}
kind[cellIndex(37, 20)] = MAST;
}
// ---------- layer 4: Seoul skyline in three depths ----------
const role = new Uint8Array(N); // 1 window, 2 edge, 3 light, 4 sign
const winChar = new Array(N);
const winRGB = new Float32Array(N * 3), winDim = new Float32Array(N * 3);
const winLit = new Uint8Array(N), winHasDim = new Uint8Array(N);
const windows = [];
const outlines = [];
const roofLights = [];
const signs = [];
let litCount = 0;
const SIGN_WORDS = ['CAFE', '24H', '약국', '편의점', 'PC', 'BAR', '치킨', 'GS25', 'HOF', '노래방'];
const SIGN_COLORS = [[255, 90, 190], [80, 220, 255], [90, 255, 140]];
const WINDOW_COLORS = [[255, 205, 90], [255, 145, 50], [240, 238, 220]];
const LAYER_BRIGHT = [0.34, 0.6, 1.0];
function cityRow(layer, minW, maxW, topMin, topMax, gapMax) {
const row = [];
const types = layer === 0 ? ['back'] : ['office', 'apartment', 'stepped', 'office', 'apartment'];
let x = -Math.floor(rand() * 3);
while (x < COLS) {
const w = minW + Math.floor(rand() * (maxW - minW + 1));
const top = topMin + Math.floor(rand() * (topMax - topMin + 1));
row.push({ layer, x0: x, w, top, type: types[Math.floor(rand() * types.length)] });
x += w + Math.floor(rand() * (gapMax + 1));
}
return row;
}
function paintBuilding(b) {
const bright = LAYER_BRIGHT[b.layer];
const edge = scale([128, 136, 168], [0.4, 0.62, 1][b.layer]);
const step = b.type === 'stepped' ? Math.min(2, (b.w - 4) >> 1) : 0;
for (let r = b.top; r < ROWS; r++) {
const inset = r < b.top + 3 ? step : 0;
const left = b.x0 + inset, right = b.x0 + b.w - 1 - inset;
const ratio = 0.6 + 0.38 * hash(b.x0 * 7 + b.layer * 131, r); // share of lit windows on this floor
for (let c = left; c <= right; c++) {
const i = cellIndex(c, r);
if (i < 0) continue;
kind[i] = CITY; wide[i] = 0; role[i] = 0;
const shoulder = step > 0 && r === b.top + 3 && (c < b.x0 + step || c > b.x0 + b.w - 1 - step);
if (r === b.top || shoulder) { // roof rim
setBase(i, b.type === 'apartment' || shoulder ? '=' : '_', edge); role[i] = 2; continue;
}
if (c === left || c === right) { setBase(i, '|', edge); role[i] = 2; continue; }
if (b.type === 'apartment' && (c - b.x0) % 4 === 0) { setBase(i, '|', scale(edge, 0.7)); role[i] = 2; continue; }
const chr = b.type === 'back' ? (hash(c, r) < 0.5 ? '.' : ':') : b.type === 'apartment' ? '=' : '#';
const pick = hash(c, r * 2 + 9);
const hue = WINDOW_COLORS[pick < 0.5 ? 0 : pick < 0.8 ? 1 : 2];
const rgb = scale(hue, bright * (0.85 + 0.15 * (r - 46) / 18) * (0.8 + 0.2 * hash(r, c)));
role[i] = 1; winChar[i] = chr;
winLit[i] = hash(c * 3 + 1, r * 5 + b.layer) < ratio ? 1 : 0;
winRGB.set(rgb, i * 3);
winHasDim[i] = b.layer > 0 ? 1 : 0;
winDim.set(scale([42, 42, 56], bright), i * 3);
setBase(i, chr, rgb);
}
}
}
function placeExtra(c, r, chr, rgb) {
const i = cellIndex(c, r);
if (i < 0 || kind[i] === CITY || kind[i] === TOWER || kind[i] === MAST) return -1;
setBase(i, chr, rgb, CITY); role[i] = 0;
return i;
}
function addRoofExtras(b) {
// water tanks, antennas and red aviation lights
const bright = LAYER_BRIGHT[b.layer];
const step = b.type === 'stepped' ? Math.min(2, (b.w - 4) >> 1) : 0;
const mid = b.x0 + (b.w >> 1);
const roll = rand();
if (roll < 0.3 && b.w >= 6) {
placeExtra(b.x0 + step + 1, b.top - 1, 'n', scale([130, 128, 140], bright));
placeExtra(b.x0 + step + 2, b.top - 1, 'n', scale([130, 128, 140], bright));
} else if (roll < 0.55) {
for (let k = 1; k <= 3; k++) placeExtra(mid, b.top - k, '|', scale([150, 150, 165], bright));
const i = placeExtra(mid, b.top - 4, 'o', RED);
if (i >= 0) roofLights.push({ i, phase: rand() });
}
if (rand() < 0.3) {
const i = placeExtra(b.x0 + b.w - 2 - step, b.top - 1, 'o', RED);
if (i >= 0) roofLights.push({ i, phase: rand() });
}
}
function addCrane(b) {
// one tower crane over a front building
const m = b.x0 + 2, top = b.top, rgb = [215, 160, 60];
for (let r = top - 1; r >= top - 8; r--) placeExtra(m, r, '#', scale(rgb, 0.85));
for (let c = m - 4; c <= m + 9; c++) placeExtra(c, top - 9, '=', rgb);
placeExtra(m, top - 10, 'A', rgb);
placeExtra(m + 7, top - 8, '|', scale(rgb, 0.6));
placeExtra(m + 7, top - 7, '|', scale(rgb, 0.6));
placeExtra(m + 7, top - 6, 'J', rgb);
const i = placeExtra(m + 9, top - 10, 'o', RED);
if (i >= 0) roofLights.push({ i, phase: 0.3 });
}
function addSigns(front) {
// neon shop signs on the ground floor of the front row
for (const b of front) {
if (b.x0 < 28 || b.w < 6 || rand() > 0.8) continue;
const word = SIGN_WORDS[Math.floor(rand() * SIGN_WORDS.length)];
const glyphs = [...word];
const need = glyphs.reduce((n, g) => n + (g.charCodeAt(0) > 255 ? 2 : 1), 0);
if (need > b.w - 2) continue;
let c = b.x0 + 1 + ((b.w - 2 - need) >> 1);
const rgb = SIGN_COLORS[Math.floor(rand() * SIGN_COLORS.length)];
const cells = [];
for (const g of glyphs) {
const i = cellIndex(c, ROWS - 1);
if (i < 0) break;
const isWide = g.charCodeAt(0) > 255;
setBase(i, g, rgb, CITY); role[i] = 4; wide[i] = isWide ? 1 : 0;
cells.push(i);
if (isWide) {
const j = cellIndex(c + 1, ROWS - 1);
if (j >= 0) { setBase(j, ' ', BLACK, CITY); role[j] = 4; }
}
c += isWide ? 2 : 1;
}
signs.push({ cells, rgb, seed: Math.floor(rand() * 1000) });
}
}
function buildCity() {
const rows = [
cityRow(0, 3, 6, 46, 50, 0), // far: narrow, small dim windows
cityRow(1, 5, 9, 48, 53, 1), // middle
cityRow(2, 8, 14, 47, 52, 1), // near: wide and bright
];
rows.forEach(row => row.forEach(paintBuilding));
rows[1].concat(rows[2]).forEach(b => addRoofExtras(b));
const host = rows[2].find(b => b.x0 >= 58 && b.x0 <= 76);
if (host) addCrane(host);
addSigns(rows[2]);
// keep a quiet corner for the caption lines
for (let r = 60; r < ROWS; r++) for (let c = 0; c < 28; c++) {
const i = cellIndex(c, r);
setBase(i, ' ', BLACK, CITY); role[i] = 0; wide[i] = 0;
}
for (let i = 0; i < N; i++) {
if (role[i] === 1) {
windows.push({
i, chr: winChar[i], lit: !!winLit[i],
rgb: [winRGB[i * 3], winRGB[i * 3 + 1], winRGB[i * 3 + 2]],
dim: winHasDim[i] ? [winDim[i * 3], winDim[i * 3 + 1], winDim[i * 3 + 2]] : null,
});
} else if (role[i] === 2) outlines.push(i);
}
litCount = windows.filter(w => w.lit).length;
}
// ---------- glow ring around the observatory drum ----------
const halo = [];
function buildHalo() {
for (let r = 12; r <= 25; r++) for (let c = 38; c <= 52; c++) {
const i = cellIndex(c, r);
if (kind[i] !== SKY) continue;
const dx = c < 41 ? 41 - c : c > 49 ? c - 49 : 0;
const dy = r < 15 ? 15 - r : r > 22 ? r - 22 : 0;
const d = Math.max(dx, dy);
if (d < 1 || d > 3) continue;
if (hash(c + 40, r) > 1.15 - d * 0.22) continue;
halo.push({ i, d, r });
}
}
// ---------- stars: three sizes, some twinkle ----------
const stars = [];
const STAR_CH = ['.', '+', '*'];
const STAR_RGB = [[120, 130, 175], [190, 200, 235], [255, 255, 255]];
function placeStars() {
let tries = 0;
while (stars.length < 380 && tries++ < 6000) {
const r = Math.floor(Math.pow(rand(), 1.4) * 42), c = Math.floor(rand() * COLS);
const i = cellIndex(c, r);
if (kind[i] !== SKY) continue;
const roll = rand();
stars.push({ i, lvl: roll < 0.6 ? 0 : roll < 0.9 ? 1 : 2, twinkle: rand() < 0.35, phase: rand() * 6.28, speed: 1.5 + rand() * 3 });
}
}
// ---------- tower lighting program ----------
const LIGHTS = [
{ at: 0, name: 'white', rgb: [225, 228, 238] },
{ at: 4, name: 'blue', rgb: [50, 130, 255] },
{ at: 8, name: 'pink', rgb: [255, 70, 170] },
{ at: 12, name: 'gold', rgb: [255, 185, 40] },
{ at: 16, name: 'green', rgb: [40, 235, 100] },
{ at: 20, name: 'rainbow', rgb: null },
];
const RAINBOW = [[255, 40, 40], [255, 140, 0], [255, 225, 0], [30, 220, 60], [40, 110, 255], [170, 50, 255]];
function phaseAt(t) {
let p = 0;
for (let k = 1; k < LIGHTS.length; k++) if (t >= LIGHTS[k].at) p = k;
return p;
}
function rainbowFlow(r, t) {
// one saturated color per window band, sliding upward
const k = Math.floor(r + t * 4);
return RAINBOW[((k % 6) + 6) % 6];
}
const phaseColor = (p, r, t) => LIGHTS[p].rgb || rainbowFlow(r, t);
function lightWave(r, t) {
// the new color climbs base to tip in 0.8s with a white-hot band riding its edge
const p = phaseAt(t);
if (p === 0) return { rgb: LIGHTS[0].rgb, hot: 0, p };
const h = (TOWER_BASE - r) / TOWER_SPAN;
const level = (t - LIGHTS[p].at) / 0.8 * (1 + 3 / TOWER_SPAN);
const ahead = level - h;
const rgb = ahead >= 0 ? phaseColor(p, r, t) : phaseColor(p - 1, r, t);
const hot = ahead >= 0 && ahead < 3 / TOWER_SPAN ? 1 - ahead * TOWER_SPAN / 6 : 0;
return { rgb, hot, p };
}
const LATTICE_WHITE = [236, 236, 240];
function towerCellColor(cell, t, wave) {
let rgb;
if (cell.part === 'antenna') rgb = scale(Math.floor((cell.r - 5) / 2) % 2 ? LATTICE_WHITE : [255, 46, 40], cell.shade);
else if (cell.part === 'glass') rgb = mix(WHITE, wave.rgb, 0.22);
else if (cell.part === 'drum') rgb = scale(wave.rgb, cell.shade);
else rgb = scale(mix(wave.rgb, WHITE, LIGHTS[wave.p].rgb ? 0.22 : 0), cell.shade);
if (wave.hot) rgb = mix(rgb, WHITE, wave.hot * 0.9);
const age = t - cell.born;
if (age < 0.25) rgb = mix(rgb, WHITE, 1 - age / 0.25); // freshly built cell flashes white
if (cell.flash > t) rgb = WHITE; // glitter: color only, never the glyph
return rgb;
}
function twinkle(t) {
// Eiffel-style glitter: at most 4% of tower cells flash white for 0.1s
const limit = Math.floor(towerCells.length * 0.04);
let active = 0;
for (const c of towerCells) if (c.flash > t) active++;
for (let k = 0; k < 2 && active < limit; k++) {
if (Math.random() < 0.7) {
towerCells[Math.floor(Math.random() * towerCells.length)].flash = t + 0.1;
active++;
}
}
}
// ---------- sparks, fireworks, smoke ----------
const PALETTE = [[255, 45, 60], [255, 190, 40], [50, 255, 100], [60, 130, 255], [185, 75, 255], [255, 80, 190], [40, 240, 230]];
const GOLD = [255, 190, 60];
const SHAPES = ['chrysanthemum', 'double', 'ring', 'willow', 'palm', 'chrysanthemum', 'double'];
let particles = [], rockets = [], flashes = [], smoke = [], sparks = [], crackles = [];
let schedule = [], nextEvent = 0, burstCount = 0;
function spawnSpark(x, y, rgb, speed, t) {
sparks.push({ x, y, vx: (Math.random() - 0.5) * speed * 2, vy: -Math.random() * speed - 30, born: t, life: 0.45 + Math.random() * 0.35, rgb });
}
function makeBurst(rnd, tBurst, side, opts = {}) {
const x = side < 0 ? 60 + rnd() * 270 : 750 + rnd() * 270;
const yMin = opts.yMin ?? 120, yMax = opts.yMax ?? 700;
const y = yMin + rnd() * (yMax - yMin);
const a = PALETTE[Math.floor(rnd() * PALETTE.length)];
let b = PALETTE[Math.floor(rnd() * PALETTE.length)];
if (b === a) b = PALETTE[(PALETTE.indexOf(a) + 3) % PALETTE.length];
const rise = 0.75 + rnd() * 0.45 * ((1000 - y) / 900);
return {
tBurst, tLaunch: tBurst - rise, x, y,
type: opts.type || SHAPES[Math.floor(rnd() * SHAPES.length)],
R: opts.R || 130 + rnd() * 70, a, b,
mixed: rnd() < 0.35, crackle: rnd() < 0.4,
};
}
function scheduleFireworks(rnd) {
// 8-16s: one per second, 16-24s: two per second, then the finale
const list = [];
let side = 1;
for (let t = 8; t < 16; t += 1) list.push(makeBurst(rnd, t + rnd() * 0.25, side = -side));
for (let t = 16; t < 24; t += 0.5) list.push(makeBurst(rnd, t + rnd() * 0.2, side = -side));
list.push(...finaleBursts(rnd));
return list.sort((p, q) => p.tLaunch - q.tLaunch);
}
function finaleBursts(rnd) {
// 24-29s: big overlapping chrysanthemums on both sides, four per second
const list = [];
for (let k = 0; k < 20; k++) {
const t = 24 + k * 0.25 + rnd() * 0.1;
list.push(makeBurst(rnd, t, k % 2 ? 1 : -1, {
type: rnd() < 0.6 ? 'chrysanthemum' : 'double', R: 170 + rnd() * 30, yMin: 140, yMax: 650,
}));
}
// 29s: the biggest gold willows, left and right at once
list.push(makeBurst(rnd, 29, -1, { type: 'willow', R: 230, yMin: 260, yMax: 320 }));
list.push(makeBurst(rnd, 29, 1, { type: 'willow', R: 230, yMin: 260, yMax: 320 }));
return list;
}
function launchRocket(ev) {
// a rocket climbs from between the buildings, dragging a | . tail
rockets.push({ ev, x0: ev.x + (Math.random() - 0.5) * 60, y0: 1010, x: ev.x, y: 1010 });
}
function explode(ev, t) {
// burst: particles, white flash, sky glow, then drifting smoke
burstCount++;
flashes.push({ x: ev.x, y: ev.y, t0: t, R: ev.R, rgb: ev.a, frames: 0 });
const P = o => particles.push(Object.assign({
x: ev.x, y: ev.y, born: t, hist: [{ x: ev.x, y: ev.y, t }],
drag: 2.2, grav: 32, tailTime: 0.34, tailMax: 9, head: '*',
life: 1.8 + Math.random() * 0.7, rgb: ev.a, vis: true, cur: WHITE,
}, o));
const spray = (count, R, rgbA, rgbB, extra = {}) => {
const drag = extra.drag || 2.2; // launch speed so the shell opens to radius R
for (let k = 0; k < count; k++) {
const ang = (k / count) * Math.PI * 2 + Math.random() * 0.08;
const s = R * drag * (0.9 + Math.random() * 0.1);
P(Object.assign({ vx: Math.cos(ang) * s, vy: Math.sin(ang) * s, rgb: rgbB && k % 2 ? rgbB : rgbA }, extra));
}
};
switch (ev.type) {
case 'chrysanthemum':
spray(60 + Math.floor(Math.random() * 21), ev.R, ev.a, ev.mixed ? ev.b : null, { tailMax: 6 + Math.floor(Math.random() * 5) });
break;
case 'double': // outer shell plus a smaller inner shell in another color
spray(64, ev.R, ev.a, null, { tailMax: 8 });
spray(32, ev.R * 0.48, ev.b, null, { tailMax: 5, life: 1.6 });
break;
case 'ring': { // tilted ellipse of o's with a few stars inside
const tilt = (Math.random() - 0.5) * 1.2, ct = Math.cos(tilt), st = Math.sin(tilt);
for (let k = 0; k < 44; k++) {
const ang = (k / 44) * Math.PI * 2;
const lx = Math.cos(ang) * ev.R, ly = Math.sin(ang) * ev.R * 0.36;
P({ vx: (lx * ct - ly * st) * 2.2, vy: (lx * st + ly * ct) * 2.2, head: 'o', tailMax: 2, tailTime: 0.12 });
}
for (let k = 0; k < 7; k++) {
const ang = Math.random() * Math.PI * 2, s = ev.R * 0.4 * Math.random();
P({ vx: Math.cos(ang) * s, vy: Math.sin(ang) * s, rgb: ev.b, tailMax: 1 });
}
break;
}
case 'willow': // gold strands that droop with long tails
spray(56, ev.R, GOLD, null, { drag: 1.5, grav: 75, tailTime: 0.9, tailMax: 8 + Math.floor(Math.random() * 5), life: 2.3 + Math.random() * 0.4 });
break;
case 'palm': { // thick curving fronds
const n = 6 + Math.floor(Math.random() * 3);
for (let f = 0; f < n; f++) {
const ang = -Math.PI / 2 + (f / (n - 1) - 0.5) * Math.PI * 1.7;
for (let j = 0; j < 6; j++) {
const s = ev.R * 1.6 * (0.62 + j * 0.08), jit = (Math.random() - 0.5) * 0.05;
P({ vx: Math.cos(ang + jit) * s, vy: Math.sin(ang + jit) * s, drag: 1.6, grav: 95, tailTime: 0.55, tailMax: 10, life: 2.0 + Math.random() * 0.4 });
}
}
break;
}
}
// faint smoke lingers and drifts with the wind
for (let k = 0; k < 18; k++) {
smoke.push({
x: ev.x + (Math.random() - 0.5) * ev.R * 1.2, y: ev.y + (Math.random() - 0.5) * ev.R,
vx: 8 + Math.random() * 10, vy: -4 - Math.random() * 4, born: t, life: 1.8 + Math.random() * 0.6,
});
}
// crackle: tiny white-gold pops as the big shell fades
if (ev.crackle && (ev.type === 'chrysanthemum' || ev.type === 'double')) {
crackles.push({ x: ev.x, y: ev.y + 20, R: ev.R, t0: t + 1.25, t1: t + 1.95 });
}
}
function updateRockets(t) {
for (const k of rockets) {
const e = k.ev;
if (t >= e.tBurst) { explode(e, t); k.done = true; continue; }
const u = Math.min(1, (t - e.tLaunch) / (e.tBurst - e.tLaunch));
const ease = 1 - (1 - u) * (1 - u); // slows as it nears the top
k.x = k.x0 + (e.x - k.x0) * u + Math.sin(u * 9) * 3;
k.y = k.y0 + (e.y - k.y0) * ease;
}
rockets = rockets.filter(k => !k.done);
}
function updateCrackles(t) {
for (const c of crackles) {
if (t < c.t0 || t > c.t1) continue;
for (let k = 0; k < 5; k++) {
const ang = Math.random() * Math.PI * 2, d = Math.sqrt(Math.random()) * c.R * 0.85;
const roll = Math.random();
particles.push({
x: c.x + Math.cos(ang) * d, y: c.y + Math.sin(ang) * d * 0.9 + 30, vx: 0, vy: 0,
drag: 0, grav: 0, born: t, life: 0.12 + Math.random() * 0.14, hist: [], tailTime: 0, tailMax: 0,
head: roll < 0.5 ? '*' : roll < 0.8 ? '+' : "'", rgb: Math.random() < 0.5 ? WHITE : GOLD,
crackle: true, vis: true, cur: WHITE,
});
}
}
crackles = crackles.filter(c => t <= c.t1);
}
function updateParticles(t, dt) {
for (const p of particles) {
// fast at first, then air drag slows it and gravity pulls it down
const fr = Math.exp(-p.drag * dt);
p.vx *= fr;
p.vy = p.vy * fr + p.grav * dt;
p.x += p.vx * dt; p.y += p.vy * dt;
p.hist.push({ x: p.x, y: p.y, t });
while (p.hist.length > 2 && t - p.hist[0].t > p.tailTime) p.hist.shift();
// white flash, deep color, then cooling embers
const age = t - p.born, k = age / p.life;
if (p.crackle) p.cur = p.rgb;
else if (age < 0.1) p.cur = WHITE;
else p.cur = scale(p.rgb, 1 - 0.68 * k);
p.vis = p.life - age > 0.3 || Math.random() < 0.55; // last 0.3s: random flicker
}
particles = particles.filter(p => t - p.born < p.life);
}
function updateDrift(t, dt) {
for (const m of smoke) { m.x += m.vx * dt; m.y += m.vy * dt; }
smoke = smoke.filter(m => t - m.born < m.life);
for (const s of sparks) { s.vy += 220 * dt; s.x += s.vx * dt; s.y += s.vy * dt; }
sparks = sparks.filter(s => t - s.born < s.life);
}
function updateTower(t, dt) {
// construction sparks for each newly placed cell
for (const c of towerCells) {
if (c.born <= t && c.born > t - dt) {
for (let k = 0; k < 3; k++) spawnSpark(c.c * CELL_W + 6, c.r * CELL_H + 10, WHITE, 120, t);
}
}
const p = phaseAt(t);
if (p === 0) return;
const since = t - LIGHTS[p].at;
if (since < 0.9) {
// sparks fly off the climbing band
const level = since / 0.8 * (1 + 3 / TOWER_SPAN);
const y = (TOWER_BASE - level * TOWER_SPAN) * CELL_H;
for (let k = 0; k < 4; k++) {
const side = Math.random() < 0.5 ? -1 : 1;
spawnSpark(546 + side * (50 + Math.random() * 50), y, phaseColor(p, 20, t), 160, t);
}
} else twinkle(t);
}
// ---------- one frame of the picture ----------
function resetFrame() {
for (let i = 0; i < N; i++) ch[i] = baseCh[i];
col.set(baseCol);
}
function drawStars(t) {
for (const s of stars) {
const b = s.twinkle ? 0.35 + 0.65 * (0.5 + 0.5 * Math.sin(t * s.speed + s.phase)) : 1;
put(s.i, STAR_CH[s.lvl], scale(STAR_RGB[s.lvl], b));
}
}
function skyGlow(t) {
// each new burst tints the surrounding sky with its color for 0.3s
for (const f of flashes) {
const age = t - f.t0;
if (age >= 0.3) continue;
const reach = f.R * 1.4, k0 = (1 - age / 0.3) * 0.32;
const c0 = Math.max(0, Math.floor((f.x - reach) / CELL_W)), c1 = Math.min(COLS - 1, Math.floor((f.x + reach) / CELL_W));
const r0 = Math.max(0, Math.floor((f.y - reach) / CELL_H)), r1 = Math.min(ROWS - 1, Math.floor((f.y + reach) / CELL_H));
for (let r = r0; r <= r1; r++) for (let c = c0; c <= c1; c++) {
const i = r * COLS + c;
if (kind[i] !== SKY) continue;
const d = Math.hypot(c * CELL_W + 6 - f.x, r * CELL_H + 10 - f.y);
if (d > reach) continue;
const k = k0 * (1 - d / reach);
if (ch[i] === ' ') { if (hash(c, r + 300) < 0.55) put(i, '.', scale(f.rgb, k)); }
else tint(i, f.rgb, k * 0.8);
}
}
}
function drawHalo(t) {
if (t < 3.8) return;
const fade = Math.min(1, (t - 3.8) / 0.5);
for (const h of halo) {
const rgb = lightWave(h.r, t).rgb;
put(h.i, h.d === 1 ? ':' : '.', scale(rgb, [0.36, 0.22, 0.13][h.d - 1] * fade));
}
}
function drawHill(t) {
// the summit catches the tower's light
if (t > 3.6) {
const rgb = lightWave(24, t).rgb;
const k = Math.min(1, (t - 3.6) / 0.6) * 0.42;
for (const i of hillLit) if (ch[i] !== ' ') tint(i, rgb, hillTint[i] * k);
}
for (const i of lamps) put(i, '.', scale([255, 205, 90], 0.8 + 0.2 * Math.sin(t * 7 + i)));
}
function drawCity(t) {
// random windows switch on and off; the city stays well over 65% lit
for (let k = 0; k < 4; k++) {
const w = windows[Math.floor(Math.random() * windows.length)];
if (w.lit && litCount / windows.length > 0.72) { w.lit = false; litCount--; }
else if (!w.lit) { w.lit = true; litCount++; }
}
for (const w of windows) {
if (w.lit) put(w.i, w.chr, w.rgb);
else if (w.dim) put(w.i, w.chr, w.dim);
else put(w.i, ' ', BLACK);
}
// firework light catches the building edges
const lights = flashes.filter(f => t - f.t0 < 0.45);
if (lights.length) {
for (const i of outlines) {
const x = (i % COLS) * CELL_W;
for (const f of lights) {
const k = (1 - (t - f.t0) / 0.45) * 0.55 * Math.max(0, 1 - Math.abs(x - f.x) / 650);
if (k > 0) tint(i, f.rgb, k);
}
}
}
// blinking red aviation lights
for (const L of roofLights) put(L.i, 'o', ((t + L.phase * 1.4) % 1.4) < 0.4 ? RED : [70, 12, 12]);
// neon signs, with the odd flicker
for (const s of signs) {
const b = hash(Math.floor(t * 8), s.seed) < 0.04 ? 0.3 : 1;
for (const i of s.cells) put(i, baseCh[i], scale(s.rgb, b));
}
}
function drawMast(t) {
for (const m of mastCells) put(m.i, m.ch, scale(m.red ? [220, 50, 45] : [205, 205, 212], 0.72 * m.shade));
put(cellIndex(37, 20), (t + 0.5) % 1 < 0.5 ? 'o' : ' ', RED);
}
function drawTower(t) {
for (const c of towerCells) {
if (t < c.born) { put(c.i, ' ', BLACK); continue; } // not built yet
put(c.i, c.ch, towerCellColor(c, t, lightWave(c.r, t)));
}
// aviation light on the tip blinks once a second
const tip = cellIndex(45, TIP_ROW);
if (t > 3.8 && t % 1 < 0.5) put(tip, 'o', [255, 30, 30]);
else put(tip, ' ', BLACK);
}
function drawSmoke(t) {
for (const m of smoke) {
const i = cellAtPx(m.x, m.y);
if (!isOpenSky(i) || (ch[i] !== ' ' && ch[i] !== '.')) continue;
put(i, '·', scale([72, 72, 82], (1 - (t - m.born) / m.life) * 0.9));
}
}
function traceTail(p, visit) {
// walk back along the particle's recent path, one distinct cell at a time
let px = p.x, py = p.y, last = cellAtPx(px, py), n = 0;
for (let k = p.hist.length - 1; k >= 0; k--) {
const q = p.hist[k];
const steps = Math.max(1, Math.ceil(Math.hypot(q.x - px, q.y - py) / 5));
for (let s = 1; s <= steps; s++) {
const i = cellAtPx(px + (q.x - px) * s / steps, py + (q.y - py) * s / steps);
if (i === last) continue;
last = i; n++;
if (i >= 0) visit(i, n);
if (n >= p.tailMax) return;
}
px = q.x; py = q.y;
}
}
function drawFireworks() {
stamp++;
for (const p of particles) { // bright heads first
if (!p.vis) continue;
const i = cellAtPx(p.x, p.y);
if (!isOpenSky(i) || mark[i] === stamp) continue;
mark[i] = stamp;
put(i, p.head, p.cur);
}
for (const p of particles) { // then fading tails, never doubling up a cell
if (!p.vis || !p.tailMax) continue;
traceTail(p, (i, n) => {
if (!isOpenSky(i) || mark[i] === stamp) return;
mark[i] = stamp;
put(i, n <= 2 ? '+' : n <= 5 ? '.' : '·', scale(p.cur, Math.max(0.22, 0.9 - n * 0.075)));
});
}
}
function flashCore() {
// white disc about 40px wide for two frames at the moment of a burst
for (const f of flashes) {
if (f.frames >= 2) continue;
f.frames++;
for (let dy = -1; dy <= 1; dy++) for (let dx = -2; dx <= 2; dx++) {
if (Math.hypot(dx * CELL_W, dy * CELL_H) > 24) continue;
const i = cellAtPx(f.x + dx * CELL_W, f.y + dy * CELL_H);
if (!isOpenSky(i)) continue;
put(i, dx === 0 && dy === 0 ? '*' : 'o', WHITE);
}
}
}
function drawRockets() {
const tail = ['|', '|', '.', '.', '·'];
const glow = [1, 0.7, 0.5, 0.35, 0.22];
for (const k of rockets) {
for (let j = tail.length - 1; j >= 0; j--) {
const i = cellAtPx(k.x, k.y + j * CELL_H);
if (!isOpenSky(i)) continue;
put(i, tail[j], scale(j === 0 ? [255, 236, 200] : [255, 170, 90], glow[j]));
}
}
}
function drawSparks(t) {
for (const s of sparks) {
const i = cellAtPx(s.x, s.y);
if (!isOpenSky(i)) continue; // sparks never cover the tower
const k = (t - s.born) / s.life;
put(i, k < 0.3 ? '*' : k < 0.65 ? '+' : '.', scale(mix(WHITE, s.rgb, Math.min(1, k * 1.5)), 1 - k * 0.55));
}
}
// ---------- rasterize the character grid ----------
const styleCache = new Map();
function rgbStyle(r, g, b) {
r = Math.min(255, r) & 252; g = Math.min(255, g) & 252; b = Math.min(255, b) & 252;
const key = (r << 16) | (g << 8) | b;
let s = styleCache.get(key);
if (!s) { s = 'rgb(' + r + ',' + g + ',' + b + ')'; styleCache.set(key, s); }
return s;
}
function renderScene() {
sctx.fillStyle = '#000';
sctx.fillRect(0, 0, W, SCENE_H);
sctx.font = FONT;
sctx.textAlign = 'center';
sctx.textBaseline = 'middle';
let letters = 0, last = '';
for (let i = 0; i < N; i++) {
const chr = ch[i];
if (chr === ' ') continue;
const r = col[i * 3], g = col[i * 3 + 1], b = col[i * 3 + 2];
if (r + g + b < 12) continue;
const st = rgbStyle(r, g, b);
if (st !== last) { sctx.fillStyle = st; last = st; }
const c = i % COLS, row = (i / COLS) | 0;
sctx.fillText(chr, c * CELL_W + (wide[i] ? CELL_W : CELL_W / 2), row * CELL_H + 11);
letters++;
}
return letters;
}
function composite() {
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, W, EDITOR_TOP);
ctx.drawImage(scene, 0, 0);
// soft bloom: a blurred copy added on top at 40%
ctx.save();
ctx.filter = 'blur(3px)';
ctx.globalAlpha = 0.4;
ctx.globalCompositeOperation = 'lighter';
ctx.drawImage(scene, 0, 0);
ctx.restore();
ctx.fillStyle = '#000';
ctx.fillRect(0, SCENE_H, W, EDITOR_TOP - SCENE_H);
}
function drawInfo(t, letters) {
// three grey caption lines in the bottom-left corner of the scene
ctx.fillStyle = '#000';
ctx.fillRect(0, 1200, 336, SCENE_H - 1200);
ctx.font = '18px ' + MONO;
ctx.textAlign = 'left';
ctx.textBaseline = 'middle';
ctx.fillStyle = 'rgb(140,140,140)';
ctx.fillText('@ai.asap.kr', 14, 1222);
ctx.fillText('lights ' + LIGHTS[phaseAt(t)].name + ' - fireworks ' + burstCount, 14, 1247);
ctx.fillText('letters ' + letters.toLocaleString('en-US'), 14, 1272);
}
// ---------- code editor: this very file, four whole lines per second ----------
const CODE_FONT = '23px ' + MONO;
const HEADER_H = 52, LINE_H = 30;
const CODE_TOP = EDITOR_TOP + HEADER_H;
const codeLines = SOURCE.replace(/\t/g, ' ').split('\n');
while (codeLines.length && !codeLines[0].trim()) codeLines.shift();
while (codeLines.length && !codeLines[codeLines.length - 1].trim()) codeLines.pop();
const VISIBLE = Math.ceil((H - CODE_TOP) / LINE_H);
const SCENE_STARTS = [0, 4, 8, 12, 16, 20, 24];
const SCENE_FUNCS = ['lightWave', 'launchRocket', 'explode', 'scheduleFireworks', 'rainbowFlow', 'finaleBursts'];
const anchors = SCENE_FUNCS.map(name => Math.max(0, codeLines.findIndex(l => l.includes('function ' + name + '('))));
const KEYWORDS = new Set(['const', 'let', 'var', 'function', 'return', 'if', 'else', 'for', 'while', 'of', 'in',
'new', 'break', 'continue', 'true', 'false', 'null', 'typeof', 'switch', 'case', 'default', 'this']);
const SYNTAX = {
comment: 'rgb(92,99,112)', string: 'rgb(152,195,121)', number: 'rgb(209,154,102)',
keyword: 'rgb(198,120,221)', call: 'rgb(97,175,239)', builtin: 'rgb(229,192,123)',
name: 'rgb(222,225,232)', punct: 'rgb(150,158,172)', space: 'rgb(0,0,0)',
};
function tokenize(line) {
// tiny highlighter: comments, strings, numbers, keywords, calls
const out = [];
const re = /(\/\/.*$)|('(?:[^'\\]|\\.)*'?|"(?:[^"\\]|\\.)*"?|`(?:[^`\\]|\\.)*`?)|(\d+(?:\.\d+)?)|([A-Za-z_$][\w$]*)|(\s+)|([^\w\s])/g;
let m;
while ((m = re.exec(line))) {
let type;
if (m[1]) type = 'comment';
else if (m[2]) type = 'string';
else if (m[3]) type = 'number';
else if (m[4]) {
if (KEYWORDS.has(m[4])) type = 'keyword';
else if (/^\s*\(/.test(line.slice(re.lastIndex))) type = 'call';
else if (/^[A-Z]/.test(m[4])) type = 'builtin';
else type = 'name';
}
else if (m[5]) type = 'space';
else type = 'punct';
out.push({ text: m[0], color: SYNTAX[type], space: type === 'space' });
}
return out;
}
ctx.font = CODE_FONT;
const codeTokens = codeLines.map(line => tokenize(line).map(tok => Object.assign(tok, { w: ctx.measureText(tok.text).width })));
function codeCursor(t) {
// each scene jumps so its function sits on the bottom line, then keeps flowing
let k = 0;
for (let s = 1; s < SCENE_STARTS.length; s++) if (t >= SCENE_STARTS[s]) k = s;
const base = k === 0 ? VISIBLE : Math.max(VISIBLE, anchors[k - 1] + 1);
return base + (t - SCENE_STARTS[k]) * 4;
}
function drawEditor(t) {
ctx.fillStyle = '#000';
ctx.fillRect(0, EDITOR_TOP, W, H - EDITOR_TOP);
ctx.fillStyle = 'rgb(64,64,64)';
ctx.fillRect(0, EDITOR_TOP, W, 2);
// header: file name left, status right
ctx.textBaseline = 'middle';
ctx.font = '22px ' + MONO;
ctx.textAlign = 'left';
ctx.fillStyle = 'rgb(205,205,205)';
ctx.fillText('namsan_tower.js', 24, EDITOR_TOP + 27);
const status = 'running · 30 fps';
ctx.textAlign = 'right';
ctx.fillStyle = 'rgb(130,130,130)';
ctx.fillText(status, W - 24, EDITOR_TOP + 27);
const sw = ctx.measureText(status).width;
ctx.fillStyle = 'rgb(255,140,40)';
ctx.beginPath();
ctx.arc(W - 24 - sw - 16, EDITOR_TOP + 27, 6, 0, Math.PI * 2);
ctx.fill();
const cur = codeCursor(t), shown = Math.floor(cur), frac = cur - shown;
const slide = Math.pow(Math.max(0, 1 - frac / 0.5), 3) * LINE_H; // ease the new line up into place
ctx.save();
ctx.beginPath();
ctx.rect(0, CODE_TOP, W, H - CODE_TOP);
ctx.clip();
ctx.font = CODE_FONT;
const L = codeLines.length;
for (let k = 0; k <= VISIBLE + 1; k++) {
const li = (((shown - 1 - k) % L) + L) % L;
const y = H - 20 - k * LINE_H + slide;
if (y < CODE_TOP - LINE_H) break;
if (k === 0) { // light band under the line that just appeared
ctx.fillStyle = 'rgba(255,255,255,' + (0.11 * (1 - frac * 0.5)).toFixed(3) + ')';
ctx.fillRect(0, y - LINE_H / 2, W, LINE_H);
}
ctx.textAlign = 'right';
ctx.fillStyle = 'rgb(88,88,88)';
ctx.fillText(String(li + 1), 62, y);
ctx.textAlign = 'left';
let x = 82;
for (const tok of codeTokens[li]) {
if (x > W) break;
if (!tok.space) { ctx.fillStyle = tok.color; ctx.fillText(tok.text, x, y); }
x += tok.w;
}
}
ctx.restore();
}
// ---------- main loop ----------
function resetCycle(n) {
particles = []; rockets = []; flashes = []; smoke = []; sparks = []; crackles = [];
schedule = scheduleFireworks(makeRandom(7000 + n * 97));
nextEvent = 0; burstCount = 0;
for (const c of towerCells) c.flash = 0;
}
function update(t, dt) {
while (nextEvent < schedule.length && schedule[nextEvent].tLaunch <= t) launchRocket(schedule[nextEvent++]);
updateRockets(t);
updateCrackles(t);
updateParticles(t, dt);
updateDrift(t, dt);
updateTower(t, dt);
flashes = flashes.filter(f => t - f.t0 < 0.5);
}
function paint(t) {
// back to front: sky, mountain, city, structures, then effects in front
resetFrame();
drawStars(t);
skyGlow(t);
drawHalo(t);
drawHill(t);
drawCity(t);
drawMast(t);
drawTower(t);
drawSmoke(t);
drawFireworks();
flashCore();
drawRockets();
drawSparks(t);
}
// build the static world once
paintSky();
paintFarRidge();
paintHill();
buildTower();
buildMast();
buildCity();
buildHalo();
placeStars();
for (let i = 0; i < N; i++) if (kind[i] === HILL && hillTint[i] > 0) hillLit.push(i);
lamps = lamps.filter(i => kind[i] === HILL);
let cycle = -1, lastTick = 0;
const startTime = performance.now();
function frame(now) {
requestAnimationFrame(frame);
if (lastTick && now - lastTick < 1000 / FPS - 3) return; // hold 30 fps
const dt = lastTick ? Math.min(0.1, (now - lastTick) / 1000) : 1 / FPS;
lastTick = now;
const total = (now - startTime) / 1000;
const loop = Math.floor(total / LOOP), t = total - loop * LOOP;
if (loop !== cycle) { cycle = loop; resetCycle(loop); } // every 30s: start over
update(t, dt);
paint(t);
const letters = renderScene();
composite();
drawInfo(t, letters);
drawEditor(t);
}
requestAnimationFrame(frame);
})();
</script>
</body>
</html>
광고