Spaces:
Running
Running
Surf commited on
Commit ·
f335a42
1
Parent(s): 93946f5
封面:真实人物半身轮廓与三套个人IP版式(推送 #21)
Browse files- README.md +5 -5
- src/film/core/covers.ts +127 -134
README.md
CHANGED
|
@@ -28,19 +28,19 @@ pinned: false
|
|
| 28 |
| 设计 | 主视觉 | 配色 | 个人 IP 识别 |
|
| 29 |
|------|--------|------|--------------|
|
| 30 |
| **IP 主视觉** | 奥德彪正面/近景半身定格,左侧或中央占画面约 55% | 香蕉黄 + 炭黑 + 信号红 | 大号“奥德彪”手写字、固定黄色姓名牌、统一头像构图 |
|
| 31 |
-
| **热带海报** | 人物半身 + 香蕉绿背景 + 红色撞色贴纸 | 热带绿 + 香蕉黄 + 红 |
|
| 32 |
| **金句人物卡** | 人物半身作为右侧主角,左侧大字金句 | 炭黑 + 香蕉黄 + 暖白 | 固定人物轮廓、统一金句排版、强化连续追更感 |
|
| 33 |
|
| 34 |
**平台缩略图原则:** 人脸/身体轮廓优先于小字;文字只保留“奥德彪”和一句短金句;高饱和色块负责抢眼,黑色描边负责在复杂画面上维持可读性。封面不是视频字幕截图,而是“人物头像 + 固定色彩 + 一句钩子”的 IP 海报。
|
| 35 |
|
| 36 |
### 素材与技术方案
|
| 37 |
|
| 38 |
-
- 原片 18.77 秒中已有真人近景帧(
|
| 39 |
- `src/film/core/covers.ts`:将 `Design` 改为 `ipHero`、`tropical`、`quoteCard`;新增统一人物背景、色块、描边、姓名牌和短金句绘制函数。
|
| 40 |
-
-
|
| 41 |
- 长金句缩成一条短钩子,避免缩略图文字过小;保留完整语录在视频内容中,不让封面承担全部文字信息。
|
| 42 |
- `CoverPanel` 继续沿用现有三比例、三款设计的预览和下载逻辑;更新设计名称、文件名与操作提示,明确“真人 IP 封面”。
|
| 43 |
-
- 构建后检查 bundle 中存在 `ipHero`、`tropical`、`quoteCard`
|
| 44 |
|
| 45 |
### 实施步骤与同步节点
|
| 46 |
|
|
@@ -63,7 +63,7 @@ pinned: false
|
|
| 63 |
### v5 过程日志(本轮改造)
|
| 64 |
|
| 65 |
- [x] **#20 计划**:已写入本节并同步,尚未改动封面实现。
|
| 66 |
-
- [
|
| 67 |
- [ ] **#22 界面提示**:待实施。
|
| 68 |
- [ ] **#23 构建质检**:待实施。
|
| 69 |
- [ ] **#24 最终同步**:待实施。
|
|
|
|
| 28 |
| 设计 | 主视觉 | 配色 | 个人 IP 识别 |
|
| 29 |
|------|--------|------|--------------|
|
| 30 |
| **IP 主视觉** | 奥德彪正面/近景半身定格,左侧或中央占画面约 55% | 香蕉黄 + 炭黑 + 信号红 | 大号“奥德彪”手写字、固定黄色姓名牌、统一头像构图 |
|
| 31 |
+
| **热带海报** | 人物半身 + 香蕉绿背景 + 红色撞色贴纸 | 热带绿 + 香蕉黄 + 红 | 真人肖像与“语录二创”来源标签、系列角标只放在安全区 |
|
| 32 |
| **金句人物卡** | 人物半身作为右侧主角,左侧大字金句 | 炭黑 + 香蕉黄 + 暖白 | 固定人物轮廓、统一金句排版、强化连续追更感 |
|
| 33 |
|
| 34 |
**平台缩略图原则:** 人脸/身体轮廓优先于小字;文字只保留“奥德彪”和一句短金句;高饱和色块负责抢眼,黑色描边负责在复杂画面上维持可读性。封面不是视频字幕截图,而是“人物头像 + 固定色彩 + 一句钩子”的 IP 海报。
|
| 35 |
|
| 36 |
### 素材与技术方案
|
| 37 |
|
| 38 |
+
- 原片 18.77 秒中已有真人近景帧(经逐帧核验为 6.2 秒),固定这张真实近景,沿肩线描摹透明轮廓,保持各条封面人物一致。
|
| 39 |
- `src/film/core/covers.ts`:将 `Design` 改为 `ipHero`、`tropical`、`quoteCard`;新增统一人物背景、色块、描边、姓名牌和短金句绘制函数。
|
| 40 |
+
- 在浏览器只解码一次原片 6.2 秒,手工轮廓蒙版保留真实人物,恢复暗部曝光;缓存透明肖像供所有封面共用。不换脸、不生成新的五官、不依赖外部抠图服务。
|
| 41 |
- 长金句缩成一条短钩子,避免缩略图文字过小;保留完整语录在视频内容中,不让封面承担全部文字信息。
|
| 42 |
- `CoverPanel` 继续沿用现有三比例、三款设计的预览和下载逻辑;更新设计名称、文件名与操作提示,明确“真人 IP 封面”。
|
| 43 |
+
- 构建后检查 bundle 中存在 `ipHero`、`tropical`、`quoteCard` 与真实肖像绘制逻辑;逐比例检查人物是否在安全区、标题是否越界。
|
| 44 |
|
| 45 |
### 实施步骤与同步节点
|
| 46 |
|
|
|
|
| 63 |
### v5 过程日志(本轮改造)
|
| 64 |
|
| 65 |
- [x] **#20 计划**:已写入本节并同步,尚未改动封面实现。
|
| 66 |
+
- [x] **#21 封面引擎**:已改为香蕉黄、热带绿、炭黑金三套真人半身海报;采用原片 6.2 秒真实人物轮廓与曝光恢复,不生成新脸;新增真实字宽适配、完整末句标题、素材失败重试及单次解码缓存。浏览器视觉和下载验收在 #23 执行。
|
| 67 |
- [ ] **#22 界面提示**:待实施。
|
| 68 |
- [ ] **#23 构建质检**:待实施。
|
| 69 |
- [ ] **#24 最终同步**:待实施。
|
src/film/core/covers.ts
CHANGED
|
@@ -1,162 +1,155 @@
|
|
| 1 |
-
// 封面
|
| 2 |
import { FONT, loadFonts } from './assets'
|
| 3 |
-
import {
|
| 4 |
import { seekExact, footageUrl } from './footage'
|
| 5 |
-
import {
|
| 6 |
-
import { drawVideo } from './fx'
|
| 7 |
-
import { adv, breakClause } from './typo'
|
| 8 |
import { pad2 } from './util'
|
| 9 |
|
| 10 |
export type Ratio = '16:9' | '4:3' | '3:4'
|
| 11 |
-
export type Design = '
|
| 12 |
export const RATIOS: { key: Ratio; w: number; h: number; label: string }[] = [
|
| 13 |
{ key: '16:9', w: 1920, h: 1080, label: '16:9 横版(B站 / 西瓜 / YouTube)' },
|
| 14 |
{ key: '4:3', w: 1440, h: 1080, label: '4:3 横版(视频号 / 公众号)' },
|
| 15 |
{ key: '3:4', w: 1080, h: 1440, label: '3:4 竖版(小红书 / 抖音)' },
|
| 16 |
]
|
| 17 |
-
export const DESIGNS: { key: Design; name: string }[] = [
|
| 18 |
-
{ key: '
|
| 19 |
-
{ key: '
|
| 20 |
-
{ key: '
|
| 21 |
]
|
| 22 |
-
const
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
-
return coverVideo
|
| 38 |
}
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
const
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
g.textBaseline = 'middle'
|
| 54 |
-
g.textAlign = 'left'
|
| 55 |
-
lines.forEach((l, li) => {
|
| 56 |
-
let x = o.align === 'left' ? o.x! : o.cx - (widths[li] * fs) / 2
|
| 57 |
-
const y = o.cy - H / 2 + fs / 2 + li * fs * o.lineH
|
| 58 |
-
for (const c of l) { g.fillText(c.ch, x, y); x += adv(g, fam, o.weight, c.ch) * fs + o.track * fs }
|
| 59 |
-
})
|
| 60 |
-
g.restore()
|
| 61 |
-
return { H, fs }
|
| 62 |
}
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
g.textBaseline = 'middle'
|
| 69 |
-
if (!track) { g.textAlign = align; g.fillText(s, x, y); g.restore(); return }
|
| 70 |
-
const fs = parseFloat(font.match(/(\d+(?:\.\d+)?)px/)![1])
|
| 71 |
-
const ws = [...s].map((c) => g.measureText(c).width)
|
| 72 |
-
const total = ws.reduce((a, b) => a + b, 0) + track * fs * (ws.length - 1)
|
| 73 |
-
let xx = align === 'center' ? x - total / 2 : align === 'right' ? x - total : x
|
| 74 |
-
g.textAlign = 'left'
|
| 75 |
-
;[...s].forEach((c, i) => { g.fillText(c, xx, y); xx += ws[i] + track * fs })
|
| 76 |
-
g.restore()
|
| 77 |
}
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
const w = R.w, h = R.h
|
| 83 |
-
const q = quoteById(id)
|
| 84 |
-
const film = new Film(q)
|
| 85 |
-
const v = await getVideo()
|
| 86 |
-
const di = DESIGNS.findIndex((d) => d.key === design)
|
| 87 |
-
await seekExact(v, FRAMES[(id + di * 2) % FRAMES.length])
|
| 88 |
-
const c = document.createElement('canvas')
|
| 89 |
-
c.width = w
|
| 90 |
-
c.height = h
|
| 91 |
-
const g = c.getContext('2d')!
|
| 92 |
-
const S = Math.min(w, h)
|
| 93 |
-
const P = h > w
|
| 94 |
-
const cl = clausesOf(q.text)
|
| 95 |
-
const no = `№ ${pad2(id)}`
|
| 96 |
-
const brand = `500 ${S * 0.03}px ${FONT.serif}`
|
| 97 |
-
const post = defaultPost()
|
| 98 |
-
post.bloom = 0; post.halation = 0; post.aberration = 0; post.flicker = 0; post.grain = 0.02; post.vignette = 0.25
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
g.fillStyle = gr
|
| 110 |
-
g.fillRect(0, 0, w, h)
|
| 111 |
-
label(g, '奥德彪语录', S * 0.06, S * 0.08, brand, '#fff', 'left', 0.42)
|
| 112 |
-
label(g, no, S * 0.06, S * 0.135, `400 ${S * 0.026}px ${FONT.latin}`, 'rgba(255,255,255,0.75)', 'left')
|
| 113 |
-
const lines = cl.flatMap((x) => breakClause(asChars(x), P ? 9 : 14))
|
| 114 |
-
g.shadowColor = 'rgba(0,0,0,0.6)'; g.shadowBlur = 16
|
| 115 |
-
block(g, lines, { cx: w / 2, cy: h * (P ? 0.78 : 0.79), maxW: w * 0.86, maxH: h * (P ? 0.3 : 0.3), size: S * 0.07, weight: 600, track: 0.1, lineH: 1.5, fill: SUB })
|
| 116 |
-
g.shadowBlur = 0
|
| 117 |
-
}
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
if (pre) {
|
| 129 |
-
const preLines = breakClause(asChars(pre), P ? 14 : 22)
|
| 130 |
-
block(g, preLines, { cx: w / 2, cy: h * 0.52 - r.H / 2 - S * 0.085, maxW: w * 0.8, maxH: S * 0.12, size: S * 0.04, weight: 400, track: 0.12, lineH: 1.45, fill: 'rgba(245,242,234,0.78)' })
|
| 131 |
-
}
|
| 132 |
-
label(g, '奥 德 彪 语 录', w / 2, h * 0.1, brand, 'rgba(255,255,255,0.85)', 'center', 0.2)
|
| 133 |
-
g.fillStyle = 'rgba(255,255,255,0.5)'
|
| 134 |
-
g.fillRect(w / 2 - S * 0.03, h * 0.9 - S * 0.045, S * 0.06, 2)
|
| 135 |
-
label(g, `${no} · ${film.chapter}`, w / 2, h * 0.9, `400 ${S * 0.026}px ${FONT.serif}`, 'rgba(255,255,255,0.7)', 'center', 0.2)
|
| 136 |
-
}
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
}
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
return out
|
| 160 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
return c
|
| 162 |
}
|
|
|
|
| 1 |
+
// 真人 IP 封面。只用原片 6.2s 的真人近景,不生成/替换人脸。
|
| 2 |
import { FONT, loadFonts } from './assets'
|
| 3 |
+
import { quoteById } from './film'
|
| 4 |
import { seekExact, footageUrl } from './footage'
|
| 5 |
+
import { getCopy } from './copy'
|
|
|
|
|
|
|
| 6 |
import { pad2 } from './util'
|
| 7 |
|
| 8 |
export type Ratio = '16:9' | '4:3' | '3:4'
|
| 9 |
+
export type Design = 'ipHero' | 'tropical' | 'quoteCard'
|
| 10 |
export const RATIOS: { key: Ratio; w: number; h: number; label: string }[] = [
|
| 11 |
{ key: '16:9', w: 1920, h: 1080, label: '16:9 横版(B站 / 西瓜 / YouTube)' },
|
| 12 |
{ key: '4:3', w: 1440, h: 1080, label: '4:3 横版(视频号 / 公众号)' },
|
| 13 |
{ key: '3:4', w: 1080, h: 1440, label: '3:4 竖版(小红书 / 抖音)' },
|
| 14 |
]
|
| 15 |
+
export const DESIGNS: { key: Design; name: string; color: string }[] = [
|
| 16 |
+
{ key: 'ipHero', name: '香蕉黄 · 主视觉', color: '#FFD21F' },
|
| 17 |
+
{ key: 'tropical', name: '热带绿 · 人物海报', color: '#086649' },
|
| 18 |
+
{ key: 'quoteCard', name: '炭黑金 · 金句人物卡', color: '#171C19' },
|
| 19 |
]
|
| 20 |
+
const YELLOW = '#FFD21F', PAPER = '#FFF8E7', INK = '#101713', RED = '#D93D2B'
|
| 21 |
+
// 固定真实肖像有利于系列识别。保留原片五官、表情、衣服,不补画头发或改变面部。
|
| 22 |
+
export const PORTRAIT_TIME = 6.2
|
| 23 |
+
let portraitPromise: Promise<HTMLCanvasElement> | null = null
|
| 24 |
|
| 25 |
+
function canvas(w: number, h: number) {
|
| 26 |
+
const c = document.createElement('canvas'); c.width = w; c.height = h; return c
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
/** 加载失败必须显式报错,禁止静默降级成风景封面或永久转圈。 */
|
| 30 |
+
async function makePortrait() {
|
| 31 |
+
const v = document.createElement('video')
|
| 32 |
+
v.muted = true; v.playsInline = true; v.crossOrigin = 'anonymous'; v.preload = 'auto'
|
| 33 |
+
try {
|
| 34 |
+
await new Promise<void>((resolve, reject) => {
|
| 35 |
+
const clean = () => { clearTimeout(timer); v.removeEventListener('loadeddata', ready); v.removeEventListener('error', fail) }
|
| 36 |
+
const ready = () => { clean(); resolve() }
|
| 37 |
+
const fail = () => { clean(); reject(new Error('原片人物素材加载失败,请检查网络后重试。')) }
|
| 38 |
+
const timer = setTimeout(fail, 30000)
|
| 39 |
+
v.addEventListener('loadeddata', ready, { once: true }); v.addEventListener('error', fail, { once: true })
|
| 40 |
+
v.src = footageUrl(); v.load()
|
| 41 |
+
})
|
| 42 |
+
await seekExact(v, PORTRAIT_TIME)
|
| 43 |
+
const raw = canvas(1920, 1080), g = raw.getContext('2d')!
|
| 44 |
+
g.drawImage(v, 0, 0, 1920, 1080)
|
| 45 |
+
// 只做曝光恢复,真实皮肤纹理不作生成式“美化”。
|
| 46 |
+
const pixels = g.getImageData(0, 0, 1920, 1080)
|
| 47 |
+
const lut = Array.from({ length: 256 }, (_, n) => Math.round(255 * Math.pow(n / 255, 0.70)))
|
| 48 |
+
for (let i = 0; i < pixels.data.length; i += 4) {
|
| 49 |
+
pixels.data[i] = lut[pixels.data[i]]; pixels.data[i + 1] = lut[pixels.data[i + 1]]; pixels.data[i + 2] = lut[pixels.data[i + 2]]
|
| 50 |
+
}
|
| 51 |
+
g.putImageData(pixels, 0, 0)
|
| 52 |
+
// 手工沿 960×540 参考帧的肩线/外套描摹轮廓,移除公路背景。
|
| 53 |
+
const path = new Path2D('M224 540 L230 480 L235 412 L247 320 L261 251 L264 235 Q278 189 310 175 L342 161 Q330 137 331 105 L329 77 Q325 35 355 0 L453 0 Q478 20 483 57 L484 71 Q494 68 492 91 L495 124 Q512 142 543 150 Q609 171 643 207 L669 244 Q702 299 716 356 Q729 403 725 451 L712 510 L706 540 Z M604 379 L625 426 L602 416 Z')
|
| 54 |
+
const out = canvas(1080, 1080), o = out.getContext('2d')!
|
| 55 |
+
o.scale(2, 2); o.translate(-205, 0); o.clip(path, 'evenodd'); o.drawImage(raw, 0, 0, 960, 540)
|
| 56 |
+
return out
|
| 57 |
+
} finally {
|
| 58 |
+
v.pause(); v.removeAttribute('src'); v.load()
|
| 59 |
}
|
|
|
|
| 60 |
}
|
| 61 |
|
| 62 |
+
function portrait() {
|
| 63 |
+
if (!portraitPromise) portraitPromise = makePortrait().catch((e) => { portraitPromise = null; throw e })
|
| 64 |
+
return portraitPromise
|
| 65 |
+
}
|
| 66 |
|
| 67 |
+
export interface CoverBox { role: string; x: number; y: number; w: number; h: number }
|
| 68 |
+
export interface CoverLayout { portrait: CoverBox; name: CoverBox; hook: CoverBox; footer: CoverBox }
|
| 69 |
+
export function coverLayout(w: number, h: number): CoverLayout {
|
| 70 |
+
const p = h > w, s = Math.min(w, h), m = s * 0.065
|
| 71 |
+
return {
|
| 72 |
+
portrait: p ? { role: 'portrait', x: w * 0.265, y: h * 0.105, w: w * 0.72, h: w * 0.72 } : { role: 'portrait', x: w * 0.485, y: h * 0.12, w: Math.min(h * 0.83, w * 0.49), h: Math.min(h * 0.83, w * 0.49) },
|
| 73 |
+
name: p ? { role: 'name', x: m, y: h * 0.17, w: w * 0.2, h: h * 0.30 } : { role: 'name', x: m, y: h * 0.17, w: w * 0.42, h: h * 0.18 },
|
| 74 |
+
hook: p ? { role: 'hook', x: m, y: h * 0.69, w: w - 2 * m, h: h * 0.205 } : { role: 'hook', x: m, y: h * 0.42, w: w * 0.42, h: h * 0.335 },
|
| 75 |
+
footer: { role: 'footer', x: m, y: h - s * 0.055, w: w - 2 * m, h: s * 0.022 },
|
| 76 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
}
|
| 78 |
|
| 79 |
+
/** 不乱截半句话:选原文中的完整末句;超长句通过多行与字号缩放适配。 */
|
| 80 |
+
export function coverHook(id: number) {
|
| 81 |
+
const clauses = quoteById(id).text.split(/[,。;?!、,;!?]+/).filter(Boolean)
|
| 82 |
+
return clauses[clauses.length - 1] || quoteById(id).text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
}
|
| 84 |
|
| 85 |
+
function text(g: CanvasRenderingContext2D, value: string, x: number, y: number, size: number, color: string, family = FONT.sans, weight = 700) {
|
| 86 |
+
g.font = `${weight} ${size}px ${family}`; g.fillStyle = color; g.textAlign = 'left'; g.textBaseline = 'top'; g.fillText(value, x, y)
|
| 87 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
/** 所有中文标题按实际字宽和容器高度缩放,返回实绘边界供回归测试。 */
|
| 90 |
+
function textBlock(g: CanvasRenderingContext2D, value: string, box: CoverBox, maxPer: number, color: string, family = FONT.sans, weight = 800): CoverBox {
|
| 91 |
+
const chars = [...value], n = Math.max(1, Math.ceil(chars.length / maxPer)), per = Math.ceil(chars.length / n)
|
| 92 |
+
const lines = Array.from({ length: n }, (_, i) => chars.slice(i * per, (i + 1) * per).join(''))
|
| 93 |
+
g.font = `${weight} 100px ${family}`
|
| 94 |
+
const units = Math.max(...lines.map((line) => g.measureText(line).width / 100))
|
| 95 |
+
const fs = Math.min(box.w / Math.max(1, units), box.h / (n * 1.19))
|
| 96 |
+
for (let i = 0; i < n; i++) text(g, lines[i], box.x, box.y + i * fs * 1.19, fs, color, family, weight)
|
| 97 |
+
return { ...box, w: units * fs, h: n * fs * 1.19 }
|
| 98 |
+
}
|
| 99 |
|
| 100 |
+
function pill(g: CanvasRenderingContext2D, value: string, x: number, y: number, size: number, bg: string, fg: string) {
|
| 101 |
+
g.font = `700 ${size}px ${FONT.sans}`
|
| 102 |
+
const w = g.measureText(value).width + size * 1.6, h = size * 1.9
|
| 103 |
+
g.fillStyle = bg; g.beginPath(); g.roundRect(x, y, w, h, size * 0.22); g.fill()
|
| 104 |
+
text(g, value, x + size * 0.8, y + size * 0.37, size, fg)
|
| 105 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
function drawPortrait(g: CanvasRenderingContext2D, source: HTMLCanvasElement, b: CoverBox, color: string, unit: number) {
|
| 108 |
+
const silhouette = canvas(source.width, source.height), sg = silhouette.getContext('2d')!
|
| 109 |
+
sg.drawImage(source, 0, 0); sg.globalCompositeOperation = 'source-in'; sg.fillStyle = color; sg.fillRect(0, 0, source.width, source.height)
|
| 110 |
+
g.save(); g.shadowColor = '#00170e55'; g.shadowBlur = unit * 22; g.shadowOffsetX = unit * 8; g.shadowOffsetY = unit * 12
|
| 111 |
+
g.drawImage(silhouette, b.x - unit * 5, b.y - unit * 5, b.w + unit * 10, b.h + unit * 10); g.restore()
|
| 112 |
+
// 薄描边使真人深色衣服与底色分离,不在脸上压字。
|
| 113 |
+
for (let i = 0; i < 12; i++) { const a = i / 12 * Math.PI * 2; g.drawImage(silhouette, b.x + Math.cos(a) * unit * 5, b.y + Math.sin(a) * unit * 5, b.w, b.h) }
|
| 114 |
+
g.drawImage(source, b.x, b.y, b.w, b.h)
|
| 115 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
export async function renderCover(id: number, ratio: Ratio, design: Design): Promise<HTMLCanvasElement> {
|
| 118 |
+
const [person] = await Promise.all([portrait(), loadFonts()])
|
| 119 |
+
const r = RATIOS.find((x) => x.key === ratio)
|
| 120 |
+
if (!r || !DESIGNS.some((x) => x.key === design)) throw new Error('未知封面比例或主题')
|
| 121 |
+
const { w, h } = r, p = h > w, s = Math.min(w, h), u = s / 1080, m = s * 0.065
|
| 122 |
+
const c = canvas(w, h), g = c.getContext('2d')!, L = coverLayout(w, h)
|
| 123 |
+
const green = design === 'tropical', dark = design === 'quoteCard'
|
| 124 |
+
const bg = green ? '#086649' : dark ? '#171C19' : YELLOW
|
| 125 |
+
const fg = green || dark ? PAPER : INK
|
| 126 |
+
g.fillStyle = bg; g.fillRect(0, 0, w, h)
|
| 127 |
+
// 主视觉装饰位于人物后方,保持脸部和标题无干扰。
|
| 128 |
+
g.save(); g.globalAlpha = dark ? 0.08 : 0.15; g.strokeStyle = fg; g.lineWidth = s * 0.002
|
| 129 |
+
for (let i = 0; i < 5; i++) { g.beginPath(); g.arc(w * 0.8, h * 0.37, s * (0.18 + i * 0.08), 0, Math.PI * 2); g.stroke() }
|
| 130 |
+
g.restore()
|
| 131 |
+
g.fillStyle = green ? YELLOW : dark ? '#2B3B2F' : '#E9B900'
|
| 132 |
+
g.beginPath(); g.moveTo(w * 0.58, h * 0.16); g.lineTo(w, h * 0.1); g.lineTo(w, h * 0.87); g.lineTo(w * 0.46, h * 0.91); g.closePath(); g.fill()
|
| 133 |
+
if (green) {
|
| 134 |
+
g.fillStyle = RED; g.beginPath(); g.arc(w * 0.9, h * 0.25, s * 0.13, 0, Math.PI * 2); g.fill()
|
| 135 |
}
|
| 136 |
+
// 页眉为系列设计,不使用“官方/本人发布/亲口说”等暗示背书的标记。
|
| 137 |
+
text(g, 'ODEBIAO / THE QUOTES', m, h * 0.047, s * 0.018, fg, FONT.mono, 400)
|
| 138 |
+
g.fillStyle = RED; g.fillRect(m, h * 0.087, s * 0.065, s * 0.008)
|
| 139 |
+
drawPortrait(g, person, L.portrait, dark || green ? YELLOW : PAPER, u)
|
| 140 |
+
const titleBox = textBlock(g, '奥德彪', L.name, p ? 1 : 3, fg, FONT.brush, 400)
|
| 141 |
+
if (p) {
|
| 142 |
+
// 竖版人物在上、文字在下,姓名竖排不挡脸。
|
| 143 |
+
g.fillStyle = bg; g.fillRect(0, h * 0.65, w, h * 0.35)
|
|
|
|
| 144 |
}
|
| 145 |
+
pill(g, getCopy(id)?.chapter || '奥德彪语录', m, p ? h * 0.608 : h * 0.34, s * 0.025, green || dark ? YELLOW : INK, green || dark ? INK : YELLOW)
|
| 146 |
+
const hookBox = textBlock(g, coverHook(id), L.hook, p ? 8 : 7, dark ? YELLOW : fg)
|
| 147 |
+
g.strokeStyle = green || dark ? '#ffffff44' : '#10171344'; g.lineWidth = Math.max(1, u)
|
| 148 |
+
g.beginPath(); g.moveTo(m, h - s * 0.09); g.lineTo(w - m, h - s * 0.09); g.stroke()
|
| 149 |
+
text(g, `语录二创 · ${pad2(id)} / 43`, L.footer.x, L.footer.y, s * 0.022, fg, FONT.sans, 500)
|
| 150 |
+
g.font = `400 ${s * 0.018}px ${FONT.mono}`; g.textAlign = 'right'; g.fillStyle = fg; g.fillText('ON THE ROAD.', w - m, L.footer.y)
|
| 151 |
+
// 内部测试读取具体边界;正常下载 PNG 不包含任何测试标记。
|
| 152 |
+
c.dataset.layout = JSON.stringify([titleBox, hookBox, L.footer])
|
| 153 |
+
c.dataset.portraitTime = String(PORTRAIT_TIME)
|
| 154 |
return c
|
| 155 |
}
|