• Vue3 实现网页背景水印功能


    经常有一些公司和组织出于系统文件或信息安全保密的需要,需要在系统网页上增加带有个人标识(系统账号或个人信息)的水印,可以简单防止截图外传

    de8d4d32112ed64a0e8b32b92ac4d5df.png

    首先我们来看这样一个水印功能的实现思路,通常是在我们原有的网页上附上一个 DIV 层,将它设置绝对定位铺满整个窗口,然后 z-index 值尽量往大了设,保证让水印层处于当前网页所有元素的上面,又不影响当前网页的操作。

    水印上的字体有两种方式添加:

    • 第一种直接将字体用块元素包裹,动态设置绝对定位,然后通过 transform 属性旋转;

    • 第二种通过在 canvas 上绘制出字体,设置好样式,然后以图片的样式导出,最后用图片作为水印层的背景图。

    出于性能方面考虑,第二种方式最优。我们来看具体怎么实现?

    作为一块独立的功能,我们在 Vue3 中常用 hooks 来实现,通过分析我们概括出实现水印需要的几个功能函数和对外接口:

    对外接口

    • 清除水印(clear)

    • 设置水印(setWatermark)

    核心功能函数

    • 绘制文字背景图(createBase64)

    • 绘制水印层(createWatermark)

    • 页面随窗口大小调整更新(updateWatermark)

    1. export function useWatermark(
    2.   appendEl: Ref = ref(document.body) as Ref
    3. ) {
    4.    // 绘制文字背景图
    5.   function createBase64() {}
    6.   // 绘制水印层
    7.   const createWatermark = () => {};
    8.   // 页面随窗口调整更新水印
    9.   function updateWatermark(){}
    10.   // 对外提供的设置水印方法
    11.   function setWatermark() {}
    12.   // 清除水印
    13.   const clear = () => {};
    14.   return { setWatermark, clear };
    15. }

    有了代码框架,就只需要实现函数和接口的内部实现了,另外还要考虑传参,来实现代码复用的灵活度和接口参数的可配置。

    我们从具体的功能函数开始:

    绘制文字背景图

    这里的参数 str 就是要添加的水印文字,attr 为文字样式的属性,我们定义了属性的类型为 attr,它包含文字的字体和大小以及颜色等值

    1. function createBase64(str: string, attr?: attr) {
    2.     const can = document.createElement("canvas");
    3.     const width = 200;
    4.     const height = 140;
    5.     Object.assign(can, { width, height });
    6.     const cans = can.getContext("2d");
    7.     if (cans) {
    8.       cans.rotate((-20 * Math.PI) / 120);
    9.       cans.font = attr?.font ?? "12px Reggae One";
    10.       cans.fillStyle = attr?.fillStyle ?? "rgba(0, 0, 0, 0.12)";
    11.       cans.textAlign = "left";
    12.       cans.textBaseline = "middle";
    13.       cans.fillText(str, width / 20, height);
    14.     }
    15.     return can.toDataURL("image/png");
    16.   }
    1. type attr = {
    2.   font?: string;
    3.   fillStyle?: string;
    4. };

    绘制水印层

    这个函数的主要逻辑是先判断如果已经绘制了水印层,直接调用更新水印方法,如果还没有,先动态创建一个 DIV 层,设置绝对定位,铺满当前整个浏览器窗口。

    1. const id = domSymbol.toString();
    2. const watermarkEl = shallowRef();
    3. const createWatermark = (str: string, attr?: attr) => {
    4.     if (unref(watermarkEl)) {
    5.       updateWatermark({ str, attr });
    6.       return id;
    7.     }
    8.     const div = document.createElement("div");
    9.     watermarkEl.value = div;
    10.     div.id = id;
    11.     div.style.pointerEvents = "none";
    12.     div.style.top = "0px";
    13.     div.style.left = "0px";
    14.     div.style.position = "absolute";
    15.     div.style.zIndex = "100000";
    16.     const el = unref(appendEl);
    17.     if (!el) return id;
    18.     const { clientHeight: height, clientWidth: width } = el;
    19.     updateWatermark({ str, width, height, attr });
    20.     el.appendChild(div);
    21.     return id;
    22.   };

    更新水印

    因为更新水印方法主要是根据当前窗口高度和宽度来的更新水印背景的设置,利用一张 Base64 格式的图片平铺即可。

    1. function updateWatermark(
    2.     options: {
    3.       width?: number;
    4.       height?: number;
    5.       str?: string;
    6.       attr?: attr;
    7.     } = {}
    8.   ) {
    9.     const el = unref(watermarkEl);
    10.     if (!el) return;
    11.     if (options.width !== "undefined") {
    12.       el.style.width = `${options.width}px`;
    13.     }
    14.     if (ioptions.height !== "undefined") {
    15.       el.style.height = `${options.height}px`;
    16.     }
    17.     if (options.str !== "undefined") {
    18.       el.style.background = `url(${createBase64(
    19.         options.str,
    20.         options.attr
    21.       )}) left top repeat`;
    22.     }
    23.   }

    到此,我们实现了主要的三个功能函数,下面就是两个对外接口:

    设置水印

    这里的主要点是考虑设置页面resize监听,来及时更新水印的位置。还要考虑 Vue 的生命周期,当我们卸载页面的时候要进行清除水印。

    1. function setWatermark(str: string, attr?: attr) {
    2.     createWatermark(str, attr);
    3.     addResizeListener(document.documentElement, func);
    4.     const instance = getCurrentInstance();
    5.     if (instance) {
    6.       onBeforeUnmount(() => {
    7.         clear();
    8.       });
    9.     }
    10.   }
    11.   const func = throttle(function () {
    12.     const el = unref(appendEl);
    13.     if (!el) return;
    14.     const { clientHeight: height, clientWidth: width } = el;
    15.     updateWatermark({ height, width });
    16.   });

    清除水印

    清除水印的时候顺便移除窗口大小监听函数

    1. const clear = () => {
    2.     const domId = unref(watermarkEl);
    3.     watermarkEl.value = undefined;
    4.     const el = unref(appendEl);
    5.     if (!el) return;
    6.     domId && el.removeChild(domId);
    7.     removeResizeListener(el, func);
    8.   };

    水印功能 hooks 的使用

    1. import { useWatermark } from "/@/hooks/watermark";
    2. const { setWatermark, clear } = useWatermark();
    3. onMounted(() => {
    4.   nextTick(() => {
    5.     setWatermark(watermarkText.value);
    6.   });
    7. });
    8. onBeforeUnmount(() => {
    9.   clear();
    10. });

    至此,Vue3 版的网页水印功能实现全部完成。这里水印的字体大小、颜色和排布参考了企业微信的背景水印,使得看起来不那么突兀。

    c23ce578466ab1eba913605f1623aa82.png - END -
  • 相关阅读:
    计数排序(Counting Sort)
    什么是草台班子?
    24v转12v转9v转5v转4.2v降压电源芯片AH8788
    【二分查找】leetcode 1818. 绝对差值和
    Java 集合类的高级特性介绍
    MySQL索引
    vue定时器
    OpenAI乱局幕后大佬浮出水面:Quora联合创始人
    找不到vcruntime140_1.dll,无法继续执行代码怎么办?5个可以解决的方案分享
    Windows 11 上使用安卓应用及安装谷歌 Google Play和亚马逊 应用商店
  • 原文地址:https://blog.csdn.net/u013919171/article/details/126204211