Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

new class RoundedRectangle and SVG support for it

Ben Hambrechtca8135a90fd8
diff --git a/mobject/geometry.py b/mobject/geometry.py
index 79896fa49..86a7fc0d2 100644
--- a/mobject/geometry.py
+++ b/mobject/geometry.py
@@ -728,6 +728,41 @@ class Square(Rectangle):
)
+class RoundedRectangle(Rectangle):
+ CONFIG = {
+ "corner_radius" : 0.5,
+ "close_new_points" : True
+ }
+
+ def generate_points(self):
+ y, x = self.height / 2., self.width / 2.
+ r = self.corner_radius
+
+ arc_ul = ArcBetweenPoints(x * LEFT + (y - r) * UP, (x - r) * LEFT + y * UP, angle = -TAU/4)
+ arc_ur = ArcBetweenPoints((x - r) * RIGHT + y * UP, x * RIGHT + (y - r) * UP, angle = -TAU/4)
+ arc_lr = ArcBetweenPoints(x * RIGHT + (y - r) * DOWN, (x - r) * RIGHT + y * DOWN, angle = -TAU/4)
+ arc_ll = ArcBetweenPoints(x * LEFT + (y - r) * DOWN, (x - r) * LEFT + y * DOWN, angle = TAU/4) # sic! bug in ArcBetweenPoints?
+
+ points = arc_ul.points
+ points = np.append(points,np.array([y * UP]), axis = 0)
+ points = np.append(points,np.array([y * UP]), axis = 0)
+ points = np.append(points,arc_ur.points, axis = 0)
+ points = np.append(points,np.array([x * RIGHT]), axis = 0)
+ points = np.append(points,np.array([x * RIGHT]), axis = 0)
+ points = np.append(points,arc_lr.points, axis = 0)
+ points = np.append(points,np.array([y * DOWN]), axis = 0)
+ points = np.append(points,np.array([y * DOWN]), axis = 0)
+ points = np.append(points,arc_ll.points[::-1], axis = 0) # sic! see comment above
+ points = np.append(points,np.array([x * LEFT]), axis = 0)
+ points = np.append(points,np.array([x * LEFT]), axis = 0)
+ points = np.append(points,np.array([x * LEFT + (y - r) * UP]), axis = 0)
+
+ points = points[::-1]
+
+ self.set_points(points)
+
+
+
class Grid(VMobject):
CONFIG = {
"height": 6.0,
diff --git a/mobject/svg/svg_mobject.py b/mobject/svg/svg_mobject.py
index 84ac55b44..3f4050058 100644
--- a/mobject/svg/svg_mobject.py
+++ b/mobject/svg/svg_mobject.py
@@ -9,6 +9,7 @@ from utils.color import *
from constants import *
from mobject.geometry import Circle
from mobject.geometry import Rectangle
+from mobject.geometry import RoundedRectangle
from utils.bezier import is_closed
from utils.config_ops import digest_config
from utils.config_ops import digest_locals
@@ -35,7 +36,7 @@ class SVGMobject(VMobject):
"file_name": None,
"unpack_groups": True, # if False, creates a hierarchy of VGroups
"stroke_width": 0,
- "fill_opacity": 1,
+ "fill_opacity": 1.0,
# "fill_color" : LIGHT_GREY,
"propagate_style_to_family": True,
}
@@ -159,55 +160,51 @@ class SVGMobject(VMobject):
fill_color = rect_element.getAttribute("fill")
stroke_color = rect_element.getAttribute("stroke")
stroke_width = rect_element.getAttribute("stroke-width")
- print "fill_color =", fill_color
- print "stroke_color =", stroke_color
- print "stroke_width =", stroke_width
+ corner_radius = rect_element.getAttribute("rx")
# input preprocessing
if fill_color in ["", "none", "#FFF", "#FFFFFF"] or Color(fill_color) == Color(WHITE):
- print "no fill"
opacity = 0
fill_color = BLACK # shdn't be necessary but avoids error msgs
if fill_color in ["#000", "#000000"]:
- print "flipping fill color"
fill_color = WHITE
if stroke_color in ["", "none", "#FFF", "#FFFFFF"] or Color(stroke_color) == Color(WHITE):
stroke_width = 0
stroke_color = BLACK
- print "no stroke color"
if stroke_color in ["#000", "#000000"]:
- print "flipping stroke color"
stroke_color = WHITE
if stroke_width in ["", "none", "0"]:
- print "no stroke width"
stroke_width = 0
# is there sth to draw?
if opacity == 0 and stroke_width == 0:
- print "nothing to draw"
return
- print "after preprocessing:"
- print "fill_color =", fill_color
- print "stroke_color =", stroke_color
- print "stroke_width =", stroke_width
- print "opacity = ", opacity
-
-
- # if rect_element.hasAttribute("fill"):
- # color_attr = str(rect_element.getAttribute("fill"))
- # if color_attr == "none":
- # return
- # elif Color(color_attr) == Color(WHITE):
- # return
- mob = Rectangle(
- width=float(rect_element.getAttribute("width")),
- height=float(rect_element.getAttribute("height")),
- stroke_width = stroke_width,
- stroke_color = stroke_color,
- fill_color = fill_color,
- fill_opacity = opacity
- )
+ if corner_radius in ["", "0", "none"]:
+ corner_radius = 0
+
+ corner_radius = float(corner_radius)
+
+ if corner_radius == 0:
+ mob = Rectangle(
+ width = float(rect_element.getAttribute("width")),
+ height = float(rect_element.getAttribute("height")),
+ stroke_width = stroke_width,
+ stroke_color = stroke_color,
+ fill_color = fill_color,
+ fill_opacity = opacity
+ )
+ else:
+ mob = RoundedRectangle(
+ width = float(rect_element.getAttribute("width")),
+ height = float(rect_element.getAttribute("height")),
+ stroke_width = stroke_width,
+ stroke_color = stroke_color,
+ fill_color = fill_color,
+ fill_opacity = opacity,
+ corner_radius = corner_radius
+ )
+
mob.shift(mob.get_center() - mob.get_corner(UP + LEFT))
return mob