Vue 添加水印

一. 创建工具类

在utills文件中创建水印的工具类,如: watermark.js。

在watermark.js文件中 可以根据设计要求更改自己的水印。一般都是更改水印的大小颜色以及旋转角度。 代码都做了注释,就根据css 写法就行调试。

/**
 * 给元素添加水印
 * @param {*} settings 传入对象
 * @returns 
 */

'use strict'
 
const watermark = {}
 
/**
 *
 * @param {要设置的水印的内容} str
 * @param {需要设置水印的容器} container
 */
const setWatermark = (str, container) => {
  const id = '1.23452384164.123412415'
 
  if (container === undefined) {
    return
  }
 
  // 查看页面上有没有,如果有则删除
  if (document.getElementById(id) !== null) {
    const childelement = document.getElementById(id)
    childelement.parentNode.removeChild(childelement)
  }
 
  var containerWidth = container.offsetWidth // 获取父容器宽
  var containerHeight = container.offsetHeight -150  // 获取父容器高
  
  // container.style.position = 'relative' // 设置布局为相对布局
 
  // 创建canvas元素(先制作一块背景图)
  const can = document.createElement('canvas')
  can.width = 390 // 设置每一块的宽度
  can.height = 200 // 高度
  const cans = can.getContext('2d') // 获取canvas画布
  cans.rotate(-20 * Math.PI / 180) // 逆时针旋转π/9
  cans.font = '20px Vedana' // 设置字体
  cans.fillStyle = 'rgba(200, 200, 200, 0.50)' // 设置字体的颜色
  cans.textAlign = 'left' // 文本对齐方式
  cans.textBaseline = 'Middle' // 文本基线
  cans.fillText(str, 0, 4 * can.height / 5) // 绘制文字
 
  // 创建一个div元素
  const div = document.createElement('div')
  div.id = id // 设置id
  div.style.pointerEvents = 'none' // 取消所有事件
  div.style.top = '0px'
  div.style.left = '0px'
  div.style.position = 'absolute'
  div.style.zIndex = '30000'
  div.style.width = containerWidth + 'px'
  div.style.height = containerHeight + 'px'
  div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
  container.appendChild(div) // 追加到页面
 
  return id
}
 
// 该方法只允许调用一次
watermark.set = (str, container) => {
  let id = setWatermark(str, container)
  // console.log(container.style.position)
  setInterval(() => {
    if (document.getElementById(id) === null) {
      id = setWatermark(str, container)
    }
  }, 500)
  // 监听页面大小的变化
  window.onresize = () => {
    setWatermark(str, container)
  }
}
 
export default watermark

二. 使用水印

可在全局文件中 或者单独文件中使用水印。

2.1 引入水印

在需要加水印的地方引入 我这里是全局使用 在home.vue文件中使用的(这里的home.vue文件是自己项目中配置的主文件除App.vue)

import Watermark from '../../utils/watermark.js'

2.2 使用

<template>
    <div class="conbarcontainer" ref="wrapperRef"></div>
</template>

<script>
    import Watermark from '../../utils/watermark.js';
    export default {
        mounted() {
            this.handleWater();
        },

        methods: {
            // 水印
            handleWater(){
                // Watermark.set( '要设置的水印的内容', '需要设置水印的容器');
                Watermark.set( '2023-12-06', this.$refs.wrapperRef);
            },    
        }
    }
</script>

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值