forked from Dev-Huang1/One-Captcha
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canva_test.html
107 lines (97 loc) · 3.55 KB
/
canva_test.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>噪点效果</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
#container {
display: flex;
flex-direction: column;
align-items: center;
}
canvas {
border: 1px solid #000;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div id="container">
<canvas id="canvas" width="400" height="400"></canvas>
<input type="file" id="imageInput" accept="image/*">
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const imageInput = document.getElementById('imageInput');
let shapeChoice;
function addStructuredNoise(img) {
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
// 添加半透明遮罩
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
const count = 3;
const spacing = canvas.width / count;
// 随机选择绘制形状
shapeChoice = Math.random() > 0.5 ? 'circle' : 'square';
for (let i = 0; i < count; i++) {
for (let j = 0; j < count; j++) {
const x = i * spacing + spacing / 2;
const y = j * spacing + spacing / 2;
// 根据随机选择的形状绘制
if (shapeChoice === 'circle') {
drawConcentricCircles(x, y, spacing / 2);
} else {
drawConcentricSquares(x, y, spacing);
}
}
}
}
function drawConcentricSquares(x, y, maxSize) {
const levels = 12;
for (let i = 0; i < levels; i++) {
const size = maxSize - (i * maxSize / levels);
// 使用较低的透明度和柔和的颜色
ctx.strokeStyle = `rgba(${Math.random() * 180},${Math.random() * 180},${Math.random() * 180},0.3)`;
ctx.lineWidth = 1;
ctx.strokeRect(x - size / 2, y - size / 2, size, size);
}
}
function drawConcentricCircles(x, y, maxRadius) {
const levels = 12;
for (let i = 0; i < levels; i++) {
const radius = maxRadius - (i * maxRadius / levels);
// 使用较低的透明度和柔和的颜色
ctx.strokeStyle = `rgba(${Math.random() * 180},${Math.random() * 180},${Math.random() * 180},0.3)`;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.stroke();
}
}
imageInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(event) {
const img = new Image();
img.onload = function() {
addStructuredNoise(img);
}
img.src = event.target.result;
}
reader.readAsDataURL(file);
}
});
</script>
</body>
</html>