Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from manim import *
- class Shuriken3(Scene):
- def construct(self):
- # Base square
- side_len = 1
- opacity = 0
- square = Square(side_length=side_len, color=WHITE, fill_opacity=0)
- # Half of the side length, for positioning
- h = side_len / 2
- # Four triangles (one on each side)
- triangles = []
- # Top triangle
- tri_top = Polygon(
- [ -h, h, 0 ], # left corner of top side
- [ h, h, 0 ], # right corner of top side
- [ 0, h + side_len, 0 ], # tip above
- color=WHITE, fill_opacity=opacity
- )
- triangles.append(tri_top)
- # Bottom triangle
- tri_bottom = Polygon(
- [ -h, -h, 0 ],
- [ h, -h, 0 ],
- [ 0, -h - side_len, 0 ],
- color=WHITE, fill_opacity=opacity
- )
- triangles.append(tri_bottom)
- # Left triangle
- tri_left = Polygon(
- [ -h, h, 0 ],
- [ -h, -h, 0 ],
- [ -h - side_len, 0, 0 ],
- color=WHITE, fill_opacity=opacity
- )
- triangles.append(tri_left)
- # Right triangle
- tri_right = Polygon(
- [ h, h, 0 ],
- [ h, -h, 0 ],
- [ h + side_len, 0, 0 ],
- color=WHITE, fill_opacity=opacity
- )
- triangles.append(tri_right)
- # Circle for end transition
- end_circle = Circle(radius = 1.5,color=WHITE,fill_opacity=0)
- # Group square + triangle
- #shuriken_shape = VGroup(square, *triangles)
- # Animate all with overlap (lag_ratio controls how much they overlap)
- self.add(square)
- #self.wait(1)
- self.play(
- AnimationGroup(
- Create(tri_right),
- Create(tri_top),
- Create(tri_left),
- Create(tri_bottom),
- lag_ratio = 0.2
- ),
- run_time = 1
- )
- # Save states of triangles so they can continuously be restored during animation
- tri_right.save_state()
- #tri_left.save_state()
- #tri_top.save_state()
- #tri_bottom.save_state()
- def update_triangle(mob, start_pos, end_pos, rotation_angle, alpha):
- mob.restore()
- mob.shift(interpolate(start_pos, end_pos, alpha))
- mob.rotate(interpolate(0, rotation_angle, alpha), about_point=mob.get_center())
- # Save states so mob.restore() has a baseline
- for t in triangles:
- t.save_state()
- self.play(
- AnimationGroup(
- UpdateFromAlphaFunc(tri_top, lambda m, a: update_triangle(m, UP*0, UP*1, PI, a)),
- UpdateFromAlphaFunc(tri_bottom, lambda m, a: update_triangle(m, DOWN*0, DOWN*1, PI, a)),
- UpdateFromAlphaFunc(tri_left, lambda m, a: update_triangle(m, LEFT*0, LEFT*1, PI, a)),
- UpdateFromAlphaFunc(tri_right, lambda m, a: update_triangle(m, RIGHT*0, RIGHT*1, PI, a)),
- lag_ratio=0.1
- ),
- run_time=1
- )
- self.play(
- AnimationGroup(
- Rotate(square,-45*DEGREES,about_point=ORIGIN),
- Rotate(tri_right,45*DEGREES,about_point=ORIGIN),
- Rotate(tri_top,45*DEGREES,about_point=ORIGIN),
- Rotate(tri_left,45*DEGREES,about_point=ORIGIN),
- Rotate(tri_bottom,45*DEGREES,about_point=ORIGIN),
- lag_ratio=0.07,
- run_time=0.7
- )
- )
- self.play(
- AnimationGroup(
- tri_right.animate.shift(DOWN*np.sqrt(4.5)+LEFT*np.sqrt(4.5)),
- tri_left.animate.shift(UP*np.sqrt(4.5)+RIGHT*np.sqrt(4.5)),
- tri_top.animate.shift(DOWN*np.sqrt(4.5)+RIGHT*np.sqrt(4.5)),
- tri_bottom.animate.shift(UP*np.sqrt(4.5)+LEFT*np.sqrt(4.5)),
- Create(end_circle)),
- run_time=0.4
- )
- # Last rotation to return to default state
- angle = 405 + 15 #little overshoot
- self.play(
- AnimationGroup(
- Rotate(square,-angle*DEGREES,about_point=ORIGIN),
- Rotate(tri_right,-angle*DEGREES,about_point=ORIGIN),
- Rotate(tri_left,-angle*DEGREES,about_point=ORIGIN),
- Rotate(tri_bottom,-angle*DEGREES,about_point=ORIGIN),
- Rotate(tri_top,-angle*DEGREES,about_point=ORIGIN),
- run_time=1.5
- )
- )
- # Make circle fade away again
- angle = -15
- self.play(
- FadeOut(end_circle,runtime=0.5),
- AnimationGroup(
- Rotate(square,-angle*DEGREES,about_point=ORIGIN),
- Rotate(tri_right,-angle*DEGREES,about_point=ORIGIN),
- Rotate(tri_left,-angle*DEGREES,about_point=ORIGIN),
- Rotate(tri_bottom,-angle*DEGREES,about_point=ORIGIN),
- Rotate(tri_top,-angle*DEGREES,about_point=ORIGIN),
- run_time=0.5
- )
- )
- self.play(
- AnimationGroup(
- Uncreate(tri_right),
- Uncreate(tri_top),
- Uncreate(tri_left),
- Uncreate(tri_bottom),
- #Uncreate(end_circle),
- #Rotate(square,angle=PI/2,rate_func=rate_functions.ease_in_out_back),
- #square.scale(0.7),
- #square.scale(1/0.7),
- #Uncreate(square,reverse_rate_function=rate_functions.exponential_decay),
- #Create(square),
- square.animate.rotate(angle=180*DEGREES,about_point=ORIGIN),
- run_time = 1.5,
- lag_ratio = 0.3
- )
- )
- #self.play(
- # Rotate(square,angle=PI/2,run_time=0.3,rate_func=rate_functions.ease_in_out_back)
- #)
- # Maybe reactive this last part again but it looks more snappy if the triangles start flying faster
- '''
- self.play(
- AnimationGroup(
- FadeOut(tri_right),
- FadeOut(tri_top),
- FadeOut(tri_left),
- FadeOut(tri_bottom),
- #FadeOut(end_circle),
- run_time = 1,
- lag_ratio = 0.2
- )
- )
- '''
Advertisement
Add Comment
Please, Sign In to add comment