vue 使用class创建和清除水印的示例代码


Posted in Vue.js onDecember 25, 2020

页面添加水印的方法有很多,以下举例使用class定义的方法进行水印内容渲染:

1、新建文件:WatermarkClass.js

export default class WatermarkClass {
  constructor({id="watermarkID", str = "", fontSize = 18, width = 400, height = 400, fillStyle="#333333", opacity = 1 }) {
    this.id = id;
    this.str = str;
    this.fontSize = fontSize;
    this.width = width;
    this.height = height;
    this.fillStyle = fillStyle
    this.opacity = opacity;

  }

  // 绘制水印
  draw() {
   if (document.getElementById(this.id) !== null) {
    document.body.removeChild(document.getElementById(this.id));
   }

   const canvas = document.createElement("canvas");
   // 设置canvas画布大小
   canvas.width = this.width;
   canvas.height = this.height;

   const ctx = canvas.getContext("2d");
   ctx.rotate(-(15 * Math.PI) / 180); // 水印旋转角度
   ctx.font = `${this.fontSize}px Vedana`;
   ctx.fillStyle = this.fillStyle;
   ctx.textAlign = "center";
   ctx.textBaseline = "middle";
   this.str.split(",").forEach((item, index) => {
    ctx.fillText(item, canvas.width / 2, canvas.height / 2 + (index * this.fontSize + 10)); // 水印在画布的位置x,y轴
   });

   const div = document.createElement("div");
   div.id = this.id;
   div.style.pointerEvents = "none";
   div.style.top = "30px";
   div.style.left = "10px";
   div.style.opacity = this.opacity;
   div.style.position = "fixed";
   div.style.zIndex = "999999";
   div.style.width = `${document.documentElement.clientWidth}px`;
   div.style.height = `${document.documentElement.clientHeight}px`;
   div.style.background = `url(${canvas.toDataURL("image/png")}) left top repeat`;
   document.body.appendChild(div);
  }

  setOptions({fontSize = 18, width = 300, height = 300, opacity = 1, str = ""}) {
   this.fontSize = fontSize;
   this.width = width;
   this.height = height;
   this.fillStyle = fillStyle
   this.opacity = opacity;
   this.str = str;
   this.draw();
  }

  // 绘制
  render() {
   this.draw();
   window.onresize = () => {
    this.draw();
   };
  }

  // 移除水印
  removeWatermark() {
   if (document.getElementById(this.id) !== null) {
    document.body.removeChild(document.getElementById(this.id));
   }
  }
 }

2、在页面种引入使用:

import watermarkClass from "@/libs/watermarkClass";
export default {
 name: "App",
 mounted: function () {
  this.initWatermark()
 },
 methods: {
  initWatermark() {
   // 方法一
   let watermark = new watermarkClass({
    id: "watermarkID",
    str: "紫藤萝-watermarkClass",
    fontSize: 20,
    width: 300,
    height: 200,
    fillStyle: "#dddddd",
    opacity: 0.4,
   });
   watermark.render();
   // 5秒后,清除水印
   setTimeout(() => {
    watermark.removeWatermark();
   }, 5000);
  }
 },
};

以上就是vue 使用class创建和清除水印的示例代码的详细内容,更多关于vue 创建和清除水印的资料请关注三水点靠木其它相关文章!

Vue.js 相关文章推荐
Vue如何循环提取对象数组中的值
Nov 18 Vue.js
vuex Module将 store 分割成模块的操作
Dec 07 Vue.js
vue实现购物车的小练习
Dec 21 Vue.js
Vue组件简易模拟实现购物车
Dec 21 Vue.js
vue实现登录、注册、退出、跳转等功能
Dec 23 Vue.js
vue3中轻松实现switch功能组件的全过程
Jan 07 Vue.js
基于vuex实现购物车功能
Jan 10 Vue.js
Vue + iView实现Excel上传功能的完整代码
Jun 22 Vue.js
vue项目中的支付功能实现(微信支付和支付宝支付)
Feb 18 Vue.js
vue3引入highlight.js进行代码高亮的方法实例
Apr 08 Vue.js
vue实力踩坑之push当前页无效
Apr 10 Vue.js
Vue操作Storage本地化存储
Apr 29 Vue.js
基于vue+echarts数据可视化大屏展示的实现
Dec 25 #Vue.js
vue3使用vue-count-to组件的实现
Dec 25 #Vue.js
vue+openlayers绘制省市边界线
Dec 24 #Vue.js
vue项目中openlayers绘制行政区划
Dec 24 #Vue.js
Vue+penlayers实现多边形绘制及展示
Dec 24 #Vue.js
Vue使用鼠标在Canvas上绘制矩形
Dec 24 #Vue.js
vue绑定class的三种方法
Dec 24 #Vue.js
You might like
Laravel5.1 框架控制器基础用法实例分析
2020/01/04 PHP
jQuery对象[0]是什么含义?
2010/07/31 Javascript
IE6/7/8/9不支持exec的简写方式
2011/05/25 Javascript
一个可拖拽列宽表格实例演示
2012/11/26 Javascript
JS实现图片翻书效果示例代码
2013/09/09 Javascript
jquery实现标签上移、下移、置顶
2015/04/26 Javascript
javascript实现数组内值索引随机化及创建随机数组的方法
2015/08/10 Javascript
IE7浏览器窗口大小改变事件执行多次bug及IE6/IE7/IE8下resize问题
2015/08/21 Javascript
JS功能代码集锦
2016/05/04 Javascript
node.js路径处理方法以及绝对路径详解
2021/03/04 Javascript
angularjs 表单密码验证自定义指令实现代码
2016/10/27 Javascript
基于JS实现checkbox全选功能实例代码
2016/10/31 Javascript
原生js实现鼠标跟随效果
2017/02/28 Javascript
JS判断Android、iOS或浏览器的多种方法(四种方法)
2017/06/29 Javascript
JavaScript创建对象的常用方式总结
2018/08/10 Javascript
nodejs通过钉钉群机器人推送消息的实现代码
2019/05/05 NodeJs
Vue打包部署到Nginx时,css样式不生效的解决方式
2020/08/03 Javascript
解决vue-router路由拦截造成死循环问题
2020/08/05 Javascript
Js Snowflake(雪花算法)生成随机ID的实现方法
2020/08/26 Javascript
[54:54]Newbee vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
用python分割TXT文件成4K的TXT文件
2009/05/23 Python
Mac OS X10.9安装的Python2.7升级Python3.3步骤详解
2013/12/04 Python
Python中os和shutil模块实用方法集锦
2014/05/13 Python
Python使用xlrd读取Excel格式文件的方法
2015/03/10 Python
python监控linux内存并写入mongodb(推荐)
2017/09/11 Python
django项目中使用手机号登录的实例代码
2019/08/15 Python
Tensorflow进行多维矩阵的拆分与拼接实例
2020/02/07 Python
简单总结CSS3中视窗单位Viewport的常见用法
2016/02/04 HTML / CSS
Penhaligon’s英国官网:成立于1870年的英国香水制造商
2021/02/18 全球购物
Can a struct inherit from another struct? (结构体能继承结构体吗)
2016/09/25 面试题
董事长秘书工作职责
2014/06/10 职场文书
消防宣传口号
2014/06/16 职场文书
最美家庭活动方案
2014/08/31 职场文书
2015年党员岗位承诺书
2015/04/27 职场文书
中学推普周活动总结
2015/05/07 职场文书
springboot中rabbitmq实现消息可靠性机制详解
2021/09/25 Java/Android