-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
135 lines (114 loc) · 3.83 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
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
134
135
const GAS_API = `https://script.google.com/macros/s/AKfycbzjR8RxB8ldm9FD27al5AO9FpHp4qpyI-Zhadpi9EPoOUTahWMDFQ8ZkxzS9Bk9XtjBmg/exec`;
const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('hex_k');
let downloadData = {};
if (myParam) {
axios.get(`${GAS_API}?k=${myParam}`).then((res) => {
if (res.data.length > 0) {
createDraw(res.data[0]);
downloadData = res.data[0];
} else {
noData();
}
});
} else {
noData();
}
function noData() {
document.querySelector(
'.contentArea'
).innerHTML = `<div class="container my-3"><h2 class="text-center"> 查無資料 </h2></div>`;
}
function createDraw(content) {
// 修正解析度
const getPixelRatio = function (context) {
const backingStore =
context.backingStorePixelRatio ||
context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio ||
1;
return (window.devicePixelRatio || 1) / backingStore;
};
let c1 = document.querySelector('#sz');
let ctx = c1.getContext('2d');
const ratio = getPixelRatio(ctx);
if (window.innerWidth < 1200) {
c1.style.width = '100%';
c1.style.height = '100%';
} else {
c1.style.width = c1.width * 1.2 + 'px';
c1.style.height = c1.height * 1.2 + 'px';
}
c1.width = c1.width * ratio;
c1.height = c1.height * ratio;
// 放大倍数
ctx.scale(ratio, ratio);
const img = new Image();
const logo = new Image();
img.src = `./images/template${content.template}.svg`;
logo.src = `./images/hexschool_logo.svg`;
// 加载字体和图像
const junction_font = new FontFace(
'Noto Sans TC',
'url(./font/NotoSansTC-Regular.otf)'
);
Promise.all([
new Promise((resolve) => {
img.onload = resolve;
}),
new Promise((resolve) => {
logo.onload = resolve;
}),
junction_font.load(),
])
.then((loaded) => {
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, c1.width, c1.height);
ctx.drawImage(img, 0, 0, 843, 596);
document.fonts.add(junction_font);
let fontFamily = junction_font.family;
let xPosition = 124;
let colorDark = 'rgba(0, 37, 36, 1)';
let colorBlack = 'rgba(0, 0, 0, 1)';
let yStart = 205;
ctx.textAlign = 'left';
ctx.font = `24px ${fontFamily}`;
ctx.fillStyle = colorDark;
ctx.fillText(`${content.course_name}`, xPosition, yStart);
ctx.fillText(`${content.title}`, xPosition, yStart + 32);
ctx.beginPath();
ctx.moveTo(xPosition, yStart + 32 + 24);
ctx.lineTo(xPosition + 350, yStart + 32 + 24);
ctx.strokeStyle = colorDark;
ctx.lineWidth = 2;
ctx.stroke();
ctx.font = `31px ${fontFamily}`;
ctx.fillStyle = 'black';
ctx.fillText(`${content.student}`, xPosition, 318);
ctx.font = `16px ${fontFamily}`;
ctx.fillStyle = colorBlack;
ctx.fillText(`${content.content}`, xPosition, 318 + 42);
ctx.fillText(`特頒此證 以茲證明`, xPosition, 318 + 42 + 24);
ctx.drawImage(logo, xPosition - 6, 424, 114, 40);
ctx.font = `14px ${fontFamily}`;
ctx.fillStyle = colorBlack;
ctx.fillText(`授課老師 ${content.teacher}`, xPosition, 473);
ctx.fillText(`${content.issue_date.split('T')[0]}`, xPosition + 529, 473);
})
.catch((error) => {
console.error('Error loading resources: ', error);
});
}
document.getElementById('saveBtn').addEventListener('click', saveImg);
function saveImg() {
let canvas = document.querySelector('#sz');
let downloadA = document.createElement('a');
downloadA.setAttribute('download', `${downloadData.course_name}_獎狀.png`);
downloadA.href = canvas
.toDataURL('image/png')
.replace('image/png', 'image/octet-stream');
downloadA.click();
}