-
Notifications
You must be signed in to change notification settings - Fork 2
/
image_generator.js
193 lines (168 loc) · 6.42 KB
/
image_generator.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const fs = require('fs')
const { loadImage, createCanvas } = require('canvas')
/**
*
*
* */
async function main (userData) {
const headerText = `${userData.username} JKT48 Wrapped`;
const headerTextOptions = 'bold 25pt Gotham';
const topVC = 'Video Call Teratas';
const topSetlist = 'Setlist Teratas';
const fontColor = '#000000';
const headerFont = 'bold 30pt Sans-Serif';
const fontStyle = '19pt Courier';
const hashtag = '#jkt48wrapped2022';
const winRate = 'Winrate Verif Teater';
const totalVideoCall = 'Jumlah Video Call';
const totalAttendance = 'Jumlah Kehadiran';
const totalTopup = 'Total pengisian point';
const singletText = '26pt Courier';
const numberFormatter = new Intl.NumberFormat('en-US');
const vcAmount = userData.vcAmount;
const attendanceAmount = userData.attendanceAmount;
const winAmount = userData.winAmount;
const lostAmount = userData.lostAmount;
const topVc = userData.vcRanks;
const setlistAttendance = userData.setlistRanks;
const percentage = Math.round(winAmount / (winAmount + lostAmount) * 100);
const topupAmount = userData.totalTopup;
const width = 1080
const height = 1600
const canvas = createCanvas(width, height)
const context = canvas.getContext('2d')
const backgroundColor = '#e0e0e0'
const fontBaseline = 'bottom'
const fontAlignment = 'left'
const rightTextPosition = width * 0.60
const leftTextPosition = width * 0.10
const topQuarter = height * 0.40
const midHeight = height * 0.55
const midWidth = width * 0.50
const winRateBreakdown = `(${winAmount} menang, ${lostAmount} kalah)`
context.fillStyle = '#ffffff'
context.fillRect(0, 0, width, height)
context.shadowBlur = 90
context.shadowColor = 'rgba(0, 0, 0, 0.5)'
context.fillStyle = backgroundColor
context.fillRect(30, 30, width - 60, height - 60)
context.shadowColor = 'transparent'
// headers
context.font = headerTextOptions
context.textBaseline = fontBaseline
context.textAlign = 'center'
context.fillStyle = fontColor
context.fillText(headerText, width / 2, 80)
// top vc
context.font = headerFont
context.textBaseline = fontBaseline
context.textAlign = fontAlignment
context.fillStyle = fontColor
context.fillText(topVC, leftTextPosition, topQuarter)
// top setlist
context.font = headerFont
context.textBaseline = fontBaseline
context.textAlign = fontAlignment
context.fillStyle = fontColor
context.fillText(topSetlist, rightTextPosition + 25, topQuarter)
for (let i = 0; i < 3; i++) {
context.font = fontStyle
context.textBaseline = fontBaseline
context.textAlign = fontAlignment
context.fillStyle = fontColor
context.fillText(`#${i + 1} ${topVc[i].name} - ${topVc[i].sum} Tiket`, leftTextPosition, topQuarter + 50 + (i * 50), 400)
}
for (let i = 0; i < 3; i++) {
context.font = fontStyle
context.textBaseline = fontBaseline
context.textAlign = fontAlignment
context.fillStyle = fontColor
context.fillText(`#${i + 1} ${setlistAttendance[i].showName} - ${setlistAttendance[i].sum} kali`, rightTextPosition + 25, topQuarter + 50 + (i * 50), 300)
}
// Jumlah Video Call
context.font = headerFont
context.textBaseline = fontBaseline
context.textAlign = fontAlignment
context.fillStyle = fontColor
context.fillText(totalVideoCall, leftTextPosition, midHeight)
// video call number
context.font = singletText
context.textBaseline = fontBaseline
context.textAlign = 'right'
context.fillStyle = fontColor
const totalVCLength = context.measureText(totalVideoCall).width;
const totalVCValue = context.measureText(`${vcAmount} tiket`).width;
context.fillText(`${vcAmount} tiket`, leftTextPosition + (totalVCLength - totalVCValue / 2), midHeight + 70)
// Jumlah Kehadiran teater
context.font = headerFont
context.textBaseline = fontBaseline
context.textAlign = fontAlignment
context.fillStyle = fontColor
context.fillText(totalAttendance, rightTextPosition, midHeight)
// attendance number
context.font = singletText
context.textBaseline = fontBaseline
context.textAlign = 'center'
context.fillStyle = fontColor
const totalAttendanceLength = context.measureText(totalAttendance).width;
const totalAttendanceValue = context.measureText(`${attendanceAmount} kali`).width;
context.fillText(`${attendanceAmount} kali`, rightTextPosition + (totalAttendanceLength / 2) + 10, midHeight + 70)
if (userData.generatePoints) {
// Total topup
context.font = headerFont
context.textBaseline = fontBaseline
context.textAlign = 'center'
context.fillStyle = fontColor
context.fillText(totalTopup, midWidth, height * 0.30)
// topup amount
context.font = singletText
context.textBaseline = fontBaseline
context.textAlign = 'center'
context.fillStyle = fontColor
if (topupAmount === 0) {
context.fillText('Nihil', midWidth, height * 0.25 + 70)
}
if (topupAmount > 0) {
context.fillText(`${numberFormatter.format(topupAmount)} P`, midWidth, height * 0.30 + 70)
}
}
// Winrate
context.font = headerFont
context.textBaseline = fontBaseline
context.textAlign = 'center'
context.fillStyle = fontColor
context.fillText(winRate, midWidth, height * 0.70)
// percentage text
context.font = singletText
context.textBaseline = fontBaseline
context.textAlign = 'center'
context.fillStyle = fontColor
context.fillText(`${percentage}%`, midWidth, height * 0.70 + 50)
// percentage breakdown
const winrateBreakdownFont = '20pt Gotham';
context.font = winrateBreakdownFont
context.textBaseline = fontBaseline
context.textAlign = 'center'
context.fillStyle = fontColor
context.fillText(winRateBreakdown, midWidth, height * 0.70 + 100);
// Footer
context.fillRect(0, height - (height * 0.04), width, (height - (height * 0.05)))
context.fillStyle = fontColor
// Footer Text
context.font = fontStyle
context.textBaseline = fontBaseline
context.textAlign = 'right'
context.fillStyle = '#ffffff'
context.fillText(hashtag, width - 20, height - (height * 0.03) + 30)
// Footer Image
const footerLogo = await loadImage('./assets/F4DCLogoTrans2.png')
context.drawImage(footerLogo, 20, height - (height * 0.040), 64, 64)
// oshi image
const oshiImage = await loadImage(`./assets/members_2022/${userData.oshi}`)
context.drawImage(oshiImage, ((width / 2) - 192 / 2), 120, 192, 270)
const imgBuffer = canvas.toBuffer('image/png')
fs.writeFileSync('./test.png', imgBuffer)
}
module.exports = {
main
}