-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
146 lines (119 loc) · 3.85 KB
/
index.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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>离线证件水印工具</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<style>
body {
margin: auto;
max-width: 80vh;
}
img {
border: 3px solid #2c3e50;
border-radius: 5px;
width: 100%;
box-sizing: border-box
}
canvas {
width: 100%;
height: 0;
}
label, a {
display: flex;
align-items: center;
margin-top: 8px;
}
</style>
</head>
<body>
<canvas id="main" ></canvas>
<div>
<input id="upload-input" type="file" accept="image/*">
<label>
水印文字:
<input id="content" type="text" value="仅供XX实名认证使用">
</label>
<label>
水印颜色:
<input id="color" type="color" value="#ffffff">
</label>
<label>
不透明度:
<input id="alpha" type="range" min="1" max="10" value="5">
</label>
<label>
水印大小:
<input id="size" type="range" min="0" max="300" value="100">
</label>
<label>
水印间隔:
<input id="gap" type="range" min="20" max="300" value="150">
</label>
<label>
水印字体:
<input id="font" type="text" value="sans-serif">
</label>
<a href="javascript:alert('请先上传图片')" id = 'downloadPic' download>保存图片</a>
</div>
<script>
var img = null;
document.addEventListener('change', function (ev) {
if (ev.target.type === 'file') {
if (!ev.target.files.length) return;
var file = ev.target.files[0];
createBitmap(file, function (result) {
img = result;
myrepaint()
});
} else {
myrepaint()
}
});
function createBitmap(blob, callback) {
var img = document.createElement('img');
img.addEventListener('load', function () {
callback(this);
});
img.src = URL.createObjectURL(blob);
}
function myrepaint(){
var data = {
content:document.getElementById('content').value,
color:document.getElementById('color').value,
alpha:document.getElementById('alpha').value/ 10,
size:+document.getElementById('size').value,
gap:+document.getElementById('gap').value,
font:document.getElementById('font').value,
};
var size = data.size;
if(!img||!data.content.length)return;
var maxSize = Math.max(img.height, img.width);
var ctx = document.getElementById('main').getContext('2d');
//画布样式处理
ctx.canvas.width = img.width;
ctx.canvas.height = img.height;
//调整比例
ctx.canvas.style.height = (img.height / img.width) * 100 + '%';
//加载图像
ctx.drawImage(img, 0, 0);
//属性配置
ctx.rotate(-45 * Math.PI / 180);
ctx.fillStyle = data.color;
ctx.globalAlpha = data.alpha;
ctx.textBaseline = 'middle';
ctx.font = size + 'px "'+data.font+'"';
//算出最长边构成的正方形对角线
var limit = Math.sqrt(2*(maxSize*maxSize));
//内容字体长度,用来换行错位
var breaker = ctx.measureText(data.content).width;
for (var h = size, even = false; h <= limit; h += size + data.gap, even = !even){
for (var v = -limit-(even?breaker/2:0); v<=limit+(even?breaker/2:0); v += breaker + size){
ctx.fillText(data.content, v, h);
}
}
document.getElementById('downloadPic').href = ctx.canvas.toDataURL()
}
</script>
</body>
</html>