@carto/kepler.gl 0.0.65-alpha.labels.8 → 0.0.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,215 +0,0 @@
1
- import {CompositeLayer} from '@deck.gl/core';
2
- import {TextLayer, _TextBackgroundLayer as TextBackgroundLayer} from '@deck.gl/layers';
3
-
4
- // TODO remove prior to release
5
- const SHOW_TEXT_BORDER = false;
6
-
7
- const [LEFT, TOP, RIGHT, BOTTOM] = [0, 1, 2, 3];
8
-
9
- class EnhancedTextBackgroundLayer extends TextBackgroundLayer {
10
- getShaders() {
11
- const shaders = super.getShaders();
12
- let vs = shaders.vs;
13
-
14
- // Modify shader so that the padding is offset by the pixel offset to ensure the padding
15
- // always captures the anchor point. As padding is uniform we cannot pass it a per-label value
16
- vs = vs.replaceAll('padding.', '_padding.');
17
- vs = vs.replace(
18
- 'void main(void) {',
19
- 'void main(void) {\n vec4 _padding = padding + instancePixelOffsets.xyxy * vec4(1.0, 1.0, -1.0, -1.0);'
20
- );
21
-
22
- return {...shaders, vs};
23
- }
24
- }
25
-
26
- // TextLayer which includes modified text-background-layer-vertex shader and only renders the
27
- // primary background layer in the collision pass
28
- class EnhancedTextLayer extends TextLayer {
29
- filterSubLayer({layer, renderPass}) {
30
- const background = layer.id.includes('primary-background');
31
- if (renderPass === 'collision') {
32
- return background; // Only draw primary background layer in collision pass
33
- } else {
34
- return !background || (SHOW_TEXT_BORDER && this.props.getBorderWidth); // Do not draw background layer in other passes
35
- }
36
- }
37
- }
38
-
39
- const defaultProps = {
40
- ...TextLayer.defaultProps,
41
- getRadius: {type: 'accessor', value: 1},
42
- radiusScale: {type: 'number', min: 0, value: 1}
43
- };
44
-
45
- export class PointLabelLayer extends CompositeLayer {
46
- static layerName = 'PointLabelLayer';
47
- static defaultProps = defaultProps;
48
-
49
- calculatePixelOffset(secondary) {
50
- const {
51
- getTextAnchor: anchor,
52
- getAlignmentBaseline: alignment,
53
- getRadius,
54
- getSecondaryText,
55
- radiusScale,
56
- secondarySizeScale,
57
- sizeScale
58
- } = this.props;
59
- const xMult = anchor === 'middle' ? 0 : anchor === 'start' ? 1 : -1;
60
- const yMult = alignment === 'center' ? 0 : alignment === 'bottom' ? 1 : -1;
61
-
62
- // Padding based on font size (font size / 4)
63
- const xPadding = sizeScale / 4;
64
- const yPadding = sizeScale * (1 + 1 / 4);
65
-
66
- // Place secondary label under main label (secondary label always 'top' baseline aligned)
67
- const secondaryOffset = 0.6 * (1 - yMult) * sizeScale;
68
- let yOffset = secondary ? secondaryOffset : 0;
69
-
70
- // Special case, position relative to secondary label
71
- if (anchor === 'middle' && alignment === 'top' && getSecondaryText) {
72
- yOffset -= secondaryOffset;
73
- yOffset -= secondarySizeScale;
74
- yOffset += sizeScale;
75
- }
76
-
77
- // Padding based on point radius (radius/ 4)
78
- const radiusPadding = 1 + 1 / 4;
79
-
80
- return typeof getRadius === 'function'
81
- ? (d, info) => {
82
- const r = (info ? getRadius(d, info) : 1) * radiusScale * radiusPadding;
83
- return [xMult * (r + xPadding), yMult * (r + yPadding) + yOffset];
84
- }
85
- : [
86
- xMult * (getRadius * radiusScale * radiusPadding + xPadding),
87
- yMult * (getRadius * radiusScale * radiusPadding + yPadding) + yOffset
88
- ];
89
- }
90
-
91
- calculateBackgroundPadding() {
92
- const {getTextAnchor: anchor, getAlignmentBaseline: alignment, sizeScale} = this.props;
93
-
94
- // Heuristics to avoid label overlap (should we base this on the text length?)
95
- const paddingX = 12 * sizeScale;
96
- const paddingY = 3 * sizeScale;
97
- const backgroundPadding = [0, 0, 0, 0];
98
- if (alignment === 'top') {
99
- backgroundPadding[TOP] = paddingY;
100
- } else if (alignment === 'bottom') {
101
- backgroundPadding[BOTTOM] = paddingY;
102
- } else {
103
- backgroundPadding[TOP] = 0.5 * paddingY;
104
- backgroundPadding[BOTTOM] = 0.5 * paddingY;
105
- }
106
- if (anchor === 'start') {
107
- backgroundPadding[LEFT] = paddingX;
108
- } else if (anchor === 'end') {
109
- backgroundPadding[RIGHT] = paddingX;
110
- } else {
111
- backgroundPadding[LEFT] = 0.5 * paddingX;
112
- backgroundPadding[RIGHT] = 0.5 * paddingX;
113
- }
114
-
115
- return backgroundPadding;
116
- }
117
-
118
- renderTextLayer(id, {updateTriggers: updateTriggersOverride, ...props}) {
119
- const {
120
- data,
121
-
122
- characterSet,
123
- fontFamily,
124
- fontSettings,
125
- fontWeight,
126
- outlineColor,
127
- outlineWidth,
128
- sizeScale,
129
- radiusScale,
130
-
131
- getAlignmentBaseline,
132
- getColor,
133
- getPosition,
134
- getTextAnchor,
135
-
136
- updateTriggers
137
- } = this.props;
138
-
139
- return new EnhancedTextLayer(
140
- this.getSubLayerProps({
141
- id,
142
- data: data,
143
-
144
- characterSet,
145
- fontFamily,
146
- fontSettings,
147
- fontWeight,
148
- outlineColor,
149
- outlineWidth,
150
- sizeScale,
151
-
152
- getAlignmentBaseline,
153
- getColor,
154
- getPosition,
155
- getTextAnchor,
156
-
157
- updateTriggers: {
158
- ...updateTriggers,
159
- ...updateTriggersOverride,
160
- getPixelOffset: [
161
- updateTriggers.getRadius,
162
- updateTriggers.getTextAnchor,
163
- updateTriggers.getAlignmentBaseline,
164
- radiusScale,
165
- sizeScale
166
- ]
167
- }
168
- }),
169
- {
170
- getSize: 1,
171
- _subLayerProps: {background: {type: EnhancedTextBackgroundLayer}}
172
- },
173
- // DEBUG
174
- SHOW_TEXT_BORDER
175
- ? {getBackgroundColor: [0, 0, 0, 0], getBorderColor: [255, 0, 0, 80], getBorderWidth: 1}
176
- : {},
177
- props
178
- );
179
- }
180
-
181
- renderLayers() {
182
- const {
183
- getText,
184
- getSecondaryColor,
185
- getSecondaryText,
186
- secondaryOutlineColor,
187
- secondarySizeScale,
188
- updateTriggers
189
- } = this.props;
190
- const getPixelOffset = this.calculatePixelOffset(false);
191
- const backgroundPadding = this.calculateBackgroundPadding();
192
- const out = [
193
- this.renderTextLayer('primary', {
194
- backgroundPadding,
195
- getText,
196
- getPixelOffset,
197
- background: true // Only use background for primary label for faster collisions
198
- }),
199
- getSecondaryText &&
200
- this.renderTextLayer('secondary', {
201
- getText: getSecondaryText,
202
- getPixelOffset: this.calculatePixelOffset(true),
203
- getAlignmentBaseline: 'top',
204
- updateTriggers: {getText: updateTriggers.getSecondaryText},
205
-
206
- // Optional overrides
207
- ...(getSecondaryColor && {getColor: getSecondaryColor}),
208
- ...(secondarySizeScale && {sizeScale: secondarySizeScale}),
209
- ...(secondaryOutlineColor && {outlineColor: secondaryOutlineColor})
210
- })
211
- ];
212
-
213
- return out;
214
- }
215
- }