|
|
@@ -104,25 +104,81 @@ async function drawWatermark(event) {
|
|
|
// }
|
|
|
}
|
|
|
|
|
|
-function drawWatermark2(img,weather) {
|
|
|
- const canvas = canvasRef.value;
|
|
|
- let scale = 3
|
|
|
- canvas.width = img.width * scale;
|
|
|
- canvas.height = img.height * scale;
|
|
|
- ctx = canvas.getContext('2d');
|
|
|
- ctx.scale(scale, scale)
|
|
|
- ctx.drawImage(img, 0, 0, img.width, img.height);
|
|
|
- drawBottom(img.width, img.height, weather)
|
|
|
- // watermark.value = canvas.toDataURL();
|
|
|
+// function drawWatermark2(img,weather) {
|
|
|
+// const canvas = canvasRef.value;
|
|
|
+// let scale = 3
|
|
|
+// canvas.width = img.width * scale;
|
|
|
+// canvas.height = img.height * scale;
|
|
|
+// ctx = canvas.getContext('2d');
|
|
|
+// ctx.scale(scale, scale)
|
|
|
+// ctx.drawImage(img, 0, 0, img.width, img.height);
|
|
|
+// drawBottom(img.width, img.height, weather)
|
|
|
+// // watermark.value = canvas.toDataURL();
|
|
|
+// }
|
|
|
+
|
|
|
+function drawWatermark2(img, weather) {
|
|
|
+ const canvas = canvasRef.value
|
|
|
+
|
|
|
+ // 1️⃣ 固定容器尺寸(关键)
|
|
|
+ const canvasWidth = 750
|
|
|
+ const canvasHeight = 1334
|
|
|
+
|
|
|
+ canvas.width = canvasWidth
|
|
|
+ canvas.height = canvasHeight
|
|
|
+
|
|
|
+ // 2️⃣ CSS 尺寸必须一致(防止二次拉伸)
|
|
|
+ canvas.style.width = canvasWidth + 'px'
|
|
|
+ canvas.style.height = canvasHeight + 'px'
|
|
|
+
|
|
|
+ // 将 ctx 赋值给全局变量,供 drawBottom 使用
|
|
|
+ ctx = canvas.getContext('2d')
|
|
|
+
|
|
|
+ // 3️⃣ 绝对不能再有 ctx.scale
|
|
|
+ // ctx.scale(...) ❌
|
|
|
+
|
|
|
+ // 4️⃣ 等比按宽度缩放
|
|
|
+ const scale = canvasWidth / img.width
|
|
|
+ const drawHeight = img.height * scale
|
|
|
+
|
|
|
+ // 5️⃣ 裁剪(居中)
|
|
|
+ const dy = (canvasHeight - drawHeight) / 2
|
|
|
+
|
|
|
+ ctx.clearRect(0, 0, canvasWidth, canvasHeight)
|
|
|
+ ctx.drawImage(img, 0, dy, canvasWidth, drawHeight)
|
|
|
+
|
|
|
+ // 传入图片实际绘制的尺寸,而不是 canvas 的尺寸
|
|
|
+ // 图片绘制区域:从 (0, dy) 开始,宽度 canvasWidth,高度 drawHeight
|
|
|
+ console.log('canvasWidth',canvasWidth)
|
|
|
+ console.log('drawHeight',drawHeight)
|
|
|
+ console.log('dy',dy)
|
|
|
+ // drawBottom(375, drawHeight, weather, 0, dy)
|
|
|
}
|
|
|
|
|
|
|
|
|
+function drawImageWidth100Cover(ctx, img, canvasWidth, canvasHeight) {
|
|
|
+ // 以宽度为基准缩放
|
|
|
+ const scale = canvasWidth / img.width
|
|
|
+ const drawHeight = img.height * scale
|
|
|
+
|
|
|
+ // 计算裁剪(居中)
|
|
|
+ const dy = (canvasHeight - drawHeight) / 2
|
|
|
+
|
|
|
+ ctx.drawImage(
|
|
|
+ img,
|
|
|
+ 0,
|
|
|
+ dy,
|
|
|
+ canvasWidth,
|
|
|
+ drawHeight
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
// console.log(data)
|
|
|
-const drawBottom = (imgWidth, imgHeight, weather) => {
|
|
|
+const drawBottom = (imgWidth, imgHeight, weather, offsetX = 0, offsetY = 0) => {
|
|
|
// 设置文本样式
|
|
|
ctx.font = "10px Arial";
|
|
|
ctx.textAlign = "left"; // 设置为左对齐
|
|
|
- const imgRect = { x: 0, y: 0, width: imgWidth, height: imgHeight };
|
|
|
+ // imgRect 应该对应图片在 canvas 上的实际绘制区域
|
|
|
+ const imgRect = { x: offsetX, y: offsetY, width: imgWidth, height: imgHeight };
|
|
|
|
|
|
// 绘制底部黑色半透明遮罩
|
|
|
const bottomRect = drawRectInRect(
|