manim icon indicating copy to clipboard operation
manim copied to clipboard

Fix code transform animation

Open Mindev27 opened this issue 1 year ago • 4 comments

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)

https://github.com/user-attachments/assets/8f380fd3-6ba2-4062-9c93-e11d28360550

Using CodeTransform (After)

https://github.com/user-attachments/assets/6ba7a7ca-63df-4ef7-9948-c7e88a9fc1d5

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

Mindev27 avatar Jan 18 '25 04:01 Mindev27

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 avatar Jan 21 '25 00:01 behackl

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?

Mindev27 avatar Jan 21 '25 05:01 Mindev27

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.

barollet avatar Jan 24 '25 16:01 barollet

@barollet Thank you for your suggestion. since line numbers are always sequential starting from 0, manual animations only apply FadeIn and FadeOut. So I want to keep numbering animation.

I’ll check if there’s specific text data tied to line numbers, but I don’t think it would change much.

Mindev27 avatar Jan 27 '25 02:01 Mindev27

Hi @behackl, apologies for the long gap on this one! I've reworked the PR along the lines you suggested back then, the transformation is now a method animation on the Code mobject:

self.play(code.animate.update_code(code_string=...))

It's up to date with the latest main and all CI checks are green now. Whenever you have a chance, I'd really appreciate another look. Thanks for your patience!

Mindev27 avatar Jul 01 '26 04:07 Mindev27