From 931c0b5673d5f29c95f5f58b3b2bb8599916fe84 Mon Sep 17 00:00:00 2001 From: jinghui Date: Fri, 13 Nov 2026 22:49:33 +0800 Subject: [PATCH] =?UTF-8?q?feat(poster):=20=E6=95=B4=E9=A1=B5=E6=97=A0?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E6=9D=A1=E8=87=AA=E9=80=82=E5=BA=94+?= =?UTF-8?q?=E5=8D=95=E5=8C=BA=E5=9F=9F=E6=BB=9A=E5=8A=A8+=E6=96=87?= =?UTF-8?q?=E5=AD=97=E5=8F=AF=E6=97=8B=E8=BD=AC+=E6=A8=A1=E6=9D=BF25?= =?UTF-8?q?=E2=86=9242(=E6=96=B0=E5=A2=9E=E6=A0=A1=E5=9B=AD10=E6=AC=BE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 布局: pmRoot按最内层n-layout-scroll-container实测高度定根(fit联动measure, resize/focus/visibilitychange复测, 余量18px), 根overflow:hidden; 桌面三列各自overflow-y内部滚动, 窄屏堆叠后仅.pm-main单区域滚动; fit()改宽高双向取min, 画布完整落入舞台不出滚动条 - 旋转: 图层接口新增rot(文字/图片/形状), 预览CSS transform绕图层中心, 导出canvas rotAround同中心旋转(预览/导出同轴不跑位), 编辑面板加旋转滑杆(-180~180)+复位; 实测模板内置-4°与滑杆-90°/-25°矩阵值精确, PNG导出98KB正常 - 模板: 25→42, 新增'校园'分类10款(教师节/开学第一课/家长会/秋季运动会/毕业季/社团招新/优秀学生表彰/快乐六一/考试加油/读书月)+促销2/节日1/活动1/美食3 - 验证: 780x437/1100x800/1600x900三档实测page_scrollable=false, 滚动只发生在指定区域; 桌面三列对齐, 校园10模板缩略图渲染正常 --- src/tools/poster/poster.vue | 85 +++++++++++++++---- src/tools/poster/templates.ts | 149 ++++++++++++++++++++++++++++++++++ 2 files changed, 219 insertions(+), 15 deletions(-) diff --git a/src/tools/poster/poster.vue b/src/tools/poster/poster.vue index 824ddd7..f0c9145 100644 --- a/src/tools/poster/poster.vue +++ b/src/tools/poster/poster.vue @@ -19,6 +19,8 @@ const bg = ref({}); const selId = ref(''); const scale = ref(0.5); const stageEl = ref(null); +const pmRoot = ref(null); +const pmH = ref(0); // 视口自适应高度:根不滚动,滚动只发生在内部区域 const fileEl = ref(null); const pmError = ref(''); const models = ref<{ id: string; name?: string }[]>([]); @@ -28,6 +30,7 @@ const bgPrompt = ref(''); const exporting = ref(false); let uid = 0; let ro: ResizeObserver | null = null; +let scRO: ResizeObserver | null = null; const filtered = computed(() => cat.value === 'hot' ? POSTER_TEMPLATES : POSTER_TEMPLATES.filter((t) => t.category === cat.value)); @@ -97,6 +100,7 @@ function boxStyle(l: L) { return { left: l.x + 'px', top: l.y + 'px', width: l.w + 'px', height: l.type === 'text' ? 'auto' : l.h + 'px', + transform: l.rot ? `rotate(${l.rot}deg)` : undefined, }; } function fontStyle(l: L) { @@ -121,6 +125,7 @@ function shapeStyle(l: L) { function miniBoxStyle(l: L) { return boxStyle(l); } const tplLayers = (t: PosterTemplate): L[] => t.layers as any; function onFill(e: Event) { const l = sel.value; if (l) l.fill = (e.target as HTMLInputElement).value; } +function onRot(e: Event) { const l = sel.value; if (l) l.rot = Number((e.target as HTMLInputElement).value); } // ---------- 选中 / 拖动 ---------- function onDown(e: PointerEvent, l: L) { @@ -224,9 +229,25 @@ async function genBg() { } // ---------- 缩放 ---------- +// 宽高双向适配:画布始终完整落在舞台内,舞台自身不出滚动条 function fit() { - const w = stageEl.value?.clientWidth || 600; - scale.value = Math.min(Math.max(w / POSTER_W, 0.18), 0.85); + measure(); // 舞台尺寸一变就顺带复测根高度(仅值变化才写,稳定后自收敛) + const el = stageEl.value; + const w = Math.max((el?.clientWidth || 600) - 20, 120); + const h = Math.max((el?.clientHeight || 760) - 20, 160); + scale.value = Math.min(Math.max(Math.min(w / POSTER_W, h / POSTER_H), 0.1), 0.85); +} + +// 测量所在滚动容器:根高度 = 容器可视高 - 上方占用,页面级永不出现滚动条 +function measure() { + const el = pmRoot.value; + if (!el) return; + // 必须取最内层滚动容器:外层是套壳,高度口径不同,用外层算出来的值会错 + const sc = el.closest('.n-layout-scroll-container') as HTMLElement | null; + if (!sc) return; + const above = el.getBoundingClientRect().top - sc.getBoundingClientRect().top + sc.scrollTop; + const h = Math.floor(sc.clientHeight - above - 18); + if (h >= 160 && h !== pmH.value) pmH.value = h; } // ---------- 导出 PNG ---------- @@ -264,9 +285,16 @@ function loadImg(src: string): Promise { i.src = src; }); } +// 绕 (cx, cy) 旋转画布坐标系:预览走 CSS transform、导出走 canvas,中心一致才不跑位 +function rotAround(ctx: CanvasRenderingContext2D, cx: number, cy: number, deg: number) { + ctx.translate(cx, cy); + ctx.rotate((deg * Math.PI) / 180); + ctx.translate(-cx, -cy); +} async function drawLayer(ctx: CanvasRenderingContext2D, l: L) { if (l.type === 'shape') { ctx.save(); + if (l.rot) rotAround(ctx, l.x + l.w / 2, l.y + l.h / 2, l.rot); ctx.globalAlpha = l.opacity ?? 1; ctx.fillStyle = l.fill; if (l.shape === 'ellipse') { @@ -282,6 +310,7 @@ async function drawLayer(ctx: CanvasRenderingContext2D, l: L) { } if (l.type === 'image') { ctx.save(); + if (l.rot) rotAround(ctx, l.x + l.w / 2, l.y + l.h / 2, l.rot); rrPath(ctx, l.x, l.y, l.w, l.h, l.radius ?? 0); ctx.clip(); const img = l.src ? await loadImg(l.src) : null; @@ -310,6 +339,8 @@ async function drawLayer(ctx: CanvasRenderingContext2D, l: L) { const asc = (m as any).fontBoundingBoxAscent ?? l.size * 0.8; const desc = (m as any).fontBoundingBoxDescent ?? l.size * 0.2; const lines = linesOf(l); + ctx.save(); + if (l.rot) rotAround(ctx, l.x + l.w / 2, l.y + (lines.length * lh) / 2, l.rot); ctx.textAlign = l.align === 'center' ? 'center' : l.align === 'right' ? 'right' : 'left'; const tx = l.align === 'center' ? l.x + l.w / 2 : l.align === 'right' ? l.x + l.w : l.x; for (let i = 0; i < lines.length; i++) { @@ -321,7 +352,7 @@ async function drawLayer(ctx: CanvasRenderingContext2D, l: L) { } ctx.fillText(lines[i], tx, y); } - ctx.shadowColor = 'transparent'; ctx.shadowBlur = 0; ctx.shadowOffsetY = 0; + ctx.restore(); } async function exportPng() { exporting.value = true; pmError.value = ''; @@ -346,10 +377,19 @@ async function exportPng() { } onMounted(async () => { + selectTemplate(POSTER_TEMPLATES[0]); + measure(); fit(); ro = new ResizeObserver(fit); if (stageEl.value) ro.observe(stageEl.value); - selectTemplate(POSTER_TEMPLATES[0]); + const sc = pmRoot.value?.closest('.n-layout-scroll-container'); + if (sc) { scRO = new ResizeObserver(measure); scRO.observe(sc); } + window.addEventListener('resize', measure); + window.addEventListener('focus', measure); + document.addEventListener('visibilitychange', measure); + // 头部字体/公告条加载完高度会微变,延迟复测两次 + setTimeout(measure, 400); + setTimeout(measure, 1500); try { const res = await fetch('/relay/ai/image-models', { headers: licenseHeaders('poster-maker') }); const j = await res.json().catch(() => ({}) as any); @@ -359,11 +399,18 @@ onMounted(async () => { } } catch { /* 模型列表拉取失败时用默认空值 */ } }); -onBeforeUnmount(() => { ro?.disconnect(); onUp(); }); +onBeforeUnmount(() => { + ro?.disconnect(); + scRO?.disconnect(); + window.removeEventListener('resize', measure); + window.removeEventListener('focus', measure); + document.removeEventListener('visibilitychange', measure); + onUp(); +});