Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added dedicated _AngleTween to improve rotation animation #28

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions lib/src/animated_map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,7 @@ class AnimatedMapController {
double startRotation = this.rotation;
double endRotation = effectiveRotation;

// If the difference between the bearings is greater than 180 degrees,
// add or subtract 360 degrees to one of them to make the shortest
// rotation direction counterclockwise.
final diff = endRotation - startRotation;
if (diff > 180.0) {
startRotation += 360.0;
} else if (diff < -180.0) {
endRotation += 360.0;
}

final rotateTween = Tween<double>(
final rotateTween = _AngleTween(
begin: startRotation,
end: endRotation,
);
Expand Down Expand Up @@ -392,3 +382,15 @@ class AnimatedMapController {
);
}
}

class _AngleTween extends Tween<double> {
_AngleTween({required double super.begin, required double super.end});

@override
double lerp(double t) => begin! + _angleDifference(begin!, end!) * t;

static double _angleDifference(double angle1, double angle2) {
final diff = (angle2 - angle1 + 180) % 360 - 180;
return diff < -180 ? diff + 360 : diff;
}
}