Skip to content

enum

Shape

pub enum Shape {
    Rect { x: f32, y: f32, w: f32, h: f32, r: f32, style: Style },
    Circle { cx: f32, cy: f32, r: f32, style: Style },
    Line { x1: f32, y1: f32, x2: f32, y2: f32, style: Style },
    Polyline { points: Vec<(f32, f32)>, closed: bool, smooth: bool, style: Style },
    Arc { cx: f32, cy: f32, r: f32, inner: f32, start: f32, end: f32, style: Style },
    Text { x: f32, y: f32, text: String, size: f32, align: TextAlign, weight: u16, style: Style },
}

A shape on a 2D canvas.

Variants

Rect

Rect { x: f32, y: f32, w: f32, h: f32, r: f32, style: Style }

r is the corner radius.

Circle

Circle { cx: f32, cy: f32, r: f32, style: Style }

A circle centred on (cx, cy).

Line

Line { x1: f32, y1: f32, x2: f32, y2: f32, style: Style }

A straight line.

Polyline

Polyline { points: Vec<(f32, f32)>, closed: bool, smooth: bool, style: Style }

smooth draws a Catmull-Rom curve through the points.

Arc

Arc { cx: f32, cy: f32, r: f32, inner: f32, start: f32, end: f32, style: Style }

A ring segment (pie slice when inner is 0). Angles are degrees, clockwise from 12 o'clock.

Text

Text { x: f32, y: f32, text: String, size: f32, align: TextAlign, weight: u16, style: Style }

y is the text baseline.

Methods

rect

pub fn rect(x: f32, y: f32, w: f32, h: f32, style: Style) -> Shape

A rectangle.

rounded_rect

pub fn rounded_rect(x: f32, y: f32, w: f32, h: f32, r: f32, style: Style) -> Shape

A rectangle with corners of radius r.

circle

pub fn circle(cx: f32, cy: f32, r: f32, style: Style) -> Shape

A circle centred at cx, cy.

line

pub fn line(x1: f32, y1: f32, x2: f32, y2: f32, style: Style) -> Shape

A line from x1, y1 to x2, y2.

arc

pub fn arc(cx: f32, cy: f32, r: f32, inner: f32, start: f32, end: f32, style: Style) -> Shape

An arc from angle start to end (radians), inner radius 0 for a pie slice, more for a ring.

text

pub fn text(x: f32, y: f32, text: &str, size: f32, style: Style) -> Shape

Text at x, y, size pixels high.

bounds

pub fn bounds(&self) -> Rect

Bounding box (text width is estimated at 0.6em per character).

contains

pub fn contains(&self, x: f32, y: f32) -> bool

Precise hit test (lines get a small tolerance so they're clickable).

style

pub fn style(&self) -> &Style

Its fill and stroke.

to_commands

pub fn to_commands(&self) -> Vec<DrawCommand>

The Canvas 2D calls that draw this shape.

Trait implementations