Skip to content

A toolbox of film styles made in code for Claude Code: 66 ready to use, and your own. Each style is a style prompt plus a demo film made entirely in code by Claude Opus 5.5, with no video model. By Lemomo. · 一套用代码做片的风格工具箱:66 种现成风格,也能做出你自己的

10.5 KB
Raw Blame
// Ink line rendering: path + pen pressure (fast = thin, slow = thick) + slightly fuzzy edges + ink pooling where slow + old-age tremor / feibai (飞白, dry-brush breaks) + the one red section
import { vnoise, clamp, lerp } from '/core/lib.js';

export const PAPER = [244, 239, 228];
export const INK = [29, 26, 23];
export const RED = [179, 51, 43];

const STY = {
  child: { w: 2.4, a: .82 },
  teen: { w: 2.5, a: .88 },
  adult: { w: 2.9, a: .96 },
  love: { w: 2.8, a: .96 },
  old: { w: 2.7, a: .93 },
  kid: { w: 2.0, a: .76 },
};

// precompute each point's style (once on the paper it never changes)
export function prepare(P) {
  const { N, S, SEG, segs, range } = P;
  const W0 = new Float64Array(N), A0 = new Float64Array(N), RED_ = new Float64Array(N), TR = new Float64Array(N), DRY = new Float64Array(N), WOB = new Float64Array(N);
  const segOf = id => segs.findIndex(s => s.id === id);
  for (let i = 0; i < N; i++) { const st = STY[segs[SEG[i]].style] || STY.adult; W0[i] = st.w; A0[i] = st.a; }
  // styles blend smoothly across segment boundaries (about ±60 units)
  const smooth = (arr, R) => { const out = new Float64Array(N); let acc = 0, lo = 0, hi = -1; for (let i = 0; i < N; i++) { while (hi < N - 1 && S[hi + 1] <= S[i] + R) acc += arr[++hi]; while (S[lo] < S[i] - R) acc -= arr[lo++]; out[i] = acc / (hi - lo + 1); } return out; };
  const W = smooth(W0, 60), A = smooth(A0, 60);
  // red line: turns red on the way down in the first-love segment, fades back to ink on the nasolabial fold in the house segment
  const li = segOf('love'), hi_ = segOf('house');
  if (li >= 0) {
    const [a, b] = range[li];
    // red starts at the mouth corner: find the point closest to the left mouth corner
    let kc = a, best = 1e9; for (let i = a; i <= b; i++) { const d = Math.hypot(P.X[i] + 138, P.Y[i] - 212); if (d < best) { best = d; kc = i; } }
    for (let i = a; i <= b; i++) RED_[i] = clamp((S[i] - (S[kc] - 12)) / 34);
    if (hi_ >= 0) { const [c, d] = range[hi_]; for (let i = c; i <= d; i++) RED_[i] = 1 - clamp((S[i] - S[c] + 6) / 30); }
  }
  // old age: tremor + feibai gradually stronger
  const oi = segOf('old');
  if (oi >= 0) {
    const [a, b] = range[oi]; const sA = S[a], L = S[b] - sA;
    for (let i = a; i <= b; i++) { const u = (S[i] - sA) / L; TR[i] = clamp((S[i] - sA) / 90) * (0.75 + 0.6 * u) * (1 - 0.5 * clamp((u - .8) / .15)); DRY[i] = (0.12 + 0.4 * u) * (1 - 0.75 * clamp((u - .8) / .12)); }
  }
  const ki = segOf('kid');
  if (ki >= 0) { const [a, b] = range[ki]; for (let i = a; i <= b; i++) WOB[i] = clamp((S[i] - S[a]) / 40); }
  // displacement: high-frequency tremor in old age (perpendicular to the path); low-frequency wobble for the child
  const X = P.X.slice(), Y = P.Y.slice();
  for (let i = 1; i < N - 1; i++) {
    if (!TR[i] && !WOB[i]) continue;
    const dx = P.X[i + 1] - P.X[i - 1], dy = P.Y[i + 1] - P.Y[i - 1], l = Math.hypot(dx, dy) || 1, nx = -dy / l, ny = dx / l;
    const s = S[i];
    const tr = TR[i] * 1.05 * ((vnoise(s / 5.5) - .5) * 1.6 + (vnoise(s / 2.3 + 40) - .5) * .7);
    const wb = WOB[i] * 3.2 * ((vnoise(s / 26 + 90) - .5) * 2 + (vnoise(s / 9 + 13) - .5) * .8);
    X[i] += nx * (tr + wb); Y[i] += ny * (tr + wb);
  }
  P.DX = X; P.DY = Y;
  // line width: age base × pen pressure (speed) × fine noise
  const WID = new Float64Array(N);
  for (let i = 0; i < N; i++) {
    const v = P.V[i], pr = 0.66 + 0.8 * Math.exp(-v / 300);
    WID[i] = W[i] * pr * (1 + 0.16 * (vnoise(S[i] / 3.1 + 7) - .5));
  }
  // colour (mixed with the paper into an opaque colour)
  const COL = new Array(N);
  for (let i = 0; i < N; i++) {
    const base = [lerp(INK[0], RED[0], RED_[i]), lerp(INK[1], RED[1], RED_[i]), lerp(INK[2], RED[2], RED_[i])];
    const a = A[i]; COL[i] = [lerp(PAPER[0], base[0], a), lerp(PAPER[1], base[1], a), lerp(PAPER[2], base[2], a)];
  }
  Object.assign(P, { WID, COL, RED: RED_, DRY, A });
  // ink blot where the pen stops (loss)
  P.blots = [];
  segs.forEach((sg, si) => (sg.holds || []).forEach(h => {
    const [a, b] = range[si]; const target = S[a] + h.f * (S[b] - S[a]); let k = a; while (k < b && S[k] < target) k++;
    P.blots.push({ k, t0: h.t, dur: h.dur, r: 9.5 });
  }));
  return P;
}

