-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
211 lines (171 loc) · 6.44 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html lang='zh-CN'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>证件照水印保护 Watermark</title>
<style>
body {
margin: 0;
}
#wm-app {
width: 800px;
margin: 0 auto;
text-align: center;
}
#wm-app .wm-main {
width: 100%;
height: 400px;
}
#canvas {
width: 100%;
}
#content {
width: 400px;
}
#select-file {
width: 250px;
}
</style>
</head>
<body>
<div id='wm-app'>
<h1> 证件照水印保护 Watermark </h1>
<div class='wm-main'>
<input type='file' id='select-file'/>
<input type='text' id='inputContent' value=''>
<label for='small-watermark'><input type='checkbox' id='small-watermark' checked/>小字水印</label>
<br/>
<canvas id='canvas' width='800' height='600'></canvas>
</div>
</div>
<script>
(function () {
class Watermark {
// 画板
canvas = null
ctx = null
// 需要处理的图片
image = null
constructor(canvas, image) {
if (!canvas instanceof HTMLCanvasElement) {
throw new Error('canvas is not Canvas Element')
}
this.canvas = canvas
this.ctx = this.canvas.getContext('2d')
// 图片可以后期推送
if (image instanceof HTMLImageElement) {
this.initCanvas(image)
}
}
// 加载远程图片
loadImg(src) {
return new Promise((resolve, reject) => {
let img = new Image()
img.onload = (event) => {
this.initCanvas(event.target)
resolve(this)
}
img.src = src
})
}
// 初始化画板
initCanvas(image) {
if (image instanceof HTMLImageElement) {
this.image = image
}
if (!this.image instanceof HTMLImageElement) {
throw new Error('No valid image provided')
}
// 设置画板宽高
let height = this.image.height,
width = this.image.width,
ratio = 1
if (height > 800 || width > 800) {
let height_ratio = 800 / height,
width_ratio = 800 / width
ratio = height_ratio < width_ratio ? height_ratio : width_ratio
height *= ratio
width *= ratio
}
this.canvas.height = height
this.canvas.width = width
// 输出 图片
this.ctx.scale(ratio, ratio)
this.ctx.drawImage(this.image, 0, 0)
this.ctx.scale(1 / ratio, 1 / ratio)
}
// 设置文字样式
setStyle(font) {
this.ctx.font = font
}
// 设置文字
setContent(content, small) {
// 重新设置图片
this.initCanvas()
// 设置默认样式
this.setStyle("24px Arial")
this.ctx.globalAlpha = 0.4
this.ctx.fillStyle = "#666"
// 测试输出文字宽度
let width = this.ctx.measureText(content).width
// 围绕图片中心设置旋转
this.ctx.rotate(-20 * Math.PI / 180)
// 循环设置水印
for (let x = -1 * this.canvas.width; x < (this.canvas.width + this.canvas.height) * 2; x += 60) {
for (let y = (-1 * this.canvas.height) - (x * width * 0.5); y < (this.canvas.width + this.canvas.height) * 2; y += width + 24) {
this.ctx.fillText(content, y, x)
}
}
// 还原旋转
this.ctx.rotate(20 * Math.PI / 180)
if (!small) return
// 再次随机添加细碎小文字
this.setStyle("12px Microsoft YaHei MingLiU Arial")
this.ctx.globalAlpha = 0.15
this.ctx.fillStyle = "#CCC"
// 测试输出文字宽度
width = this.ctx.measureText(content).width
// 循环设置水印
for (let x = 0, i = 0; x < this.canvas.width; x += 36, i++) {
for (let y = -1 * (i % 2 == 1 ? width / 2 : 0); y < this.canvas.height + width; y += width + 36) {
this.ctx.fillText(content, y, x)
}
}
}
}
// 加载远程图片
let app = new Watermark(document.querySelector('#canvas'))
app.loadImg('./default.jpg').then((app) => {
generate()
})
// 生成水印
function generate() {
let content = document.getElementById('inputContent').value,
small = document.getElementById('small-watermark').checked
app.setContent(content, small)
}
// 选择文件后
document.getElementById('select-file').onchange = (event) => {
let reader = new FileReader()
reader.onload = (event) => {
app.loadImg(event.target.result).then((app) => {
generate()
})
}
reader.readAsDataURL(event.target.files[0])
}
// <input type="text" id="content" value="此证件照仅供 XXX 于 2020 年 12 月 30 日前 实名认证 使用。">
let inputContent = document.getElementById('inputContent')
// 获取三天后的时间
let dateTime = new Date(Date.now() + 3 * 24 * 60 * 60 * 1000)
console.log(inputContent, dateTime)
let year = dateTime.getFullYear()
let month = dateTime.getMonth() + 1
let day = dateTime.getDate()
inputContent.value = `此证件照仅供 XXX 于 ${year} 年 ${month} 月 ${day} 日前 实名认证 使用。`
document.getElementById('inputContent').oninput = document.getElementById('small-watermark').onchange = generate
})()
</script>
</body>
</html>