-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (45 loc) · 1.3 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
const defaultOptions = {
imgPath: "/favicon.svg",
textColor: "white",
badgeColor: "red",
};
async function createBadgeFavicon(label, options) {
const opts = Object.assign({}, defaultOptions, options);
const canvas = document.createElement("canvas");
canvas.width = 64;
canvas.height = 64;
const ctx = canvas.getContext("2d");
// Draw base icon
const icon = new Image();
icon.src = opts.imgPath;
await icon.decode();
ctx.drawImage(icon, 0, 0, 64, 64);
const fontSize = 28;
// Measure text
ctx.font = `${fontSize}px Arial, sans-serif`;
ctx.textAlign = "right";
ctx.textBaseline = "top";
const textMetrics = ctx.measureText(label);
// Draw badge
const paddingX = 7;
const paddingY = 4;
const cornerRadius = 8;
const width = textMetrics.width + paddingX * 2;
const height = fontSize + paddingY * 2;
const x = canvas.width - width;
const y = canvas.height - height - 1;
ctx.fillStyle = opts.badgeColor;
ctx.roundRect(x, y, width, height, cornerRadius);
ctx.fill();
// Draw badge text
const textPaddingX = 7;
const textPaddingY = 3;
ctx.fillStyle = opts.textColor;
ctx.fillText(
label,
canvas.width - textPaddingX,
canvas.height - fontSize - textPaddingY
);
return canvas.toDataURL("image/png");
}
module.exports = { createBadgeFavicon };