async function drawCanvas() { const mainFile = document.getElementById('mainImg').files[0]; const bgFile = document.getElementById('bgImg').files[0]; if (!mainFile) return alert('请上传主图'); // 加载图片 const main = await loadImage(URL.createObjectURL(mainFile)); const titleText = document.getElementById('title').value; const descText = document.getElementById('desc').value; const descLines = descText.split('\n'); // --- 动态高度计算 --- const padding = 15; // 边缘空隙 const imgW = 1080 - (padding * 2); const imgH = (main.height * imgW) / main.width; // 按比例缩放图片高度 // 预留:标题(60px) + 文字间距(20px) + 描述(每行30px) + 底部信息(150px) const contentH = 60 + 20 + (descLines.length * 30) + 150; const totalH = padding + imgH + 20 + contentH + padding; canvas.width = 1080; canvas.height = totalH; // --- 绘图 --- ctx.fillStyle = "#ffffff"; ctx.fillRect(0, 0, canvas.width, canvas.height); // 绘制背景图 if (bgFile) { const bg = await loadImage(URL.createObjectURL(bgFile)); ctx.drawImage(bg, 0, 0, 1080, totalH); } // 绘制主图 (居中,左右空隙15px) ctx.drawImage(main, padding, padding, imgW, imgH); // 绘制标题 (紧凑排列) const textTop = padding + imgH + 30; // 标题离图片只有30px ctx.fillStyle = "#1e293b"; ctx.font = "bold 50px sans-serif"; ctx.fillText(titleText, padding, textTop); // 绘制描述 ctx.font = "28px sans-serif"; descLines.forEach((line, i) => { ctx.fillText(line, padding, textTop + 50 + (i * 35)); }); // 底部右对齐信息 const qr = await loadImage('https://album.880200.xyz/image/qrcode_fengcblog.png'); const bottomY = totalH - 140; if(qr) ctx.drawImage(qr, 1080 - 125, bottomY - 10, 110, 110); ctx.textAlign = "right"; ctx.font = "24px sans-serif"; ctx.fillText("Shared from fengc's Blog", 1080 - 145, bottomY + 30); ctx.fillText("https://fengcblog.880200.xyz", 1080 - 145, bottomY + 70); }