-
Notifications
You must be signed in to change notification settings - Fork 0
/
woshishei.html
113 lines (101 loc) · 2.92 KB
/
woshishei.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0;
}
</style>
</head>
<body>
<div style="display: flex;flex-wrap: wrap;gap: 12px;">
<canvas id="wss" width="500" height="370"></canvas>
<canvas id="pkq" width="500" height="370"></canvas>
<div>
<input type="file" onchange="handleImage(this)">
<br>
<input type="text" placeholder="我是谁" oninput="handleWss(this)">
<br>
<input type="text" placeholder="说明" oninput="handlePlaceholder(this)">
<div>
<button onclick="handleDownLoadWss(this)">下载第一张</button>
<button onclick="handleDownLoadPkq(this)">下载第二张</button>
</div>
</div>
</div>
<script>
// 我是谁
const wss = document.querySelector('canvas#wss');
const wssCtx = wss.getContext('2d');
drawImage(wssCtx, '../assets/pkq4.png');
// 皮卡丘
const pkq = document.getElementById('pkq');
const pkqCtx = pkq.getContext('2d');
drawImage(pkqCtx, '../assets/pkq3.png');
const handleWss = (e) => {
drawText(pkqCtx, e.value, 292, 80)
}
const handlePlaceholder = (e) => {
drawText(pkqCtx, e.value, 292, 140)
}
const handleImage = (e) => {
const file = e.files[0];
const fileReader = new FileReader();
fileReader.onload = (e2) => {
drawImage(pkqCtx, e2.target.result, 30, 30, 175, 200).then(() => {
drawImage(pkqCtx, '../assets/pkq3.png');
});
}
fileReader.readAsDataURL(file);
}
const handleDownLoadWss = () => {
const url = wss.toDataURL();
downLoad(url, '我是谁');
}
const handleDownLoadPkq = () => {
const url = pkq.toDataURL();
downLoad(url, '皮卡丘');
}
function drawText(ctx, text, x, y) {
ctx.save();
ctx.clearRect(x, y - 40, 500, 50);
ctx.fillStyle = '#395980'
ctx.fillRect(x, y - 40, 209, 50);
ctx.beginPath();
ctx.font = '30px 微软雅黑';
ctx.fillStyle = '#d8f386'
ctx.fillText(text, x, y);
ctx.restore();
}
function drawImage(ctx, url, x = 0, y = 0, w = 500, h = 370) {
return new Promise((resolve, reject) => {
ctx.save();
ctx.beginPath();
const image = new Image();
image.src = url;
image.onload = () => {
ctx.drawImage(image, x, y, w, h);
ctx.restore();
resolve(image);
}
image.onerror = reject;
});
}
function downLoad(url, name) {
const a = document.createElement('a');
a.href = url;
a.target = '_blank';
a.download = name;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
a.remove();
}
</script>
</body>
</html>