-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4c.html
72 lines (70 loc) · 2.62 KB
/
4c.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>四除生成器</title>
</head>
<body>
文本:<input id="valText" value="不戴套不算艹"/>字号:<input id="valSize" type="number" size="3" style="width:6ex;" value="20"/>px
坐标:(<input id="valX" type="number" size="3" style="width:6ex;" value="112"/>,<input id="valY" type="number" size="3" style="width:6ex;" value="56"/>)
<button id="btnGenerate">生成</button><button id="btnCopy">复制</button><button id="btnSave">保存</button><div>
<canvas id="myCanvas" width="496" height="409">
你的浏览器不支持canvas!
</canvas></div>
<img id="imgBg" src="4c.png" width="496" height="409" alt="" style="display:none;"/> <script type="text/javascript">
var cav = document.getElementById('myCanvas');
var btnGenerate = document.getElementById('btnGenerate');
var btnCopy = document.getElementById('btnCopy');
var btnSave = document.getElementById('btnSave');
var valText = document.getElementById("valText");
var valSize = document.getElementById("valSize");
var valX = document.getElementById("valX");
var valY = document.getElementById("valY");
//var canvas = document.createElement('canvas');
if(cav.getContext){
var ctx = cav.getContext('2d');
var img = document.getElementById("imgBg");
img.onload = function() {
btnGenerate.onclick = function(){
ctx.drawImage(img,0,0);
ctx.font=valSize.value+"px sans-serif";
var h=ctx.measureText(valText.value).fontBoundingBoxAscent;
ctx.fillText(valText.value,parseInt(valX.value),parseInt(valY.value)+h,150);
}
btnGenerate.onclick()
btnCopy.onclick = () => {
cav.toBlob(async (blob) => {
console.log(blob);
const data = [
new ClipboardItem({
[blob.type]: blob,
}),
]
await navigator.clipboard.write(data).then(
() => {
console.log("Copied to clipboard successfully!");
},
() => {
console.error("Unable to write to clipboard.");
}
);
});
};
btnSave.onclick = function(){
// 创建一个 a 标签,并设置 href 和 download 属性
const el = document.createElement('a');
// 设置 href 为图片经过 base64 编码后的字符串,默认为 png 格式
el.href = cav.toDataURL();
el.download = '表情.png';
// 创建一个点击事件并对 a 标签进行触发
const event = new MouseEvent('click');
el.dispatchEvent(event);
}
}
console.log("OK!");
}else{
console.log("Naze!");
}
</script>
</body>
</html>