Zooms on to a given array of mobjects (or a singular mobject) and automatically resizes to frame all the mobjects. .. NOTE:: This method only works when 2D-objects in the XY-plane are considered, it will not work correctly when the camera has been rotated.
(
self,
mobjects: Iterable[Mobject],
margin: float = 0,
only_mobjects_in_frame: bool = False,
animate: bool = True,
)
| 167 | return [self.frame] |
| 168 | |
| 169 | def auto_zoom( |
| 170 | self, |
| 171 | mobjects: Iterable[Mobject], |
| 172 | margin: float = 0, |
| 173 | only_mobjects_in_frame: bool = False, |
| 174 | animate: bool = True, |
| 175 | ) -> Mobject: |
| 176 | """Zooms on to a given array of mobjects (or a singular mobject) |
| 177 | and automatically resizes to frame all the mobjects. |
| 178 | |
| 179 | .. NOTE:: |
| 180 | |
| 181 | This method only works when 2D-objects in the XY-plane are considered, it |
| 182 | will not work correctly when the camera has been rotated. |
| 183 | |
| 184 | Parameters |
| 185 | ---------- |
| 186 | mobjects |
| 187 | The mobject or array of mobjects that the camera will focus on. |
| 188 | |
| 189 | margin |
| 190 | The width of the margin that is added to the frame (optional, 0 by default). |
| 191 | |
| 192 | only_mobjects_in_frame |
| 193 | If set to ``True``, only allows focusing on mobjects that are already in frame. |
| 194 | |
| 195 | animate |
| 196 | If set to ``False``, applies the changes instead of returning the corresponding animation |
| 197 | |
| 198 | Returns |
| 199 | ------- |
| 200 | Union[_AnimationBuilder, ScreenRectangle] |
| 201 | _AnimationBuilder that zooms the camera view to a given list of mobjects |
| 202 | or ScreenRectangle with position and size updated to zoomed position. |
| 203 | |
| 204 | """ |
| 205 | ( |
| 206 | scene_critical_x_left, |
| 207 | scene_critical_x_right, |
| 208 | scene_critical_y_up, |
| 209 | scene_critical_y_down, |
| 210 | ) = self._get_bounding_box(mobjects, only_mobjects_in_frame) |
| 211 | |
| 212 | # calculate center x and y |
| 213 | x = (scene_critical_x_left + scene_critical_x_right) / 2 |
| 214 | y = (scene_critical_y_up + scene_critical_y_down) / 2 |
| 215 | |
| 216 | # calculate proposed width and height of zoomed scene |
| 217 | new_width = abs(scene_critical_x_left - scene_critical_x_right) |
| 218 | new_height = abs(scene_critical_y_up - scene_critical_y_down) |
| 219 | |
| 220 | m_target = self.frame.animate if animate else self.frame |
| 221 | # zoom to fit all mobjects along the side that has the largest size |
| 222 | if new_width / self.frame.width > new_height / self.frame.height: |
| 223 | return m_target.set_x(x).set_y(y).set(width=new_width + margin) |
| 224 | else: |
| 225 | return m_target.set_x(x).set_y(y).set(height=new_height + margin) |
| 226 |