-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (46 loc) · 1.55 KB
/
index.js
File metadata and controls
57 lines (46 loc) · 1.55 KB
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
import { promises } from 'fs';
import { join } from 'path';
import {Canvas} from 'skia-canvas';
import colorsea from "colorsea";
import 'dotenv/config';
import express from "express";
import crypto from "crypto";
const app = express();
app.use(express.json());
const port = process.env.PORT;
async function createTag(word, colour,darken) {
let width = (word.length * 7)+2;
colour = colorsea(colour);
const canvas = new Canvas(width, 7);
const context = canvas.getContext('2d');
context.imageSmoothingEnabled = false;
context.fillStyle = colour.hex();
context.fillRect(0, 0, width, 7);
context.font = "5px Beef'd";
let textPath = context.outlineText(word)
// Select the style that will be used to fill the text in
context.fillStyle = colour.darken(darken).hex();
context.fill(textPath.offset(1, 7)) //SHADOW
context.fillStyle = '#ffffff';
context.fill(textPath.offset(0, 7)) //TEXT
const png = await canvas.png
await promises.writeFile(join("./output", `${word}-${crypto.randomBytes(12).toString('hex')}.png`), png);
return png;
}
app.get('/tag/create', async (req, res) => {
const text = req.body.text;
const colour = req.body.colour;
let darken = req.body.darken;
if (text === null || colour === null) {
res.sendStatus(422).send("Missing requirement parameters.")
return;
}
if(darken == null){
darken = 10;
}
const tag = await createTag(text, colour, darken);
res.send(tag);
})
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});