Created
April 24, 2019 15:00
-
-
Save slightfoot/c868858c16b9aaf9441309af43eeb20d to your computer and use it in GitHub Desktop.
Trim Path Flutter Dart - 5th February 2019
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:ui' show Path, PathMetric, Offset; | |
| Path trimPath(Path source, double percentage) { | |
| /// WARNING: computeMetrics returns a iterator that can only be used ONCE! | |
| percentage = percentage.clamp(0.0, 1.0); | |
| if (percentage == 0.0) return Path(); | |
| if (percentage == 1.0) return Path.from(source); | |
| final dest = Path(); | |
| final target = source.computeMetrics().fold(0.0, (value, metric) => value + metric.length) * percentage; | |
| double start = 0.0; | |
| for (final PathMetric metric in source.computeMetrics()) { | |
| if (start > target) { | |
| break; | |
| } | |
| final length = (target - start).clamp(0.0, metric.length); | |
| dest.addPath(metric.extractPath(0.0, length), Offset.zero); | |
| start += metric.length; | |
| } | |
| return dest; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment