-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
36 lines (32 loc) · 1021 Bytes
/
utils.js
File metadata and controls
36 lines (32 loc) · 1021 Bytes
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
export const wrapText = (ctx, text, x, y, maxWidth, lineHeight) => {
const words = text.split(' ');
let line = '';
let currentY = y;
for (let n = 0; n < words.length; n++) {
const testLine = line + words[n] + ' ';
const metrics = ctx.measureText(testLine);
const testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
ctx.fillText(line, x, currentY);
line = words[n] + ' ';
currentY += lineHeight;
} else {
line = testLine;
}
}
ctx.fillText(line, x, currentY);
};
export const drawPill = (ctx, x, y, text, color, scale) => {
ctx.font = `bold ${20 * scale}px "JetBrains Mono"`;
const padding = 20 * scale;
const textWidth = ctx.measureText(text).width;
const height = 40 * scale;
const width = textWidth + padding * 2;
ctx.fillStyle = color;
ctx.globalAlpha = 0.2;
ctx.roundRect(x, y - height / 1.5, width, height, height / 2);
ctx.fill();
ctx.globalAlpha = 1.0;
ctx.fillStyle = color;
ctx.fillText(text, x + padding, y);
};