-
Notifications
You must be signed in to change notification settings - Fork 32
/
script.js
89 lines (74 loc) · 2.01 KB
/
script.js
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
// JavaScript Document
var can = document.getElementById("myCanVas");
var myCan = can.getContext("2d");
var img = new Image();
img.src = "bg.jpg";
img.onload = function () {
myCan.drawImage(img, 0, 0, 600, 843);
}
var zsname = document.getElementById("name");
var txt = document.getElementById("text");
var pr = document.getElementById("prize");
var da = document.getElementById("day");
var but = document.getElementById("submit");
but.onclick = function () {
myCan.beginPath();
myCan.font = "22px Arial";
myCan.fillText(txt.value, 140, 372);
drawText(pr.value, 128, 410, 358);
myCan.fillText(da.value, 350, 660);
myCan.closePath();
myCan.font = "bold 60px serif";
myCan.fillText(zsname.value, 180, 210);
}
function drawText(t, x, y, w) {
var chr = t.split("");
var temp = "";
var row = [];
var count = 0;
myCan.font = "21px 宋体";
for (var a = 0; a < chr.length; a++) {
if (count == 0) {
if (myCan.measureText(temp).width < w - 40) {
;
} else {
count += 1;
row.push(temp);
temp = "";
}
temp += chr[a];
} else {
if (myCan.measureText(temp).width < w) {
;
} else {
count += 1;
row.push(temp);
temp = "";
}
temp += chr[a];
}
}
row.push(temp);
for (var b = 0; b < row.length; b++) {
myCan.fillText(row[b], b == 0 ? x + 40 : x, y + (b + 1) * 30);
}
}
setTimeout(function () {
var imgt = new Image();
imgt.src = "seal.svg";
imgt.onload = function () {
myCan.drawImage(imgt, 400, 600, 100, 100);
}
}, 100 /*设置100的延迟*/);
function exportCanvasAsPNG(id, fileName) {
var canvasElement = document.getElementById(id);
var MIME_TYPE = "image/png";
var imgURL = canvasElement.toDataURL(MIME_TYPE);
var dlLink = document.createElement('a');
dlLink.download = fileName;
dlLink.href = imgURL;
dlLink.dataset.downloadurl = [MIME_TYPE, dlLink.download, dlLink.href].join(':');
document.body.appendChild(dlLink);
dlLink.click();
document.body.removeChild(dlLink);
}