mx / dengtaishan-src /src /app /Still.tsx
Ethscriptions's picture
登泰山记:渲染引擎、九个篇章场景、程序化配乐音效、MP4 导出、发布文案与封面(初版)
6fff8e3 verified
Raw History Blame Contribute Delete
3.43 kB
// 单帧预览 / 版面自检(开发检查用):?still=秒数,秒数&o=l|p&w=宽度 ?still=check&step=0.5
import { useEffect, useRef } from 'react'
import { Renderer } from '../video/render'
import { loadFonts } from '../video/fonts'
import { TL, DURATION } from '../video/timeline'
import { DEBUG } from '../video/draw'
type Box = (typeof DEBUG.boxes)[number]
function inter(a: Box, b: Box) {
const w = Math.min(a.x1, b.x1) - Math.max(a.x0, b.x0)
const h = Math.min(a.y1, b.y1) - Math.max(a.y0, b.y0)
if (w <= 0 || h <= 0) return 0
const area = Math.min((a.x1 - a.x0) * (a.y1 - a.y0), (b.x1 - b.x0) * (b.y1 - b.y0))
return (w * h) / Math.max(1, area)
}
export default function Still() {
const ref = useRef<HTMLCanvasElement>(null)
useEffect(() => {
const q = new URLSearchParams(location.search)
const portrait = q.get('o') === 'p'
const w = Number(q.get('w') || 960)
const W = portrait ? Math.round((w * 9) / 16) : w
const H = portrait ? w : Math.round((w * 9) / 16)
const win = window as unknown as Record<string, unknown>
win.TL = TL
loadFonts().then(() => {
const c = ref.current!
if (q.get('still') === 'check') {
const step = Number(q.get('step') || 0.5)
const from = Number(q.get('from') || 0)
const to = Number(q.get('to') || DURATION)
c.width = W
c.height = H
const g = c.getContext('2d')!
const r = new Renderer(W, H)
const issues: Record<string, { first: number; n: number }> = {}
const add = (k: string, t: number) => {
if (!issues[k]) issues[k] = { first: t, n: 0 }
issues[k].n++
}
DEBUG.on = true
for (let t = from; t < to; t += step) {
DEBUG.boxes = []
r.render(g, t)
const bs = DEBUG.boxes
for (const b of bs) {
const m = 2
if (b.x0 < -m || b.y0 < -m || b.x1 > W + m || b.y1 > H + m) add(`OOB ${b.s}`, t)
}
for (let i = 0; i < bs.length; i++)
for (let j = i + 1; j < bs.length; j++) {
const a = bs[i]
const b = bs[j]
if (a.s === b.s) continue
if (/^「[,。;:、?!“”]$/.test(a.s) || /^「[,。;:、?!“”]$/.test(b.s)) continue
const o = inter(a, b)
if (o > 0.18) add(`OVL ${a.s} <> ${b.s}`, t)
}
}
DEBUG.on = false
win.REPORT = issues
document.title = 'ready'
return
}
const ts = (q.get('still') || '0').split(',').map(Number)
const cols = Math.min(ts.length, portrait ? 4 : 2)
const rows = Math.ceil(ts.length / cols)
c.width = W * cols
c.height = H * rows
const g = c.getContext('2d')!
const r = new Renderer(W, H)
const tmp = document.createElement('canvas')
tmp.width = W
tmp.height = H
const tg = tmp.getContext('2d')!
const t0 = performance.now()
ts.forEach((t, i) => {
r.render(tg, t)
g.drawImage(tmp, (i % cols) * W, Math.floor(i / cols) * H)
g.fillStyle = '#ff0'
g.font = '14px monospace'
g.fillText(t.toFixed(1), (i % cols) * W + 6, Math.floor(i / cols) * H + 16)
})
win.MS = (performance.now() - t0) / ts.length
document.title = 'ready'
})
}, [])
return <canvas ref={ref} style={{ display: 'block' }} />
}