Skip to content

Fix Write() stroke width to scale with font_size or object scale #4222

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

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

Conversation

irvanalhaq9
Copy link
Contributor

@irvanalhaq9 irvanalhaq9 commented Apr 19, 2025

Overview: What does this pull request change?

  1. Fix Write() creates strokes that do not scale with font_size or scale #4220
  2. Add docstring

Motivation and Explanation: Why and how do your changes improve the library?

Previously, the stroke effect was hardcoded and appeared too thick on small fonts and too thin on large ones.

This PR introduces a helper method _adjust_stroke_width_for_text() that dynamically adjusts the stroke width based on the font size of the text, proportionally scaled to 25% of the default stroke width.

The adjustment only applies when:

  • The the font_size is too small, such as below 20, so zooming-in is needed.
  • The the font_size is too big, but this is actually not a problem, but scaling stroke_width make it look better.
  • The stroke_width is not explicitly provided by the user.

For all other cases, the stroke_width is 2.0 (default from DrawBorderThenFill), and behaves like before this change.

Links to added or changed documentation pages

https://manimce--4222.org.readthedocs.build/en/4222/reference/manim.animation.creation.Write.html

Further Information and Comments

Test:

class WriteScaleStrokeWidth(MovingCameraScene):
    def construct(self):
        self.add(NumberPlane())
        test1 = MathTex(r"\text{MatText Test}").scale(4).set_color(RED)
        self.play(Write(test1,run_time=2))
        
        test2 = Text("Text Test").scale(0.1).set_color(BLUE).next_to(test1, DOWN*2)
        camera_frame = self.camera.frame
        self.play(
            camera_frame.animate(rate_func=linear).set(width=test2.width+0.2).move_to(test2)
        )
        wr = Write(test2,run_time=2)
        self.play(wr)
        self.wait()

Before the fix:

WriteBug2.mp4

After the fix:

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

@github-project-automation github-project-automation bot moved this to 🆕 New in Dev Board Apr 19, 2025
@irvanalhaq9 irvanalhaq9 force-pushed the fix-write-stroke-scaling branch 2 times, most recently from 7f108a6 to e786eb6 Compare April 20, 2025 11:18
@irvanalhaq9 irvanalhaq9 force-pushed the fix-write-stroke-scaling branch from 3c7c210 to b236cac Compare April 21, 2025 17:23
@irvanalhaq9
Copy link
Contributor Author

irvanalhaq9 commented Apr 21, 2025

I'm still not sure why the tests are failing — the code works fine on my local machine. Is there something in my code that I need to change?

Edit:
I've addressed the failed test by scaling stroke_width only if font_size is too small or too large, and the vmobject has a non-zero height.

My investigation:

  1. Too small in normal Scene appear: the font_size < 20, so zooming-in is needed.
  2. Too big is actually not a problem, but scaling stroke_width make it look better.
  3. If the mobject height is zero, accessing font_size causes ZeroDivisionError.

For other cases, it behaves like before.

@irvanalhaq9 irvanalhaq9 force-pushed the fix-write-stroke-scaling branch from a0786b4 to 04cc8be Compare April 23, 2025 09:40
@irvanalhaq9 irvanalhaq9 force-pushed the fix-write-stroke-scaling branch from 7b6ae92 to 40987d5 Compare April 23, 2025 10:51
@irvanalhaq9 irvanalhaq9 force-pushed the fix-write-stroke-scaling branch from 9db7521 to 7eddf9e Compare April 23, 2025 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

Successfully merging this pull request may close these issues.

Write() creates strokes that do not scale with font_size or scale
1 participant