一、整个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>
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