我报名参与金石计划1期挑战——瓜分10万奖池,这是我的第2篇文章,点击检查活动概况

完成原理

经过在 canvas 上制作出字体,设置好款式,然后以图片的款式导出,最后用图片作为水印层的背景图。

完成作用

全屏完成

vue3 项目增加水印的完成办法

完成办法

(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>