使用Cavans实现图片水印的绘制,具体步骤参考如下代码示例:
<template>
<div>
<img ref="image" src="https://example.com/image.jpg" alt="Original Image">
</div>
</template>
<script>
export default {
mounted() {
this.addWatermark();
},
methods: {
addWatermark() {
const image = this.$refs.image;
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = image.width;
canvas.height = image.height;
ctx.drawImage(image, 0, 0, image.width, image.height);
const watermarkText = '过路云野';
const fontSize = 20;
const textWidth = ctx.measureText(watermarkText).width;
const textHeight = fontSize;
const rows = Math.ceil(image.height / (textHeight * 2));
const cols = Math.ceil(image.width / (textWidth * 2));
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const x = j * (textWidth * 2);
const y = i * (textHeight * 2);
ctx.font = `${fontSize}px Arial`;
ctx.fillStyle = 'rgba(255, 255, 255, 0.5)';
ctx.fillText(watermarkText, x, y);
}
}
image.src = canvas.toDataURL('image/png');
}
}
}
</script>