添加全局水印

This commit is contained in:
qq787203167
2022-02-18 18:02:11 +08:00
parent a2e0091b8b
commit db223cc98c
7 changed files with 125 additions and 40 deletions
+48
View File
@@ -0,0 +1,48 @@
let watermark = {}
let setWatermark = (str) => {
let id = '1.23452384164.123412415'
if (document.getElementById(id) !== null) {
document.body.removeChild(document.getElementById(id))
}
let can = document.createElement('canvas')
can.width = 190
can.height = 120
let cans = can.getContext('2d')
cans.rotate(-30 * Math.PI / 180)
cans.font = '15px Vedana'
cans.fillStyle = '#f4f4f4'
cans.textAlign = 'left'
cans.textBaseline = 'Middle'
cans.fillText(str, can.width / 20, can.height)
let div = document.createElement('div')
div.id = id
div.style.pointerEvents = 'none'
div.style.top = '-30px'
div.style.left = '0px'
div.style.position = 'absolute'
div.style.zIndex = '1'
div.style.width = '100%'
div.style.height = '100%'
div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
document.getElementsByClassName('main')[0].appendChild(div)
return id
}
// 该方法只允许调用一次
watermark.set = (str) => {
let id = setWatermark(str)
setInterval(() => {
if (document.getElementById(id) === null) {
id = setWatermark(str)
}
}, 2000)
window.onresize = () => {
setWatermark(str)
}
}
export default watermark