diff --git a/lib/src/layer/polyline_layer/painter.dart b/lib/src/layer/polyline_layer/painter.dart index 4656f83a4..945f39c0e 100644 --- a/lib/src/layer/polyline_layer/painter.dart +++ b/lib/src/layer/polyline_layer/painter.dart @@ -48,7 +48,6 @@ class _PolylinePainter extends CustomPainter points: projectedPolyline.points, shift: shift, ); - if (!areOffsetsVisible(offsets)) return WorldWorkControl.invisible; final strokeWidth = polyline.useStrokeWidthInMeter ? metersToScreenPixels( @@ -56,6 +55,11 @@ class _PolylinePainter extends CustomPainter polyline.strokeWidth, ) : polyline.strokeWidth; + + if (!areOffsetsVisible(offsets, strokeWidth)) { + return WorldWorkControl.invisible; + } + final hittableDistance = math.max( strokeWidth / 2 + polyline.borderStrokeWidth / 2, minimumHitbox, @@ -133,18 +137,6 @@ class _PolylinePainter extends CustomPainter points: projectedPolyline.points, shift: shift, ); - if (!areOffsetsVisible(offsets)) return WorldWorkControl.invisible; - - final hash = polyline.renderHashCode; - if (needsLayerSaving || (lastHash != null && lastHash != hash)) { - drawPaths(); - } - lastHash = hash; - needsLayerSaving = polyline.color.a < 1 || - (polyline.gradientColors?.any((c) => c.a < 1) ?? false); - - // strokeWidth, or strokeWidth + borderWidth if relevant. - late double largestStrokeWidth; late final double strokeWidth; if (polyline.useStrokeWidthInMeter) { @@ -155,7 +147,20 @@ class _PolylinePainter extends CustomPainter } else { strokeWidth = polyline.strokeWidth; } - largestStrokeWidth = strokeWidth; + + if (!areOffsetsVisible(offsets, strokeWidth)) + return WorldWorkControl.invisible; + + final hash = polyline.renderHashCode; + if (needsLayerSaving || (lastHash != null && lastHash != hash)) { + drawPaths(); + } + lastHash = hash; + needsLayerSaving = polyline.color.a < 1 || + (polyline.gradientColors?.any((c) => c.a < 1) ?? false); + + // strokeWidth, or strokeWidth + borderWidth if relevant. + double largestStrokeWidth = strokeWidth; final isSolid = polyline.pattern == const StrokePattern.solid(); final isDashed = polyline.pattern.segments != null; diff --git a/lib/src/layer/shared/feature_layer_utils.dart b/lib/src/layer/shared/feature_layer_utils.dart index 981eb775e..0fbd6057f 100644 --- a/lib/src/layer/shared/feature_layer_utils.dart +++ b/lib/src/layer/shared/feature_layer_utils.dart @@ -26,7 +26,7 @@ mixin FeatureLayerUtils on CustomPainter { /// Determine whether the specified offsets are visible within the viewport /// /// Always returns `false` if the specified list is empty. - bool areOffsetsVisible(Iterable offsets) { + bool areOffsetsVisible(Iterable offsets, [double margin = 0]) { if (offsets.isEmpty) { return false; } @@ -43,7 +43,12 @@ mixin FeatureLayerUtils on CustomPainter { if (maxX < offset.dx) maxX = offset.dx; if (maxY < offset.dy) maxY = offset.dy; } - return viewportRect.overlaps(Rect.fromLTRB(minX, minY, maxX, maxY)); + return viewportRect.overlaps(Rect.fromLTRB( + minX - margin, + minY - margin, + maxX + margin, + maxY + margin, + )); } /// Perform the callback in all world copies (until stopped)