导出图片&添加水印
<div>
<a-button @click="toPic">导出</a-button>
<div id="canvasToPic">要导出的内容区域</div>
</div>
import html2canvas from "html2canvas"
toPic() {
window.scrollTo(0,0);
this.$nextTick(() => {
html2canvas(document.getElementById('canvasToPic'), {
scale: 1,
height: document.getElementById('canvasToPic').scrollHeight,
windowHeight: document.getElementById('canvasToPic').scrollHeight,
backgroundColor: null,
}).then(canvas => {
const watermark = document.createElement("canvas");
watermark.width = 300;
watermark.height = 150;
const wmCtx = watermark.getContext("2d");
wmCtx.lineWidth = "1";
wmCtx.font = "bold 20px Arial";
wmCtx.textBaseline = "middle";
wmCtx.fillStyle = "rgba(0 , 255 , 255 , 0.5)";
wmCtx.fillText("== 水印 ==", 20, 50);
wmCtx.drawImage(watermark, canvas.width - watermark.width, canvas.height - watermark.height);
const ctx = canvas.getContext("2d");
ctx.fillStyle = ctx.createPattern(watermark, 'repeat');
ctx.fillRect(0, 0, canvas.width, canvas.height);
this.url= canvas.toDataURL("image/png");
const link= document.createElement("a");
link.href = this.url;
link.download = "页面导出";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
});
},