当前位置:首页 > VUE

vue实现水印组件

2026-03-08 07:31:33VUE

Vue 水印组件实现方法

基础实现

创建一个名为Watermark.vue的组件文件,使用Canvas绘制水印:

vue实现水印组件

<template>
  <div class="watermark-container" ref="container"></div>
</template>

<script>
export default {
  props: {
    text: {
      type: String,
      default: 'Watermark'
    },
    fontSize: {
      type: Number,
      default: 16
    },
    color: {
      type: String,
      default: 'rgba(0, 0, 0, 0.1)'
    },
    angle: {
      type: Number,
      default: -20
    }
  },
  mounted() {
    this.drawWatermark()
  },
  methods: {
    drawWatermark() {
      const canvas = document.createElement('canvas')
      const ctx = canvas.getContext('2d')
      const width = 200
      const height = 100

      canvas.width = width
      canvas.height = height

      ctx.font = `${this.fontSize}px Arial`
      ctx.fillStyle = this.color
      ctx.textAlign = 'center'
      ctx.textBaseline = 'middle'
      ctx.translate(width / 2, height / 2)
      ctx.rotate(this.angle * Math.PI / 180)
      ctx.fillText(this.text, 0, 0)

      const dataURL = canvas.toDataURL('image/png')
      this.$refs.container.style.backgroundImage = `url(${dataURL})`
    }
  }
}
</script>

<style>
.watermark-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 9999;
  background-repeat: repeat;
}
</style>

动态更新水印

添加响应式更新功能,当props变化时重新绘制水印:

vue实现水印组件

watch: {
  text() {
    this.drawWatermark()
  },
  fontSize() {
    this.drawWatermark()
  },
  color() {
    this.drawWatermark()
  },
  angle() {
    this.drawWatermark()
  }
}

防止水印被删除

添加MutationObserver防止DOM被篡改:

mounted() {
  this.drawWatermark()
  this.initMutationObserver()
},
methods: {
  initMutationObserver() {
    const observer = new MutationObserver((mutations) => {
      mutations.forEach((mutation) => {
        if (mutation.removedNodes.length > 0) {
          mutation.removedNodes.forEach((node) => {
            if (node === this.$el) {
              document.body.appendChild(this.$el)
            }
          })
        }
      })
    })

    observer.observe(document.body, {
      childList: true,
      subtree: true
    })
  }
}

使用示例

在父组件中使用水印组件:

<template>
  <div>
    <Watermark 
      text="Confidential" 
      :fontSize="20"
      color="rgba(0, 0, 0, 0.15)"
      :angle="-15"
    />
    <!-- 页面其他内容 -->
  </div>
</template>

<script>
import Watermark from './Watermark.vue'

export default {
  components: {
    Watermark
  }
}
</script>

优化建议

  1. 对于大量重复水印,可以考虑使用CSS repeating-linear-gradient替代Canvas绘制
  2. 添加水印密度控制参数,调整水印之间的间距
  3. 考虑支持多行文本水印
  4. 添加水印位置控制,可以固定在页面四角或中心
  5. 对于动态内容页面,考虑使用IntersectionObserver检测可视区域变化

标签: 水印组件
分享给朋友:

相关文章

vue实现广告组件

vue实现广告组件

vue实现广告组件的方法 使用动态组件实现 在Vue中可以通过动态组件的方式加载广告组件,根据不同的条件展示不同的广告内容。动态组件使用<component :is="currentCompon…

vue table组件实现

vue table组件实现

Vue 表格组件实现方案 基础表格实现 使用<table>标签结合v-for指令渲染数据是最简单的实现方式: <template> <table> &…

vue实现search组件

vue实现search组件

Vue 实现搜索组件 基础实现 创建一个基础的搜索组件需要包含输入框和搜索逻辑。以下是一个简单的实现示例: <template> <div class="search-cont…

vue 滑杆组件实现

vue 滑杆组件实现

实现基础滑杆组件 使用Vue的v-model和原生<input type="range">实现基础功能: <template> <div> <i…

vue实现布局组件

vue实现布局组件

Vue 实现布局组件的方法 基础布局组件结构 使用 Vue 的单文件组件(SFC)创建一个基础布局组件。布局组件通常包含头部、侧边栏、主体内容和页脚。 <template> <…

vue实现图片组件

vue实现图片组件

Vue 实现图片组件的方法 基础图片组件实现 创建一个基础的图片组件,支持动态传入图片路径和替代文本。在Vue项目中新建一个ImageComponent.vue文件: <template>…