Coverage for src/manimgx/mobjects/logo.py: 100%

76 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-10-26 05:52 +0000

1# SPDX-FileCopyrightText: 2026 Academa, Inc. 

2# SPDX-FileCopyrightText: 2024 the Manim Community Developers 

3# SPDX-FileCopyrightText: 2018 3Blue1Brown LLC 

4# SPDX-License-Identifier: MIT 

5 

6"""Ported from Manim CE 0.21 (MIT); `expand` re-derived as a function of its progress.""" 

7 

8from __future__ import annotations 

9 

10from warnings import deprecated 

11 

12__all__ = ["ManimBanner"] 

13from typing import ClassVar, Literal, Unpack 

14 

15import numpy as np 

16 

17from manimgx import constants as cst 

18from manimgx.animation.easing import ease_in_out_cubic, linear, smooth, squish_rate_func 

19from manimgx.animation.motion import Create, FadeIn, SpiralIn 

20from manimgx.animation.timeline import Animation, AnimationGroup, AnimationOptions 

21from manimgx.mobject import VGroup, override_animation 

22from manimgx.mobjects.shapes import Circle, Square, Triangle 

23from manimgx.mobjects.svg import VMobjectFromSVGPath 

24 

25# the logo's letters as SVG path data, parsed when a banner is made 

