Files
drawlogo/js/core/poster-exporter.js
T
sgp 5bcd5ab0f2 feat(poster): 海报画布接入思源黑体(Noto Sans SC),SVG 导出走相对路径引用
- poster-renderer:注入 @font-face(按需加载,不阻塞首屏),文字默认字族 Noto Sans SC;新增 renderLinked(相对路径)与 renderEmbedded(PNG 内联,字体缺失自动降级)
- poster-exporter:SVG 导出用相对路径(约 1KB),PNG 仍内联字体保证自包含
- 实测:页面内字体生效(中文墨迹 7129 vs 回退 6952);导出的 SVG 站点内打开生效(8154 vs 7959);PNG 900x1200 正常
- poster.html 相关脚本统一 ?v=20261105a
2026-11-05 20:14:13 +08:00

62 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 画个标 · 海报导出:SVG / PNG(按画布规格导出)
* - SVG:@font-face 用相对路径引用站点内思源黑体(文件小,留在站点内打开即正确渲染)
* - PNG:SVG 作为 <img> 渲染时拿不到页面 @font-face,故内联字重后再栅格化
*/
const HGB_PosterExporter = (function () {
'use strict';
function buildSvg(spec, elements) {
return HGB_PosterRenderer.render(spec, elements);
}
/** SVG 导出用(相对路径字体,文件小) */
function buildSvgLinked(spec, elements) {
return HGB_PosterRenderer.renderLinked(spec, elements);
}
/** 内联字体后的 SVG(Promise<string>,PNG 栅格化用) */
function buildSvgEmbedded(spec, elements) {
return HGB_PosterRenderer.renderEmbedded(spec, elements);
}
function download(name, blob) {
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = name;
document.body.appendChild(a);
a.click();
setTimeout(() => { URL.revokeObjectURL(a.href); a.remove(); }, 0);
}
function exportSvg(spec, elements, base) {
const svg = buildSvgLinked(spec, elements);
download((base || '海报') + '.svg', new Blob([svg], { type: 'image/svg+xml' }));
}
/** 渲染 SVG 到位图并下载(scale 控制放大倍数,默认 1) */
function svgToCanvas(spec, elements, scale) {
return buildSvgEmbedded(spec, elements).then(svg => new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
const w = Math.round(spec.w * scale), h = Math.round(spec.h * scale);
const c = document.createElement('canvas');
c.width = w; c.height = h;
const ctx = c.getContext('2d');
ctx.drawImage(img, 0, 0, w, h);
resolve(c);
};
img.onerror = () => reject(new Error('SVG 渲染失败'));
img.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);
}));
}
async function exportPng(spec, elements, base, scale) {
const c = await svgToCanvas(spec, elements, scale || 1);
const blob = await new Promise(res => c.toBlob(res, 'image/png'));
download((base || '海报') + '.png', blob);
}
return { buildSvg, buildSvgLinked, buildSvgEmbedded, exportSvg, exportPng, svgToCanvas };
})();