此方法开箱即用,在vue项目中import即可。
例如:
//在vue组件中
import Watermark from '@/utils/watermark.js'
//在methods中
Watermark.set({color:"",text:""})//设置水印
Watermark.remove() //删除水印
const watermark = {}
const id = '1.23452384164.123412415'
const setWatermark = (option) => {
if (document.getElementById(id) !== null) {
document.body.removeChild(document.getElementById(id))
}
const can = document.createElement('canvas')
can.width = 350
can.height = 250
const cans = can.getContext('2d')
cans.rotate(-20 * Math.PI / 180)
cans.font = '15px Vedana'
cans.fillStyle = option.color||'rgba(200, 200, 200, 0.30)'
cans.textAlign = 'left'
cans.textBaseline = 'Middle'
cans.fillText(option.text, 1, can.height)
const div = document.createElement('div')
div.id = id
div.style.pointerEvents = 'none'
div.style.top = '0'
div.style.left = '0'
div.style.position = 'fixed'
div