const rgb = c => `rgb(${c[0] | 0},${c[1] | 0},${c[2] | 0})`;

// draw the line over [0, head] on ctx. M = world → screen transform function, k = pixels per unit
export function drawLine(ctx0, P, head, M, k, opts = {}) {
  let ctx = ctx0;
  const n = Math.min(P.N - 1, Math.floor(head)); if (n < 1) return;
  const X = P.DX, Y = P.DY, WID = P.WID, COL = P.COL, DRY = P.DRY;
  const f = head - n;
  const kw = Math.max(k, 1.3);   // zoom used for line width: the line stops getting thinner when zoomed out
  // screen coordinates
  const sx = new Float32Array(n + 2), sy = new Float32Array(n + 2);
  for (let i = 0; i <= n; i++) { const p = M(X[i], Y[i]); sx[i] = p[0]; sy[i] = p[1]; }
  let m = n;
  if (f > 0 && n + 1 < P.N) { const p = M(lerp(X[n], X[n + 1], f), lerp(Y[n], Y[n + 1], f)); sx[n + 1] = p[0]; sy[n + 1] = p[1]; m = n + 1; }
  const idx = i => Math.min(i, n);
  // normals
  const nx = new Float32Array(m + 1), ny = new Float32Array(m + 1);
  for (let i = 0; i <= m; i++) {
    const a = Math.max(0, i - 1), b = Math.min(m, i + 1); let dx = sx[b] - sx[a], dy = sy[b] - sy[a]; const l = Math.hypot(dx, dy) || 1; nx[i] = -dy / l; ny[i] = dx / l;
  }
  // off-screen culling: only draw point runs near the visible area
  const Wd = ctx.canvas.width, Ht = ctx.canvas.height, mg = 60;
  const vis = new Uint8Array(m + 1);
  for (let i = 0; i <= m; i++) vis[i] = (sx[i] > -mg && sx[i] < Wd + mg && sy[i] > -mg && sy[i] < Ht + mg) ? 1 : 0;
  // line narrows slightly at the head, at the pen tip (the pen has just arrived)
  const wAt = (i, scale) => {
    let w = WID[idx(i)] * kw * scale;
    const back = (m - i) * 0.6; if (back < 3) w *= 0.75 + 0.25 * back / 3;
    return Math.max(w, 0.6);
  };
  // split into runs by colour / visibility / feibai ink breaks, each run filled as one polygon
  function runs(pass) {
    const out = []; let cur = null;
    for (let i = 0; i <= m; i++) {
      const ii = idx(i);
      let on = vis[i] || (i > 0 && vis[i - 1]) || (i < m && vis[i + 1]);
      if (on && DRY[ii] > 0 && pass.strand == null) {
        // old-age ink breaks: long-scale noise decides the break points
        const g = vnoise(P.S[ii] / 23 + 3.7) * .7 + vnoise(P.S[ii] / 7 + 9) * .3;
        if (g < DRY[ii] * 0.36) on = false;
      }
      if (on && pass.strand != null) {
        const g = vnoise(P.S[ii] / (9 + pass.strand * 3) + pass.strand * 17.3);
        if (g < 0.2 + DRY[ii] * 0.35) on = false;
      }
      const ck = COL[ii]; const key = ((ck[0] / 6) | 0) * 10000 + ((ck[1] / 6) | 0) * 100 + ((ck[2] / 6) | 0);
      if (!on) { if (cur) { out.push(cur); cur = null; } continue; }
      if (!cur) cur = { a: i, b: i, key };
      else if (key !== cur.key) { cur.b = i; out.push(cur); cur = { a: i, b: i, key }; }
      else cur.b = i;
    }
    if (cur) out.push(cur);
    return out;
  }
  function fillRun(r, scale, offset, colorFn) {
    if (r.b <= r.a) return;
    ctx.beginPath();
    for (let i = r.a; i <= r.b; i++) { const w = wAt(i, scale) / 2, o = offset ? offset * wAt(i, 1) : 0; ctx.lineTo(sx[i] + nx[i] * (w + o), sy[i] + ny[i] * (w + o)); }
    for (let i = r.b; i >= r.a; i--) { const w = wAt(i, scale) / 2, o = offset ? offset * wAt(i, 1) : 0; ctx.lineTo(sx[i] - nx[i] * (w - o), sy[i] - ny[i] * (w - o)); }
    ctx.closePath(); ctx.fillStyle = colorFn(COL[idx(r.a)]); ctx.fill();
    // round cap
    for (const i of [r.a, r.b]) { const w = wAt(i, scale) / 2; ctx.beginPath(); ctx.arc(sx[i] + nx[i] * (offset ? offset * wAt(i, 1) : 0), sy[i] + ny[i] * (offset ? offset * wAt(i, 1) : 0), w, 0, Math.PI * 2); ctx.fill(); }
  }
  const core = c => rgb(c);
  // 1 bleed edge (ink soaking into the paper fibres): draw a widened line off-screen, blur it as a whole and composite back at low opacity
  const R0 = runs({});
  if (!drawLine.halo) { drawLine.halo = document.createElement('canvas'); drawLine.halo.width = Wd; drawLine.halo.height = Ht; }
  const hc = drawLine.halo, hx = hc.getContext('2d'); hx.setTransform(1, 0, 0, 1, 0, 0); hx.clearRect(0, 0, Wd, Ht);
  const main = ctx; ctx = hx;
  for (const r of R0) fillRun(r, 1.0 + 1.6 / Math.max(1, WID[idx(r.a)]), 0, core);
  ctx = main;
  ctx.save(); ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.globalAlpha = .22; ctx.filter = `blur(${Math.max(.6, k * .9).toFixed(2)}px)`; ctx.drawImage(hc, 0, 0); ctx.restore();
  // 2 main body
  for (const r of R0) fillRun(r, 1.0, 0, core);
  // 3 old-age feibai: a few bristle tracks running through the ink breaks
  if (P.DRY[idx(m)] > 0 || opts.allDry) for (let s = 0; s < 3; s++) {
    const R = runs({ strand: s }).filter(r => DRY[idx(r.a)] > 0);
    const off = [-0.32, 0.05, 0.36][s];
    for (const r of R) fillRun(r, 0.28, off, core);
  }
  // ink pooling where slow: add round ink dots at very low-speed points
  ctx.fillStyle = rgb(INK);
  for (let i = 0; i <= n; i += 2) {
    if (!vis[i]) continue; const v = P.V[i]; if (v > 45) continue;
    const w = WID[i] * kw * (0.62 + 0.3 * (1 - v / 45)); ctx.fillStyle = core(COL[i]); ctx.beginPath(); ctx.arc(sx[i], sy[i], w, 0, Math.PI * 2); ctx.fill();
  }
  return { tip: [sx[m], sy[m]], dir: [sx[m] - sx[Math.max(0, m - 6)], sy[m] - sy[Math.max(0, m - 6)]] };
}

