This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/server/banner.js

61 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-07-26 03:59:49 -07:00
import { buildTemplate, parseTemplates } from "../src/buildTemplate";
2020-07-22 13:19:23 -07:00
import { createCanvas, registerFont, loadImage } from 'canvas';
2020-07-26 03:59:49 -07:00
import Papa from 'papaparse';
import fs from 'fs';
const loadTsv = (filename) => {
return Papa.parse(fs.readFileSync(__dirname + '/../data/' + filename).toString('utf-8'), {
dynamicTyping: true,
header: true,
skipEmptyLines: true,
}).data;
};
2020-07-22 13:19:23 -07:00
export default async function (req, res, next) {
if (req.url.substr(req.url.length - 4) !== '.png') {
res.statusCode = 404;
res.write('Not found');
res.end();
return;
}
2020-07-26 03:59:49 -07:00
const template = buildTemplate(
parseTemplates(loadTsv('templates.tsv')),
decodeURIComponent(req.url.substr(1, req.url.length - 5))
);
2020-07-22 13:19:23 -07:00
const width = 1200
const height = 600
const mime = 'image/png';
const imageSize = 200;
const leftRatio = template ? 4 : 5;
registerFont('static/fonts/quicksand-v21-latin-ext_latin-regular.ttf', { family: 'Quicksand', weight: 'regular'});
registerFont('static/fonts/quicksand-v21-latin-ext_latin-700.ttf', { family: 'Quicksand', weight: 'bold'});
const canvas = createCanvas(width, height)
const context = canvas.getContext('2d')
context.fillStyle = '#fff'
context.fillRect(0, 0, width, height)
context.fillStyle = '#000'
const image = await loadImage('node_modules/@fortawesome/fontawesome-pro/svgs/light/tags.svg');
context.drawImage(image, width / leftRatio - imageSize / 2, height / 2 - imageSize / 1.25 / 2, imageSize, imageSize / 1.25)
if (template) {
context.font = 'regular 48pt Quicksand'
context.fillText('Moje zaimki to:', width / leftRatio + imageSize / 1.5, height / 2 - 36)
context.font = 'bold 70pt Quicksand'
context.fillText(template.name(), width / leftRatio + imageSize / 1.5, height / 2 + 72)
} else {
context.font = 'regular 120pt Quicksand'
context.fillText('Zaimki.pl', width / leftRatio + imageSize / 1.5, height / 2 + 48)
}
res.setHeader('content-type', mime);
res.write(canvas.toBuffer(mime));
res.end()
}