-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
108 lines (92 loc) · 3.89 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
<!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>
</head>
<body>
<label>显示文字:</label>
<input id="text" type="text" />
<br>
<br>
<button id='btn'>预览图片</button>
<button id='exp'>导出图片</button>
<p class="intro">支持以下文字的显示,感谢
<a href="https://www.youziku.com/webfont/cssdetail/9F284B4A1A087D16069A812EAAAAA635">有字库</a>
提供免费字体资源。
</p>
<p class="text">显示文字预览导出图片1234567890第期前端晚自修</p>
<br>
<br>
<canvas id="drawing" width="960" height="540">A drawing of something.</canvas>
<style>
@font-face {
font-family: 'chenjishiguyun-13c94e564b183ba';
src: url('//cdn.webfont.youziku.com/webfonts/nomal/99258/45811/5b6d9ae4f629d919b4accb33.gif?r=82303333002');
src: url('//cdn.webfont.youziku.com/webfonts/nomal/99258/45811/5b6d9ae4f629d919b4accb33.gif?r=82303333002?#iefix') format('embedded-opentype'), url('//cdn.webfont.youziku.com/webfonts/nomal/99258/45811/5b6d9ae4f629d919b4accb33.png?r=82303333002') format('woff2'), url('//cdn.webfont.youziku.com/webfonts/nomal/99258/45811/5b6d9ae4f629d919b4accb33.bmp?r=82303333002') format('woff'), url('//cdn.webfont.youziku.com/webfonts/nomal/99258/45811/5b6d9ae4f629d919b4accb33.jpg?r=82303333002') format('truetype');
font-weight: normal;
font-style: normal;
}
.css13c94e564b183ba {
font-family: 'chenjishiguyun-13c94e564b183ba';
}
* {
font-size: 20px;
font-family: 'chenjishiguyun-13c94e564b183ba';
text-align: center;
}
.intro {
font-size: 12px;
}
.text {
font-size: 12px;
text-decoration: underline;
}
canvas {
border: 1px solid #ddd;
}
</style>
<script>
var canvas = document.getElementById('drawing');
var context = canvas.getContext("2d");
var image = new Image();
image.setAttribute("crossOrigin", 'Anonymous'); // 解决跨域
image.src = 'http://pazgkbbu5.bkt.clouddn.com/bg.png';
var btn = document.getElementById('btn');
var exp = document.getElementById('exp');
btn.onclick = function () {
context.drawImage(image, 0, 0, canvas.width, canvas.height);
var input = document.getElementById('text');
var text = input.value;
if (text) {
console.log(text);
context.textAlign = 'center'
context.fillStyle = '#fff';
context.font = "40px chenjishiguyun-13c94e564b183ba";
context.fillText(text, 480, 150);
} else {
alert('请输入文字');
}
}
exp.onclick = function () {
setTimeout(function () {
var exportImage = canvas.toDataURL('image/png');
// console.log(exportImage);
var input = document.getElementById('text');
var text = input.value;
// 下载功能代码借鉴 https://juejin.im/post/5a31dbc951882510b27563b9
var saveLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
saveLink.href = exportImage;
// 设置下载图片的名称
saveLink.download = text + '.png';
//下载图片
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
saveLink.dispatchEvent(event);
}, 1000);
}
</script>
</body>
</html>