当前位置:首页 > 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 中实现组件循环通常使用 v-for 指令,可以循环渲染数组或对象的数据到组件上。 基本数组循环 通过 v-for 指令遍历数组,渲染多个相同结构的组件。 &…

vue实现组件循环图片

vue实现组件循环图片

Vue 实现组件循环图片的方法 在 Vue 中实现组件循环图片通常可以通过 v-for 指令结合动态数据绑定完成。以下是几种常见的实现方式: 使用静态图片路径数组 假设有一组本地图片路径,可以通过数…

vue组件传值实现分页

vue组件传值实现分页

Vue组件传值实现分页的方法 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方式: 使用props传递数据 父组件通过props向子组件传递当前页码和总页数等分页信息:…

react如何删除组件

react如何删除组件

删除 React 组件的几种方法 条件渲染法 通过状态控制组件是否渲染。当状态为 false 时,组件会被移除: const [showComponent, setShowComponent…

vue实现递归组件

vue实现递归组件

递归组件的实现方法 在Vue中实现递归组件主要依靠组件调用自身的能力。以下是几种常见的实现方式: 使用组件name属性 通过组件的name属性实现递归调用是最简单的方式: <template…

vue抽屉组件实现

vue抽屉组件实现

Vue 抽屉组件实现 使用 Element UI 实现 Element UI 提供了现成的抽屉组件 el-drawer,可以快速实现抽屉效果。 安装 Element UI: npm install…