Skip to content

Commit 597b6ba

Browse files
Calculate polyline visibility based on strokeWidth
1 parent dc24088 commit 597b6ba

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

lib/src/layer/polyline_layer/painter.dart

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ class _PolylinePainter<R extends Object> extends CustomPainter
4848
points: projectedPolyline.points,
4949
shift: shift,
5050
);
51-
if (!areOffsetsVisible(offsets)) return WorldWorkControl.invisible;
5251

5352
final strokeWidth = polyline.useStrokeWidthInMeter
5453
? metersToScreenPixels(
55-
projectedPolyline.polyline.points.first,
56-
polyline.strokeWidth,
57-
)
54+
projectedPolyline.polyline.points.first,
55+
polyline.strokeWidth,
56+
)
5857
: polyline.strokeWidth;
58+
59+
if (!areOffsetsVisible(offsets, strokeWidth)) return WorldWorkControl.invisible;
60+
5961
final hittableDistance = math.max(
6062
strokeWidth / 2 + polyline.borderStrokeWidth / 2,
6163
minimumHitbox,
@@ -133,18 +135,6 @@ class _PolylinePainter<R extends Object> extends CustomPainter
133135
points: projectedPolyline.points,
134136
shift: shift,
135137
);
136-
if (!areOffsetsVisible(offsets)) return WorldWorkControl.invisible;
137-
138-
final hash = polyline.renderHashCode;
139-
if (needsLayerSaving || (lastHash != null && lastHash != hash)) {
140-
drawPaths();
141-
}
142-
lastHash = hash;
143-
needsLayerSaving = polyline.color.a < 1 ||
144-
(polyline.gradientColors?.any((c) => c.a < 1) ?? false);
145-
146-
// strokeWidth, or strokeWidth + borderWidth if relevant.
147-
late double largestStrokeWidth;
148138

149139
late final double strokeWidth;
150140
if (polyline.useStrokeWidthInMeter) {
@@ -155,7 +145,19 @@ class _PolylinePainter<R extends Object> extends CustomPainter
155145
} else {
156146
strokeWidth = polyline.strokeWidth;
157147
}
158-
largestStrokeWidth = strokeWidth;
148+
149+
if (!areOffsetsVisible(offsets, strokeWidth)) return WorldWorkControl.invisible;
150+
151+
final hash = polyline.renderHashCode;
152+
if (needsLayerSaving || (lastHash != null && lastHash != hash)) {
153+
drawPaths();
154+
}
155+
lastHash = hash;
156+
needsLayerSaving = polyline.color.a < 1 ||
157+
(polyline.gradientColors?.any((c) => c.a < 1) ?? false);
158+
159+
// strokeWidth, or strokeWidth + borderWidth if relevant.
160+
double largestStrokeWidth = strokeWidth;
159161

160162
final isSolid = polyline.pattern == const StrokePattern.solid();
161163
final isDashed = polyline.pattern.segments != null;

lib/src/layer/shared/feature_layer_utils.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mixin FeatureLayerUtils on CustomPainter {
2626
/// Determine whether the specified offsets are visible within the viewport
2727
///
2828
/// Always returns `false` if the specified list is empty.
29-
bool areOffsetsVisible(Iterable<Offset> offsets) {
29+
bool areOffsetsVisible(Iterable<Offset> offsets, [double margin = 0]) {
3030
if (offsets.isEmpty) {
3131
return false;
3232
}
@@ -43,7 +43,12 @@ mixin FeatureLayerUtils on CustomPainter {
4343
if (maxX < offset.dx) maxX = offset.dx;
4444
if (maxY < offset.dy) maxY = offset.dy;
4545
}
46-
return viewportRect.overlaps(Rect.fromLTRB(minX, minY, maxX, maxY));
46+
return viewportRect.overlaps(Rect.fromLTRB(
47+
minX - margin,
48+
minY - margin,
49+
maxX + margin,
50+
maxY + margin,
51+
));
4752
}
4853

4954
/// Perform the callback in all world copies (until stopped)

0 commit comments

Comments
 (0)