Coder Social home page Coder Social logo

hathibelagal-dev / path_morph_for_flutter Goto Github PK

View Code? Open in Web Editor NEW
38.0 0.0 8.0 1.11 MB

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

Dart 11.54% Kotlin 0.24% Swift 3.28% Objective-C 0.07% CMake 35.52% C++ 43.17% C 2.70% HTML 3.49%
animation flutter path

path_morph_for_flutter's Issues

Code modification suggestions

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;
  }
}

Explanation of Changes

  1. 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.

  2. 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.