Last active
July 31, 2024 22:56
-
-
Save uwezi/3271ad9c6f63c7e77911b9c5cdbb91b6 to your computer and use it in GitHub Desktop.
[Text animation] Text sliding up letter by letter. #manim #text #animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://discord.com/channels/581738731934056449/1268142621180690483/1268142621180690483 | |
| from manim import * | |
| class SlideUpLetterByLetter(Animation): | |
| def __init__( | |
| self, | |
| text: Text, | |
| lag: float=0.4, | |
| buff: float = 0.0, | |
| **kwargs, | |
| ) -> None: | |
| self.lag = lag # lag between neighboring chars | |
| self.slope = 1/(1+(len(text)-1)*self.lag) # normalized time per transition | |
| self.text = text | |
| self.rect = SurroundingRectangle(text, fill_opacity=1, stroke_width=0, color=text.get_color(),buff=buff) | |
| self.bottom = self.rect.get_bottom()[1] | |
| self.deltay = text.get_top()[1]-self.bottom | |
| text.shift(self.deltay*DOWN) | |
| self.obj = text.copy() | |
| super().__init__(self.obj, **kwargs) | |
| def interpolate_mobject(self, alpha: float) -> None: | |
| dummy = self.text.copy() | |
| for i, letter in enumerate(dummy): | |
| individual_alpha = (alpha/self.slope-i*self.lag) | |
| individual_alpha = min(1, max(0, individual_alpha)) | |
| letter.shift((self.deltay * rate_functions.smoothstep(individual_alpha))*UP) | |
| letter.become(Intersection(self.rect,letter, fill_opacity=1, stroke_width=0)) | |
| self.obj.become(dummy) | |
| class SlideUpExample(Scene): | |
| def construct(self): | |
| text = Tex("Hello World!").scale(2).shift(2*UP) | |
| self.play(SlideUpLetterByLetter(text[0], lag=0.7, buff=1), run_time=4) | |
| eqn = MathTex(r"x_{1,2}=-\frac{p}{2}\pm\sqrt{\frac{p^2}{4}-q}").scale(2).shift(DOWN) | |
| self.play(SlideUpLetterByLetter(eqn[0], lag=0.2), run_time=4) | |
| self.wait() | |
| class SlideDownLetterByLetter(Animation): | |
| def __init__( | |
| self, | |
| text: MathTex, | |
| lag: float = 0.4, | |
| buff: float = 0.0, | |
| **kwargs, | |
| ) -> None: | |
| self.lag = lag # lag between neighboring chars | |
| self.slope = 1 / (1 + (len(text) - 1) * self.lag) # normalized time per transition | |
| self.text = text | |
| self.rect = SurroundingRectangle(text, fill_opacity=1, stroke_width=0, color=text.get_color(), buff=buff) | |
| self.top = text.get_top()[1] | |
| self.deltay = self.top - self.rect.get_bottom()[1] | |
| self.obj = text.copy() | |
| super().__init__(self.obj, **kwargs) | |
| def interpolate_mobject(self, alpha: float) -> None: | |
| dummy = self.text.copy() | |
| for i, letter in enumerate(dummy): | |
| individual_alpha = (alpha / self.slope - (len(dummy) - 1 - i) * self.lag) | |
| individual_alpha = min(1, max(0, individual_alpha)) | |
| letter.shift((self.deltay * rate_functions.smoothstep(individual_alpha)) * DOWN) | |
| letter.become(Intersection(self.rect,letter, fill_opacity=1, stroke_width=0)) | |
| self.obj.become(dummy) | |
| class SlideDownExample(Scene): | |
| def construct(self): | |
| text = MathTex(r"1000000000000000").scale(2).shift(2*UP) | |
| self.wait(0.3) | |
| self.play(SlideDownLetterByLetter(text[0], lag=0.3, buff=1), run_time=1) | |
| self.wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
