mx / quanxue-src /src /quanxue /render.ts
Ethscriptions's picture
Step 7: 手机竖屏适配、排版与字幕优化、全片质检、最终构建与验收
d03936a verified
Raw History Blame Contribute Delete
9.48 kB
// 帧合成:镜头 → 墨晕转场 → 引文竖排 → 字幕 → 章节标识 → 暗角 → 胶片颗粒
import { TL, DURATION, keyT, getLine, shotAt, chapterAt, type Line, type Shot } from './timeline'
import { clamp, easeInOut, fbm1, ramp, smooth, type Rect } from './engine/util'
import { COL, F, background, columns, font, grain, makeCanvas, revealChar, rgba, text, vignette, type Col, type Ctx } from './engine/draw'
import { SCENES } from './scenes'
export type SceneCtx = {
c: Ctx; W: number; H: number; u: number; P: boolean
t: number; lt: number; dur: number; shot: Shot
/** 镜头内相对时间:某句某词被念出的时刻 */
k: (id: string, sub: string, off?: number) => number
ls: (id: string) => number
le: (id: string) => number
art: Rect; txt: Rect; full: Rect
}
export function layout(W: number, H: number) {
const P = H > W
const art: Rect = P ? { x: 0.05 * W, y: 0.41 * H, w: 0.9 * W, h: 0.4 * H } : { x: 0.045 * W, y: 0.1 * H, w: 0.57 * W, h: 0.73 * H }
const txt: Rect = P ? { x: 0.08 * W, y: 0.075 * H, w: 0.84 * W, h: 0.31 * H } : { x: 0.645 * W, y: 0.12 * H, w: 0.3 * W, h: 0.66 * H }
const full: Rect = P ? { x: 0.06 * W, y: 0.12 * H, w: 0.88 * W, h: 0.68 * H } : { x: 0.06 * W, y: 0.1 * H, w: 0.88 * W, h: 0.72 * H }
return { P, art, txt, full }
}
function sceneCtx(c: Ctx, W: number, H: number, t: number, shot: Shot): SceneCtx {
const L = layout(W, H)
return {
c, W, H, u: Math.min(W, H) / 1080, P: L.P, t, lt: t - shot.start, dur: shot.end - shot.start, shot,
k: (id, sub, off = 0) => keyT(id, sub, off) - shot.start,
ls: (id) => getLine(id).start - shot.start,
le: (id) => getLine(id).end - shot.start,
art: L.art, txt: L.txt, full: L.full,
}
}
// ---------------- 引文:竖排、乌丝栏、朱色句读 ----------------
const quoteCache = new Map<string, { size: number; cols: Col[]; maxLen: number }>()
function fitQuote(line: Line, r: Rect, sizeMax: number) {
const key = `${line.id}_${r.w | 0}_${r.h | 0}_${sizeMax | 0}`
let q = quoteCache.get(key)
if (q) return q
let size = sizeMax
// 最长小句(不允许被拆到两列)
const longest = Math.max(...line.text.split(/[,。;:?!、]/).map((c) => [...c].filter((ch) => !'“”‘’《》'.includes(ch)).length))
for (let it = 0; it < 40; it++) {
const max = Math.max(3, Math.floor(r.h / (size * 1.13)))
const cols = columns(line.text, max)
const maxLen = Math.max(...cols.map((c) => c.chars.length))
if ((cols.length * size * 1.62 <= r.w && max >= longest) || it === 39) { q = { size, cols, maxLen }; break }
size *= 0.95
}
quoteCache.set(key, q!)
return q!
}
export function drawQuote(S: SceneCtx, line: Line, r: Rect, opts: { sizeMax?: number; fam?: string; color?: string; alpha?: number } = {}) {
const { c, u } = S
const t = S.t
if (t < line.start - 0.4) return
const q = fitQuote(line, r, (opts.sizeMax ?? 74) * u)
const { size, cols, maxLen } = q
const pitch = size * 1.62
const totalW = cols.length * pitch
const x0 = r.x + r.w / 2 + totalW / 2 - pitch / 2
const y0 = r.y + (r.h - maxLen * size * 1.13) / 2 + size * 0.5
const dim = 1 - 0.55 * ramp(t, line.end + 0.3, 1.0)
const A = (opts.alpha ?? 1) * dim * clamp((t - line.start + 0.4) / 0.4)
// 乌丝栏
c.lineWidth = Math.max(1, u * 1.1)
for (let i = 0; i <= cols.length; i++) {
const x = x0 + pitch / 2 - i * pitch
const p = ramp(t, line.start - 0.3 + i * 0.05, 0.9)
const yTop = y0 - size * 0.75, yBot = y0 + maxLen * size * 1.13 - size * 0.25
c.strokeStyle = rgba(COL.gold, 0.22 * A)
c.beginPath(); c.moveTo(x, yTop); c.lineTo(x, yTop + (yBot - yTop) * p); c.stroke()
}
const fam = opts.fam ?? F.kai
const color = opts.color ?? COL.paper
cols.forEach((col, ci) => {
const x = x0 - ci * pitch
col.chars.forEach((ch, k) => {
const y = y0 + k * size * 1.13
const ct = line.charT[ch.i]
const rv = (t - ct + 0.06) / 0.45
revealChar(c, ch.ch, x, y, size, fam, color, clamp(rv), A)
if (ch.mark && rv > 0.4) {
// 朱笔圈点
c.strokeStyle = rgba(COL.red, 0.9 * A); c.fillStyle = rgba(COL.red, 0.9 * A); c.lineWidth = Math.max(1, size * 0.04)
c.beginPath()
if (ch.mark === 'stop') { c.arc(x + size * 0.56, y + size * 0.38, size * 0.075, 0, Math.PI * 2); c.stroke() }
else { c.arc(x + size * 0.56, y + size * 0.4, size * 0.05, 0, Math.PI * 2); c.fill() }
}
})
})
if (line.src) {
const lastX = x0 - (cols.length - 1) * pitch - pitch * 0.85
const by = y0 + maxLen * size * 1.13 - size * 0.4
c.save(); c.translate(lastX, by)
c.font = font(size * 0.34, F.song); c.textAlign = 'center'; c.textBaseline = 'bottom'
c.fillStyle = rgba(COL.gold, 0.85 * A * ramp(t, line.end - 0.5, 0.8))
let yy = 0
for (const chh of [...line.src].reverse()) { if ('《》·'.includes(chh)) continue; c.fillText(chh, 0, yy); yy -= size * 0.4 }
c.restore()
}
}
// ---------------- 字幕 ----------------
function drawSubtitle(c: Ctx, W: number, H: number, t: number) {
const P = H > W, u = Math.min(W, H) / 1080
const line = TL.lines.find((l) => l.kind === 'say' && t >= l.start - 0.2 && t <= l.end + 0.4)
if (!line) return
const chunks = P ? line.chunksP : line.chunksL
const ch = chunks.find((k) => t >= k.start && t < k.end)
if (!ch) return
const a = Math.min(clamp((t - ch.start) / 0.18), clamp((ch.end - t) / 0.18))
const y = P ? H * 0.868 : H * 0.895
const size = (P ? 46 : 42) * u
// 柔和底衬
const g = c.createLinearGradient(0, y - size * 1.6, 0, y + size * 1.6)
g.addColorStop(0, 'rgba(8,7,6,0)'); g.addColorStop(0.5, `rgba(8,7,6,${0.5 * a})`); g.addColorStop(1, 'rgba(8,7,6,0)')
c.fillStyle = g; c.fillRect(0, y - size * 1.6, W, size * 3.2)
c.save()
c.shadowColor = 'rgba(0,0,0,0.8)'; c.shadowBlur = size * 0.3
text(c, ch.text, W / 2, y, size, F.kai, COL.paper, a * 0.96, 'center', size * 0.06)
c.restore()
}
function drawHud(c: Ctx, W: number, H: number, t: number) {
const ci = chapterAt(t)
if (ci < 0) return
const ch = TL.chapters[ci]
const shot = TL.shots[shotAt(t)]
if (shot.vis === 'card') return
const a = 0.75 * Math.min(ramp(t, ch.start + 3.6, 1), 1 - ramp(t, ch.end - 0.6, 0.6))
if (a <= 0) return
const P = H > W, u = Math.min(W, H) / 1080
const x = P ? W / 2 : 72 * u, y = P ? 62 * u : 60 * u
const label = `${ch.no === '终' ? '终章' : '第' + ch.no + '章'} ${ch.title}`
c.save()
if (P) {
text(c, label, x, y, 24 * u, F.song, COL.gold, a, 'center', 6 * u)
} else {
c.fillStyle = rgba(COL.red, a); c.fillRect(x, y - 9 * u, 6 * u, 18 * u)
text(c, label, x + 20 * u, y, 24 * u, F.song, COL.gold, a, 'left', 6 * u)
text(c, '劝学 · 荀子', W - 72 * u, y, 22 * u, F.song, COL.muted, a * 0.8, 'right', 8 * u)
}
c.restore()
}
// ---------------- 镜头渲染 ----------------
function renderShot(c: Ctx, W: number, H: number, t: number, shot: Shot) {
const S = sceneCtx(c, W, H, t, shot)
const sc = SCENES[shot.vis]
c.save()
background(c, W, H)
// 缓慢推镜
const zoom = 1 + 0.035 * smooth(S.lt / Math.max(1, S.dur)) * (sc?.zoom ?? 1)
c.translate(W / 2, H / 2); c.scale(zoom, zoom); c.translate(-W / 2, -H / 2)
if (sc) sc.draw(S)
if (shot.quote && !sc?.customQuote) drawQuote(S, shot.quote, S.txt, { sizeMax: sc?.quoteSize })
c.restore()
}
let layer: HTMLCanvasElement | null = null
function inkMask(c: Ctx, W: number, H: number, p: number, seed: number) {
if (p >= 1) { c.fillRect(0, 0, W, H); return }
const R = Math.hypot(W, H) * 0.62
const centers = [[0.5 + 0.2 * Math.sin(seed * 3.1), 0.5 + 0.15 * Math.cos(seed * 1.7)], [0.18 + 0.1 * Math.sin(seed), 0.3], [0.82, 0.72 + 0.1 * Math.cos(seed)]]
centers.forEach(([x, y], i) => {
const pp = clamp((p - i * 0.12) / (1 - i * 0.12))
const r = R * easeInOut(pp) * (i === 0 ? 1.1 : 0.8)
if (r <= 0) return
for (const [s, a] of [[1.12, 0.25], [1.05, 0.5], [1, 1]] as const) {
c.globalAlpha = a
c.beginPath()
for (let k = 0; k <= 90; k++) {
const ang = (k / 90) * Math.PI * 2
const n = fbm1(Math.cos(ang) * 1.5 + Math.sin(ang) + seed * 5 + i * 9, 5)
const rr = r * s * (1 + 0.25 * n)
const px = x * W + Math.cos(ang) * rr, py = y * H + Math.sin(ang) * rr
k ? c.lineTo(px, py) : c.moveTo(px, py)
}
c.fill()
}
})
c.globalAlpha = 1
}
const TR = 0.95
export function renderFrame(c: Ctx, W: number, H: number, t: number) {
t = clamp(t, 0, DURATION)
const i = shotAt(t)
const shot = TL.shots[i]
const inTr = i > 0 && t < shot.start + TR
if (inTr) {
const prev = TL.shots[i - 1]
renderShot(c, W, H, t, prev)
if (!layer || layer.width !== W || layer.height !== H) layer = makeCanvas(W, H)
const L = layer.getContext('2d')!
L.globalCompositeOperation = 'source-over'
L.clearRect(0, 0, W, H)
renderShot(L, W, H, t, shot)
L.globalCompositeOperation = 'destination-in'
L.fillStyle = '#000'
inkMask(L, W, H, (t - shot.start) / TR, i)
L.globalCompositeOperation = 'source-over'
c.drawImage(layer, 0, 0)
} else renderShot(c, W, H, t, shot)
drawSubtitle(c, W, H, t)
drawHud(c, W, H, t)
vignette(c, W, H)
grain(c, W, H, t, 0.075)
// 首尾淡入淡出
const fade = Math.max(1 - clamp(t / 0.8), clamp((t - (DURATION - 1.6)) / 1.6))
if (fade > 0) { c.fillStyle = `rgba(0,0,0,${fade})`; c.fillRect(0, 0, W, H) }
}