后台管理系统加水印

首先创建一个waterMark.js文件

let watermark = {};

let setWatermark = (line1, line2, line3) => {

  let id = "1.23452384164.123412416";

  if (document.getElementById(id) !== null) {

    document.body.removeChild(document.getElementById(id));

  }

  //创建一个画布

  let can = document.createElement("canvas");

  //设置画布的长宽

  can.width = 400;

  can.height = 280;

  let cans = can.getContext("2d");

  //旋转角度

  cans.rotate((-15 * Math.PI) / 150);

  cans.font = "18px Vedana";

  //设置填充绘画的颜色、渐变或者模式

  cans.fillStyle = "rgba(200, 200, 200, 0.50)";

  //设置文本内容的当前对齐方式

  cans.textAlign = "left";

  //设置在绘制文本时使用的当前文本基线

  cans.textBaseline = "middle";

  const textWidth1 = cans.measureText(line1).width;

  const textWidth2 = cans.measureText(line2).width;

  const textWidth3 = cans.measureText(line3).width;

  const maxWidth = Math.max(textWidth1, textWidth2, textWidth3);

  if (maxWidth <= can.width) {

    cans.fillText(line1, (can.width - textWidth1) / 2, can.height / 2 - 20);

    cans.fillText(line2, (can.width - textWidth2) / 2, can.height / 2);

    cans.fillText(line3, (can.width - textWidth3) / 2, can.height / 2 + 20);

  } else {

    const scale = can.width / maxWidth;

    cans.scale(scale, scale);

    cans.fillText(

      line1,

      (can.width / scale - textWidth1) / 2,

      can.height / 2 / scale - 20 / scale

    );

    cans.fillText(

      line2,

      (can.width / scale - textWidth2) / 2,

      can.height / 2 / scale

    );

    cans.fillText(

      line3,

      (can.width / scale - textWidth3) / 2,

      can.height / 2 / scale + 20 / scale

    );

  }

  let div = document.createElement("div");

  div.id = id;

  div.style.pointerEvents = "none";

  div.style.top = "120px";

  div.style.left = "0px";

  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;

};

// 该方法只允许调用一次

watermark.set = (line1, line2, line3) => {

  let id = setWatermark(line1, line2, line3);

  setInterval(() => {

    if (document.getElementById(id) === null) {

      id = setWatermark(line1, line2, line3);

    }

  }, 500);

  window.onresize = () => {

    setWatermark(line1, line2, line3);

  };

};

const outWatermark = (id) => {

  if (document.getElementById(id) !== null) {

    const div = document.getElementById(id);

    div.style.display = "none";

  }

};

watermark.out = () => {

  const str = "1.23452384164.123412416";

  outWatermark(str);

};

export default watermark;

 这段代码是添加三行水印,可以根据自己的需求来改写

在main.js中引入

// //水印

import warterMark from "@/utils/waterMark";

router.afterEach((item) => {

  //warterMark.set("水印")

  if (item.name !== "login") {

    //判断是否为登录页面

    //warterMark.set('我是水印-张三')

  } else {

    warterMark.out(); // 清除水印

  }

});

 然后在需要的地方通过

  warterMark.set(

                "xxxxx",

                `用户名:${this.$store.state.user.name}`,

                this.gettiem()

            );  调用

        warterMark.out(); // 清除水印

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 CSS3 的伪元素 ::after 来实现页面水印效果,同时使用 JavaScript 获取系统时间来显示水印内容。以下是一个示例代码: HTML: ```html <div id="watermark"> <div class="watermark-content"> <span>系统时间:</span> <span id="system-time"></span> </div> </div> ``` CSS: ```css #watermark { position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: -1; pointer-events: none; } .watermark-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 16px; color: rgba(0,0,0,0.2); } .watermark-content::after { content: ''; position: absolute; top: 0; left: 0; right: 0; bottom: 0; z-index: -1; background-image: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"%3E%3Ctext x="50%" y="50%" dy="0.4em" text-anchor="middle" font-size="35"%3EWatermark%3C/text%3E%3C/svg%3E'); background-repeat: repeat; opacity: 0.2; } ``` JavaScript: ```javascript function updateSystemTime() { const now = new Date(); const year = now.getFullYear(); const month = now.getMonth() + 1; const date = now.getDate(); const hour = now.getHours(); const minute = now.getMinutes(); const second = now.getSeconds(); const systemTime = `${year}-${month}-${date} ${hour}:${minute}:${second}`; document.getElementById('system-time').textContent = systemTime; } setInterval(updateSystemTime, 1000); ``` 你可以将以上代码保存为一个 vue 文件,然后在需要显示水印的页面中引入即可。注意,以上代码只是一个简单的示例,你需要根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值