当前位置:首页 > JavaScript

js实现水印

2026-03-02 05:58:40JavaScript

使用Canvas绘制水印

在HTML中创建一个Canvas元素,通过JavaScript绘制文本或图片作为水印。Canvas提供了灵活的控制方式,可以调整水印的透明度、旋转角度和位置。

function createWatermark(text) {
  const canvas = document.createElement('canvas');
  canvas.width = 200;
  canvas.height = 100;
  const ctx = canvas.getContext('2d');

  ctx.font = '16px Arial';
  ctx.fillStyle = 'rgba(200, 200, 200, 0.5)';
  ctx.rotate(-0.2);
  ctx.fillText(text, 10, 50);

  return canvas.toDataURL();
}

const watermark = document.createElement('div');
watermark.style.position = 'fixed';
watermark.style.top = '0';
watermark.style.left = '0';
watermark.style.width = '100%';
watermark.style.height = '100%';
watermark.style.pointerEvents = 'none';
watermark.style.backgroundImage = `url(${createWatermark('Confidential')})`;
watermark.style.backgroundRepeat = 'repeat';

document.body.appendChild(watermark);

使用CSS伪元素添加水印

通过CSS的::after或::before伪元素实现简单水印效果,适合不需要复杂样式的水印场景。这种方法性能较好,但自定义程度有限。

const style = document.createElement('style');
style.innerHTML = `
  body::after {
    content: "Watermark Text";
    position: fixed;
    opacity: 0.2;
    font-size: 50px;
    color: #cccccc;
    z-index: 9999;
    transform: rotate(-30deg);
    pointer-events: none;
    left: 30%;
    top: 40%;
  }
`;
document.head.appendChild(style);

SVG背景水印方案

使用SVG创建可缩放的水印图形,特别适合需要高清晰度显示的场景。SVG水印可以完美适应不同屏幕尺寸而不失真。

function createSvgWatermark(text) {
  const svg = `
    <svg xmlns="http://www.w3.org/2000/svg" width="300px" height="200px">
      <text x="50" y="100" 
        font-family="Arial" 
        font-size="20" 
        fill="rgba(200,200,200,0.5)" 
        transform="rotate(-30 50,100)">
        ${text}
      </text>
    </svg>
  `;
  return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
}

const watermarkDiv = document.createElement('div');
watermarkDiv.style.cssText = `
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  background-image: url("${createSvgWatermark('SECRET')}");
  background-repeat: repeat;
  z-index: 9999;
`;
document.body.appendChild(watermarkDiv);

防删除水印实现

为了防止用户通过开发者工具删除水印元素,可以采用MutationObserver监听DOM变化,当水印被移除时自动重新添加。

const watermark = document.createElement('div');
watermark.id = '__watermark__';
// 设置水印样式...

document.body.appendChild(watermark);

const observer = new MutationObserver((mutations) => {
  mutations.forEach((mutation) => {
    if (!document.getElementById('__watermark__')) {
      document.body.appendChild(watermark);
    }
  });
});

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

动态水印方案

结合用户信息生成个性化水印,通常用于内部系统追踪信息泄露来源。水印内容可以包含用户ID、时间戳等唯一标识信息。

js实现水印

function generateDynamicWatermark(userId) {
  const now = new Date();
  const watermarkText = `${userId} ${now.toISOString()}`;

  const pattern = document.createElement('canvas');
  pattern.width = 200;
  pattern.height = 100;
  const pctx = pattern.getContext('2d');

  pctx.font = '12px Arial';
  pctx.fillStyle = 'rgba(100,100,100,0.2)';
  pctx.fillText(watermarkText, 10, 20);

  const div = document.createElement('div');
  div.style.position = 'fixed';
  div.style.top = '0';
  div.style.left = '0';
  div.style.width = '100%';
  div.style.height = '100%';
  div.style.pointerEvents = 'none';
  div.style.backgroundImage = `url(${pattern.toDataURL()})`;
  div.style.backgroundRepeat = 'repeat';

  return div;
}

document.body.appendChild(generateDynamicWatermark('user123'));

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

相关文章

js实现dh

js实现dh

JavaScript 实现 DH(Diffie-Hellman)密钥交换 Diffie-Hellman(DH)密钥交换是一种安全协议,允许双方在不安全的通信信道上建立一个共享密钥。以下是如何在 Jav…

js实现目录

js实现目录

实现目录的基本思路 在JavaScript中实现目录功能通常涉及以下几个核心步骤:解析文档中的标题元素(如h1-h6),动态生成目录结构,并添加交互逻辑(如点击跳转)。以下是具体实现方法: 解析标题…

js实现上传文件

js实现上传文件

文件上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现文件上传功能。 <input type="file"…

js 实现日历

js 实现日历

实现日历的基本思路 日历的核心功能是展示日期,并允许用户进行日期选择或导航。JavaScript 可以动态生成日历的 HTML 结构,并处理用户交互逻辑。 基础日历结构 日历通常包含头部(显示月份和…

js实现点击效果

js实现点击效果

实现点击效果的JavaScript方法 使用addEventListener绑定点击事件 通过document.getElementById或document.querySelector获取DOM元素…

js实现div隐藏

js实现div隐藏

使用CSS的display属性隐藏div 通过设置元素的display属性为none,可以完全隐藏div元素。这种方法不仅隐藏元素,还会从文档流中移除,不占据页面空间。 document.getEl…