d9ec43e607
Lay the running highlight out by rounded-border arc length so it advances at a steady linear speed without a loop jump, while preserving the existing colours and reduced-motion border. Add focused geometry tests and implementation notes.
114 lines
3.6 KiB
Dart
114 lines
3.6 KiB
Dart
import 'dart:math' as math;
|
|
import 'dart:ui' show PathMetric;
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// Arc-length geometry for the composer's running border.
|
|
///
|
|
/// The highlight is laid out by distance travelled along the outline, not by
|
|
/// angle around the centre, so equal time steps move it equal lengths around
|
|
/// a rounded rectangle: no racing along the long edges, no crawling at ends.
|
|
///
|
|
/// One lap is `progress` in [0, 1]; progress 1 is the same place as 0, so the
|
|
/// loop restarts without a jump.
|
|
|
|
/// The colour bands, as fractions of a lap measured forward from the origin.
|
|
/// They match the original sweep's stops and colours.
|
|
const List<double> runningHighlightStops = [0, .12, .24, .38, 1];
|
|
|
|
/// Half a device pixel of overlap so neighbouring segments leave no seam.
|
|
const double _seamOverlap = .5;
|
|
|
|
/// One piece of the outline and the highlight colour drawn over it.
|
|
class RunningArcSegment {
|
|
const RunningArcSegment({
|
|
required this.start,
|
|
required this.end,
|
|
required this.color,
|
|
});
|
|
|
|
/// Arc length from the outline's start where the piece begins.
|
|
final double start;
|
|
|
|
/// Arc length where the piece ends (exclusive, may overlap by a seam).
|
|
final double end;
|
|
|
|
final Color color;
|
|
}
|
|
|
|
/// The arc length of the highlight's origin along an outline of [length].
|
|
double runningHighlightOrigin(double progress, double length) {
|
|
final lap = progress - progress.floorToDouble();
|
|
return lap * length;
|
|
}
|
|
|
|
/// The highlight colour [fraction] of a lap ahead of its origin. Fractions
|
|
/// wrap, so `f` and `f + 1` are the same colour.
|
|
Color runningHighlightColorAt(double fraction, Color primary, Color tertiary) {
|
|
final colors = [
|
|
primary.withValues(alpha: 0),
|
|
primary,
|
|
tertiary,
|
|
tertiary.withValues(alpha: 0),
|
|
primary.withValues(alpha: 0),
|
|
];
|
|
final f = fraction - fraction.floorToDouble();
|
|
for (var i = 1; i < runningHighlightStops.length; i++) {
|
|
final stop = runningHighlightStops[i];
|
|
if (f <= stop) {
|
|
final from = runningHighlightStops[i - 1];
|
|
final t = (f - from) / (stop - from);
|
|
return Color.lerp(colors[i - 1], colors[i], t)!;
|
|
}
|
|
}
|
|
return colors.last;
|
|
}
|
|
|
|
/// Splits an outline of [length] into pieces about [step] long and colours
|
|
/// each by where it sits in the highlight at [progress]. Pieces with no
|
|
/// visible colour are left out.
|
|
List<RunningArcSegment> runningHighlightSegments({
|
|
required double progress,
|
|
required double length,
|
|
required Color primary,
|
|
required Color tertiary,
|
|
double step = 3,
|
|
}) {
|
|
if (length <= 0) return const [];
|
|
final count = math.max(1, (length / step).ceil());
|
|
final size = length / count;
|
|
final origin = runningHighlightOrigin(progress, length);
|
|
final segments = <RunningArcSegment>[];
|
|
for (var i = 0; i < count; i++) {
|
|
final mid = (i + .5) * size;
|
|
final color = runningHighlightColorAt(
|
|
(mid - origin) / length,
|
|
primary,
|
|
tertiary,
|
|
);
|
|
if (color.a == 0) continue;
|
|
segments.add(
|
|
RunningArcSegment(
|
|
start: math.max(0, i * size - _seamOverlap),
|
|
end: math.min(length, (i + 1) * size + _seamOverlap),
|
|
color: color,
|
|
),
|
|
);
|
|
}
|
|
return segments;
|
|
}
|
|
|
|
/// The part of an outline between arc lengths [start] and [end]. The outline
|
|
/// may be made of several contours, so the range is taken across all of them.
|
|
Path runningArcPath(List<PathMetric> metrics, double start, double end) {
|
|
final out = Path();
|
|
var offset = 0.0;
|
|
for (final metric in metrics) {
|
|
final from = math.max(start, offset) - offset;
|
|
final to = math.min(end, offset + metric.length) - offset;
|
|
if (to > from) out.addPath(metric.extractPath(from, to), Offset.zero);
|
|
offset += metric.length;
|
|
}
|
|
return out;
|
|
}
|