Files
pre-commit-ci[bot] 859d48ad2e [pre-commit.ci] pre-commit autoupdate (#270)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/psf/black: 23.7.0 → 23.9.1](https://github.com/psf/black/compare/23.7.0...23.9.1)
- [github.com/astral-sh/ruff-pre-commit: v0.0.287 → v0.0.288](https://github.com/astral-sh/ruff-pre-commit/compare/v0.0.287...v0.0.288)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-09-12 16:03:46 +02:00

4.0 KiB

Examples

Contents of example.py.

Do not forget to import Manim Slides and Manim or ManimGL:

from manim import *
from manim_slides import Slide, ThreeDSlide

or

from manimlib import *
from manim_slides import Slide, ThreeDSlide

Then, each presentation, named SCENE, was generated with those two commands:

manim example.py SCENE # or manimgl example SCENE
manim-slides convert SCENE scene.html -ccontrols=true

where -ccontrols=true indicates that we want to display the blue navigation arrows.

Basic Example

Basic example from quickstart.

.. manim-slides:: ../../../example.py:BasicExample
    :hide_source:
    :quality: high

.. literalinclude:: ../../../example.py
   :language: python
   :linenos:
   :pyobject: BasicExample

3D Example

Example using 3D camera. As Manim and ManimGL handle 3D differently, definitions are slightly different.

With Manim

.. manim-slides:: ../../../example.py:ThreeDExample
    :hide_source:
    :quality: high

.. literalinclude:: ../../../example.py
   :language: python
   :linenos:
   :dedent: 4
   :start-after: [manim-3d]
   :end-before: [manim-3d]

With ManimGL

.. literalinclude:: ../../../example.py
   :language: python
   :linenos:
   :dedent: 4
   :start-after: [manimgl-3d]
   :end-before: [manimgl-3d]

Subclass Custom Scenes

For compatibility reasons, Manim Slides only provides subclasses for Scene and ThreeDScene. However, subclassing other scene classes is totally possible, and very simple to do actually!

For example, you can subclass the MovingCameraScene class from manim with the following code:

:linenos:

from manim import *
from manim_slides import Slide


class MovingCameraSlide(Slide, MovingCameraScene):
    pass

And later use this class anywhere in your code:

:linenos:

class SubclassExample(MovingCameraSlide):
    """Example taken from ManimCE's docs."""

    def construct(self):
        self.camera.frame.save_state()

        ax = Axes(x_range=[-1, 10], y_range=[-1, 10])
        graph = ax.plot(lambda x: np.sin(x), color=WHITE, x_range=[0, 3 * PI])

        dot_1 = Dot(ax.i2gp(graph.t_min, graph))
        dot_2 = Dot(ax.i2gp(graph.t_max, graph))
        self.add(ax, graph, dot_1, dot_2)

        self.play(self.camera.frame.animate.scale(0.5).move_to(dot_1))
        self.next_slide()
        self.play(self.camera.frame.animate.move_to(dot_2))
        self.next_slide()
        self.play(Restore(self.camera.frame))
        self.wait()

:::{note} If you do not plan to reuse MovingCameraSlide more than once, then you can directly write the construct method in the body of MovingCameraSlide. :::

.. manim-slides:: SubclassExample
    :hide_source:
    :quality: high

    from manim import *
    from manim_slides import Slide


    class MovingCameraSlide(Slide, MovingCameraScene):
        pass

    class SubclassExample(MovingCameraSlide):
        def construct(self):
            self.camera.frame.save_state()

            ax = Axes(x_range=[-1, 10], y_range=[-1, 10])
            graph = ax.plot(lambda x: np.sin(x), color=WHITE, x_range=[0, 3 * PI])

            dot_1 = Dot(ax.i2gp(graph.t_min, graph))
            dot_2 = Dot(ax.i2gp(graph.t_max, graph))
            self.add(ax, graph, dot_1, dot_2)

            self.play(self.camera.frame.animate.scale(0.5).move_to(dot_1))
            self.next_slide()
            self.play(self.camera.frame.animate.move_to(dot_2))
            self.next_slide()
            self.play(Restore(self.camera.frame))
            self.wait()

Advanced Example

A more advanced example is ConvertExample, which is used as demo slide and tutorial.

.. manim-slides:: ../../../example.py:ConvertExample
    :hide_source:
    :quality: high

.. literalinclude:: ../../../example.py
   :language: python
   :linenos:
   :pyobject: ConvertExample