
在 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, # corner_radius的别名(不建议使用) )
|
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, # 光泽效果强度 [0-1] 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, # 网格背景X步长 grid_ystep=None, # 网格背景Y步长 shading=None, # 着色参数 gloss=0.0, # 光泽度 )
|
2.5.3D与渲染
1 2 3 4 5
| RoundedRectangle( shade_in_3d=False, # 是否在3D中显示阴影 joint_type="auto", # 连接点类型("auto", "round", "bevel") flat_stroke=False, # 平面描边(非3D) )
|
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))
# 修改箭头起点为group1的底部 arrow = Arrow(group1.get_bottom(), ORIGIN) arrow.set_stroke(width=3) # 将宽度从0改为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()
|
来看演示的效果
圆角矩形控制好大小和样式,可以当做其他对象的背景,例如按钮,至于更有意义的作用,等待日后更新文章。