当前位置:首页 > VUE

vue实现水印组件

2026-01-08 17:02:23VUE

Vue 水印组件实现

基础水印组件实现

创建一个基础水印组件,通过 props 接收文本、颜色、大小等参数:

<template>
  <div class="watermark-container">
    <slot></slot>
    <div class="watermark" :style="watermarkStyle"></div>
  </div>
</template>

<script>
export default {
  props: {
    text: {
      type: String,
      default: 'Watermark'
    },
    color: {
      type: String,
      default: 'rgba(0, 0, 0, 0.1)'
    },
    fontSize: {
      type: Number,
      default: 16
    },
    rotate: {
      type: Number,
      default: -30
    }
  },
  computed: {
    watermarkStyle() {
      return {
        backgroundImage: `url("${this.generateWatermark()}")`,
        position: 'absolute',
        top: 0,
        left: 0,
        width: '100%',
        height: '100%',
        pointerEvents: 'none',
        zIndex: 9999
      }
    }
  },
  methods: {
    generateWatermark() {
      const canvas = document.createElement('canvas')
      canvas.width = 200
      canvas.height = 150
      const ctx = canvas.getContext('2d')

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

      return canvas.toDataURL()
    }
  }
}
</script>

<style scoped>
.watermark-container {
  position: relative;
}
</style>

防止水印被删除

添加MutationObserver监听DOM变化,防止水印被删除:

mounted() {
  this.observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      mutation.removedNodes.forEach((node) => {
        if (node === this.$el.querySelector('.watermark')) {
          this.$el.appendChild(this.createWatermark())
        }
      })
    })
  })

  this.observer.observe(this.$el, {
    childList: true
  })
},
beforeDestroy() {
  this.observer.disconnect()
},
methods: {
  createWatermark() {
    const watermark = document.createElement('div')
    watermark.className = 'watermark'
    watermark.style = this.watermarkStyle
    return watermark
  }
}

多行水印实现

修改generateWatermark方法支持多行水印:

generateWatermark() {
  const canvas = document.createElement('canvas')
  canvas.width = 400
  canvas.height = 300
  const ctx = canvas.getContext('2d')

  ctx.font = `${this.fontSize}px Arial`
  ctx.fillStyle = this.color
  ctx.textAlign = 'center'

  for (let i = 0; i < 10; i++) {
    for (let j = 0; j < 10; j++) {
      ctx.save()
      ctx.translate(i * 150, j * 100)
      ctx.rotate((Math.PI / 180) * this.rotate)
      ctx.fillText(this.text, 0, 0)
      ctx.restore()
    }
  }

  return canvas.toDataURL()
}

使用示例

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

<template>
  <Watermark text="Confidential" color="rgba(0, 0, 0, 0.2)" :fontSize="20">
    <div class="content">
      <!-- 你的内容 -->
    </div>
  </Watermark>
</template>

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

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

动态更新水印

添加watch监听props变化,动态更新水印:

watch: {
  text() {
    this.updateWatermark()
  },
  color() {
    this.updateWatermark()
  },
  fontSize() {
    this.updateWatermark()
  },
  rotate() {
    this.updateWatermark()
  }
},
methods: {
  updateWatermark() {
    const watermark = this.$el.querySelector('.watermark')
    if (watermark) {
      watermark.style.backgroundImage = `url("${this.generateWatermark()}")`
    }
  }
}

vue实现水印组件

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

相关文章

vue实现组件循环图片

vue实现组件循环图片

Vue 中实现组件循环图片的方法 在 Vue 中,可以通过 v-for 指令循环渲染图片组件。假设有一组图片数据需要循环展示,可以采用以下方式: 数据准备 在组件的 data 或 props 中定…

vue组件传值实现分页

vue组件传值实现分页

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

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式,…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…

vue实现组件跟随

vue实现组件跟随

实现组件跟随的常见方法 使用CSS定位 通过CSS的position: fixed或position: absolute属性实现组件跟随。需要结合JavaScript计算目标元素的位置。 &…

vue实现拖拽实现组件嵌套

vue实现拖拽实现组件嵌套

Vue 拖拽实现组件嵌套 使用 Vue 实现拖拽组件嵌套功能可以通过原生 HTML5 拖拽 API 或第三方库如 vuedraggable 实现。以下是两种方法的详细说明: 使用 HTML5 拖拽…