26MANIM_SVG_PATHS = ( 

27 ( 

28 "M4.64259-2.092154L2.739726-6.625156C2.660025-6.824408 2.650062-6.824408" 

29 " 2.381071-6.824408H.52802C.348692-6.824408 .199253-6.824408" 

30 " .199253-6.645081C.199253-6.475716 .37858-6.475716" 

31 " .428394-6.475716C.547945-6.475716 .816936-6.455791" 

32 " 1.036115-6.37609V-1.05604C1.036115-.846824 1.036115-.408468" 

33 " .358655-.348692C.169365-.328767 .169365-.18929 .169365-.179328C.169365 0" 

34 " .328767 0 .508095 0H2.052304C2.231631 0 2.381071 0" 

35 " 2.381071-.179328C2.381071-.268991 2.30137-.33873" 

36 " 2.221669-.348692C1.454545-.408468 1.454545-.826899" 

37 " 1.454545-1.05604V-6.017435L1.464508-6.027397L3.895392-.209215C3.975093-.029888" 

38 " 4.044832 0 4.104608 0C4.224159 0 4.254047-.079701" 

39 " 4.303861-.199253L6.744707-6.027397L6.75467-6.017435V-1.05604C6.75467-.846824" 

40 " 6.75467-.408468 6.07721-.348692C5.88792-.328767 5.88792-.18929" 

41 " 5.88792-.179328C5.88792 0 6.047323 0 6.22665 0H8.886675C9.066002 0 9.215442 0" 

42 " 9.215442-.179328C9.215442-.268991 9.135741-.33873" 

43 " 9.05604-.348692C8.288917-.408468 8.288917-.826899" 

44 " 8.288917-1.05604V-5.768369C8.288917-5.977584 8.288917-6.41594" 

45 " 8.966376-6.475716C9.066002-6.485679 9.155666-6.535492" 

46 " 9.155666-6.645081C9.155666-6.824408 9.006227-6.824408" 

47 " 8.826899-6.824408H6.90411C6.645081-6.824408 6.625156-6.824408" 

48 " 6.535492-6.615193L4.64259-2.092154ZM4.343711-1.912827C4.423412-1.743462" 

49 " 4.433375-1.733499" 

50 " 4.552927-1.693649L4.11457-.637609H4.094645L1.823163-6.057285C1.77335-6.1868" 

51 " 1.693649-6.356164" 

52 " 1.554172-6.475716H2.420922L4.343711-1.912827ZM1.334994-.348692H1.165629C1.185554-.37858" 

53 " 1.205479-.408468 1.225405-.428394C1.235367-.438356 1.235367-.448319" 

54 " 1.24533-.458281L1.334994-.348692ZM7.103362-6.475716H8.159402C7.940224-6.22665" 

55 " 7.940224-5.967621 7.940224-5.788294V-1.036115C7.940224-.856787" 

56 " 7.940224-.597758 8.169365-.348692H6.884184C7.103362-.597758 7.103362-.856787" 

57 " 7.103362-1.036115V-6.475716Z" 

58 ), 

59 ( 

60 "M1.464508-4.024907C1.464508-4.234122 1.743462-4.393524" 

61 " 2.092154-4.393524C2.669988-4.393524 2.929016-4.124533" 

62 " 2.929016-3.516812V-2.789539C1.77335-2.440847 .249066-2.042341" 

63 " .249066-.916563C.249066-.308842 .71731 .139477 1.354919 .139477C1.92279" 

64 " .139477 2.381071-.059776 2.929016-.557908C3.038605-.049813 3.257783 .139477" 

65 " 3.745953 .139477C4.174346 .139477 4.483188-.019925" 

66 " 4.861768-.428394L4.712329-.637609L4.612702-.537983C4.582814-.508095" 

67 " 4.552927-.498132 4.503113-.498132C4.363636-.498132 4.293898-.587796" 

68 " 4.293898-.747198V-3.347447C4.293898-4.184309 3.536737-4.712329" 

69 " 2.321295-4.712329C1.195517-4.712329 .438356-4.204234" 

70 " .438356-3.457036C.438356-3.048568 .67746-2.799502" 

71 " 1.085928-2.799502C1.484433-2.799502 1.763387-3.038605" 

72 " 1.763387-3.377335C1.763387-3.676214 1.464508-3.88543" 

73 " 1.464508-4.024907ZM2.919054-.996264C2.650062-.687422 2.450809-.56787" 

74 " 2.211706-.56787C1.912827-.56787 1.703611-.836862" 

75 " 1.703611-1.235367C1.703611-1.8132 2.122042-2.231631" 

76 " 2.919054-2.440847V-.996264Z" 

77 ), 

78 ( 

79 "M2.948941-4.044832C3.297634-4.044832 3.466999-3.775841" 

80 " 3.466999-3.217933V-.806974C3.466999-.438356 3.337484-.278954" 

81 " 2.998755-.239103V0H5.339975V-.239103C4.951432-.268991 4.851806-.388543" 

82 " 4.851806-.806974V-3.307597C4.851806-4.164384 4.323786-4.712329" 

83 " 3.506849-4.712329C2.909091-4.712329 2.450809-4.433375" 

84 " 2.082192-3.845579V-4.592777H.179328V-4.353674C.617684-4.283935" 

85 " .707347-4.184309 .707347-3.765878V-.836862C.707347-.418431 .627646-.328767" 

86 " .179328-.239103V0H2.580324V-.239103C2.211706-.288917 2.092154-.438356" 

87 " 2.092154-.806974V-3.466999C2.092154-3.576588 2.530511-4.044832" 

88 " 2.948941-4.044832Z" 

89 ), 

90 ( 

91 "M2.15193-4.592777H.239103V-4.353674C.67746-4.26401 .767123-4.174346" 

92 " .767123-3.765878V-.836862C.767123-.428394 .697385-.348692" 

93 " .239103-.239103V0H2.6401V-.239103C2.291407-.288917 2.15193-.428394" 

94 " 2.15193-.806974V-4.592777ZM1.454545-6.884184C1.026152-6.884184" 

95 " .67746-6.535492 .67746-6.117061C.67746-5.668742 1.006227-5.339975" 

96 " 1.444583-5.339975S2.221669-5.668742 2.221669-6.107098C2.221669-6.535492" 

97 " 1.882939-6.884184 1.454545-6.884184Z" 

98 ), 

99 ( 

100 "M2.929016-4.044832C3.317559-4.044832 3.466999-3.815691" 

101 " 3.466999-3.217933V-.806974C3.466999-.398506 3.35741-.268991" 

102 " 2.988792-.239103V0H5.32005V-.239103C4.971357-.278954 4.851806-.428394" 

103 " 4.851806-.806974V-3.466999C4.851806-3.576588 5.310087-4.044832" 

104 " 5.69863-4.044832C6.07721-4.044832 6.22665-3.805729" 

105 " 6.22665-3.217933V-.806974C6.22665-.388543 6.117061-.268991" 

106 " 5.738481-.239103V0H8.109589V-.239103C7.721046-.259029 7.611457-.37858" 

107 " 7.611457-.806974V-3.307597C7.611457-4.164384 7.083437-4.712329" 

108 " 6.266501-4.712329C5.69863-4.712329 5.32005-4.483188" 

109 " 4.801993-3.845579C4.503113-4.473225 4.154421-4.712329" 

110 " 3.526775-4.712329S2.440847-4.443337" 

111 " 2.062267-3.845579V-4.592777H.179328V-4.353674C.617684-4.293898" 

112 " .707347-4.174346 .707347-3.765878V-.836862C.707347-.428394 .617684-.318804" 

113 " .179328-.239103V0H2.550436V-.239103C2.201743-.288917 2.092154-.428394" 

114 " 2.092154-.806974V-3.466999C2.092154-3.58655 2.530511-4.044832" 

115 " 2.929016-4.044832Z" 

116 ), 

117) 

