-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
133 lines (122 loc) · 3.08 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>微信头像国庆生成工具</title>
<style>
html,
body {
padding: 0;
margin: 0;
height: 100%;
background: linear-gradient(#ff4b3d, #ffde28) #ffde28;
}
ul {
width: 360px;
margin: 0 auto 30px;
overflow: hidden;
list-style: none;
padding: 0;
}
ul li {
width: 25%;
float: left;
padding: 12px;
box-sizing: border-box;
}
ul li img {
width: 100%;
}
canvas {
margin: auto;
display: none;
background: transparent;
}
button {
margin: 40px auto;
display: block;
width: 240px;
height: 60px;
color: #fff;
background: linear-gradient(#ffe984, #ed8917) #ed8917;
font-size: 22px;
font-weight: bold;
border-radius: 12px;
border: none;
outline: none;
}
.generateImg {
display: block;
margin: auto;
}
</style>
</head>
<body>
<ul>
<li><img src="./imgs/avatar-1.png"></li>
<li><img src="./imgs/avatar-2.png"></li>
<li><img src="./imgs/avatar-3.png"></li>
<li><img src="./imgs/avatar-4.png"></li>
</ul>
<button>上传图片</button>
<canvas id="canvas" width="320" height="320"></canvas>
<img src="" class="generateImg">
<input type="file" accept="image/*" hidden>
<script>
const ul = document.querySelector('ul')
const img = document.querySelectorAll('ul img')
const button = document.querySelector('button')
const canvas = document.querySelector('#canvas')
const ctx = canvas.getContext('2d')
const generateImg = document.querySelector('.generateImg')
const fileUpload = document.querySelector('input')
let upLoadImg
let avatarIcon
// 画canvas
function drawImage(img) {
ctx.drawImage(img, 0, 0, 320, 320)
}
// 清空canvas
function resetCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = 'rgba(255, 255, 255, 0)'
}
function canvasToImg() {
generateImg.src = canvas.toDataURL("image/png")
}
// 点击icon
ul.addEventListener('click', function (event) {
if (event.target.nodeName.toLowerCase() === 'img') {
resetCanvas()
upLoadImg && drawImage(upLoadImg)
drawImage(event.target)
canvasToImg()
avatarIcon = event.target
}
})
// 上传图片
button.addEventListener('click', function () {
fileUpload.click()
})
// 监听文件变化
fileUpload.addEventListener('change', function () {
const img = new Image()
img.src = URL.createObjectURL(this.files[0])
img.onload = function () {
drawImage(img)
drawImage(avatarIcon)
canvasToImg()
upLoadImg = img
}
})
// 默认是第一张
img[0].onload = function () {
drawImage(img[0])
avatarIcon = img[0]
canvasToImg()
}
</script>
</body>
</html>