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 code transform animation #4114

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

Mindev27
Copy link

@Mindev27 Mindev27 commented Jan 18, 2025

Fixed #4103

I added a new CodeTransform function. The current Transform with Code looked weird, but now it works much better.

The logic is as follows:

When applying CodeTransform from before_code to after_code:

  • Split both before_code and after_code into lines and remove leading indentation.
  • Use a brute-force algorithm to match exactly the same lines. (This naive approach has a time complexity of O(N²), but since the typical code length is not very long, I believe it works fine in practice.)
  • If a line in before_code does not match, it will be removed with a FadeOut animation. If a line in after_code does not match, it will be added with a FadeIn animation. This transition is visually presented through animation.

This is my first PR! Any advice or suggestions will be appreciated :)

Below are videos demonstrating the difference before and after applying the CodeTransform.

Using Transform (Before)

CodeAnimation.mp4

Using CodeTransform (After)

CodeAnimation.mp4

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

@behackl
Copy link
Member

behackl commented Jan 21, 2025

Hey there! Thanks for looking into this!

I do like this new animation, but I am not convinced that it should be implemented like this, in a separate module with a separate class.

Now that the Code mobject has been reimplemented in #4115, it might be an interesting idea to add a method to the mobject that allows changing the rendered code snippet, and then provide a custom animate animation for it.

That is:

self.play(code_mob.animate.update_code(code_string=..., code_file=...))

would play the animation you have implemented here. Now, maybe passing a Code mobject might be a better idea, then this would be similar to the existing become method. But still, I think implementing your transformation as a method animation makes more sense.

What do you think?

@behackl behackl added the new feature Enhancement specifically adding a new feature (feature request should be used for issues instead) label Jan 21, 2025
@behackl behackl added this to the v0.19.1 milestone Jan 21, 2025
@Mindev27
Copy link
Author

Thank you for the feedback!

I understand that the Code mobject has been updated, and I also see the distinction between CodeTransform and the existing become method. I'd like to proceed by integrating my transformation into the animate method.

Would it be okay to continue this way?

@barollet
Copy link

Hi ! I think that the line numbering should also be transformed manually. Common numbers are transformed individually, new numbers are faded in and old numbers are faded out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature Enhancement specifically adding a new feature (feature request should be used for issues instead)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code Transform Animation applies to the entire code instead of the changed lines
3 participants