
在 Manim(数学动画引擎) 中,RoundedRectangle 是一个常用的形状类,用于创建带有圆角的矩形。今天我们来学习一下Manim中的圆角矩形,从简单的例子开始做测试,先来看下面的例子。
1.简单例子
就是简单的创建一个蓝色的圆角矩形,里面涉及到了一些参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| from manim import *
class RoundedRectangleExample(Scene): def construct(self): rr = RoundedRectangle( corner_radius=0.5, width=4, height=2, color=BLUE, fill_opacity=0.5 ) self.play(Create(rr)) self.wait()
|
来看演示效果
2.参数分析
RoundedRectangle 圆角矩形的参见参数相对来说比较多,我们逐一分析
2.1.几何参数
1 2 3 4 5 6
| RoundedRectangle( corner_radius=0.5, width=4.0, height=2.0, radius=None, )
|
2.2.颜色与填充
1 2 3 4 5 6 7
| RoundedRectangle( color=WHITE, stroke_color=WHITE, fill_color=None, stroke_opacity=1.0, fill_opacity=0.0, )
|
2.3.描边样式
1 2 3 4 5 6
| RoundedRectangle( stroke_width=4.0, stroke_opacity=1.0, sheen_factor=0.0, sheen_direction=[1, 0, 0], )
|
2.4.效果与背景
1 2 3 4 5 6 7 8 9 10
| RoundedRectangle( background_image=None, background_stroke_color=WHITE, background_stroke_width=0.0, background_stroke_opacity=1.0, grid_xstep=None, grid_ystep=None, shading=None, gloss=0.0, )
|
2.5.3D与渲染
1 2 3 4 5
| RoundedRectangle( shade_in_3d=False, joint_type="auto", flat_stroke=False, )
|
3.简单实例
我们来看一下圆角矩形与普通矩形的比较
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class CompareCorners(Scene): def construct(self): rect = Rectangle( width=2, height=2, color=RED ) rounded = RoundedRectangle( width=2, height=2, corner_radius=0.3, color=BLUE ).next_to(rect, RIGHT, buff=1) labels = VGroup( Text("Rectangle", font_size=24).next_to(rect, DOWN), Text("RoundedRectangle", font_size=24).next_to(rounded, DOWN) ) self.add(rect, rounded, labels)
|
4.复杂实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| from manim import *
class geometry(Scene): def construct(self): params = { "width": 4, "height": 2, "fill_color": BLUE, "fill_opacity": 0.75 } exe1 = RoundedRectangle(**params) text1 = Text("既然都看到这儿了", font="msyh", font_size=28) group1 = VGroup(exe1, text1) self.play(Create(group1)) self.play(group1.animate.move_to(UP * 2))
arrow = Arrow(group1.get_bottom(), ORIGIN) arrow.set_stroke(width=3) arrow.set_color(BLUE) self.play(Create(arrow))
exe2 = RoundedRectangle(**params) text2 = Text("那就点个赞吧:D", font="msyh", font_size=28) group2 = VGroup(exe2, text2) group2.move_to(DOWN) self.play(Create(group2))
total = VGroup( group1, arrow, group2 )
self.play(total.animate.move_to(ORIGIN)) self.wait()
|
来看演示的效果
圆角矩形控制好大小和样式,可以当做其他对象的背景,例如按钮,至于更有意义的作用,等待日后更新文章。
主观感受,仅供参考。本文采用 本文采用 CC BY-NC-SA 4.0 许可 协议,
完整转载请注明来自
数学动画引擎。