-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
node-labels.ts
38 lines (32 loc) · 1.19 KB
/
node-labels.ts
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
import { Attributes } from "graphology-types";
import { Settings } from "../settings";
import { NodeDisplayData, PartialButFor } from "../types";
export type NodeLabelDrawingFunction<
N extends Attributes = Attributes,
E extends Attributes = Attributes,
G extends Attributes = Attributes,
> = (
context: CanvasRenderingContext2D,
data: PartialButFor<NodeDisplayData, "x" | "y" | "size" | "label" | "color">,
settings: Settings<N, E, G>,
) => void;
export function drawDiscNodeLabel<
N extends Attributes = Attributes,
E extends Attributes = Attributes,
G extends Attributes = Attributes,
>(
context: CanvasRenderingContext2D,
data: PartialButFor<NodeDisplayData, "x" | "y" | "size" | "label" | "color">,
settings: Settings<N, E, G>,
): void {
if (!data.label) return;
const size = settings.labelSize,
font = settings.labelFont,
weight = settings.labelWeight,
color = settings.labelColor.attribute
? data[settings.labelColor.attribute] || settings.labelColor.color || "#000"
: settings.labelColor.color;
context.fillStyle = color;
context.font = `${weight} ${size}px ${font}`;
context.fillText(data.label, data.x + data.size + 3, data.y + size / 3);
}