mx / dengtaishan-src /src /video /draw.ts
Ethscriptions's picture
登泰山记:渲染引擎、九个篇章场景、程序化配乐音效、MP4 导出、发布文案与封面(初版)
6fff8e3 verified
Raw History Blame Contribute Delete
19.9 kB
// 通用绘制:文字(逐字显现/竖排)、引文、字幕、篇章卡、取景框 HUD、印章
import { Assets, glow } from './assets'
import { F, font } from './fonts'
import { Ctx, clamp, easeOut, easeOutBack, inv, smooth, rgba, mulberry32 } from './util'
import { LINE, Line, Scene } from './timeline'
export interface RC {
g: Ctx
W: number
H: number
u: number
P: boolean // 竖屏
A: Assets
t: number
}
export const C = {
ink: '#16161a',
inkSoft: '#2b2b30',
paper: '#ece5d5',
paperHi: '#f6f1e6',
gold: '#c9a86a',
goldHi: '#e7cf98',
red: '#b3261e',
cinnabar: '#d23a22',
snow: '#eef1f5',
night: '#0b0d12',
mist: '#d9dde3',
}
const PUNCT = ',。;:、?!—…“”《》'
/** 版面调试:记录每帧文字包围盒(仅检查工具使用) */
export const DEBUG: { on: boolean; boxes: { s: string; x0: number; y0: number; x1: number; y1: number; a: number }[] } = { on: false, boxes: [] }
function rec(g: Ctx, s: string, x0: number, yc: number, w: number, size: number, a: number) {
const aa = a * g.globalAlpha
if (!DEBUG.on || aa < 0.35) return
const m = g.getTransform()
const p0 = m.transformPoint(new DOMPoint(x0, yc - size * 0.5))
const p1 = m.transformPoint(new DOMPoint(x0 + w, yc + size * 0.5))
DEBUG.boxes.push({ s, x0: Math.min(p0.x, p1.x), y0: Math.min(p0.y, p1.y), x1: Math.max(p0.x, p1.x), y1: Math.max(p0.y, p1.y), a: aa })
}
export const isPunct = (c: string) => PUNCT.includes(c)
/** 带字距的单行文字 */
export function text(
g: Ctx, s: string, x: number, y: number, size: number,
opt: { fam?: keyof typeof F; color?: string; align?: 'left' | 'center' | 'right'; alpha?: number; ls?: number; base?: CanvasTextBaseline } = {},
) {
const a = opt.alpha ?? 1
if (a <= 0.003) return 0
g.save()
g.globalAlpha *= a
g.font = font(size, opt.fam ?? 'serif')
g.fillStyle = opt.color ?? '#fff'
g.textBaseline = opt.base ?? 'middle'
const ls = (opt.ls ?? 0) * size
if (!ls) {
g.textAlign = opt.align ?? 'left'
g.fillText(s, x, y)
const w = g.measureText(s).width
rec(g, s, opt.align === 'center' ? x - w / 2 : opt.align === 'right' ? x - w : x, opt.base && opt.base !== 'middle' ? y - size * 0.4 : y, w, size, 1)
g.restore()
return w
}
const chars = [...s]
const ws = chars.map((c) => g.measureText(c).width)
const total = ws.reduce((p, v) => p + v, 0) + ls * (chars.length - 1)
let cx = opt.align === 'center' ? x - total / 2 : opt.align === 'right' ? x - total : x
rec(g, s, cx, y, total, size, 1)
g.textAlign = 'left'
chars.forEach((c, i) => {
g.fillText(c, cx, y)
cx += ws[i] + ls
})
g.restore()
return total
}
export function measure(g: Ctx, s: string, size: number, fam: keyof typeof F = 'serif', ls = 0) {
g.font = font(size, fam)
return g.measureText(s).width + ls * size * Math.max(0, [...s].length - 1)
}
/** 逐字显现(横排),p: 0~1 显现进度 */
export function revealText(
g: Ctx, s: string, x: number, y: number, size: number, p: number,
opt: { fam?: keyof typeof F; color?: string; align?: 'left' | 'center' | 'right'; alpha?: number; ls?: number; rise?: number; hl?: Record<number, string> } = {},
) {
const a0 = opt.alpha ?? 1
if (a0 <= 0.003) return
g.save()
g.font = font(size, opt.fam ?? 'serif')
g.textBaseline = 'middle'
g.textAlign = 'left'
const chars = [...s]
const ls = (opt.ls ?? 0) * size
const ws = chars.map((c) => g.measureText(c).width)
const total = ws.reduce((q, v) => q + v, 0) + ls * (chars.length - 1)
let cx = opt.align === 'center' ? x - total / 2 : opt.align === 'right' ? x - total : x
rec(g, s, cx, y, total, size, a0 * (p > 0.5 ? 1 : 0))
const n = chars.length
chars.forEach((c, i) => {
const k = smooth(clamp(p * (n + 3) - i, 0, 3) / 3)
if (k > 0) {
g.globalAlpha = a0 * k
g.fillStyle = opt.hl?.[i] ?? opt.color ?? '#fff'
g.fillText(c, cx, y + (1 - k) * (opt.rise ?? 0.25) * size)
}
cx += ws[i] + ls
})
g.restore()
}
/** 把引文按标点切成短句 */
export function phrases(s: string) {
const out: string[] = []
let cur = ''
for (const ch of s) {
cur += ch
if (',。;:?!'.includes(ch)) {
out.push(cur)
cur = ''
}
}
if (cur) out.push(cur)
return out
}
export interface QuoteOpt {
mode: 'v' | 'h'
color: string
fam?: keyof typeof F
maxSize?: number
hl?: string[] // 需要高亮的词
hlColor?: string
maxPerLine?: number
align?: 'center' | 'left'
glowColor?: string
}
/** 原文引文:逐字落墨。box: [x, y, w, h],tt: 当前时间,line: 对应朗读句 */
export function drawQuote(rc: RC, s: string, box: [number, number, number, number], line: Line, alpha: number, o: QuoteOpt) {
if (alpha <= 0.003) return
const { g } = rc
const [bx, by, bw, bh] = box
const chars = [...s]
// 每个字的显现时间(标点占更多时长)
const weights = chars.map((c) => (isPunct(c) ? 1.6 : 1))
const totalW = weights.reduce((a, b) => a + b, 0)
const times: number[] = []
let acc = 0
for (const w of weights) {
times.push(line.start - 0.05 + (acc / totalW) * line.dur * 0.94)
acc += w
}
// 高亮字下标
const hlIdx = new Set<number>()
for (const h of o.hl ?? []) {
let from = 0
for (;;) {
const i = s.indexOf(h, from)
if (i < 0) break
for (let k = 0; k < h.length; k++) hlIdx.add(i + k)
from = i + h.length
}
}
// 分行/分列
let rows: number[][] = []
const ph = phrases(s)
let idx = 0
const maxPer = o.maxPerLine ?? (o.mode === 'v' ? 9 : 11)
const phIdx = ph.map((p) => {
const arr = [...p].map(() => idx++)
return arr
})
if (o.mode === 'v') {
for (const arr of phIdx) {
for (let k = 0; k < arr.length; k += maxPer) rows.push(arr.slice(k, k + maxPer))
}
} else {
let cur: number[] = []
for (const arr of phIdx) {
if (cur.length && cur.length + arr.length > maxPer) {
rows.push(cur)
cur = []
}
if (arr.length > maxPer) {
for (let k = 0; k < arr.length; k += maxPer) rows.push(arr.slice(k, k + maxPer))
} else cur = cur.concat(arr)
}
if (cur.length) rows.push(cur)
}
rows = rows.filter((r) => r.length)
const cellCount = (r: number[]) => r.reduce((a, i) => a + (isPunct(chars[i]) ? 0.45 : 1), 0)
const longest = Math.max(...rows.map(cellCount))
let size: number
const gap = 1.55
if (o.mode === 'v') size = Math.min(bh / (longest + 0.3), bw / (rows.length * gap), o.maxSize ?? 999)
else size = Math.min(bw / (longest + 0.3), bh / (rows.length * gap), o.maxSize ?? 999)
const fam = o.fam ?? 'kai'
g.save()
g.font = font(size, fam)
g.textAlign = 'center'
g.textBaseline = 'middle'
const t = rc.t
const drawChar = (i: number, x: number, y: number, small: boolean) => {
const k = smooth(inv(times[i], times[i] + 0.38, t))
if (k <= 0) return
const c = chars[i]
const col = hlIdx.has(i) ? o.hlColor ?? C.cinnabar : o.color
if (!small && o.glowColor && k < 1) glow(g, x, y, size * 0.9, o.glowColor, (1 - k) * 0.35 * alpha)
g.globalAlpha = alpha * k
rec(g, '「' + c, x - size * (small ? 0.35 : 0.5), y, size * (small ? 0.7 : 1), size * (small ? 0.7 : 1), 1)
g.fillStyle = col
const sc = 1 + (1 - easeOut(k)) * 0.18
if (small) {
g.font = font(size * 0.7, fam)
g.fillText(c, x, y)
g.font = font(size, fam)
} else if (sc !== 1) {
g.save()
g.translate(x, y)
g.scale(sc, sc)
g.fillText(c, 0, 0)
g.restore()
} else g.fillText(c, x, y)
}
if (o.mode === 'v') {
const colW = size * gap
const totalWd = rows.length * colW
let x = bx + bw / 2 + totalWd / 2 - colW / 2
for (const r of rows) {
let y = by + (bh - longest * size) / 2 + size / 2
for (const i of r) {
if (isPunct(chars[i])) {
drawChar(i, x + size * 0.42, y - size * 0.62, true)
y += size * 0.45
} else {
drawChar(i, x, y, false)
y += size
}
}
x -= colW
}
} else {
const rowH = size * gap
let y = by + bh / 2 - (rows.length * rowH) / 2 + rowH / 2
for (const r of rows) {
const wRow = cellCount(r) * size
let x = o.align === 'left' ? bx + size / 2 : bx + bw / 2 - wRow / 2 + size / 2
for (const i of r) {
if (isPunct(chars[i])) {
drawChar(i, x - size * 0.22, y + size * 0.1, true)
x += size * 0.45
} else {
drawChar(i, x, y, false)
x += size
}
}
y += rowH
}
}
g.restore()
}
/** 旁白字幕:自动切分为短句并按字数分配时长 */
function chunks(s: string, max: number) {
const parts: string[] = []
let cur = ''
for (const ch of s) {
cur += ch
if (',。;:?!—'.includes(ch) && cur.length >= 4) {
parts.push(cur)
cur = ''
}
}
if (cur) parts.push(cur)
// 合并过短、拆分过长
const out: string[] = []
for (const p of parts) {
const last = out[out.length - 1]
if (last && last.length + p.length <= max * 0.75 && !/[。?!]$/.test(last)) out[out.length - 1] = last + p
else if (p.length > max) {
const n = Math.ceil(p.length / max)
const L = Math.ceil(p.length / n)
for (let k = 0; k < p.length; k += L) out.push(p.slice(k, k + L))
} else out.push(p)
}
return out.map((x) => x.replace(/^——/, '').trim()).filter(Boolean)
}
const chunkCache = new Map<string, string[]>()
export function drawSubtitle(rc: RC, line: Line | null, tone: 'dark' | 'light') {
if (!line || line.k !== 'n') return
const { g, W, H, u, P, t } = rc
if (t < line.start - 0.1 || t > line.end + 0.25) return
const max = P ? 15 : 24
const key = line.id + max
let cs = chunkCache.get(key)
if (!cs) {
cs = chunks(line.t, max)
chunkCache.set(key, cs)
}
const total = cs.reduce((a, c) => a + c.length, 0)
let acc = 0
let cur = cs[cs.length - 1]
let cStart = line.start
let cEnd = line.end
for (const c of cs) {
const s0 = line.start + (acc / total) * line.dur
const s1 = line.start + ((acc + c.length) / total) * line.dur
if (t < s1 || c === cs[cs.length - 1]) {
cur = c
cStart = s0
cEnd = s1
break
}
acc += c.length
}
const a = Math.min(smooth(inv(cStart - 0.1, cStart + 0.12, t)), 1 - smooth(inv(Math.max(cEnd, line.end) + 0.05, line.end + 0.25, t)))
const show = cur.replace(/[,。;:、]$/, '')
const size = (P ? 50 : 42) * u
const y = P ? H * 0.865 : H * 0.905
g.save()
if (tone === 'dark') {
const gr = g.createLinearGradient(0, y - size * 2.2, 0, H)
gr.addColorStop(0, 'rgba(0,0,0,0)')
gr.addColorStop(1, `rgba(0,0,0,${0.45 * a})`)
g.fillStyle = gr
g.fillRect(0, y - size * 2.2, W, H - y + size * 2.2)
g.shadowColor = 'rgba(0,0,0,0.85)'
g.shadowBlur = 12 * u
} else {
g.shadowColor = 'rgba(246,241,230,0.95)'
g.shadowBlur = 14 * u
}
text(g, show, W / 2, y, size, { color: tone === 'dark' ? '#f3efe6' : '#1d1b18', align: 'center', alpha: a, ls: 0.06 })
g.restore()
}
/** 镜头篇章卡 */
export function chapterCard(rc: RC, sc: Scene, lt: number, tone: 'dark' | 'light') {
const { g, W, H, u, P, A } = rc
const D = sc.intro
const a = Math.min(smooth(inv(0.45, 1.1, lt)), 1 - smooth(inv(D - 0.7, D + 0.1, lt)))
if (a <= 0) return
const fg = tone === 'dark' ? '#f2ede2' : C.ink
const cx = W / 2
const cy = H * (P ? 0.44 : 0.47)
g.save()
// 背景压暗/压亮
g.fillStyle = tone === 'dark' ? `rgba(8,9,12,${0.55 * a})` : `rgba(240,234,222,${0.55 * a})`
g.fillRect(0, 0, W, H)
const isShot = sc.id.startsWith('shot')
const lineW = (P ? 300 : 380) * u * easeOut(inv(0.15, 1.3, lt))
g.strokeStyle = rgba(C.gold, 0.85 * a)
g.lineWidth = 1.2 * u
const yTop = cy - (P ? 150 : 130) * u
g.beginPath()
g.moveTo(cx - lineW, yTop)
g.lineTo(cx - 40 * u, yTop)
g.moveTo(cx + 40 * u, yTop)
g.lineTo(cx + lineW, yTop)
g.stroke()
const label = isShot ? `第 ${sc.no} 镜` : sc.no === '深' ? '深 读' : sc.no === '背' ? '背 诵' : ''
text(g, label, cx, yTop, 30 * u, { color: C.gold, align: 'center', alpha: a * smooth(inv(0.2, 0.9, lt)), ls: 0.3 })
// 主标题
const tp = easeOut(inv(0.25, 1.4, lt))
const big = (P ? 200 : 190) * u * (sc.title.length > 3 ? 0.55 : 1)
text(g, sc.title, cx, cy, big, { fam: 'xing', color: fg, align: 'center', alpha: a * tp, ls: 0.12 + (1 - tp) * 0.25 })
// 英文 + 对应段落
const sub = isShot ? `${sc.en} · 课文第${'一二三四五'[Number(sc.id.slice(4)) - 1]}段` : sc.en
text(g, sub, cx, cy + (P ? 150 : 135) * u, 24 * u, { color: tone === 'dark' ? rgba('#f2ede2', 0.7) : rgba(C.ink, 0.7), align: 'center', alpha: a * smooth(inv(0.6, 1.4, lt)), ls: 0.35 })
// 印章
const sp = inv(1.0, 1.35, lt)
if (sp > 0) {
const s = (P ? 96 : 88) * u * (1 + (1 - easeOutBack(sp)) * 0.6)
const sx = cx + (P ? 200 : 260) * u
const sy = cy - (P ? 60 : 70) * u
g.globalAlpha = a * smooth(sp) * 0.92
g.drawImage(A.seal(sc.no), sx - s / 2, sy - s / 2, s, s)
}
g.restore()
}
/** 取景框 HUD(呼应“镜头”概念) */
export function hud(rc: RC, sc: Scene, lt: number, tone: 'dark' | 'light', shotIdx: number) {
const { g, W, H, u, P, t } = rc
const a = smooth(inv(0.3, 1.5, lt)) * (1 - smooth(inv(sc.dur - 0.8, sc.dur, lt)))
if (a <= 0) return
const col = tone === 'dark' ? rgba('#f2ede2', 0.5 * a) : rgba(C.ink, 0.45 * a)
const m = (P ? 46 : 42) * u
const L = 34 * u
g.save()
g.strokeStyle = col
g.lineWidth = 1.4 * u
const corners: [number, number, number, number][] = [
[m, m, 1, 1], [W - m, m, -1, 1], [m, H - m, 1, -1], [W - m, H - m, -1, -1],
]
for (const [x, y, dx, dy] of corners) {
g.beginPath()
g.moveTo(x, y + dy * L)
g.lineTo(x, y)
g.lineTo(x + dx * L, y)
g.stroke()
}
const fs = 19 * u
const ty = m + 26 * u
if (shotIdx >= 0) {
// 录制红点
const blink = (Math.floor(t * 1.2) % 2 === 0 ? 1 : 0.35) * a
g.fillStyle = rgba(C.cinnabar, blink)
g.beginPath()
g.arc(m + 22 * u, ty, 6 * u, 0, Math.PI * 2)
g.fill()
text(g, `SHOT 0${shotIdx + 1} ${sc.en}`, m + 40 * u, ty, fs, { color: col, ls: 0.2, fam: 'serif' })
const tc = Math.max(0, lt)
const ff = Math.floor((tc % 1) * 24)
const ss = Math.floor(tc) % 60
const mm = Math.floor(tc / 60)
const code = `${String(mm).padStart(2, '0')}:${String(ss).padStart(2, '0')}:${String(ff).padStart(2, '0')}`
text(g, code, W - m - 22 * u, ty, fs, { color: col, align: 'right', ls: 0.15 })
}
text(g, '登泰山记 · 五个镜头', P ? W / 2 : W - m - 22 * u, P ? H - m - 26 * u : H - m - 26 * u, fs, {
color: col, align: P ? 'center' : 'right', ls: 0.25,
})
g.restore()
}
export function drawSeal(rc: RC, s: string, x: number, y: number, size: number, alpha: number, white = true) {
if (alpha <= 0) return
rc.g.save()
rc.g.globalAlpha *= alpha
rc.g.drawImage(rc.A.seal(s, white), x - size / 2, y - size / 2, size, size)
rc.g.restore()
}
/** 雪/尘粒子:确定性 */
export function snow(rc: RC, n: number, opt: { wind?: number; speed?: number; size?: number; alpha?: number; seed?: number; color?: string; toward?: number } = {}) {
const { g, W, H, u, t } = rc
const rnd = mulberry32(opt.seed ?? 42)
const wind = opt.wind ?? 0.3
const sp = opt.speed ?? 1
const al = opt.alpha ?? 0.8
if (al <= 0) return
g.save()
g.fillStyle = opt.color ?? '#ffffff'
for (let i = 0; i < n; i++) {
const x0 = rnd()
const y0 = rnd()
const z = 0.3 + rnd() * 0.7
const ph = rnd() * 10
if (opt.toward) {
// 迎面扑来的风雪:从中心向外放射
const life = ((t * 0.35 * sp * opt.toward + y0) % 1)
const ang = x0 * Math.PI * 2
const rr = Math.pow(life, 2.2) * Math.max(W, H) * 0.9
const x = W / 2 + Math.cos(ang) * rr
const y = H / 2 + Math.sin(ang) * rr * 0.8
const s = (1 + life * 7) * u * z * (opt.size ?? 1)
g.globalAlpha = al * Math.min(1, life * 3) * (1 - life) * 1.6
g.beginPath()
g.arc(x, y, s, 0, Math.PI * 2)
g.fill()
continue
}
const vy = (0.05 + z * 0.09) * sp
const vx = wind * (0.04 + z * 0.08) * sp
let x = (x0 + t * vx + Math.sin(t * 0.8 + ph) * 0.006) % 1
let y = (y0 + t * vy) % 1
if (x < 0) x += 1
if (y < 0) y += 1
const s = (1 + z * 2.6) * u * (opt.size ?? 1)
g.globalAlpha = al * (0.35 + z * 0.65)
g.beginPath()
g.arc(x * W, y * H, s, 0, Math.PI * 2)
g.fill()
}
g.restore()
}
/** 画背景纹理(宣纸/墨底) */
export function bg(rc: RC, kind: 'light' | 'dark' | 'stone', alpha = 1) {
const { g, W, H, A } = rc
g.save()
g.globalAlpha = alpha
g.drawImage(A.paper(kind), 0, 0, W, H)
g.restore()
}
export function vignette(rc: RC, strength = 0.55, color = '0,0,0') {
const { g, W, H } = rc
const r = Math.hypot(W, H) / 2
const rg = g.createRadialGradient(W / 2, H / 2, r * 0.45, W / 2, H / 2, r)
rg.addColorStop(0, `rgba(${color},0)`)
rg.addColorStop(1, `rgba(${color},${strength})`)
g.fillStyle = rg
g.fillRect(0, 0, W, H)
}
/** 引文中某个词被朗读到的时间(与 drawQuote 的逐字节奏一致) */
export function wordTime(id: string, word: string, nth = 0) {
const l = LINE[id]
if (!l) return 0
const chars = [...l.t]
let idx = -1
let from = 0
for (let k = 0; k <= nth; k++) {
idx = l.t.indexOf(word, from)
if (idx < 0) break
from = idx + word.length
}
if (idx < 0) return l.start
const ws = chars.map((c) => (isPunct(c) ? 1.6 : 1))
const total = ws.reduce((a, b) => a + b, 0)
let acc = 0
for (let i = 0; i < idx; i++) acc += ws[i]
return l.start - 0.05 + (acc / total) * l.dur * 0.94
}
/** 画一层水墨山(素材按宽度倍数生成,可横向平移) */
export function ridge(rc: RC, key: string, o: import('./assets').RidgeOpts, dx = 0, dy = 0, alpha = 1, sc = 1) {
if (alpha <= 0) return
const c = rc.A.ridge(key, o)
const wf = o.wf ?? 1
const w = rc.W * wf * sc
const h = rc.H * sc
rc.g.save()
rc.g.globalAlpha *= alpha
rc.g.drawImage(c, (rc.W - w) / 2 + dx, rc.H - h + dy, w, h)
rc.g.restore()
}
/** 标签:圆点 + 细线 + 文字 */
export function tag(rc: RC, x: number, y: number, s: string, a: number, o: { color?: string; dir?: 'l' | 'r' | 'u' | 'd'; size?: number; dot?: string; len?: number; fam?: keyof typeof F } = {}) {
if (a <= 0) return
const { g, u } = rc
const col = o.color ?? '#f2ede2'
const len = (o.len ?? 46) * u
const dir = o.dir ?? 'r'
const size = (o.size ?? 28) * u
g.save()
g.globalAlpha *= a
g.fillStyle = o.dot ?? C.cinnabar
g.beginPath()
g.arc(x, y, 5 * u, 0, Math.PI * 2)
g.fill()
g.strokeStyle = rgba(col, 0.7)
g.lineWidth = 1.2 * u
const ex = x + (dir === 'r' ? len : dir === 'l' ? -len : 0) * easeOut(a)
const ey = y + (dir === 'd' ? len : dir === 'u' ? -len : 0) * easeOut(a)
g.beginPath()
g.moveTo(x, y)
g.lineTo(ex, ey)
g.stroke()
const tx = ex + (dir === 'r' ? 10 * u : dir === 'l' ? -10 * u : 0)
const ty = ey + (dir === 'd' ? size * 0.8 : dir === 'u' ? -size * 0.8 : 0)
text(g, s, tx, ty, size, { color: col, align: dir === 'r' ? 'left' : dir === 'l' ? 'right' : 'center', ls: 0.08, fam: o.fam ?? 'serif' })
g.restore()
}
/** 时间辅助:针对某一句的进度 */
export const L = (id: string) => LINE[id]
export const at = (rc: RC, id: string, delay = 0, len = 0.8) => {
const l = LINE[id]
return l ? clamp((rc.t - l.start - delay) / len) : 0
}
export const during = (rc: RC, fromId: string, toId: string | number, fin = 0.5, fout = 0.5) => {
const a = LINE[fromId]?.start ?? 0
const b = typeof toId === 'number' ? toId : LINE[toId]?.end ?? a
return Math.min(smooth(inv(a - 0.15, a - 0.15 + fin, rc.t)), 1 - smooth(inv(b, b + fout, rc.t)))
}