vue项目加水印
vue项目加水印
·
vue项目加水印
一、整个vue工程每个页面都加水印
1、集成步骤
(1)、工具类新建waterMark.js,
代码如下,直接粘贴即可(由canvas画布实现)
/** 水印添加方法 */
let setWatermark = (str1, str2) => {
let id = '1.23452384164.123412415'
if (document.getElementById(id) !== null) {
document.body.removeChild(document.getElementById(id))
}
let can = document.createElement('canvas')
// 设置canvas画布大小
can.width = 400
can.height = 300
let cans = can.getContext('2d')
cans.rotate(-20 * Math.PI / 180) // 水印旋转角度
cans.font = '50px Vedana'
cans.fillStyle = '#666666'
cans.textAlign = 'center'
cans.textBaseline = 'Middle'
cans.fillText(str1, can.width / 2, can.height) // 水印在画布的位置x,y轴
cans.fillText(str2, can.width / 2, can.height + 22)
let div = document.createElement('div')
div.id = id
div.style.pointerEvents = 'none'
div.style.top = '40px'
div.style.left = '0px'
div.style.opacity = '0.15'
div.style.position = 'fixed'
div.style.zIndex = '100000'
div.style.width = document.documentElement.clientWidth + 'px'
div.style.height = document.documentElement.clientHeight + 'px'
div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
document.body.appendChild(div)
return id
}
// 添加水印方法
export const setWaterMark = (str1, str2) => {
let id = setWatermark(str1, str2)
if (document.getElementById(id) === null) {
id = setWatermark(str1, str2)
}
}
// 移除水印方法
export const removeWatermark = () => {
let id = '1.23452384164.123412415'
if (document.getElementById(id) !== null) {
document.body.removeChild(document.getElementById(id))
}
}
(2)、在App.vue页面使用
(2-1)引入方法
import { removeWatermark, setWaterMark } from "./utils/waterMark";
(2-2)在创建方法和销毁方法中调用
mounted() {
setWaterMark("", "自定义水印");
},
destroyed() {
removeWatermark();
},
(3)、效果如下
二、局部单个页面或部分加水印
1、集成步骤
(1)、新建waterMark.js,导出方法,代码如下:
export default {
//tool.js
addWaterMark() {
const strArr = `${localStorage.getItem(
"loginUserName"
)}${localStorage
.getItem("logunUserPhone")
.slice(7, 11)}[${new Date()}]`;
let ctx = document.createElement("canvas");
ctx.width = 250;
ctx.height = 100;
ctx.style.display = "none";
let cans = ctx.getContext("2d");
cans.rotate((-20 * Math.PI) / 180);
cans.font = "12px Microsoft YaHei";
cans.fillStyle = "rgba(17, 17, 17, 0.20)";
cans.textAlign = "left";
cans.textBaseline = "Middle";
cans.fillText(strArr, 0, 100);
cans.save();
return ctx.toDataURL();
}
}
(2)、在需要的页面使用
<template>
//绑定样式
<el-table :data="dataList" border :style="{backgroundImage:`url(${orgBackground})`}">
</template>
<script>
//引入js
import watermark from "./utils/waterMark";
export default {
data() {
return {
orgBackground: "",
};
},
mounted() {
localStorage.setItem("loginUserName", "哈哈哈");
localStorage.setItem("logunUserPhone", "123456");
this.orgBackground = watermark.addWaterMark();
},
};
</script>
<style lang='less' scoped>
</style>
</script>
更多推荐
已为社区贡献4条内容
所有评论(0)