|
| 1 | +r"""Mobjects that inherit from lines and contain a label along the length.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +__all__ = ["LabeledLine", "LabeledArrow"] |
| 6 | + |
| 7 | +from manim.constants import * |
| 8 | +from manim.mobject.geometry.line import Arrow, Line |
| 9 | +from manim.mobject.geometry.shape_matchers import ( |
| 10 | + BackgroundRectangle, |
| 11 | + SurroundingRectangle, |
| 12 | +) |
| 13 | +from manim.mobject.text.tex_mobject import MathTex, Tex |
| 14 | +from manim.mobject.text.text_mobject import Text |
| 15 | +from manim.utils.color import WHITE, Color |
| 16 | + |
| 17 | + |
| 18 | +class LabeledLine(Line): |
| 19 | + """Constructs a line containing a label box somewhere along its length. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + label : str | Tex | MathTex | Text |
| 24 | + Label that will be displayed on the line. |
| 25 | + label_position : float | optional |
| 26 | + A ratio in the range [0-1] to indicate the position of the label with respect to the length of the line. Default value is 0.5. |
| 27 | + font_size : float | optional |
| 28 | + Control font size for the label. This parameter is only used when `label` is of type `str`. |
| 29 | + label_color: numpy.ndarray | optional |
| 30 | + The color of the label's text. This parameter is only used when `label` is of type `str`. |
| 31 | + label_frame : Bool | optional |
| 32 | + Add a `SurroundingRectangle` frame to the label box. |
| 33 | + frame_fill_color : numpy.ndarray | optional |
| 34 | + Background color to fill the label box. If no value is provided, the background color of the canvas will be used. |
| 35 | + frame_fill_opacity : float | optional |
| 36 | + Determine the opacity of the label box by passing a value in the range [0-1], where 0 indicates complete transparency and 1 means full opacity. |
| 37 | +
|
| 38 | + .. seealso:: |
| 39 | + :class:`LabeledArrow` |
| 40 | +
|
| 41 | + Examples |
| 42 | + -------- |
| 43 | + .. manim:: LabeledLineExample |
| 44 | + :save_last_frame: |
| 45 | +
|
| 46 | + class LabeledLineExample(Scene): |
| 47 | + def construct(self): |
| 48 | + line = LabeledLine( |
| 49 | + label = '0.5', |
| 50 | + label_position = 0.8, |
| 51 | + font_size = 20, |
| 52 | + label_color = WHITE, |
| 53 | + label_frame = True, |
| 54 | +
|
| 55 | + start=LEFT+DOWN, |
| 56 | + end=RIGHT+UP) |
| 57 | +
|
| 58 | +
|
| 59 | + line.set_length(line.get_length() * 2) |
| 60 | + self.add(line) |
| 61 | + """ |
| 62 | + |
| 63 | + def __init__( |
| 64 | + self, |
| 65 | + label: str | Tex | MathTex | Text, |
| 66 | + label_position: float = 0.5, |
| 67 | + font_size: float = DEFAULT_FONT_SIZE, |
| 68 | + label_color: Color | str | None = WHITE, |
| 69 | + label_frame: bool = True, |
| 70 | + frame_fill_color: Color | str | None = None, |
| 71 | + frame_fill_opacity: float = 1, |
| 72 | + *args, |
| 73 | + **kwargs, |
| 74 | + ) -> None: |
| 75 | + if isinstance(label, str): |
| 76 | + from manim import MathTex |
| 77 | + |
| 78 | + rendered_label = MathTex(label, color=label_color, font_size=font_size) |
| 79 | + else: |
| 80 | + rendered_label = label |
| 81 | + |
| 82 | + super().__init__(*args, **kwargs) |
| 83 | + |
| 84 | + # calculating the vector for the label position |
| 85 | + line_start, line_end = self.get_start_and_end() |
| 86 | + new_vec = (line_end - line_start) * label_position |
| 87 | + label_coords = line_start + new_vec |
| 88 | + |
| 89 | + # rendered_label.move_to(self.get_vector() * label_position) |
| 90 | + rendered_label.move_to(label_coords) |
| 91 | + |
| 92 | + box = BackgroundRectangle( |
| 93 | + rendered_label, |
| 94 | + buff=0.05, |
| 95 | + color=frame_fill_color, |
| 96 | + fill_opacity=frame_fill_opacity, |
| 97 | + stroke_width=0.5, |
| 98 | + ) |
| 99 | + self.add(box) |
| 100 | + |
| 101 | + if label_frame: |
| 102 | + box_frame = SurroundingRectangle( |
| 103 | + rendered_label, buff=0.05, color=label_color, stroke_width=0.5 |
| 104 | + ) |
| 105 | + |
| 106 | + self.add(box_frame) |
| 107 | + |
| 108 | + self.add(rendered_label) |
| 109 | + |
| 110 | + |
| 111 | +class LabeledArrow(LabeledLine, Arrow): |
| 112 | + """Constructs an arrow containing a label box somewhere along its length. |
| 113 | + This class inherits its label properties from `LabeledLine`, so the main parameters controlling it are the same. |
| 114 | +
|
| 115 | + Parameters |
| 116 | + ---------- |
| 117 | + label : str | Tex | MathTex | Text |
| 118 | + Label that will be displayed on the line. |
| 119 | + label_position : float | optional |
| 120 | + A ratio in the range [0-1] to indicate the position of the label with respect to the length of the line. Default value is 0.5. |
| 121 | + font_size : float | optional |
| 122 | + Control font size for the label. This parameter is only used when `label` is of type `str`. |
| 123 | + label_color: numpy.ndarray | optional |
| 124 | + The color of the label's text. This parameter is only used when `label` is of type `str`. |
| 125 | + label_frame : Bool | optional |
| 126 | + Add a `SurroundingRectangle` frame to the label box. |
| 127 | + frame_fill_color : numpy.ndarray | optional |
| 128 | + Background color to fill the label box. If no value is provided, the background color of the canvas will be used. |
| 129 | + frame_fill_opacity : float | optional |
| 130 | + Determine the opacity of the label box by passing a value in the range [0-1], where 0 indicates complete transparency and 1 means full opacity. |
| 131 | +
|
| 132 | +
|
| 133 | + .. seealso:: |
| 134 | + :class:`LabeledLine` |
| 135 | +
|
| 136 | + Examples |
| 137 | + -------- |
| 138 | + .. manim:: LabeledArrowExample |
| 139 | + :save_last_frame: |
| 140 | +
|
| 141 | + class LabeledArrowExample(Scene): |
| 142 | + def construct(self): |
| 143 | + l_arrow = LabeledArrow("0.5", start=LEFT*3, end=RIGHT*3 + UP*2, label_position=0.5) |
| 144 | +
|
| 145 | + self.add(l_arrow) |
| 146 | + """ |
| 147 | + |
| 148 | + def __init__( |
| 149 | + self, |
| 150 | + *args, |
| 151 | + **kwargs, |
| 152 | + ) -> None: |
| 153 | + super().__init__(*args, **kwargs) |
0 commit comments