-
-
Notifications
You must be signed in to change notification settings - Fork 799
/
Utils.ts
112 lines (97 loc) · 2.74 KB
/
Utils.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import type { ConnectContainer, LinkParticle } from "./Types.js";
import {
type ICoordinates,
type Particle,
clamp,
colorMix,
drawLine,
getStyleFromHsl,
getStyleFromRgb,
} from "@tsparticles/engine";
const gradientMin = 0,
gradientMax = 1,
defaultLinksWidth = 0;
/**
* Creates a gradient using two particles colors and opacity.
* @param context - The canvas context to draw on.
* @param p1 - The first particle.
* @param p2 - The second particle.
* @param opacity - The opacity of the gradient.
* @returns The gradient.
*/
export function gradient(
context: CanvasRenderingContext2D,
p1: Particle,
p2: Particle,
opacity: number,
): CanvasGradient | undefined {
const gradStop = Math.floor(p2.getRadius() / p1.getRadius()),
color1 = p1.getFillColor(),
color2 = p2.getFillColor();
if (!color1 || !color2) {
return;
}
const sourcePos = p1.getPosition(),
destPos = p2.getPosition(),
midRgb = colorMix(color1, color2, p1.getRadius(), p2.getRadius()),
grad = context.createLinearGradient(sourcePos.x, sourcePos.y, destPos.x, destPos.y);
grad.addColorStop(gradientMin, getStyleFromHsl(color1, opacity));
grad.addColorStop(clamp(gradStop, gradientMin, gradientMax), getStyleFromRgb(midRgb, opacity));
grad.addColorStop(gradientMax, getStyleFromHsl(color2, opacity));
return grad;
}
/**
* @param context -
* @param width -
* @param lineStyle -
* @param begin -
* @param end -
*/
export function drawConnectLine(
context: CanvasRenderingContext2D,
width: number,
lineStyle: CanvasGradient,
begin: ICoordinates,
end: ICoordinates,
): void {
drawLine(context, begin, end);
context.lineWidth = width;
context.strokeStyle = lineStyle;
context.stroke();
}
/**
* @param container -
* @param ctx -
* @param p1 -
* @param p2 -
* @returns The gradient.
*/
export function lineStyle(
container: ConnectContainer,
ctx: CanvasRenderingContext2D,
p1: Particle,
p2: Particle,
): CanvasGradient | undefined {
const options = container.actualOptions,
connectOptions = options.interactivity.modes.connect;
if (!connectOptions) {
return;
}
return gradient(ctx, p1, p2, connectOptions.links.opacity);
}
/**
* @param container -
* @param p1 -
* @param p2 -
*/
export function drawConnection(container: ConnectContainer, p1: LinkParticle, p2: LinkParticle): void {
container.canvas.draw(ctx => {
const ls = lineStyle(container, ctx, p1, p2);
if (!ls) {
return;
}
const pos1 = p1.getPosition(),
pos2 = p2.getPosition();
drawConnectLine(ctx, p1.retina.linksWidth ?? defaultLinksWidth, ls, pos1, pos2);
});
}