【进阶】如何创建vue项目的水印,创建水印watermark?

首先,在utils目录下创建一个watermark.js文件
/* eslint-disable prettier/prettier */
let watermark = {};

let id = "set-customer-watermark-id";

let setWatermark = (dom, str) => {
  if (document.getElementById(id) !== null) {
    dom
      ? dom.removeChild(document.getElementById(id))
      : document.body.removeChild(document.getElementById(id));
  }

  // 创建一个画布
  let can = document.createElement("canvas");
  can.width = 200;
  can.height = 200;

  // 设置画布样式
  let cans = can.getContext("2d");
  cans.rotate((-30 * Math.PI) / 180);
  cans.font = "15px Vedana";
  cans.fillStyle = "rgba(0,0,0,0.15)";
  cans.textAlign = "left";
  cans.textBaseline = "Middle";
  cans.fillText(str, 0, can.height);

  // 创建整体容器,设置样式
  let div = document.createElement("div");
  div.id = id;
  div.style.pointerEvents = "none";
  div.style.top = "0px";
  div.style.left = "-100px";
  div.style.position = "absolute";
  div.style.zIndex = "1000";
  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";

  dom ? dom.appendChild(div) : document.body.appendChild(div);
  return id;
};

// 设置水印
watermark.set = (dom, str) => {
  let id = setWatermark(dom, str);
  setInterval(() => {
    if (document.getElementById(id) === null) {
      id = setWatermark(dom, str);
    }
  }, 500);
  window.onresize = () => {
    setWatermark(dom, str);
  };
};

// 清除水印
watermark.remove = () => {
  if (document.getElementById(id) !== null) {
    document.getElementById(id).style.display = "none";
  }
};

export default watermark;

其次,外部这样使用(简单示例):



你可能感兴趣的:(【进阶】如何创建vue项目的水印,创建水印watermark?)