hathibelagal-dev / path_morph_for_flutter Goto Github PK
View Code? Open in Web Editor NEWThis is a Flutter package that lets you smoothly morph one path into another.
Home Page: https://pub.dev/packages/path_morph
License: Apache License 2.0
This is a Flutter package that lets you smoothly morph one path into another.
Home Page: https://pub.dev/packages/path_morph
License: Apache License 2.0
import 'dart:ui';
import 'package:flutter/animation.dart';
import './sampled_path_data.dart';
/// This class has all the methods you need to create your morph animations.
class PathMorph {
/// This method is responsible for sampling both the paths. It generates a
/// [SampledPathData] object containing all the details required for the
/// morph animation.
static SampledPathData samplePaths(Path path1, Path path2,
{double precision = 0.01}) {
var data = SampledPathData();
var k = 0;
var points1Length = 0;
var points2Length = 0;
// Sample path1
path1.computeMetrics().forEach((metric) {
for (var i = 0.0; i < 1.1; i += precision) {
Tangent? tangent = metric.getTangentForOffset(metric.length * i);
if (tangent == null) continue;
Offset position = tangent.position;
data.points1.add(position);
data.shiftedPoints.add(position);
points1Length++;
}
});
// Sample path2
path2.computeMetrics().forEach((metric) {
data.endIndices.add(k);
for (var i = 0.0; i < 1.1; i += precision) {
Tangent? tangent = metric.getTangentForOffset(metric.length * i);
if (tangent == null) continue;
k += 1;
data.points2.add(tangent.position);
points2Length++;
}
});
// Ensure both paths have the same number of sample points
var maxLength = points1Length > points2Length ? points1Length : points2Length;
// If path1 has fewer points than the maximum length, add points to match
if (points1Length < maxLength) {
var lastPoint = data.points1.last;
for (var i = 0; i < maxLength - points1Length; i++) {
data.points1.add(lastPoint);
data.shiftedPoints.add(lastPoint);
}
}
// If path2 has fewer points than the maximum length, add points to match
if (points2Length < maxLength) {
var lastPoint = data.points2.last;
for (var i = 0; i < maxLength - points2Length; i++) {
data.points2.add(lastPoint);
}
}
return data;
}
/// Generates a bunch of animations that are responsible for moving
/// all the points of paths into the right positions.
static void generateAnimations(
AnimationController controller, SampledPathData data, Function func) {
for (var i = 0; i < data.points1.length; i++) {
var start = data.points1[i];
var end = data.points2[i];
var tween = Tween(begin: start, end: end);
var animation = tween.animate(controller);
animation.addListener(() {
func(i, animation.value);
});
}
}
/// Generates a path using the [SampledPathData] object.
/// You can use this path while drawing the frames of
/// the morph animation on your canvas.
static Path generatePath(SampledPathData data) {
Path p = Path();
for (var i = 0; i < data.shiftedPoints.length; i++) {
for (var j = 0; j < data.endIndices.length; j++) {
if (i == data.endIndices[j]) {
p.moveTo(data.shiftedPoints[i].dx, data.shiftedPoints[i].dy);
break;
}
}
p.lineTo(data.shiftedPoints[i].dx, data.shiftedPoints[i].dy);
}
return p;
}
}
Track the Number of Sampled Points: Variables points1Length
and points2Length
were introduced to track the number of sampled points in each path. This ensures that both paths have the same number of points, which is crucial for generating a smooth morph animation.
Ensure Equal Number of Points: After sampling both paths, the code checks if the number of sampled points in path1
and path2
are equal. If not, it adds the last sampled point repeatedly to the path with fewer points until both paths have an equal number of points. This ensures that each point in path1
has a corresponding point in path2
for the animation.
These changes ensure that the morphing animation can proceed smoothly without discrepancies in the number of points between the two paths, which could otherwise lead to unexpected behavior or visual glitches in the animation.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.