-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
183 lines (144 loc) · 4.32 KB
/
index.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
const express = require('express');
const app = express();
const port = 3000;
const { createCanvas, loadImage } = require('canvas');
const { qrcanvas, setCanvasModule } = require('qrcanvas');
// Enable node-canvas
setCanvasModule(require('canvas'));
app.get('/', async (req, res) => {
const { name, id, usd, ars, btc } = req.query;
const FONT_SIZE_TITLE = 30;
const FONT_SIZE_TIMESTAMP = 12;
const FONT_SIZE_ARS = 24;
const FONT_SIZE_USD = 14;
const CANVAS_WIDTH = 250;
const CANVAS_HEIGHT = 122;
const PADDING = 10;
const RED = '#bd2217';
const drawCanvas = (
productName,
productPriceARS,
productPriceUSD,
productPriceBTC,
id
) =>
new Promise((resolve, reject) => {
const canvas = createCanvas(CANVAS_WIDTH, CANVAS_HEIGHT);
const data = new Date();
// hh:mm:ss 02:30:15
const timeStamp = data.toLocaleTimeString('en-US', {
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
});
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Title
ctx.fillStyle = 'black';
ctx.font = `bold ${FONT_SIZE_TITLE}px Impact`;
ctx.fillText(
productName,
FONT_SIZE_TITLE / 2 + PADDING,
FONT_SIZE_TITLE + PADDING / 2
);
// Line
// draw a line under the title
ctx.beginPath();
ctx.moveTo(10, FONT_SIZE_TITLE + PADDING);
ctx.lineTo(CANVAS_WIDTH - 10, FONT_SIZE_TITLE + PADDING);
ctx.stroke();
ctx.font = `${FONT_SIZE_TIMESTAMP}px Impact`;
// ctx.fillText(
// `Last update ${productPriceARS}`,
// CANVAS_WIDTH - productPriceText.width - PADDING,
// 80
// );
const timeStampText = ctx.measureText(`Last update ${timeStamp}`);
ctx.fillText(
`Last update ${timeStamp}`,
CANVAS_WIDTH - timeStampText.width - PADDING,
55
);
ctx.fillStyle = RED;
ctx.font = `bold ${FONT_SIZE_ARS}px Impact`;
const productPriceText = ctx.measureText(`${productPriceARS} ARS`);
// ctx.fillText(
// `${productPriceARS} ARS`,
// CANVAS_WIDTH - productPriceText.width - PADDING,
// CANVAS_HEIGHT - FONT_SIZE_TITLE
// );
ctx.fillText(
`${productPriceARS} ARS`,
CANVAS_WIDTH - productPriceText.width - PADDING,
80
);
ctx.font = ` ${FONT_SIZE_USD}px Impact`;
ctx.fillText(
`${productPriceUSD} USD`,
CANVAS_WIDTH - productPriceText.width - PADDING,
100
);
ctx.fillText(
`${productPriceBTC} BTC`,
CANVAS_WIDTH - productPriceText.width - PADDING,
120
);
// Solum Logo
// ctx.fillRect(CANVAS_WIDTH - 52, 0, 52, 17);
// ctx.fillStyle = 'white';
// ctx.fillText(`SOLUM`, CANVAS_WIDTH - 45, 14);
const canvas2 = qrcanvas({
data: 'https://junction2023.vercel.app/qr/' + id,
});
ctx.drawImage(canvas2, 10, 53, 60, 60);
// return canvas;
const logo_image = loadImage('./blink_red.png');
logo_image.then((image) => {
// console.log(image);
ctx.drawImage(image, CANVAS_WIDTH - 88, 0, 88, 35);
resolve(canvas);
});
});
const ToBase64 = (canvas) => {
const base64 = canvas.toDataURL();
const base64Data = base64.replace(/^data:image\/png;base64,/, '');
// console.log(base64Data);
return base64Data;
};
// const result = ToBase64(drawCanvas(name, ars, usd, btc, id));
// const result = ToBase64(drawCanvas(name, ars, usd, btc, id));
drawCanvas(name, ars, usd, btc, id).then((canvas) => {
const result = ToBase64(canvas);
const elsMap = {
1: '085C1B03E1DA',
2: '085C1B0DE1D4',
3: '085C1B2FE1D4',
4: '085C1B3FE1D5',
5: '085C1B48E1D5',
};
res.status(200).json({
labelCode: elsMap[id],
page: 1,
frontPage: 1,
image: result,
});
});
// const elsMap = {
// 1: '085C1B03E1DA',
// 2: '085C1B0DE1D4',
// 3: '085C1B2FE1D4',
// 4: '085C1B3FE1D5',
// 5: '085C1B48E1D5',
// };
// res.status(200).json({
// labelCode: elsMap[id],
// page: 1,
// frontPage: 1,
// image: result,
// });
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});