Docs menu: Basic shapes Documentation home
On this page Rectangle Circle Ellipse Line Anchoring and transforms

Basic shapes

rect, circle, ellipse and line.

Four elements draw the common geometric shapes: rect, circle, ellipse and line. Each takes id, transform and style in addition to the fields below.

basic_shapes rendered
examples/basic_shapes.msx · 25 lines · open in playground · all samples
Source
@CONFIG( version -> "1.0.0" )
@QUICKFUNCS(
  ~shape<object>(type, x, y, w, h, color) {
    return {
      type = type
      x = x  y = y  width = w  height = h
      style = { fill = color, stroke = #ffffff, stroke_width = 1.5, opacity = 1.0 }
    }
  }
)
@DATA(
  scene = { width = 600, height = 400, background = #1a1a2e }
  elements::
    shape("rect",  20,  20, 160, 120, #e94560)
    shape("rect", 200,  20, 160, 120, #4a9eff)
    shape("rect", 400,  20, 160, 120, #22c55e)
    shape("rect",  20, 180, 160, 120, #f5a623)
    shape("rect", 200, 180, 160, 120, #a78bfa)
    shape("rect", 400, 180, 160, 120, #533483)
    { type = "circle", cx = 300, cy = 300, r = 60,
      style = { fill = #0f3460, stroke = #4a9eff, stroke_width = 3.0, opacity = 0.9 } }
    { type = "text", x = 300, y = 390, content = "MSX Basic Shapes",
      style = { fill = #ffffff, font_size = 14, text_anchor = "middle",
                stroke = "none", stroke_width = 0, opacity = 1.0 } }
)

Rectangle

{ type = "rect", x = 20, y = 20, width = 160, height = 120, rx = 12,
  style = { fill = #e94560 } }
Field Meaning Required
x, y Top left corner yes
width, height Size yes
rx Horizontal corner radius no
ry Vertical corner radius. Defaults to rx no

A corner radius larger than half the width or height is clamped to half. A rect with only rx set has circular corners.

Circle

{ type = "circle", cx = 300, cy = 300, r = 60,
  style = { fill = #0f3460, stroke = #4a9eff, stroke_width = 3 } }

cx and cy give the center and r the radius. All three are required.

Ellipse

{ type = "ellipse", cx = 200, cy = 100, rx = 80, ry = 40,
  style = { fill = #22c55e } }

rx and ry are the horizontal and vertical radii. Both are required.

Line

{ type = "line", x1 = 20, y1 = 20, x2 = 180, y2 = 120,
  style = { stroke = #ffffff, stroke_width = 2 } }

Warning A line has no interior, and the default stroke is none. A line without a stroke in its style draws nothing.

Anchoring and transforms

All four shapes are positioned by their own coordinates. A transform moves the shape afterward, so rotate with no pivot turns a shape about the canvas origin. To rotate a shape about its own center, pass the center as the pivot, for example rotate(45, 300, 300) for the circle above.