118 

119 

120@deprecated("Manim's logo, kept for Manim CE's examples", category=None) 

121class ManimBanner(VGroup): 

122 """The Manim logo: a large "M" beside a green circle, a blue square and a red 

123 triangle, which [create][manimgx.ManimBanner.create] brings in and 

124 [expand][manimgx.ManimBanner.expand] opens into the banner: the wordmark "Manim" 

125 beside the shapes. 

126 

127 The letters are light, for a dark background, unless `dark_theme` is False. The 

128 letters "anim" are not submobjects until `expand` sets them after the "M", at the 

129 banner's size wherever it is. Playing [Create][manimgx.Create] on the banner plays 

130 its `create`. 

131 

132 Args: 

133 dark_theme: Whether the letters are light (`#ece6e2`), for a dark background, 

134 or dark (`#343434`), for a light one. 

135 

136 Examples: 

137 ```python 

138 import manimgx as m 

139 

140 

141 class ManimBannerExample(m.Scene): 

142 def construct(self) -> None: 

143 banner = m.ManimBanner() 

144 self.play(banner.create()) 

145 self.play(banner.expand()) 

146 ``` 

147 """ 

148 

149 def __init__(self, dark_theme: bool = True): 

150 import svgelements as se 

151 

152 super().__init__() 

153 logo_green = "#81b29a" 

154 logo_blue = "#454866" 

155 logo_red = "#e07a5f" 

156 self.font_color = "#ece6e2" if dark_theme else "#343434" 

157 self.M = ( 

158 VMobjectFromSVGPath(se.Path(MANIM_SVG_PATHS[0])).flip(cst.RIGHT).center() 

159 ) 

160 self.M.set(stroke_width=0).scale( 

161 7 * cst.DEFAULT_FONT_SIZE * cst.SCALE_FACTOR_PER_FONT_POINT 

162 ) 

163 self.M.set_fill(color=self.font_color, opacity=1).shift( 

164 2.25 * cst.LEFT + 1.5 * cst.UP 

165 ) 

166 self.built_m_height = ( 

167 self.M.height 

168 ) # the banner's scale: its M's height over this 

169 self.circle = Circle(color=logo_green, fill_opacity=1).shift(cst.LEFT) 

170 self.square = Square(color=logo_blue, fill_opacity=1).shift(cst.UP) 

171 self.triangle = Triangle(color=logo_red, fill_opacity=1).shift(cst.RIGHT) 

172 self.shapes = VGroup(self.triangle, self.square, self.circle) 

173 self.add(self.shapes, self.M) 

174 self.move_to(cst.ORIGIN) 

175 anim = VGroup() # "anim": sized and set beside the M when the banner expands 

176 for ind, path in enumerate(MANIM_SVG_PATHS[1:]): 

177 tex = VMobjectFromSVGPath(se.Path(path)).flip(cst.RIGHT).center() 

178 tex.set(stroke_width=0).scale( 

179 cst.DEFAULT_FONT_SIZE * cst.SCALE_FACTOR_PER_FONT_POINT 

180 ) 

181 if ind > 0: 

182 tex.next_to(anim, buff=0.01) 

183 tex.align_to(self.M, cst.DOWN) 

184 anim.add(tex) 

185 anim.set_fill(color=self.font_color, opacity=1) 

186 self.anim = anim 

187 

188 @override_animation(Create) 

189 def create(self, run_time: float = 2) -> AnimationGroup: 

190 """Make the animation that brings the logo in: the shapes spiral in while the 

191 "M" fades in, from a tenth to six tenths of the run time. 

192 

193 Both begin at once, so the logo starts from nothing, even if it is already 

194 shown. [Create][manimgx.Create] plays it for the banner. 

195 

196 Args: 

197 run_time: How long it takes, in seconds. 

198 

199 Returns: 

200 A new animation. 

201 """ 

202 return AnimationGroup( 

203 SpiralIn(self.shapes, run_time=run_time), 

204 FadeIn( 

205 self.M, 

206 run_time=0.6 * run_time, 

207 rate_func=squish_rate_func(smooth, 1 / 6, 1), 

208 ), 

209 ) 

210 

211 def expand( 

212 self, 

213 run_time: float = 1.5, 

214 direction: Literal["left", "right", "center"] = "center", 

215 ) -> Animation: 

