-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpage.html
198 lines (177 loc) · 6.28 KB
/
page.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dynamic Canvas Watermark</title>
<link rel="stylesheet" href="./src/styles.css" />
<style>
.watermarked-content {
position: relative;
width: 100vw;
height: 100vh;
background-color: #f5f5f5;
overflow: hidden;
}
.inner-content {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
z-index: 900;
}
.watermark-canvas {
position: absolute;
top: 0px;
left: 0px;
transform-origin: center center;
}
</style>
</head>
<body>
<nav>
<a href="/" aria-current="page">Home</a>
<a href="/page-1.html">加码</a>
<a href="/page-2.html">解码</a>
<a href="/page-3.html">提取内容</a>
</nav>
<div id="targetElement" class="watermarked-content">
<div class="inner-content">
<span>Your content goes here.</span>
</div>
</div>
<script>
// 创建或重新创建水印 Canvas
function createWatermarkCanvas(
text = 'user name',
spacing = 80,
opacity = 0.1,
angle = -30
) {
const container = document.querySelector('.watermarked-content');
let canvas = document.querySelector('.watermark-canvas');
if (!canvas) {
canvas = document.createElement('canvas');
canvas.className = 'watermark-canvas';
container.appendChild(canvas);
}
const ctx = canvas.getContext('2d');
const rad = angle * (Math.PI / 180);
const fontSize = 20;
// const canvasWidth = container.offsetWidth;
// const canvasHeight = container.offsetHeight;
const width = container.offsetWidth;
const height = container.offsetHeight;
const newWidth =
Math.abs(width * Math.cos(rad)) + Math.abs(height * Math.sin(rad));
const newHeight =
Math.abs(height * Math.cos(rad)) + Math.abs(width * Math.sin(rad));
const canvasWidth = newWidth;
const canvasHeight = newHeight;
const textWidth = ctx.measureText(text).width;
const textHeight = fontSize; // Approximation
canvas.width = canvasWidth;
canvas.height = canvasHeight;
// 调整 canvas 的位置
canvas.style.position = 'absolute';
canvas.style.top = `-${(newHeight - height) / 2}px`;
canvas.style.left = `-${(newWidth - width) / 2}px`;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.font = `${fontSize}px Arial`;
ctx.fillStyle = `rgba(0, 0, 0, ${opacity})`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
// Calculate the number of text repetitions needed for width and height
const numRows = Math.ceil(canvasHeight / (textHeight + spacing));
const numCols = Math.ceil(canvasWidth / (textWidth + spacing));
ctx.save();
ctx.translate(canvasWidth / 2, canvasHeight / 2);
ctx.rotate(rad); // Rotate text if needed
// Draw repeated text with spacing
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
const x = col * (textWidth + spacing) - canvasWidth / 2;
const y = row * (textHeight + spacing) - canvasHeight / 2;
ctx.fillText(text, x, y);
}
}
ctx.restore();
}
// 确保 Canvas 始终位于最上层
function ensureCanvasOnTop(defaultMaxZIndex = 1000) {
const canvas = document.querySelector('.watermark-canvas');
if (canvas) {
// 获取所有元素的最高 z-index
let maxZIndex = Array.from(document.querySelectorAll('body *'))
.map((el) => parseFloat(window.getComputedStyle(el).zIndex))
.filter((zIndex) => !isNaN(zIndex))
.reduce((max, zIndex) => Math.max(max, zIndex), defaultMaxZIndex);
// 只有在发现新的最大 z-index 超过默认值时,才更新 canvas 的 z-index
if (maxZIndex >= defaultMaxZIndex) {
canvas.style.zIndex = maxZIndex + 1;
}
}
}
// 确保 Canvas 始终可见
function ensureCanvasVisibility() {
const canvas = document.querySelector('.watermark-canvas');
if (canvas) {
const style = window.getComputedStyle(canvas);
// 确保 Canvas 始终可见
if (
style.display === 'none' ||
style.visibility === 'hidden' ||
parseFloat(style.opacity) === 0
) {
canvas.style.display = 'block';
canvas.style.visibility = 'visible';
canvas.style.opacity = 1;
createWatermarkCanvas(); // 重新绘制水印
}
}
}
// 设置 MutationObserver
function setupObservers() {
const targetNode = document.querySelector('.watermarked-content');
const canvas = document.querySelector('.watermark-canvas');
if (targetNode) {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
// 监听属性变动
if (
mutation.type === 'attributes' &&
mutation.attributeName === 'style'
) {
ensureCanvasVisibility();
}
// 监听 DOM 变动
ensureCanvasOnTop();
createWatermarkCanvas();
});
});
// 监听水印容器的子元素、属性变化
observer.observe(targetNode, {
attributes: true,
childList: true,
subtree: true,
});
}
// 监听 Canvas 内容变动
if (canvas) {
const canvasObserver = new MutationObserver(() => {
createWatermarkCanvas();
});
canvasObserver.observe(canvas, { childList: true, subtree: true });
}
}
// 初始化函数
function initializeWatermark() {
createWatermarkCanvas();
ensureCanvasOnTop();
setupObservers();
}
window.onload = initializeWatermark;
</script>
</body>
</html>