首页 > 其他 > 详细

vue水印-第二种方法:通过指令

时间:2021-06-03 23:44:36      阅读:35      评论:0      收藏:0      [点我收藏+]

1、utils文件夹下新建 directives.js:

技术分享图片
import Vue from ‘vue‘

Vue.directive(‘watermark‘, (el, binding) => {
  function addWaterMarker(str, parentNode, font, textColor) {
    // 水印文字,父元素,字体,文字颜色
    var can = document.createElement(‘canvas‘)
    parentNode.appendChild(can)
    can.width = 400
    can.height = 200
    can.style.display = ‘none‘
    var cans = can.getContext(‘2d‘)
    cans.rotate((-20 * Math.PI) / 180)
    cans.font = font || ‘16px Microsoft JhengHei‘
    cans.fillStyle = textColor || ‘rgba(180, 180, 180, 0.3)‘
    cans.textAlign = ‘left‘
    cans.textBaseline = ‘Middle‘
    cans.fillText(str, can.width / 3, can.height / 2)
    parentNode.style.backgroundImage = ‘url(‘ + can.toDataURL(‘image/png‘) + ‘)‘
  }
  addWaterMarker(
    binding.value.text,
    el,
    binding.value.font,
    binding.value.textColor
  )
})
View Code

2、main.js中引入

import  ‘@utils/directives‘

3、在需要使用水印的标签上加上指令

  <div class="box" v-watermark="{text:‘hello~‘,textColor:‘red‘}">
    123
  </div>

效果图:

技术分享图片

 

vue水印-第二种方法:通过指令

原文:https://www.cnblogs.com/wuqilang/p/14846374.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!