216 """Make the animation that opens the logo into the banner: the "M" and the 

217 shapes part, a little too far and back, uncovering "anim" after the "M" to spell 

218 "Manim". 

219 

220 It plays from the banner as it is when it begins, scaled or moved: the "M" and 

221 the shapes part along x over the first two thirds of the run time, eased in and 

222 out, then settle back by an overshoot, every length in proportion to the 

223 banner's size. The letters travel with the "M", behind the shapes, each shown 

224 once the square's middle has passed its own, and come in front where the parts 

225 are furthest apart and nothing overlaps. Once open, the banner's parts are the 

226 shapes, the "M" and "anim", in that order. 

227 

228 Args: 

229 run_time: How long it takes, in seconds. 

230 direction: What holds still: the "M" ("right": the banner opens to the 

231 right), the shapes ("left"), or the banner's middle ("center": each 

232 moves half as far). 

233 

234 Returns: 

235 A new animation. 

236 

237 Examples: 

238 ```python 

239 import manimgx as m 

240 

241 

242 class ManimBannerExpandExample(m.Scene): 

243 def construct(self) -> None: 

244 banners = [m.ManimBanner().scale(0.5) for _ in range(3)] 

245 for banner, y in zip(banners, (2.5, 0, -2.5)): 

246 self.add(banner.shift(y * m.UP)) 

247 self.play( 

248 banners[0].expand(direction="left"), 

249 banners[1].expand(direction="center"), 

250 banners[2].expand(direction="right"), 

251 ) 

252 ``` 

253 """ 

254 return _Expand(self, _SHARES[direction], run_time=run_time) 

255 

256 

257# of the parting, what the wordmark and the shapes each move 

258_SHARES = {"right": (0.0, 1.0), "center": (-0.5, 0.5), "left": (-1.0, 0.0)} 

259 

260 

261class _Expand(Animation[ManimBanner]): 

262 """The logo opening into the banner: a function of its progress, from the banner as it 

263 begins. 

264 

265 The wordmark (the M, "anim" set after it) and the shapes part along x by (6.25 + 0.8)·s over 

266 the first two thirds, eased in and out, then settle back by the 0.8·s overshoot (s: the 

267 banner's scale, its M's height over the height it is built at). The letters travel with the 

268 M behind the shapes, each shown once the square's middle has passed its own. Furthest apart 

269 nothing overlaps: there the letters come in front of the shapes, as the M is. It reorders 

270 the banner's parts, so it acts on the banner itself, updaters and all (no model). 

271 """ 

272 

273 defaults: ClassVar[AnimationOptions] = { 

274 "rate_func": linear, # its two phases ease themselves 

275 "suspend_mobject_updating": False, 

276 } 

277 parting = 2 / 3 # of its progress; then it settles 

278 

279 def __init__( 

280 self, 

281 banner: ManimBanner, 

282 shares: tuple[float, float], 

283 **kwargs: Unpack[AnimationOptions], 

284 ) -> None: 

285 self.shares = shares 

286 super().__init__(banner, **kwargs) 

287 

288 def begin(self) -> None: 

289 b = self.mobject 

290 scale = b.M.height / b.built_m_height 

291 b.anim.height = 0.75748 * b.M.height 

292 b.anim.next_to(b.M, buff=0.06 * scale).align_to(b.M, cst.DOWN) 

293 self.settled, self.overshoot = 6.25 * scale, 0.8 * scale 

294 square = b.square.get_x() 

295 self.letters = [ # each: how far apart the square passes it; its paints 

296 (letter, letter.get_x() - square, letter.paint, letter.set_opacity(0).paint) 

297 for letter in b.anim 

298 ] 

299 self.moving = [ # each leaf: where it begins, its share of the parting 

300 (leaf, leaf._geometry, share) 

301 for share, part in zip(self.shares, (VGroup(b.M, b.anim), b.shapes)) 

302 for leaf in part.family_members_with_points() 

303 ] 

304 b.add_to_back(b.anim) 

305 self.take() 

306 

307 def interpolate_mobject(self, alpha: float) -> None: 

308 t = self.rate_func(alpha) 

309 parting = t < self.parting 

310 furthest = self.settled + self.overshoot 

311 apart = ( 

312 furthest * ease_in_out_cubic(t / self.parting) 

313 if parting 

314 else furthest 

315 - self.overshoot * smooth((t - self.parting) / (1 - self.parting)) 

316 ) 

317 for leaf, geometry, share in self.moving: 

318 leaf._geometry = geometry.translated(np.array([share * apart, 0.0, 0.0])) 

319 for letter, shown_at, shown, unseen in self.letters: 

320 letter.paint = shown if not parting or apart > shown_at else unseen 

321 b = self.mobject 

322 b.submobjects = [b.anim, b.shapes, b.M] if parting else [b.shapes, b.M, b.anim]