From 3fed213c30e8d1fd2691709db3c9153a15b64171 Mon Sep 17 00:00:00 2001 From: zzc <1761997216@qq.com> Date: Sat, 31 Jan 2026 11:56:39 +0800 Subject: [PATCH] fix: make page --- pages/make/index.vue | 63 +++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/pages/make/index.vue b/pages/make/index.vue index e7d6e65..68d7834 100644 --- a/pages/make/index.vue +++ b/pages/make/index.vue @@ -9,14 +9,17 @@ /> 年禧集.马年春节祝福 - + + + 祝福语宽度 + + 署名位置 (左右) { ctx.fillRect(0, 0, W, H); // 3️⃣ 标题 - ctx.fillStyle = "#ffffff"; - ctx.font = "42px sans-serif"; // 默认字体 - ctx.textAlign = "center"; - ctx.textBaseline = "alphabetic"; // Canvas 2D 默认是 alphabetic - ctx.fillText("新春快乐", W / 2, 120); + // ctx.fillStyle = "#ffffff"; + // ctx.font = "42px sans-serif"; // 默认字体 + // ctx.textAlign = "center"; + // ctx.textBaseline = "alphabetic"; // Canvas 2D 默认是 alphabetic + // ctx.fillText("新春快乐", W / 2, 120); - ctx.font = "22px sans-serif"; - ctx.globalAlpha = 0.9; - ctx.fillText("2026 YEAR OF THE HORSE", W / 2, 165); - ctx.globalAlpha = 1; + // ctx.font = "22px sans-serif"; + // ctx.globalAlpha = 0.9; + // ctx.fillText("2026 YEAR OF THE HORSE", W / 2, 165); + // ctx.globalAlpha = 1; // 4️⃣ 祝福语气泡 drawBubbleText(ctx, { text: targetName.value + "\n " + blessingText.value.content, - x: 70, + x: 70, y: 260 + bubbleOffsetY.value, - maxWidth: 400, + maxWidth: bubbleMaxWidth.value, // 使用动态宽度 + canvasWidth: W, // 传入画布宽度以实现自动居中 fontSize: fontSize.value, lineHeight: fontSize.value * 1.5, backgroundColor: "rgba(255,255,255,0.85)", @@ -806,6 +822,7 @@ function drawBubbleText(ctx, options) { textColor = "#fff", fontSize = 32, fontFamily = "PingFang SC", + canvasWidth, // 新增:画布宽度,用于居中计算 } = options; if (!text) return; @@ -835,6 +852,20 @@ function drawBubbleText(ctx, options) { if (line) lines.push(line); }); + // 计算实际最大宽度以支持居中 + let maxLineWidth = 0; + lines.forEach((line) => { + const { width } = ctx.measureText(line); + if (width > maxLineWidth) maxLineWidth = width; + }); + + // 如果提供了 canvasWidth,则自动计算居中的 x 坐标 + let drawX = x; + if (canvasWidth) { + const totalWidth = maxLineWidth + padding * 2; + drawX = (canvasWidth - totalWidth) / 2; + } + // 2️⃣ 计算气泡尺寸 // const bubbleWidth = maxWidth @@ -843,7 +874,7 @@ function drawBubbleText(ctx, options) { // 3️⃣ 绘制气泡(圆角矩形) // drawRoundRect( // ctx, - // x, + // drawX, // y, // bubbleWidth, // bubbleHeight, @@ -855,7 +886,7 @@ function drawBubbleText(ctx, options) { ctx.fillStyle = textColor; lines.forEach((line, index) => { - ctx.fillText(line, x + padding, y + padding + index * lineHeight); + ctx.fillText(line, drawX + padding, y + padding + index * lineHeight); }); }