vue用js 添加水印

// 定义水印函数
const addWatermark = ({
   
                        container = document.body, // 水印添加到的容器,默认为 body
                        width = "300px", // 水印 canvas 的宽度
                        height = "150px", // 水印 canvas 的高度
                        textAlign = "center", // 水印文字的对齐方式
                        textBaseline = "middle", // 水印文字的基线
                        font = "16px Microsoft Yahei", // 水印文字的字体
                        fillStyle = "rgba(184, 184, 184, 0.3)", // 水印文字的填充样式
                        content = "", // 水印文字的内容
                        rotate = -30, // 水印文字的旋转角度
                        zIndex = 10000, // 水印的 z-index 值
                      }) => {
   
  // 生成水印 canvas
  const canvas = document.createElement("canvas");
  canvas.setAttribute("width", width);
  canvas.setAttribute("height", height);
  const ctx = canvas.getContext("2d");
  ctx.textAlign = textAlign;
  ctx.textBaseline = textBaseline;
  ctx.font = font;
  ctx.fillStyle = fillStyle;
  console.log((Math.PI / 180) * rotate,container.clientWidth)
  ctx.rotate((Math.PI / 180) * rotate);
  ctx.fillText(content, parseFloat(width) / 2, parseFloat(height) / 1);

  // 将 canvas 转换为 base64 URL
  const base64Url = canvas.toDataURL("image/png");
  console.log(base64Url);
  const __wm = document.querySelector(".__wm");
  const watermarkDiv = __wm || document.createElement("div");
  const styleStr = `
              position: absolute;
              top: 0;
              left: 0;
              bottom: 0;
              right: 0;
              width: ${
     container.clientWidth}px;
              height: ${
     container.clientHeight}px;
              z-index: ${
     zIndex};
              pointer-events: none;
              background: url('${
     base64Url}') left top repeat;
          `;
  watermarkDiv.setAttribute("style", styleStr);
  watermarkDiv.classList.add("__wm");
  // 则创建一个 div 并设置样式和类名

  if (!__wm) {
   
    container.style.position = "relative";
    container.insertBefore(watermarkDiv, container.firstChild);
  }
  // 监听容器变化,当容器发生变化时重新调用 addWatermark 函数
  const {
    MutationObserver } = window;
  if (MutationObserver) {
   
    let mo = new MutationObserver(function () {
   
      const __wm = document.querySelector(".__wm");
      // 只在__wm元素变动才重新调用__canvasWM
      if ((__wm && __wm.getAttribute("style") !== styleStr) || !__wm) {
   
        // 避免一直触发
        mo.disconnect();
        mo = new MutationObserver(() => {
   });

        addWatermark({
   
          container:document.body, // 水印添加到的容器,默认为 body
          width: "200px",
          height: "100px",
          textAlign: "center",
          textBaseline: "middle",
          font: "16px Microsoft Yahei",
          fillStyle: "rgba(184, 184, 184, 0.3 )",
          content,
          rotate: -30,
          zIndex: 10000,
        });
      }
    });

    mo.observe(container, {
   
      attributes: true,
      subtree: true,
      childList: true,
    });
  }
};

export default addWatermark;

相关推荐

  1. vuejs 添加水印

    2024-02-21 22:56:03       47 阅读
  2. Python 音频添加水印

    2024-02-21 22:56:03       46 阅读
  3. SpringBoot实现PDF添加水印

    2024-02-21 22:56:03       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-21 22:56:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-21 22:56:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-21 22:56:03       82 阅读
  4. Python语言-面向对象

    2024-02-21 22:56:03       91 阅读

热门阅读

  1. 2月20日,每日信息差

    2024-02-21 22:56:03       51 阅读
  2. 记录C#导出数据慢的优化方法

    2024-02-21 22:56:03       41 阅读
  3. Docker Compose 安装 MinIO 并设置用户名和密码

    2024-02-21 22:56:03       55 阅读
  4. 2024洞见丨暴雨信息总裁孙辉:未来进行时

    2024-02-21 22:56:03       47 阅读
  5. go build

    2024-02-21 22:56:03       54 阅读