Documentation / Reference

Rectangle

Draw a closed rectangle that fills the supplied bounds.

Construction

Import the drawing API from refract::prelude::*.

rectangle.rs
use refract::prelude::*;

fn illustration() -> impl View {
    shape(Rectangle)
        .fill(Color::BLUE)
        .width(120.0)
        .height(80.0)
}

Geometry and layout

Rectangle produces a four-sided path with square corners. A view's radius modifier does not change this path; draw custom geometry when the path itself needs rounded corners.

Rectangle is a unit struct implementing Shape. It has no constructor arguments or view modifiers of its own. Use shape(Rectangle) to create a view, or Rectangle.path(bounds) to obtain a path inside a canvas.

View-specific reference

Configure these methods before wrappers or type erasure consume the concrete builder.

MethodPurpose
fillSets the shape's fill to a solid Color or a Paint gradient. Shapes start with a black fill. Filling does not remove an existing stroke; the fill is drawn first.
no_fillRemoves the fill. Use before stroke for an outline-only shape. A shape with neither a fill nor a stroke draws no ink.
strokeSets the outline paint and stroke style. Width uses logical pixels; caps, joins, dashes, and dash offset come from StrokeStyle. Strokes are centered on the path, and ink outside the canvas content box is clipped. This does not remove the fill or inset the geometry.

fill

Sets the shape's fill to a solid Color or a Paint gradient. Shapes start with a black fill. Filling does not remove an existing stroke; the fill is drawn first.

Signature
fn fill(self, paint: impl Into<Paint>) -> Self

no_fill

Removes the fill. Use before stroke for an outline-only shape. A shape with neither a fill nor a stroke draws no ink.

Signature
fn no_fill(self) -> Self

stroke

Sets the outline paint and stroke style. Width uses logical pixels; caps, joins, dashes, and dash offset come from StrokeStyle. Strokes are centered on the path, and ink outside the canvas content box is clipped. This does not remove the fill or inset the geometry.

Signature
fn stroke(self, paint: impl Into<Paint>, style: impl Into<StrokeStyle>) -> Self

Common view modifiers

fill, no_fill, and stroke preserve ShapeView<S>. Configure them first: shared modifiers such as width, height, background, and padding use ViewExt and return wrappers, so shape-specific methods are no longer available afterward. foreground does not replace a shape's explicit fill.

Shape values are geometry, not views on their own. Pass them to shape(...) or call their path(bounds) method inside a canvas. See View Modifiers and Canvas.

Source reference

The implementation is in crates/core/src/drawing/shape.rs.