基于vue3 分装一个添加水印的自定义指令 v-watermark
1、封装添加水印hook
const globalCanvas = null
const globalWaterMark = null
// watermark 样式
let style = `
display: block;
overflow: hidden;
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-repeat: repeat;
pointer-events: none;`
const getDataUrl = ({ font, fillStyle, textAlign, textBaseline, text, rotate = -20 }) => {
font = font || '16px normal'
fillStyle = fillStyle || 'rgba(180, 180, 180, 0.2)'
text = text || ''
const canvas = globalCanvas || document.createElement('canvas')
const ctx = canvas.getContext('2d') // 获取画布上下文
ctx.rotate((rotate * Math.PI) / 180)
ctx.font = font
ctx.fillStyle = fillStyle
ctx.textAlign = textAlign || 'left'
ctx.textBaseline = textBaseline || 'middle'
ctx.fillText(text, c