Scenes & Manim Integration
MathViz provides first-class support for Manim, the mathematical animation engine. Scene blocks compile directly to Manim Scene classes.
Scene Blocks
A scene block defines a Manim animation:
MathViz
scene MyAnimation {
fn construct(self) {
// Your animation code here
let circle = Circle()
play(Create(circle))
wait(1.0)
}
}
This compiles to:
Python
from manim import *
class MyAnimation(Scene):
def construct(self):
circle = Circle()
self.play(Create(circle))
self.wait(1.0)
Mobjects
Mobjects (mathematical objects) are the building blocks of Manim animations:
Shapes
MathViz
// Basic shapes
let circle = Circle(radius: 2.0)
let square = Square(side_length: 2.0)
let rectangle = Rectangle(width: 3.0, height: 2.0)
let triangle = Triangle()
let line = Line(start: [-2, 0, 0], end: [2, 0, 0])
let arrow = Arrow(start: ORIGIN, end: RIGHT * 2)
let dot = Dot(point: ORIGIN)
// Polygons
let polygon = Polygon([-1, 0, 0], [1, 0, 0], [0, 1, 0])
let regular_polygon = RegularPolygon(n: 6) // Hexagon
Text and Math
MathViz
// Plain text
let text = Text("Hello, World!")
let large_text = Text("Big!", font_size: 72)
// LaTeX math
let equation = MathTex("E = mc^2")
let integral = MathTex("\\int_0^1 x^2 dx = \\frac{1}{3}")
let formula = MathTex("\\sum_{n=1}^{\\infty} \\frac{1}{n^2} = \\frac{\\pi^2}{6}")
// Mixed text and math
let mixed = Tex("The area is $A = \\pi r^2$")
Graphs and Axes
MathViz
// Number line
let number_line = NumberLine(x_range: [-5, 5, 1])
// 2D axes
let axes = Axes(
x_range: [-3, 3, 1],
y_range: [-2, 2, 1],
x_length: 6,
y_length: 4
)
// Plot a function
let graph = axes.plot(|x| x ^ 2, color: BLUE)
let sin_graph = axes.plot(|x| sin(x), color: RED)
Animation Functions
Creation Animations
| Animation | Description |
|---|---|
Create(mobject) | Draw the mobject from nothing |
Write(text) | Write text character by character |
FadeIn(mobject) | Fade in from transparent |
GrowFromCenter(mobject) | Grow from center point |
DrawBorderThenFill(mobject) | Draw border, then fill |
Transformation Animations
| Animation | Description |
|---|---|
Transform(a, b) | Transform mobject a into b |
ReplacementTransform(a, b) | Replace a with b |
MoveToTarget(mobject) | Move to target copy |
Rotate(mobject, angle) | Rotate by angle |
ScaleInPlace(mobject, factor) | Scale in place |
Exit Animations
| Animation | Description |
|---|---|
FadeOut(mobject) | Fade to transparent |
Uncreate(mobject) | Reverse of Create |
ShrinkToCenter(mobject) | Shrink to center point |
The animate Property
Use .animate for method chaining animations:
MathViz
scene AnimateExample {
fn construct(self) {
let square = Square()
play(Create(square))
// Animate method calls
play(square.animate.shift(RIGHT * 2))
play(square.animate.scale(2))
play(square.animate.rotate(PI / 4))
play(square.animate.set_color(RED))
// Chain multiple transformations
play(
square.animate
.shift(LEFT * 4)
.scale(0.5)
.set_color(BLUE)
)
}
}
play() and wait()
MathViz
scene PlayWaitExample {
fn construct(self) {
let circle = Circle()
let square = Square()
// Play a single animation
play(Create(circle))
// Play with custom run_time
play(Transform(circle, square), run_time: 2.0)
// Play multiple animations simultaneously
play(
circle.animate.shift(LEFT * 2),
square.animate.shift(RIGHT * 2)
)
// Wait (pause)
wait() // Default 1 second
wait(2.0) // Wait 2 seconds
}
}
Positioning
Direction Constants
| Constant | Value |
|---|---|
ORIGIN | Center of screen |
UP, DOWN | Vertical directions |
LEFT, RIGHT | Horizontal directions |
UL, UR | Upper left/right |
DL, DR | Down left/right |
Positioning Methods
MathViz
let circle = Circle()
let text = Text("Hello")
// Position relative to another mobject
text.next_to(circle, DOWN) // Below circle
text.next_to(circle, RIGHT, buff: 0.5) // Right with buffer
// Move to specific location
circle.move_to(ORIGIN)
circle.move_to([2, 1, 0])
// Shift by amount
circle.shift(RIGHT * 2)
circle.shift(UP + LEFT)
// Align with edges
circle.to_edge(LEFT)
circle.to_corner(UL)
Colors
Built-in Colors
MathViz provides all Manim color constants:
MathViz
// Primary colors
RED, GREEN, BLUE, YELLOW, ORANGE, PURPLE, PINK, WHITE, BLACK
// Shades
BLUE_A, BLUE_B, BLUE_C, BLUE_D, BLUE_E // Light to dark
RED_A, RED_B, RED_C, RED_D, RED_E
// ... same for other colors
// Usage
let circle = Circle()
circle.set_color(BLUE)
circle.set_fill(BLUE, opacity: 0.5)
circle.set_stroke(WHITE, width: 2)
Complete Example
MathViz
// pythagorean.mviz - Visualize the Pythagorean theorem
scene PythagoreanTheorem {
fn construct(self) {
// Create a right triangle
let triangle = Polygon(
[0, 0, 0],
[3, 0, 0],
[3, 4, 0]
)
triangle.set_stroke(WHITE, width: 2)
// Create squares on each side
let a_square = Square(side_length: 3)
a_square.set_fill(RED, opacity: 0.5)
a_square.next_to(triangle, DOWN, buff: 0)
let b_square = Square(side_length: 4)
b_square.set_fill(GREEN, opacity: 0.5)
b_square.next_to(triangle, RIGHT, buff: 0)
let c_square = Square(side_length: 5)
c_square.set_fill(BLUE, opacity: 0.5)
c_square.rotate(atan(4/3))
c_square.move_to([1.5, 2, 0])
// Labels
let a_label = MathTex("a^2 = 9")
let b_label = MathTex("b^2 = 16")
let c_label = MathTex("c^2 = 25")
a_label.next_to(a_square, DOWN)
b_label.next_to(b_square, RIGHT)
c_label.next_to(c_square, UP)
// Theorem text
let theorem = MathTex("a^2 + b^2 = c^2")
theorem.to_edge(UP)
// Animate!
play(Create(triangle))
wait(0.5)
play(FadeIn(a_square), Write(a_label))
play(FadeIn(b_square), Write(b_label))
play(FadeIn(c_square), Write(c_label))
wait(1.0)
play(Write(theorem))
wait(2.0)
}
}
Run with:
Shell
mathviz run pythagorean.mviz -q h --preview