在vue3项目中给页面添加水印的实现方法
作者:代码搬运媛
这篇文章主要给大家介绍一下在vue3项目中添加水印的实现方法,文中有详细的代码示例供大家参考,具有一定的参考价值,感兴趣的小伙伴跟着小编一起来看看吧
实现原理
通过在 canvas 上绘制出字体,设置好样式,然后以图片的样式导出,最后用图片作为水印层的背景图。
实现效果
全屏实现
实现方法
(1)在src下创建一个util文件夹,创建一个watermark.ts文件
export const getmark = () => { const setWatermark = (str: any) => { const id = "1.23452384164.123412416"; \ if (document.getElementById(id) !== null) { document.body.removeChild(document.getElementById(id)!); } \ //创建一个画布 const can = document.createElement("canvas"); //设置画布的长宽 can.width = 150; can.height = 120; \ const cans = can.getContext("2d")!; //旋转角度 cans.rotate((-15 * Math.PI) / 180); cans.font = "18px Vedana"; //设置填充绘画的颜色、渐变或者模式 cans.fillStyle = "rgba(200, 200, 200, 0.40)"; //设置文本内容的当前对齐方式 cans.textAlign = "left"; //设置在绘制文本时使用的当前文本基线 // cans.textBaseline = "Middle"; //在画布上绘制填色的文本(输出的文本,开始绘制文本的X坐标位置,开始绘制文本的Y坐标位置) cans.fillText(str, can.width / 8, can.height / 2); \ const div = document.createElement("div"); div.id = id; div.style.pointerEvents = "none"; div.style.top = "30px"; 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; }; \ // 该方法只允许调用一次 const watermark = (str: string) => { let id = setWatermark(str); setInterval(() => { if (document.getElementById(id) === null) { id = setWatermark(str); } }, 500); window.onresize = () => { setWatermark(str); }; }; return { watermark }; };
(2)设置水印:在需要水印的页面添加如下代码
<script setup lang="ts"> import {onMounted, onUnmounted, ref} from 'vue'; import { getmark } from "./util/watermark"; const { watermark } = getmark(); onMounted(() => { watermark("你的名字");//水印名 }); onUnmounted(() => { watermark(""); }); </script>
vue2 的使用方法基本相同,只需换为对应的生命周期即可。下面把相应代码也贴出来,供大家参考。
(1)项目中新建一个 watermark.js 文件
let watermark = {}; let setWatermark = (text, sourceBody) => { let id = Math.random() * 10000 + "-" + Math.random() * 10000 + "/" + Math.random() * 10000; if (document.getElementById(id) !== null) { document.body.removeChild(document.getElementById(id)); } let can = document.createElement("canvas"); can.width = 120; //设置水印之间的左右间距 can.height = 80; //设置水印之间的上下间距 let cans = can.getContext("2d"); cans.rotate((-20 * Math.PI) / 180); cans.font = "15px Vedana"; cans.fillStyle = "rgba(0, 0, 0, .5)"; cans.textAlign = "left"; cans.textBaseline = "Middle"; cans.fillText(text, can.width / 20, can.height); let water_div = document.createElement("div"); water_div.id = id; water_div.className = "watermarkClass"; water_div.style.pointerEvents = "none"; water_div.style.background = "url(" + can.toDataURL("image/png") + ") left top repeat"; if (sourceBody) { water_div.style.width = "100%"; water_div.style.height = "100%"; sourceBody.appendChild(water_div); } else { water_div.style.top = "3px"; water_div.style.left = "0px"; water_div.style.position = "fixed"; water_div.style.zIndex = "100000"; water_div.style.width = document.documentElement.clientWidth + "px"; water_div.style.height = document.documentElement.clientHeight + "px"; document.body.appendChild(water_div); } return id; }; /** * 该方法只允许调用一次 * @param: * @text == 水印内容 * @sourceBody == 水印添加的位置,不传就是body * */ watermark.set = (text, sourceBody) => { const domArr = Array.from(document.getElementsByClassName("watermarkClass")); for (let i = 0; i < domArr.length; i++) { const element = domArr[i]; // element.parentNode.removeChild(element); element.remove(); } if (text == "") { return; } setWatermark(text, sourceBody); // let id = setWatermark(text, sourceBody); // setInterval(() => { // if (document.getElementById(id) === null) { // id = setWatermark(text, sourceBody); // } // }, 2000); // window.onresize = () => { // setWatermark(text, sourceBody); // }; }; export default watermark;
(2)main.js 中引入 watermark.js
// 引入水印文件地址 import watermark from './util/watermark.js' Vue.prototype.$watermark = watermark
(3)全屏水印
<template> <div class="page"> </div> </template> <script> export default { data() { return {}; }, mounted(){ this.$watermark.set("水印") }, beforeDestroy() { this.$watermark.set(""); } }; </script> <style scoped> .page{ width: 100%; height: 100%; background-color: skyblue; } </style>
局部水印:
<template> <div class="page"> <div ref="content" style="width: 500px;height: 500px;border: 2px solid red;"></div> </div> </template> <script> export default { data() { return {}; }, mounted(){ this.$watermark.set("水印",this.$refs.content) }, beforeDestroy() { this.$watermark.set("",this.$refs.content); } }; </script> <style scoped> .page{ width: 100%; height: 100%; background-color: skyblue; } </style>
到此这篇关于在vue3项目中添加水印的实现方法的文章就介绍到这了,更多相关vue3添加水印内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!