Spaces:
Running
Running
Download dengtaishan-src/src/video/covers.ts from Ethscriptions/mx: direct link, hf CLI and curl.
- Browser
- Download file 9.54 kB
-
https://huggingface.co/spaces/Ethscriptions/mx/resolve/main/dengtaishan-src/src/video/covers.ts
- Command line
-
hf download hf://spaces/Ethscriptions/mx/dengtaishan-src/src/video/covers.ts
-
curl -L -o covers.ts https://huggingface.co/spaces/Ethscriptions/mx/resolve/main/dengtaishan-src/src/video/covers.ts
9.54 kB
| // 封面生成:4 种设计 × 3 种比例(16:9 / 4:3 / 3:4),与视频同一套水墨视觉 | |
| import { Assets, glow } from './assets' | |
| import { RC, C, text, ridge, drawSeal, snow, vignette, measure } from './draw' | |
| import { makeCanvas, ctx2d, rgba, mulberry32 } from './util' | |
| import { SHOT_NAMES } from './timeline' | |
| export type Ratio = '16:9' | '4:3' | '3:4' | |
| export const RATIO_SIZE: Record<Ratio, [number, number]> = { '16:9': [1920, 1080], '4:3': [1440, 1080], '3:4': [1080, 1440] } | |
| export interface CoverDef { | |
| id: string | |
| name: string | |
| desc: string | |
| draw: (rc: RC) => void | |
| } | |
| function title(rc: RC, x: number, y: number, size: number, color = '#fbf6ec', fam: 'kai' | 'xing' | 'serifB' = 'kai') { | |
| rc.g.save() | |
| rc.g.shadowColor = 'rgba(0,0,0,0.45)' | |
| rc.g.shadowBlur = size * 0.12 | |
| text(rc.g, '登泰山记', x, y, size, { fam, color, align: 'center', ls: 0.12 }) | |
| rc.g.restore() | |
| } | |
| function pill(rc: RC, s: string, x: number, y: number, size: number, bgc: string, fg: string) { | |
| const w = measure(rc.g, s, size, 'serifB', 0.15) + size * 1.4 | |
| rc.g.fillStyle = bgc | |
| rc.g.fillRect(x - w / 2, y - size * 0.85, w, size * 1.7) | |
| text(rc.g, s, x, y, size, { fam: 'serifB', color: fg, align: 'center', ls: 0.15 }) | |
| } | |
| const sunrise: CoverDef = { | |
| id: 'sun', name: '正赤如丹', desc: '日出朱砂 · 主视觉', | |
| draw: (rc) => { | |
| const { g, W, H, u } = rc | |
| const P = H > W | |
| const gr = g.createLinearGradient(0, 0, 0, H) | |
| gr.addColorStop(0, '#0d1226') | |
| gr.addColorStop(0.45, '#3e2f5a') | |
| gr.addColorStop(0.68, '#c0563e') | |
| gr.addColorStop(0.8, '#f2a45e') | |
| gr.addColorStop(1, '#2a1a22') | |
| g.fillStyle = gr | |
| g.fillRect(0, 0, W, H) | |
| const sx = P ? W * 0.5 : W * 0.68 | |
| const sy = H * (P ? 0.6 : 0.58) | |
| glow(g, sx, sy, Math.max(W, H) * 0.6, '#ff6a3a', 0.45) | |
| g.fillStyle = '#d8261a' | |
| g.beginPath() | |
| g.arc(sx, sy, (P ? 150 : 130) * u, 0, Math.PI * 2) | |
| g.fill() | |
| for (let i = 0; i < 18; i++) { | |
| g.fillStyle = `rgba(255,${90 + i * 4},50,${0.6 - i * 0.03})` | |
| g.fillRect(sx - (220 - i * 9) * u, sy + (150 + i * 9) * u, (440 - i * 18) * u, 3 * u) | |
| } | |
| ridge(rc, 'c-sun-1', { seed: 81, base: 0.86, amp: 0.12, freq: 2.6, color: '#2a1828', alpha: 0.8, depth: 0.4, snow: 0.5 }, 0, 0) | |
| ridge(rc, 'c-sun-2', { seed: 82, base: 1.0, amp: 0.16, freq: 2, color: '#0e0a10', alpha: 0.96, depth: 0.5, snow: 0.3 }, 0, 0) | |
| vignette(rc, 0.5) | |
| const tx = P ? W / 2 : W * 0.3 | |
| title(rc, tx, H * (P ? 0.2 : 0.4), (P ? 210 : 190) * u) | |
| text(g, '姚 鼐', tx, H * (P ? 0.3 : 0.4) + (P ? 30 : 150) * u, 36 * u, { color: C.goldHi, align: 'center', ls: 0.6 }) | |
| pill(rc, '五个镜头 · 读懂背会', tx, H * (P ? 0.38 : 0.4) + (P ? 30 : 250) * u, 40 * u, 'rgba(201,168,106,0.92)', '#1a1310') | |
| drawSeal(rc, '正赤', W - 110 * u, 110 * u, 120 * u, 1) | |
| }, | |
| } | |
| const snowpeak: CoverDef = { | |
| id: 'snow', name: '苍山负雪', desc: '冷调雪山 · 竖排书法', | |
| draw: (rc) => { | |
| const { g, W, H, u } = rc | |
| const P = H > W | |
| const gr = g.createLinearGradient(0, 0, 0, H) | |
| gr.addColorStop(0, '#121a28') | |
| gr.addColorStop(0.6, '#4b5a74') | |
| gr.addColorStop(1, '#9aa6ba') | |
| g.fillStyle = gr | |
| g.fillRect(0, 0, W, H) | |
| glow(g, W * 0.3, H * 0.5, Math.max(W, H) * 0.6, '#f4efe2', 0.25) | |
| ridge(rc, 'c-sn-1', { seed: 31, base: 0.72, amp: 0.16, freq: 2.4, color: '#7d889c', alpha: 0.55, depth: 0.4, snow: 1 }, 0, 0) | |
| ridge(rc, 'c-sn-2', { seed: 32, base: 0.84, amp: 0.2, freq: 3.2, color: '#465264', alpha: 0.85, depth: 0.5, snow: 1 }, 0, 0) | |
| ridge(rc, 'c-sn-3', { seed: 33, base: 1.0, amp: 0.24, freq: 2.6, color: '#1c232e', alpha: 0.97, depth: 0.6, snow: 0.9, tex: 1.4 }, 0, 0) | |
| snow({ ...rc, t: 3.3 }, 220, { alpha: 0.7, seed: 5 }) | |
| vignette(rc, 0.45) | |
| const s = (P ? 190 : 170) * u | |
| const x = W * (P ? 0.78 : 0.82) | |
| ;[...'登泰山记'].forEach((c, i) => { | |
| g.save() | |
| g.shadowColor = 'rgba(0,0,0,0.5)' | |
| g.shadowBlur = 20 * u | |
| text(g, c, x, H * 0.14 + s * 0.55 + i * s * 1.02, s, { fam: 'xing', color: '#fbf8f2', align: 'center' }) | |
| g.restore() | |
| }) | |
| drawSeal(rc, '姚鼐', x - s * 0.95, H * 0.14 + s * 3.9, 90 * u, 1) | |
| const lx = W * (P ? 0.22 : 0.16) | |
| ;['苍山负雪', '明烛天南'].forEach((ln, j) => { | |
| ;[...ln].forEach((c, i) => text(g, c, lx - j * 80 * u, H * 0.16 + i * 72 * u, 58 * u, { fam: 'kai', color: '#f2ede2', align: 'center' })) | |
| }) | |
| text(g, '高中语文 · 必修上', W / 2, H - 70 * u, 30 * u, { color: '#e8e2d6', align: 'center', ls: 0.4 }) | |
| }, | |
| } | |
| const cloudsea: CoverDef = { | |
| id: 'cloud', name: '一部电影', desc: '云海樗蒱 · 电影画幅', | |
| draw: (rc) => { | |
| const { g, W, H, u, A } = rc | |
| const P = H > W | |
| const gr = g.createLinearGradient(0, 0, 0, H) | |
| gr.addColorStop(0, '#1a2140') | |
| gr.addColorStop(0.42, '#7a4474') | |
| gr.addColorStop(0.55, '#f09a52') | |
| gr.addColorStop(0.56, '#e8a890') | |
| gr.addColorStop(1, '#4a3448') | |
| g.fillStyle = gr | |
| g.fillRect(0, 0, W, H) | |
| const hy = H * 0.55 | |
| glow(g, W * 0.55, hy, W * 0.6, '#ffb070', 0.4) | |
| const r = mulberry32(8) | |
| for (let i = 0; i < 14; i++) { | |
| const x = r() * W | |
| const y = hy + H * (0.03 + r() * 0.15) | |
| const w = (40 + r() * 70) * u | |
| const h = (40 + r() * 60) * u | |
| g.fillStyle = 'rgba(250,240,236,0.9)' | |
| g.beginPath() | |
| g.moveTo(x - w, y) | |
| g.bezierCurveTo(x - w * 0.8, y - h, x + w * 0.8, y - h, x + w, y) | |
| g.fill() | |
| } | |
| for (let i = 0; i < 16; i++) { | |
| const cl = A.cloud(i % 6) | |
| const w = W * (0.4 + r() * 0.4) | |
| g.globalAlpha = 0.65 | |
| g.drawImage(cl as CanvasImageSource, r() * W * 1.2 - w / 2, hy + r() * H * 0.4 - w * 0.1, w, w * 0.3) | |
| } | |
| g.globalAlpha = 1 | |
| // 电影画幅 | |
| const bar = H * (P ? 0.1 : 0.11) | |
| g.fillStyle = '#060608' | |
| g.fillRect(0, 0, W, bar) | |
| g.fillRect(0, H - bar, W, bar) | |
| for (let x = 20 * u; x < W; x += 46 * u) { | |
| g.fillStyle = 'rgba(240,235,225,0.22)' | |
| g.fillRect(x, bar * 0.35, 22 * u, bar * 0.3) | |
| g.fillRect(x, H - bar * 0.65, 22 * u, bar * 0.3) | |
| } | |
| title(rc, W / 2, H * (P ? 0.3 : 0.33), (P ? 200 : 180) * u) | |
| text(g, '其实是一部用文字拍成的电影', W / 2, H * (P ? 0.3 : 0.33) + (P ? 150 : 140) * u, (P ? 44 : 46) * u, { fam: 'serifB', color: '#fff4e6', align: 'center', ls: 0.15 }) | |
| text(g, SHOT_NAMES.join(' · '), W / 2, H - bar - 50 * u, 32 * u, { color: '#fff1de', align: 'center', ls: 0.3 }) | |
| }, | |
| } | |
| const memory: CoverDef = { | |
| id: 'memo', name: '背不下来?', desc: '宣纸记忆卡 · 强标题', | |
| draw: (rc) => { | |
| const { g, W, H, u, A } = rc | |
| const P = H > W | |
| g.drawImage(A.paper('light') as CanvasImageSource, 0, 0, W, H) | |
| ridge(rc, 'c-mem', { seed: 5, base: 1.02, amp: 0.2, freq: 2.2, color: '#2a2a2e', alpha: 0.35, depth: 0.5, snow: 0.6 }, 0, 0) | |
| g.fillStyle = C.red | |
| const bandH = (P ? 300 : 250) * u | |
| g.fillRect(0, H * (P ? 0.1 : 0.09), W, bandH) | |
| text(g, '《登泰山记》背不下来?', W / 2, H * (P ? 0.1 : 0.09) + bandH * 0.36, (P ? 74 : 76) * u, { fam: 'serifB', color: '#fff7ea', align: 'center', ls: 0.05 }) | |
| text(g, '记住这 5 个镜头就够了', W / 2, H * (P ? 0.1 : 0.09) + bandH * 0.74, (P ? 54 : 52) * u, { fam: 'serifB', color: '#ffe3b0', align: 'center', ls: 0.12 }) | |
| const keys = ['阳汶西 阴济东', '历穿越至 循越循至', '一线·五彩·正赤', '岱祠·行宫·石刻', '多石少土 雪与膝齐'] | |
| const top = H * (P ? 0.1 : 0.09) + bandH + (P ? 50 : 40) * u | |
| const n = 5 | |
| if (P || W / H < 1.5) { | |
| const rowH = (H * 0.92 - top) / n | |
| for (let i = 0; i < n; i++) { | |
| const y = top + rowH * (i + 0.45) | |
| text(g, `0${i + 1}`, W * 0.1, y, 30 * u, { color: C.red, ls: 0.1 }) | |
| text(g, SHOT_NAMES[i], W * 0.18, y, (P ? 76 : 70) * u, { fam: 'xing', color: C.ink }) | |
| text(g, keys[i], W * 0.92, y, (P ? 34 : 34) * u, { fam: 'kai', color: rgba(C.ink, 0.85), align: 'right' }) | |
| g.strokeStyle = rgba(C.ink, 0.2) | |
| g.beginPath() | |
| g.moveTo(W * 0.08, y + rowH * 0.5) | |
| g.lineTo(W * 0.92, y + rowH * 0.5) | |
| g.stroke() | |
| } | |
| } else { | |
| const cw = (W * 0.88) / n | |
| for (let i = 0; i < n; i++) { | |
| const x = W * 0.06 + cw * (i + 0.5) | |
| const y = top + 90 * u | |
| text(g, `镜头${'一二三四五'[i]}`, x, y - 50 * u, 26 * u, { color: C.red, align: 'center', ls: 0.2 }) | |
| text(g, SHOT_NAMES[i], x, y + 40 * u, 96 * u, { fam: 'xing', color: C.ink, align: 'center' }) | |
| keys[i].split(/[ ·]/).forEach((k, j) => text(g, k, x, y + (150 + j * 54) * u, 34 * u, { fam: 'kai', color: rgba(C.ink, 0.85), align: 'center' })) | |
| } | |
| } | |
| drawSeal(rc, '姚鼐', W - 90 * u, H - 90 * u, 100 * u, 1) | |
| }, | |
| } | |
| export const COVER_DEFS = [sunrise, snowpeak, cloudsea, memory] | |
| export function renderCover(def: CoverDef, ratio: Ratio, scale = 1): HTMLCanvasElement { | |
| const [w, h] = RATIO_SIZE[ratio] | |
| const W = Math.round(w * scale) | |
| const H = Math.round(h * scale) | |
| const c = document.createElement('canvas') | |
| c.width = W | |
| c.height = H | |
| const A = new Assets(W, H) | |
| const rc: RC = { g: c.getContext('2d')!, W, H, u: Math.min(W, H) / 1080, P: H > W, A, t: 0 } | |
| def.draw(rc) | |
| // 胶片颗粒 | |
| const tile = A.grain(1) | |
| const pat = rc.g.createPattern(tile as CanvasImageSource, 'repeat') | |
| if (pat) { | |
| rc.g.save() | |
| rc.g.globalAlpha = 0.05 | |
| rc.g.globalCompositeOperation = 'overlay' | |
| rc.g.fillStyle = pat | |
| rc.g.fillRect(0, 0, W, H) | |
| rc.g.restore() | |
| } | |
| A.dispose() | |
| return c | |
| } | |
| export { makeCanvas, ctx2d } | |