|
| 1 | +import React, { useEffect, useMemo, useState } from 'react'; |
| 2 | + |
| 3 | +interface WatermarkProps { |
| 4 | + text: string; |
| 5 | + fontSize?: number; |
| 6 | + fontFamily?: string; |
| 7 | + fontWeight?: string; |
| 8 | + textColor?: string; |
| 9 | + rotate?: number; |
| 10 | + zIndex?: number; |
| 11 | + theme?: 'light' | 'dark'; |
| 12 | + density?: number; |
| 13 | +} |
| 14 | + |
| 15 | +const Watermark: React.FC<WatermarkProps> = ({ |
| 16 | + text, |
| 17 | + fontSize = 20, |
| 18 | + fontFamily = 'Arial', |
| 19 | + textColor = 'rgba(0, 0, 0, 0.1)', |
| 20 | + rotate = -45, |
| 21 | + zIndex = 9999, |
| 22 | + theme = 'light', |
| 23 | + fontWeight = 'bold', |
| 24 | + density = 1, |
| 25 | +}) => { |
| 26 | + const [dimensions, setDimensions] = useState({ width: window.innerWidth, height: window.innerHeight }); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + const handleResize = () => { |
| 30 | + setDimensions({ |
| 31 | + width: window.innerWidth, |
| 32 | + height: window.innerHeight, |
| 33 | + }); |
| 34 | + }; |
| 35 | + |
| 36 | + window.addEventListener('resize', handleResize); |
| 37 | + return () => window.removeEventListener('resize', handleResize); |
| 38 | + }, []); |
| 39 | + |
| 40 | + const watermarkDataUrl = useMemo(() => { |
| 41 | + const canvas = document.createElement('canvas'); |
| 42 | + const context = canvas.getContext('2d'); |
| 43 | + |
| 44 | + if (context) { |
| 45 | + const scale = window.devicePixelRatio; |
| 46 | + canvas.width = dimensions.width * scale; |
| 47 | + canvas.height = dimensions.height * scale; |
| 48 | + context.scale(scale, scale); |
| 49 | + |
| 50 | + context.font = `${fontWeight} ${fontSize}px ${fontFamily}`; |
| 51 | + context.fillStyle = theme === 'light' ? textColor : 'rgba(255, 255, 255, 0.1)'; |
| 52 | + context.rotate((rotate * Math.PI) / 180); |
| 53 | + context.fillText(text, 0, 100); |
| 54 | + |
| 55 | + const metrics = context.measureText(text); |
| 56 | + const textWidth = metrics.width; |
| 57 | + const textHeight = fontSize; |
| 58 | + |
| 59 | + const spacingX = (textWidth + 180) / density; |
| 60 | + const spacingY = (textHeight + 180) / density; |
| 61 | + |
| 62 | + for (let y = -canvas.height; y < canvas.height * 2; y += spacingY) { |
| 63 | + for (let x = -canvas.width; x < canvas.width * 2; x += spacingX) { |
| 64 | + context.fillText(text, x, y); |
| 65 | + } |
| 66 | + } |
| 67 | + return canvas.toDataURL(); |
| 68 | + } |
| 69 | + return ''; |
| 70 | + }, [density, dimensions.height, dimensions.width, fontFamily, fontSize, fontWeight, rotate, text, textColor, theme]); |
| 71 | + |
| 72 | + return ( |
| 73 | + <div |
| 74 | + style={{ |
| 75 | + position: 'fixed', |
| 76 | + top: 0, |
| 77 | + left: 0, |
| 78 | + width: '100%', |
| 79 | + height: '100%', |
| 80 | + zIndex, |
| 81 | + pointerEvents: 'none', |
| 82 | + backgroundImage: `url(${watermarkDataUrl})`, |
| 83 | + backgroundRepeat: 'repeat', |
| 84 | + }} |
| 85 | + /> |
| 86 | + ); |
| 87 | +}; |
| 88 | + |
| 89 | +export default Watermark; |
0 commit comments