当前位置:首页 > VUE

vue实现水印

2026-01-07 20:21:46VUE

Vue 实现水印的方法

使用 CSS 背景图

通过 CSS 的 background-imagebackground-repeat 属性实现水印效果。创建一个包含水印文本的 Canvas,将其转换为 base64 图片,设置为背景。

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

<script>
export default {
  mounted() {
    this.generateWatermark();
  },
  methods: {
    generateWatermark() {
      const canvas = document.createElement('canvas');
      canvas.width = 200;
      canvas.height = 150;
      const ctx = canvas.getContext('2d');
      ctx.font = '16px Arial';
      ctx.fillStyle = 'rgba(200, 200, 200, 0.5)';
      ctx.rotate(-0.2);
      ctx.fillText('Watermark', 10, 80);
      const watermarkUrl = canvas.toDataURL('image/png');

      const container = document.querySelector('.watermark-container');
      container.style.backgroundImage = `url(${watermarkUrl})`;
      container.style.backgroundRepeat = 'repeat';
    }
  }
};
</script>

<style>
.watermark-container {
  width: 100%;
  height: 100vh;
}
</style>

使用自定义指令

通过 Vue 的自定义指令实现动态水印,适用于需要动态更新水印内容的场景。

<template>
  <div v-watermark="watermarkText"></div>
</template>

<script>
export default {
  data() {
    return {
      watermarkText: 'Confidential'
    };
  },
  directives: {
    watermark: {
      inserted(el, binding) {
        const canvas = document.createElement('canvas');
        canvas.width = 200;
        canvas.height = 150;
        const ctx = canvas.getContext('2d');
        ctx.font = '16px Arial';
        ctx.fillStyle = 'rgba(100, 100, 100, 0.2)';
        ctx.rotate(-0.2);
        ctx.fillText(binding.value, 10, 80);
        const watermarkUrl = canvas.toDataURL('image/png');

        el.style.backgroundImage = `url(${watermarkUrl})`;
        el.style.backgroundRepeat = 'repeat';
      }
    }
  }
};
</script>

使用第三方库

可以借助第三方库如 vue-watermark 快速实现水印功能。

安装库:

npm install vue-watermark

使用示例:

<template>
  <vue-watermark :text="watermarkText" :opacity="0.3" :width="200" :height="150">
    <div>Your content here</div>
  </vue-watermark>
</template>

<script>
import VueWatermark from 'vue-watermark';

export default {
  components: { VueWatermark },
  data() {
    return {
      watermarkText: 'Sample Watermark'
    };
  }
};
</script>

动态更新水印

如果需要动态更新水印内容,可以通过监听数据变化重新生成水印。

vue实现水印

<template>
  <div ref="watermarkEl"></div>
  <button @click="updateWatermark">Update Watermark</button>
</template>

<script>
export default {
  data() {
    return {
      watermarkText: 'Initial Watermark'
    };
  },
  mounted() {
    this.applyWatermark();
  },
  methods: {
    applyWatermark() {
      const canvas = document.createElement('canvas');
      canvas.width = 200;
      canvas.height = 150;
      const ctx = canvas.getContext('2d');
      ctx.font = '16px Arial';
      ctx.fillStyle = 'rgba(150, 150, 150, 0.3)';
      ctx.rotate(-0.2);
      ctx.fillText(this.watermarkText, 10, 80);
      const watermarkUrl = canvas.toDataURL('image/png');

      this.$refs.watermarkEl.style.backgroundImage = `url(${watermarkUrl})`;
      this.$refs.watermarkEl.style.backgroundRepeat = 'repeat';
    },
    updateWatermark() {
      this.watermarkText = 'Updated Watermark';
      this.applyWatermark();
    }
  }
};
</script>

标签: 水印vue
分享给朋友:

相关文章

vue实现sql操作

vue实现sql操作

在Vue中实现SQL操作通常需要结合后端服务或本地数据库技术,因为前端JavaScript无法直接连接SQL数据库。以下是几种常见的实现方式: 通过API与后端交互 Vue前端通过HTTP请求(如a…

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListen…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…

vue实现答辩

vue实现答辩

Vue 实现答辩系统的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装 Vue Router 管理路由,Pinia/Vuex 进行状态管理。配置 ESLint 和 Pre…

vue实现反转

vue实现反转

实现数组反转 在Vue中反转数组可以通过多种方式实现,以下是几种常见方法: 使用JavaScript原生reverse方法 // 在methods中定义方法 methods: { revers…