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

Allow bounce easings along a path to return a position outside of the path #2457

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class PathKeyframeAnimation extends KeyframeAnimation<PointF> {
private final PointF point = new PointF();
private final float[] pos = new float[2];
private final float[] tangent = new float[2];
private final PathMeasure pathMeasure = new PathMeasure();
private PathKeyframe pathMeasureKeyframe;

Expand Down Expand Up @@ -39,8 +40,23 @@ pathKeyframe.startValue, pathKeyframe.endValue, getLinearCurrentKeyframeProgress
pathMeasureKeyframe = pathKeyframe;
}

pathMeasure.getPosTan(keyframeProgress * pathMeasure.getLength(), pos, null);
point.set(pos[0], pos[1]);
// allow bounce easings to calculate positions outside the path
// by using the tangent at the extremities

float length = pathMeasure.getLength();

if (keyframeProgress < 0) {
pathMeasure.getPosTan(0, pos, tangent);
float tangentAmount = keyframeProgress * length;
point.set(pos[0] + tangent[0] * tangentAmount, pos[1] + tangent[1] * tangentAmount);
} else if (keyframeProgress > 1) {
pathMeasure.getPosTan(length, pos, tangent);
float tangentAmount = (keyframeProgress - 1) * length;
point.set(pos[0] + tangent[0] * tangentAmount, pos[1] + tangent[1] * tangentAmount);
} else {
pathMeasure.getPosTan(keyframeProgress * length, pos, null);
point.set(pos[0], pos[1]);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: since getPosTan() already clamps to end points, you could consolidate some calls

float distance =  keyframeProgress * length;
pathMeasure.getPosTan(distance, pos, tangent);
point.set(pos[0], pos[1]);

if (distance < 0) {
  point.offset(tangent[0] * distance, tangent[1] * distance);
} else if (distance > length) {
  point.offset(tangent[0] * (distance - length), tangent[1] * (distance - length));
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @fmalita
@idlewan want to put up another PR for this or would you like me to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, thanks for proposing a cleaner version!
Here's the PR: #2459

return point;
}
}
Loading
Loading