Skip to content

Commit 2570a25

Browse files
Boris-Filinpre-commit-ci[bot]JasonGrace2282chopan050
authored
Add scale_stroke boolean parameter to VMobject.scale() to allow scaling stroke_width along with the VMobject (#3965)
* Add option to scale object outlines with objects * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Move outline scaling into VMobject.scale() * Add unit tests for stroke width * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix docstring for VMobject.scale Co-authored-by: Aarush Deshpande <[email protected]> * Remove tautology from VMobject.scale Co-authored-by: Aarush Deshpande <[email protected]> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Aarush Deshpande <[email protected]> Co-authored-by: Francisco Manríquez Novoa <[email protected]>
1 parent f65b7f8 commit 2570a25

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

manim/mobject/types/vectorized_mobject.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,64 @@ def set_opacity(self, opacity: float, family: bool = True) -> Self:
481481
self.set_stroke(opacity=opacity, family=family, background=True)
482482
return self
483483

484+
def scale(self, scale_factor: float, scale_stroke: bool = False, **kwargs) -> Self:
485+
r"""Scale the size by a factor.
486+
487+
Default behavior is to scale about the center of the vmobject.
488+
489+
Parameters
490+
----------
491+
scale_factor
492+
The scaling factor :math:`\alpha`. If :math:`0 < |\alpha| < 1`, the mobject
493+
will shrink, and for :math:`|\alpha| > 1` it will grow. Furthermore,
494+
if :math:`\alpha < 0`, the mobject is also flipped.
495+
scale_stroke
496+
Boolean determining if the object's outline is scaled when the object is scaled.
497+
If enabled, and object with 2px outline is scaled by a factor of .5, it will have an outline of 1px.
498+
kwargs
499+
Additional keyword arguments passed to
500+
:meth:`~.Mobject.scale`.
501+
502+
Returns
503+
-------
504+
:class:`VMobject`
505+
``self``
506+
507+
Examples
508+
--------
509+
510+
.. manim:: MobjectScaleExample
511+
:save_last_frame:
512+
513+
class MobjectScaleExample(Scene):
514+
def construct(self):
515+
c1 = Circle(1, RED).set_x(-1)
516+
c2 = Circle(1, GREEN).set_x(1)
517+
518+
vg = VGroup(c1, c2)
519+
vg.set_stroke(width=50)
520+
self.add(vg)
521+
522+
self.play(
523+
c1.animate.scale(.25),
524+
c2.animate.scale(.25,
525+
scale_stroke=True)
526+
)
527+
528+
See also
529+
--------
530+
:meth:`move_to`
531+
532+
"""
533+
if scale_stroke:
534+
self.set_stroke(width=abs(scale_factor) * self.get_stroke_width())
535+
self.set_stroke(
536+
width=abs(scale_factor) * self.get_stroke_width(background=True),
537+
background=True,
538+
)
539+
super().scale(scale_factor, **kwargs)
540+
return self
541+
484542
def fade(self, darkness: float = 0.5, family: bool = True) -> Self:
485543
factor = 1.0 - darkness
486544
self.set_fill(opacity=factor * self.get_fill_opacity(), family=False)

tests/module/mobject/types/vectorized_mobject/test_stroke.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,25 @@ def test_streamline_attributes_for_single_color():
3939
)
4040
assert vector_field[0].stroke_width == 1.0
4141
assert vector_field[0].stroke_opacity == 0.2
42+
43+
44+
def test_stroke_scale():
45+
a = VMobject()
46+
b = VMobject()
47+
a.set_stroke(width=50)
48+
b.set_stroke(width=50)
49+
a.scale(0.5)
50+
b.scale(0.5, scale_stroke=True)
51+
assert a.get_stroke_width() == 50
52+
assert b.get_stroke_width() == 25
53+
54+
55+
def test_background_stroke_scale():
56+
a = VMobject()
57+
b = VMobject()
58+
a.set_stroke(width=50, background=True)
59+
b.set_stroke(width=50, background=True)
60+
a.scale(0.5)
61+
b.scale(0.5, scale_stroke=True)
62+
assert a.get_stroke_width(background=True) == 50
63+
assert b.get_stroke_width(background=True) == 25

0 commit comments

Comments
 (0)