Lab · 2026-11-20

글자로 빚은 달항아리

달항아리를 46조각으로 썰어 고리마다 '건강하세요', '행복하세요' 같은 소원을 한 글자씩 두르고, 천천히 돌다가 아래부터 금빛이 차오르는 애니메이션이에요. 바닥 달빛과 별까지 전부 글자로 그렸어요. 영상은 @ai.asap.kr 릴스에 있어요.

글자로 빚은 달항아리 릴스 표지
HTML 파일 받기브라우저에서 바로 보기

HTML 파일 한 개, 319줄.

광고

코드

클로드가 짠 코드를 다듬어 녹화했어요.

광고
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>moon_jar.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>
(() => {
// moon_jar.js : a Korean moon jar built from wishes, one slice at a time
const SOURCE = document.currentScript.text.replace(/^\n+/, '').trimEnd().split('\n');

// ---- layout ----
const W = 1080, H = 1920, SCENE_H = 1290, CODE_TOP = 1300;
const LOOP = 30, CODE_ROWS = 18, LINE_H = 30;
const CX = 540, BASE_Y = 1130, JAR_H = 860, ROWS = 46;   // jar sits on an invisible floor
const PITCH = 0.3, FOCAL = 2600;                           // camera a little above, looking down
const MOON = [236, 230, 214], SHADE = [118, 128, 156], WISH = [255, 204, 128];
const WISHES = '건강하세요 · 행복하세요 · 꿈을 이루세요 · 사랑이 가득하길 · ' +
  '하는 일 다 잘되길 · 따뜻한 겨울 되세요 · 새해 복 많이 받으세요 · ';
const LETTERS = Array.from(WISHES);

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 smooth = p => p * p * (3 - 2 * p);
const hash = n => { const s = Math.sin(n * 127.1 + 311.7) * 43758.5453; return s - Math.floor(s); };
const mixRGB = (a, b, t) => a.map((v, i) => lerp(v, b[i], t));

const view = document.getElementById('view');
const out = view.getContext('2d');
const letters = document.createElement('canvas');
letters.width = W; letters.height = SCENE_H;
const L = letters.getContext('2d');

// ---- the side outline: a few points along the jar's edge, the rule does the rest ----
// (height from foot 0 to rim 1, radius in jar widths). Moon jars bulge high and sit on a small foot.
const OUTLINE = [[0, 0.19], [0.025, 0.205], [0.09, 0.28], [0.2, 0.38], [0.34, 0.455],
  [0.5, 0.475], [0.64, 0.455], [0.77, 0.39], [0.87, 0.295], [0.925, 0.225], [0.955, 0.19], [0.975, 0.185], [1, 0.205]];
function radiusAt(v) {
  // Catmull-Rom through the outline points: a smooth curve, never a corner
  let i = 0;
  while (i < OUTLINE.length - 2 && OUTLINE[i + 1][0] < v) i++;
  const p0 = OUTLINE[Math.max(0, i - 1)], p1 = OUTLINE[i], p2 = OUTLINE[i + 1], p3 = OUTLINE[Math.min(OUTLINE.length - 1, i + 2)];
  const t = clamp((v - p1[0]) / (p2[0] - p1[0]));
  const t2 = t * t, t3 = t2 * t;
  return 0.5 * (2 * p1[1] + (-p0[1] + p2[1]) * t + (2 * p0[1] - 5 * p1[1] + 4 * p2[1] - p3[1]) * t2 +
    (-p0[1] + 3 * p1[1] - 3 * p2[1] + p3[1]) * t3);
}

// ---- slice the jar like bread: one ring per row, letters placed around each ring ----
const JAR_W = JAR_H * 0.96;
const RINGS = [];
function buildRings() {
  for (let i = 0; i < ROWS; i++) {
    const v = (i + 0.5) / ROWS;
    const r = radiusAt(v) * JAR_W;
    const slope = (radiusAt(v + 0.004) - radiusAt(v - 0.004)) * JAR_W / (0.008 * JAR_H);
    const size = JAR_H / ROWS * 0.92;
    const count = Math.max(8, Math.floor(2 * Math.PI * r / (size * 1.02)));
    const start = (i * 11) % LETTERS.length;
    const glyphs = [];
    for (let k = 0; k < count; k++) glyphs.push(LETTERS[(start + k) % LETTERS.length]);
    RINGS.push({ v, r, slope, size, count, glyphs });
  }
}

// ---- timing ----
// one full turn that starts slow, speeds up and settles facing front again
const turn = T => Math.PI * 2 * ease(clamp((T - 1.5) / 25));
const bob = T => Math.sin(T / LOOP * Math.PI * 4) * 7;
const builtUpTo = T => clamp((T - 0.3) / 3.4);          // rings appear foot to rim
const wishFill = T => smooth(clamp((T - 15) / 7));        // warm wish light rises 15s..22s
const sweepAngle = T => lerp(-0.85, 0.85, ease(clamp((T - 5) / 9)));   // the glaze glint glides across

// ---- light: dots facing the light get brighter, so the jar looks round ----
const LIGHT = norm([-0.55, 0.6, 0.6]);
function norm(a) { const l = Math.hypot(...a); return a.map(x => x / l); }
const VIEW = [0, Math.sin(PITCH), Math.cos(PITCH)];

function project(x, y, z) {
  // tilt the world down by PITCH, then perspective
  const yy = y * Math.cos(PITCH) - z * Math.sin(PITCH);
  const zz = y * Math.sin(PITCH) + z * Math.cos(PITCH);
  const s = FOCAL / (FOCAL - zz);
  return [CX + x * s, s, yy, zz];
}

function drawJar(T) {
  L.clearRect(0, 0, W, SCENE_H);
  const rot = turn(T), built = builtUpTo(T), fill = wishFill(T), lift = bob(T);
  const sweep = sweepAngle(T);
  const glint = norm([Math.sin(sweep), 0.25, Math.cos(sweep)]);
  const half = [(LIGHT[0] + VIEW[0]) / 2, (LIGHT[1] + VIEW[1]) / 2, (LIGHT[2] + VIEW[2]) / 2];
  const H1 = norm(half);
  const items = [];
  let count = 0;
  for (let i = 0; i < ROWS; i++) {
    const ring = RINGS[i];
    // build: each ring threads itself around, the next one a beat later (head to tail)
    const grow = smooth(clamp(built * ROWS - i * 0.82, 0, 1.4) / 1.4);
    if (grow <= 0) continue;
    const y = (ring.v - 0.5) * JAR_H;
    const frontLine = fill * 1.08 - 0.04;             // the wish light's top edge
    for (let k = 0; k < ring.count; k++) {
      const frac = k / ring.count;
      if (frac > grow) break;
      const g = ring.glyphs[k];
      if (g === ' ') continue;
      const th = frac * Math.PI * 2 + rot;
      const sx = Math.sin(th), cz = Math.cos(th);
      const x = ring.r * sx, z = ring.r * cz;
      const n = norm([sx, -ring.slope, cz]);
      const facing = n[0] * VIEW[0] + n[1] * VIEW[1] + n[2] * VIEW[2];
      if (facing < 0.04) continue;                     // porcelain is opaque: the back hides
      const diff = Math.max(0, n[0] * LIGHT[0] + n[1] * LIGHT[1] + n[2] * LIGHT[2]);
      const spec = Math.max(0, n[0] * H1[0] + n[1] * H1[1] + n[2] * H1[2]) ** 46;
      const gl = Math.max(0, n[0] * glint[0] + n[1] * glint[1] + n[2] * glint[2]) ** 28 * 0.62 * clamp((facing - 0.35) * 3);   // glint stays on the front, never a loose edge
      const b = clamp(0.06 + 0.6 * diff ** 1.3 + 0.5 * spec + 0.9 * gl);
      const [px, s, yy, zz] = project(x, y, z);
      items.push({ g, px, py: BASE_Y - JAR_H / 2 - yy * s + lift, s, zz, facing, b, v: ring.v,
        size: ring.size, edge: frontLine - ring.v, i, k });
    }
  }
  items.sort((a, b) => a.zz - b.zz);                     // far letters first, near ones cover them
  L.textAlign = 'center'; L.textBaseline = 'middle';
  for (const it of items) {
    // warm wish light: below the line the letters glow gold, the edge itself shimmers
    const inWish = clamp(it.edge * 14 + 0.5);
    const atEdge = Math.exp(-((it.edge * 26) ** 2)) * (fill > 0 && fill < 1 ? 1 : 0);
    const twinkle = 0.85 + 0.15 * Math.sin(T * 5 + it.i * 1.7 + it.k * 0.9);
    let rgb = mixRGB(SHADE, MOON, clamp(it.b * 1.25));
    // wish light keeps the shading: lit side gold, shadow side deep amber
    rgb = mixRGB(rgb, mixRGB([120, 82, 52], WISH, clamp(it.b * 1.3 + 0.1)), inWish * 0.9);
    rgb = mixRGB(rgb, [255, 250, 240], Math.max(clamp((it.b - 0.86) * 5), atEdge * 0.8));
    const alpha = clamp((0.22 + it.b * 0.95) * (0.45 + 0.55 * it.facing) * (inWish ? twinkle : 1) + atEdge * 0.5);
    const dark = it.b < 0.2 && inWish < 0.5;
    const size = it.size * it.s;
    L.setTransform(Math.max(0.18, it.facing ** 0.75) * it.s, 0, 0, it.s, it.px, it.py);
    L.fillStyle = 'rgba(' + rgb.map(Math.round).join(',') + ',' + alpha.toFixed(3) + ')';
    if (dark) {                                         // the shadow side keeps only quiet dots
      L.font = '700 ' + (it.size * 0.8).toFixed(1) + 'px Menlo, monospace';
      L.fillText(it.b < 0.12 ? '.' : ':', 0, 0);
    } else {
      L.font = (it.b > 0.7 || inWish > 0.5 ? '800 ' : '500 ') + it.size.toFixed(1) +
        'px "Apple SD Gothic Neo", "Noto Sans KR", sans-serif';
      L.fillText(it.g, 0, 0);
    }
    count++;
  }
  L.setTransform(1, 0, 0, 1, 0, 0);
  count += drawRim(T, rot, built, lift);
  return count;
}

// ---- the mouth: seen from a little above, the rim is an ellipse of dots; its far half shows too ----
function drawRim(T, rot, built, lift) {
  const show = smooth(clamp(built * 1.15 - 1));
  if (show <= 0 && built < 1) return 0;
  const r = radiusAt(1) * JAR_W, y = 0.5 * JAR_H, n = 64;
  L.textAlign = 'center'; L.textBaseline = 'middle';
  L.font = '800 15px Menlo, monospace';
  let c = 0;
  for (let k = 0; k < n; k++) {
    const th = k / n * Math.PI * 2 + rot;
    const [px, s, yy, zz] = project(r * Math.sin(th), y, r * Math.cos(th));
    const front = Math.cos(th) > 0;
    const a = (front ? 0.9 : 0.4) * Math.max(show, built >= 1 ? 1 : 0);
    L.fillStyle = 'rgba(240,235,222,' + a.toFixed(3) + ')';
    L.fillText(front ? 'o' : '.', px, BASE_Y - JAR_H / 2 - yy * s + lift);
    c++;
  }
  return c;
}

// ---- the floor: a pool of moonlight printed as letters, brighter near the foot ----
const POOL = ' .\u00b7:-=+';
function drawFloor(T) {
  const fill = wishFill(T);
  const warm = mixRGB([200, 206, 228], WISH, fill);
  out.font = '13px Menlo, monospace'; out.textAlign = 'center'; out.textBaseline = 'middle';
  const cy = BASE_Y + 18;
  for (let row = -5; row <= 6; row++) {
    for (let col = -40; col <= 40; col++) {
      const x = CX + col * 9, y = cy + row * 9;
      const d = Math.hypot(col / 38, row / 5.6);              // ellipse, flat like a floor
      if (d > 1) continue;
      const b = (1 - d) ** 1.6 * (0.55 + 0.35 * fill) * (0.8 + 0.2 * hash(col * 31 + row * 7 + Math.floor(T * 2)));
      const ch = POOL[Math.min(POOL.length - 1, Math.floor(b * POOL.length * 1.3))];
      if (ch === ' ') continue;
      out.fillStyle = 'rgba(' + warm.map(Math.round).join(',') + ',' + (0.12 + 0.5 * b).toFixed(3) + ')';
      out.fillText(ch, x, y);
    }
  }
}

// ---- night sky: deep navy, faint star dust ----
const STARS = Array.from({ length: 110 }, (_, j) => [hash(j + 50) * W, hash(j + 90) * 1180, hash(j + 130)]);
function drawSky(T) {
  out.fillStyle = '#05070d'; out.fillRect(0, 0, W, SCENE_H);   // empty cells only: everything seen is a letter
  out.font = '15px Menlo, monospace'; out.textAlign = 'center'; out.textBaseline = 'middle';
  for (const [x, y, k] of STARS) {
    const tw = 0.5 + 0.5 * Math.sin(T * (0.6 + k) + k * 40);
    out.fillStyle = 'rgba(200,210,235,' + (0.05 + 0.13 * tw * k).toFixed(3) + ')';
    out.fillText(k > 0.93 ? '+' : '.', x, y);
  }
}

// ---- info line and live code editor ----
const phase = T => T < 3.8 ? 'slicing' : T < 15 ? 'turning' : T < 22 ? 'wishes rising' : 'resting';
function drawInfo(T, 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('slices ' + ROWS + '   ' + phase(T) + '   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);
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.font = '14px Menlo, monospace'; out.textBaseline = 'middle'; out.textAlign = 'left';
  out.fillStyle = '#2b2e35'; out.fillText('\u2500'.repeat(130), 0, CODE_TOP + 1);   // divider made of box-drawing letters
  out.font = '21px Menlo, Consolas, monospace'; out.textBaseline = 'middle';
  out.textAlign = 'left'; out.fillStyle = '#c8ccd4';
  out.fillText('moon_jar.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.font = '16px Menlo, monospace'; out.textAlign = 'center'; out.fillText('\u25cf', W - 46 - status, CODE_TOP + 34);
  // 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)).toFixed(3) + ')';
      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();
}

// a short line at the very end, typed out letter by letter
function drawMessage(t) {
  const msg = '소 원 을   담 아', st = 23.6;
  if (t < st) return;
  const chars = Array.from(msg);
  const n = Math.min(chars.length, Math.floor((t - st) / 0.16) + 1);
  const fade = t > 29.4 ? Math.max(0, 1 - (t - 29.4) / 0.6) : 1;
  out.save();
  out.font = '700 34px "Apple SD Gothic Neo", "Noto Sans KR", sans-serif';
  out.textAlign = 'center'; out.textBaseline = 'top';
  out.fillStyle = 'rgba(255,220,160,' + fade.toFixed(2) + ')';
  out.fillText(chars.slice(0, n).join(''), W / 2, 70);
  out.restore();
}

// ---- main loop ----
let started = performance.now(), lastPaint = 0;
function frame(now) {
  requestAnimationFrame(frame);
  if (now - lastPaint < 31) return;   // about 30 fps
  lastPaint = now;
  const T = ((now - started) / 1000) % LOOP;
  drawSky(T);
  drawFloor(T);
  const count = drawJar(T);
  // the jar fades in at the start and out at the very end so the loop joins up
  const fadeIn = smooth(clamp(T / 0.6)), fadeOut = 1 - smooth(clamp((T - 29.2) / 0.8));
  out.save(); out.globalAlpha = fadeIn * fadeOut; out.drawImage(letters, 0, 0);
  out.restore();
  drawInfo(T, count);
  drawEditor(T);
}
buildRings();
requestAnimationFrame(frame);
})();
</script>
</body>
</html>
광고