You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Manim时如何在已有对象的背景层绘制新图形?

Manim 平行四边形填充层遮挡箭头的解决方法

Manim 中对象的渲染层级由 z_index 属性控制,数值越高的对象越优先显示在上层,后添加到场景的对象默认 z_index 会高于之前添加的对象,因此你后绘制的带填充的平行四边形会覆盖先绘制的箭头,下面是两种可直接落地的解决方案:


方案1:创建多边形时直接指定层级

在初始化 Polygon 时添加 z_index=-1 参数,将平行四边形的层级设置为低于默认层级的箭头即可,修改后的对应代码行如下:

poly = Polygon( *parallelogram, color = PURPLE, fill_opacity = 0.5, z_index=-1 )

该方案无需额外操作,播放填充动画的全程都不会出现遮挡箭头的问题。


方案2:调用层级调整方法移动到下层

如果你不想提前指定层级,也可以在多边形添加到场景后,调用 bring_to_back() 方法将其移动到所有已有对象的最下层,修改后的末尾代码如下:

self.play( Write( poly ) )
poly.bring_to_back()
self.wait()

如果希望动画过程中就不出现遮挡,可以提前创建好多边形并调整层级后再播放动画:

poly = Polygon( *parallelogram, color = PURPLE, fill_opacity = 0.5 )
poly.bring_to_back()
self.play( Write( poly ) )

完整可运行修改版代码(采用方案1)

from manim import *
import numpy as np

class DrawParallelogram( Scene ):
    def construct( self ):
        o = np.array( [ 0, -2, 0 ] )
        p1 = np.array( [ 3, 1, 0 ] ) # u
        p2 = np.array( [ 1, 3, 0 ] ) # v
        op1 = o + p1
        op2 = o + p2
        op3 = o + p1 + p2

        v1 = Arrow( start = o, end = op1, buff = 0, color = RED ) # u
        v2 = Arrow( start = o, end = op2, buff = 0, color = YELLOW ) # v
        v1p = Arrow( start = op2, end = op3, buff = 0, color = RED ) # u'
        v2p = Arrow( start = op1, end = op3, buff = 0, color = YELLOW ) # v'

        parallelogram = [ o, op1, op3, op2 ]
        # 新增z_index参数设置层级
        poly = Polygon( *parallelogram, color = PURPLE, fill_opacity = 0.5, z_index=-1 )

        self.play( AnimationGroup( Write( v1 ), Write( v2 ), Write( v1p ), Write( v2p ) ) )
        self.wait( )
        self.play( Write( poly ) )
        self.wait()

内容的提问来源于stack exchange,提问作者Peeter Joot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 15:39:00