局部加水印

在公共类js下,新建一个watermask.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();
    }
}

在需要的页面引入

<template>
//绑定样式
    <el-table :data="dataList" border :style="{backgroundImage:`url(${orgBackground})`}">
</template>

<script>
//引入js
import watermark from "@/tools/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>

页面全部加水印

同样在公共类js里新建一个watermark.js,代码如下

/**  水印添加方法  */
 
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 = 150
  can.height = 80
 
  let cans = can.getContext('2d')
  cans.rotate(-20 * Math.PI / 180) // 水印旋转角度
  cans.font = '15px 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))
  }
}

然后在页面中引入

import { removeWatermark, setWaterMark } from '@/common/watermark'
mounted() {
    setWaterMark('hahaha', '哈哈哈');
},
destroyed() {
    removeWatermark();
},

以上两种方式均已尝试,可以使用,具体选那种方式根据项目需求

转载链接:局部加水印,https://blog.csdn.net/qq_44775782/article/details/107681734

单页面加水印:https://www.jqhtml.com/62620.html

Logo

前往低代码交流专区

更多推荐