当前位置:首页 > JavaScript

js 实现水印功能

2026-04-07 17:40:07JavaScript

实现水印功能的几种方法

使用Canvas绘制水印

通过Canvas动态生成水印图片,将其作为背景或覆盖层添加到页面上。

function createWatermark(text, options = {}) {
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');
  const fontSize = options.fontSize || 16;
  const fontFamily = options.fontFamily || 'Arial';
  const color = options.color || 'rgba(200, 200, 200, 0.3)';
  const angle = options.angle || -20;
  const spacing = options.spacing || 100;

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

  return canvas.toDataURL();
}

function applyWatermark(containerSelector, text) {
  const container = document.querySelector(containerSelector);
  const watermarkUrl = createWatermark(text);
  container.style.backgroundImage = `url(${watermarkUrl})`;
  container.style.backgroundRepeat = 'repeat';
}

使用CSS伪元素添加水印

通过CSS的::after或::before伪元素实现简单的水印效果。

function addCssWatermark(text, options = {}) {
  const style = document.createElement('style');
  const color = options.color || 'rgba(200, 200, 200, 0.3)';
  const fontSize = options.fontSize || '20px';

  style.textContent = `
    body::after {
      content: "${text}";
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      pointer-events: none;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: ${fontSize};
      color: ${color};
      z-index: 9999;
      transform: rotate(-30deg);
    }
  `;
  document.head.appendChild(style);
}

防止水印被移除的保护措施

增加MutationObserver监听DOM变化,当水印被移除时自动重新添加。

function protectWatermark(watermarkElement) {
  const observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      if (!document.body.contains(watermarkElement)) {
        document.body.appendChild(watermarkElement);
      }
    });
  });

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

// 使用示例
const watermark = document.createElement('div');
watermark.textContent = 'Confidential';
watermark.style.position = 'fixed';
watermark.style.zIndex = '9999';
document.body.appendChild(watermark);
protectWatermark(watermark);

SVG水印实现

使用SVG创建更灵活的水印效果,支持复杂图形和更好的缩放。

js 实现水印功能

function createSvgWatermark(text, options = {}) {
  const svg = `
    <svg xmlns="http://www.w3.org/2000/svg" width="300" height="200">
      <text x="50%" y="50%" 
            font-family="${options.fontFamily || 'Arial'}" 
            font-size="${options.fontSize || '20'}" 
            fill="${options.color || 'rgba(200,200,200,0.3)'}" 
            text-anchor="middle" 
            dominant-baseline="middle"
            transform="rotate(-30, 150, 100)">
        ${text}
      </text>
    </svg>
  `;
  return `data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(svg)))}`;
}

每种方法适用于不同场景,Canvas适合需要动态生成的水印,CSS方法最简单,SVG提供最好的清晰度,保护措施可以防止简单的前端篡改。可以根据实际需求选择合适的方法或组合使用。

标签: 水印功能
分享给朋友:

相关文章

vue 实现 功能

vue 实现 功能

Vue.js 是一个流行的前端框架,用于构建用户界面和单页应用程序。以下是一些常见的 Vue 功能实现方法: 数据绑定 Vue 的核心特性之一是数据绑定,可以通过 v-model 指令实现双向数据绑…

vue实现功能插件

vue实现功能插件

Vue 功能插件实现方法 插件基本结构 Vue 插件通常需要暴露一个 install 方法,该方法接收 Vue 构造函数作为参数。基本结构如下: const MyPlugin = { insta…

php秒杀功能的实现

php秒杀功能的实现

秒杀功能的核心设计 高并发场景下秒杀系统的核心在于解决超卖问题和性能瓶颈。需要结合缓存、队列、锁机制等技术实现。 数据库设计 商品表需包含库存字段,例如: CREATE TABLE `seckil…

vue 实现多选功能

vue 实现多选功能

Vue 实现多选功能的方法 使用 v-model 绑定数组 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选功能。适用于复选框组(checkbox)或下拉多选(select multi…

vue实现插件功能

vue实现插件功能

Vue 插件实现方法 安装与注册插件 在 Vue 项目中,插件通常以独立模块形式存在。通过 Vue.use() 方法注册插件,该方法会自动调用插件的 install 函数。 // 引入插件 imp…

vue实现截图功能

vue实现截图功能

Vue实现截图功能的方法 使用html2canvas库 html2canvas是一个流行的JavaScript库,可以将HTML元素转换为Canvas,进而生成图片。在Vue项目中安装html2can…