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(); +});