// loss: the ink blot at the pen stop slowly spreads (teardrop shape: point upward touching the stop point, round belly sagging down; with a dark tide line and an irregular edge)
export function drawBlots(ctx, P, t, M, k) {
  for (const b of P.blots) {
    if (t < b.t0) continue;
    const u = clamp((t - b.t0) / b.dur), g = 1 - Math.pow(1 - u, 2.2);
    const R = (P.WID[b.k] * 0.7 + b.r * g) * k, c0 = M(P.DX[b.k], P.DY[b.k]);
    const drop = g * 0.9;                       // the more it spreads, the further it sags
    const c = [c0[0], c0[1] + R * (0.25 + drop)];
    const pts = []; const n = 90;
    for (let i = 0; i < n; i++) {
      const a = i / n * Math.PI * 2; let x = Math.cos(a), y = Math.sin(a);
      const up = Math.pow(Math.max(0, -y), 1.6) * drop;       // pull the upper half into a point
      x *= 1 - 0.72 * up; y *= 1 + 0.95 * up;
      const rr = R * (0.9 + 0.16 * vnoise(i * 0.37 + 5) + 0.08 * vnoise(i * 1.3 + 20) * g);
      pts.push([c[0] + x * rr, c[1] + y * rr]);
    }
    ctx.beginPath(); pts.forEach(p => ctx.lineTo(p[0], p[1])); ctx.closePath();
    const gr = ctx.createRadialGradient(c[0], c[1] + R * .2, 0, c[0], c[1], R * 1.25);
    gr.addColorStop(0, 'rgba(29,26,23,0.82)'); gr.addColorStop(0.7, 'rgba(29,26,23,0.66)'); gr.addColorStop(0.9, 'rgba(22,19,17,0.95)'); gr.addColorStop(1, 'rgba(22,19,17,0.7)');
    ctx.fillStyle = gr; ctx.fill();
  }
}