Skip to content

Commit 92ca197

Browse files
fix: 修复水印展示不全 (#385)
1 parent caf9378 commit 92ca197

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

packages/core/plugin/WaterMarkPlugin.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,28 @@ class WaterMarkPlugin {
118118
ctx = null;
119119
},
120120
[POSITION.full]: (width: number, height: number, cb: (imgString: string) => void) => {
121+
const angle = -30; // 按逆时针30度算
122+
const R = (angle * Math.PI) / 180;
123+
const font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
121124
let waterCanvas: HTMLCanvasElement | null = this.createCanvas(width, height);
122125
let ctx: CanvasRenderingContext2D | null = waterCanvas.getContext('2d')!;
126+
ctx.font = font;
123127
const textW = ctx.measureText(this.drawOps.text).width + 40;
124128
let patternCanvas: HTMLCanvasElement | null = this.createCanvas(
125-
this.drawOps.isRotate ? textW * 2 : textW, // 若有倾斜,那么斜边都会大于直角边 按30度算2倍(简单)
126-
this.drawOps.isRotate ? textW + 20 : this.drawOps.size + 20
129+
this.drawOps.isRotate ? textW * Math.abs(Math.cos(R)) + this.drawOps.size : textW,
130+
this.drawOps.isRotate
131+
? textW * Math.abs(Math.sin(R)) + this.drawOps.size
132+
: this.drawOps.size + 20
127133
);
128134
document.body.appendChild(patternCanvas);
129135
let ctxWater: CanvasRenderingContext2D | null = patternCanvas.getContext('2d')!;
130136
ctxWater.textAlign = 'left';
131137
ctxWater.textBaseline = 'top';
132-
ctxWater.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
138+
ctxWater.font = font;
133139
ctxWater.fillStyle = `${this.drawOps.color}`;
134140
if (this.drawOps.isRotate) {
135-
ctxWater.translate(0, textW - 10);
136-
ctxWater.rotate((-30 * Math.PI) / 180); // 简单例子 按30度算
141+
ctxWater.translate(0, textW * Math.abs(Math.sin(R)));
142+
ctxWater.rotate(R);
137143
ctxWater.fillText(this.drawOps.text, 0, 0);
138144
} else {
139145
ctxWater.fillText(this.drawOps.text, 10, 10);

0 commit comments

Comments
 (0)