此方法开箱即用,在vue项目中import即可。文章来源:https://www.toymoban.com/news/detail-836660.html
例如:文章来源地址https://www.toymoban.com/news/detail-836660.html
//在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.style.zIndex = '100000'
div.style.width = document.documentElement.clientWidth - 100 + 'px'
div.style.height = document.documentElement.clientHeight - 100 + 'px'
div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
return div
}
// 该方法只允许调用一次
watermark.set = (option) => {
const div = setWatermark(option)
document.body.appendChild(div)
// setInterval(() => {
// if (document.getElementById(id) === null) {
// id = setWatermark(str)
// }
// }, 500)
window.onresize = () => {
const div = setWatermark(option)
document.body.appendChild(div)
}
}
watermark.remove = () => {
const obj = document.getElementById(id)
if (obj != null) {
obj.parentNode.removeChild(obj)
}
}
export default watermark
到了这里,关于vue系列--通过js生成前端水印的方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!