zoukankan      html  css  js  c++  java
  • vue水印-第一种方法

    1、common文件夹下新建 watermark.js:

    /**  水印添加方法  */
    
    let setWatermark = (str1, str2) => {
      let id = '1.23452384164.123412415'
    
      if (document.getElementById(id) !== null) {
        document.body.removeChild(document.getElementById(id))
      }
    
      let can = document.createElement('canvas')
      // 设置canvas画布大小
      can.width = 150
      can.height = 80
    
      let cans = can.getContext('2d')
      cans.rotate((-20 * Math.PI) / 180) // 水印旋转角度
      cans.font = '15px Vedana'
      cans.fillStyle = '#666666'
      cans.textAlign = 'center'
      cans.textBaseline = 'Middle'
      cans.fillText(str1, can.width / 2, can.height) // 水印在画布的位置x,y轴
      cans.fillText(str2, can.width / 2, can.height + 22)
    
      let div = document.createElement('div')
      div.id = id
      div.style.pointerEvents = 'none'
      div.style.top = '40px'
      div.style.left = '0px'
      div.style.opacity = '0.15'
      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
    }
    
    // 添加水印方法
    export const setWaterMark = (str1, str2) => {
      let id = setWatermark(str1, str2)
      if (document.getElementById(id) === null) {
        id = setWatermark(str1, str2)
      }
    }
    
    // 移除水印方法
    export const removeWatermark = () => {
      let id = '1.23452384164.123412415'
      if (document.getElementById(id) !== null) {
        document.body.removeChild(document.getElementById(id))
      }
    }
    View Code

    2、组件中引入和使用

    import { removeWatermark, setWaterMark } from '@common/watermark'
      mounted() {
        setWaterMark('我是你爸爸', '1234')
      },
      destroyed() {
        removeWatermark()
      }

    效果图:

  • 相关阅读:
    Linux下解析域名命令-dig 命令使用详解
    重写、覆盖、重载、多态几个概念的区别分析
    介绍python中运算符优先级
    介绍Python中6个序列的内置类型
    Mysql(Mariadb)数据库主从复制
    winscp中使用sudo的方法
    git push跳过用户名和密码认证配置教程
    案例:通过shell脚本实现mysql数据备份与清理
    毕业季,我的Linux求职之路
    PHP和ajax详解
  • 原文地址:https://www.cnblogs.com/wuqilang/p/14846355.html
Copyright © 2011-2022 走看看