lightweight-watermark

EN ZH

Lightweight Watermark

A lightweight, dependency-free JavaScript ES6 class for adding persistent text watermarks to any webpage.

The watermark is drawn using <canvas> and applied as a repeating background, with automatic restoration if removed from the DOM.

✨ Features

📦 Installation

use npm

npm install lw-watermark

🚀Usage

Basic Example

import Watermark from 'lw-watermark';

const wm = new Watermark({
  content: 'lightweight watermark',
  font: 'Arial',
  fontSize: 24,
  fontWeight: 'bold',
  rotate: 30,
  color: 'rgba(255, 0, 0, 0.15)',
  zIndex: 999999999
});

wm.create()
  .then(() => console.log('Watermark added!'))
  .catch(err => console.error('Watermark error:', err));

Options

Option Type Default Description
content string "lw-watermark" Text to display in the watermark
font string "serif" CSS font family
fontSize number | string 30 Font size in pixels
fontWeight string | number "normal" CSS font-weight (e.g., normal, bold, 300, 700)
rotate number 30 Rotation angle in degrees
color string (rgba) "rgba(0, 0, 0, 0.6)" Watermark color
zIndex number 999999999 CSS z-index
maxLength number 30 Max allowed length of content text

Methods

Error Handling

The class throws custom ErrorMessage instances with both code and message.

Code Message
1011 The maximum length of content is exceeded
1021 The content should be of string type
1022 The font-size should be of string or number type
1023 The font-weight should be of string or number type
1031 The font-size is invalid
1032 The font-weight is invalid
1033 The color is invalid, you should use rgba(R, G, B, alpha) format

Advantages over other solutions

License

MIT

Tip: This watermark is a visual deterrent. It won’t stop determined users from copying text or screenshots, but it’s great for discouraging casual sharing and maintaining brand presence.