-
Notifications
You must be signed in to change notification settings - Fork 0
/
characters.js
49 lines (40 loc) · 1.06 KB
/
characters.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
'use strict';
const sharp = require('sharp');
const font = require('./font');
const sprites = require('./sprites');
let characterImg;
module.exports = {
async getCharacterScreen() {
if (!characterImg) {
const { spriteTree: { characters } } = await sprites.getSprites();
const cw = 50;
const ch = 50;
const th = 20;
const comps = (await Promise.all(characters.map(async (char, i) => [
{
input: char.buffer,
left: cw * (i + 0.5) - Math.floor(char.width / 2),
top: cw / 2 - Math.floor(char.height / 2),
},
{
input: await font.renderLine(`${i + 1}`),
left: cw * (i + 0.5) - 4,
top: ch + th / 2 - 4,
},
]))).flat();
characterImg = sharp(
{
create: {
width: characters.length * 50,
height: 70,
channels: 4,
background: { r: 0, g: 0, b: 0, alpha: 0 },
},
})
.composite(comps)
.png()
.toBuffer();
}
return characterImg;
},
};