The svg_rect() function adds a rectangle to an svg object. The position of the rectangle is given by x and y, and this refers to the upper left point of the rectangle. The width and the height are the dimensions of the rectangle. All of these dimensions are in units of px. The optional rx and ry parameter are corner radius values (again, in px units) that define x and y radius of the corners of the rectangle.

svg_rect(
  svg,
  x,
  y,
  width,
  height,
  rx = NULL,
  ry = NULL,
  stroke = NULL,
  stroke_width = NULL,
  fill = NULL,
  opacity = NULL,
  attrs = list(),
  anims = list(),
  filters = list(),
  id = NULL
)

Arguments

svg

The svg object that is created using the SVG() function.

x, y

The x and y positions of the upper left point of the rectangle to be drawn. The x and y values are relative to upper left of the SVG drawing area.

width, height

The width and height of the element that is to be drawn. The width is the distance in the 'x' direction from point x (proceeding right) and the height is the distance in the 'y' direction from point y (proceeding downward).

rx, ry

Optional corner radius values in the 'x' and 'y' directions. Applies to all corners of the rectangle. If only one value is provided (say, just for rx) then the unset value will take that set value as well.

stroke

The color of the stroke applied to the element (i.e., the outline).

stroke_width

The width of the stroke in units of pixels.

fill

The fill color of the element.

opacity

The opacity of the element. Must be a value in the range of 0 to 1.

attrs

A presentation attribute list. The helper function svg_attrs_pres() can help us easily generate this named list object. For the most part, the list's names are the presentation attribute names and the corresponding values are the matching attribute values.

anims

An animation directive list for the element. This should be structured using the anims() function.

filters

A filter directive list for the element. This is easily created by using a list of filter_*() functions (e.g., list(filter_gaussian_blur(2), filter_drop_shadow(2, 2))).

id

An optional ID value to give to the built tag. This is useful for modifying this element in a later function call or for interacting with CSS.

Examples

# Create an SVG with a single # rectangle element svg_1 <- SVG(width = 100, height = 100) %>% svg_rect( x = 20, y = 10, width = 40, height = 15, stroke = "blue", fill = "yellow" ) # Create an SVG with a single # rectangle element that moves # to new `x` positions svg_2 <- SVG(width = 300, height = 300) %>% svg_rect( x = 50, y = 50, width = 50, height = 50, stroke = "magenta", fill = "lightblue", anims = anims( 0.5 ~ list( anim_position( x = 50, y = 50, easing_fn = ease_out() ), anim_rotation(rotation = 0) ), 2.0 ~ list( anim_position( x = 200, y = 50, easing_fn = ease_in_out() ), anim_rotation(rotation = 90) ) ) )