美文网首页
自定义vue水印指令

自定义vue水印指令

作者: 已不淘气很多年 | 来源:发表于2023-12-24 15:14 被阅读0次

水印在项目开发中经常会使用到,所以自己尝试写了一个vue版的自定义水印指令,包含功能:
1 自定义水印显示的文字
2 显示当前时间(可以设置是否显示时间时刻变化)
3 自定义位置
4 自定义角度
5 自定义透明度
6 自定义大小(宽度和高度)

直接上代码:

1 新建自定义指令文件,编写代码

watermark.js

import dayjs from 'dayjs'; // 时间格式化,需要自行下载,也可以使用其他代替,并且修改下方时间转换方法。
export default {
  install (Vue, Opt) {
    Vue.directive('watermark', (el, binding) => {
      let timer = null
      const addWaterMarker = ({text, time, width, height, rotate, positionX, positionY, opacity, isInterval}, el) => {
        const canvas = document.createElement('canvas')
        canvas.width = width || 400
        canvas.height = height || 200

        const canvasCon = canvas.getContext('2d')
        canvasCon.rotate(rotate || (-26 * Math.PI) / 180) // 旋转弧度
        canvasCon.font = '14px Microsoft JhengHei' // 字体
        canvasCon.fillStyle = `rgba(220, 220, 220, ${opacity || 0.45})` // 字体填充颜色
        canvasCon.textAlign = 'center' // 对齐方式
        canvasCon.textBaseline = 'Middle' // 基线
        canvasCon.fillText(text, positionX || canvas.width / 3, positionY || 180) // 被填充的文本

        const currentTime = isInterval ? Date.now() : time
        if (currentTime) canvasCon.fillText(dayjs(currentTime).format('YYYY-MM-DD HH:mm:ss'), positionX ||     canvas.width / 3, (positionY + 20) || 200)

        // 被填充的文本, pointeer-events为了解决水印遮盖按钮没法点击
        const watermarkCss =
          ` pointer-events: none;
            position: absolute;
            width: 100%;
            height: 100%;
            left: 0;
            top: 0;
            z-index: 9;
            background: url(${canvas.toDataURL('image/png')});
          `

        const watermarkEle = document.createElement('div')
        watermarkEle.setAttribute('id', 'watermark')
        watermarkEle.style.cssText = watermarkCss
        el.style.position = 'relative'

        if (el.querySelector('#watermark')) {
          el.querySelector('#watermark').remove()
        }
        el.append(watermarkEle)
        if (!isInterval) {
          clearInterval(timer)
        }
      }

      const { text, time, width, height, rotate, positionX, positionY, opacity, isInterval } = binding.value
      const isHasWatermark = !!el.querySelector('#watermark')
      if (!isHasWatermark) {
        addWaterMarker({text, time, width, height, rotate, positionX, positionY, opacity, isInterval}, el)
        if (!isInterval) return
        timer = setInterval(() => {
          addWaterMarker({text, time, width, height, rotate, positionX, positionY, opacity, isInterval}, el)
        }, 1000)
      }
    })
  }
}

2 新建一个index.js文件,作为全局集中处理自定义指令

index.js

import watermark from './watermark';  // 可以修改对应的路径
export default {
  install (Vue, Opt) {
    Vue.use(watermark);
    // 下方可以继续添加更多的指令
  }
}

3 main.js文件中引入index.js文件

import customDirectives from './customDirectives/index';
Vue.use(customDirectives)

4 水印指令使用

template

<div v-watermark='watermark'></div>

js

computed: {
    watermark () {
      return {
        text: '水印测试/这是一个水印测试',
        time: Date.now(),
        isInterval: true
      }
    }
  },

说明:上面是我自己定义的水印过程,下方贴出文件结构

 customDirectives // 文件夹
    index.js             // 文件,集中引入指令
    watermark.js     //水印指令

相关文章

  • season2-全局API

    第1节:Vue.directive 自定义指令 Vue.directive自定义指令 自定义的指令:changec...

  • Vue div节点滚动事件-加载更多

    使用Vue.directive注册全局指令 绑定事件 对于Vue自定义指令不明白的同学请移步: Vue自定义指令 ...

  • VUE-2:自定义指令、事件

    directive自定义指令 我们还可以通过`Vue`提供的directive方法来自定义指令 注册指令 `vue...

  • vue入门6---vue基本指令、自定义指令、插件

    一、vue常用指令概览 二、vue自定义指令 注册全局指令Vue.directive('my-directive'...

  • vue自定义指令初探

    vue自定义指令初探 一、什么是自定义指令 自定义指令是用来操作DOM的。尽管Vue推崇数据驱动视图的理念,但并非...

  • vue 有自定义指令

    vue 的自定义指令,分为全局自定义指令和局部自定义指令,局部自定义指令等价于局部组件。 自定义指令可以对DOM进...

  • Vue指令钩子函数

    Vue指令上篇文章有讲过了,现在分析Vue指令钩子函数。 自定义指令 全局定义:Vue.directive( ' ...

  • vue自定义指令

    除了内置的指令外,Vue 也允许注册自定义指令。 vue用Vue.directive(id,definition)...

  • vue知识集锦(三)

    自定义指令 除了核心功能默认内置的指令 (v-model和v-show),Vue 也允许注册自定义指令。尽管Vue...

  • Vue基础(五)--自定义指令与过渡

    1.自定义指令 分类:全局指令、局部指令 1.1 自定义全局指令 使用全局方法 Vue.directive(指令I...

网友评论

      本文标题:自定义vue水印指令

      本文链接:https://www.haomeiwen.com/subject/irlhndtx.html