-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathedge-label-layer.ts
More file actions
50 lines (47 loc) · 1.92 KB
/
Copy pathedge-label-layer.ts
File metadata and controls
50 lines (47 loc) · 1.92 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
// deck.gl-community
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import {CompositeLayer} from '@deck.gl/core';
import {ZoomableTextLayer} from '../common-layers/zoomable-text-layer/zoomable-text-layer';
export class EdgeLabelLayer extends CompositeLayer {
static layerName = 'EdgeLabelLayer';
renderLayers() {
const {data, getLayoutInfo, positionUpdateTrigger = 0, stylesheet, transitions} =
this.props as any;
return [
new ZoomableTextLayer(
this.getSubLayerProps({
id: 'edge-label-layer',
data,
getPosition: (e) => {
const {sourcePosition, targetPosition, controlPoints = []} = getLayoutInfo(e);
// consider all the points on this edge
const allPoints = [sourcePosition, targetPosition, ...controlPoints];
const sumX = allPoints.reduce((res, p) => res + p[0], 0);
const sumY = allPoints.reduce((res, p) => res + p[1], 0);
// find the centroid of those points
return [sumX / allPoints.length, sumY / allPoints.length];
},
getAngle: (e) => {
const {sourcePosition, targetPosition} = getLayoutInfo(e);
// sort the nodes from left to right
const [newSourcePosition, newTargetPosition] =
sourcePosition[0] < targetPosition[0]
? [sourcePosition, targetPosition]
: [targetPosition, sourcePosition];
// angle in degrees
const deltaX = newTargetPosition[0] - newSourcePosition[0];
const deltaY = newTargetPosition[1] - newSourcePosition[1];
return (Math.atan2(deltaY, deltaX) * -180) / Math.PI;
},
...stylesheet.getDeckGLAccessors(),
transitions,
updateTriggers: {
...stylesheet.getDeckGLUpdateTriggers(),
getPosition: positionUpdateTrigger
}
})
)
];
}
}