Documentation / Reference

shape

Fill or stroke built-in geometry, an existing path, or your own Shape implementation.

Construction

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

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

fn disc() -> impl View {
    shape(Circle)
        .fill(Color::BLUE)
        .width(80.0)
        .height(80.0)
}

Geometry from bounds

shape<S: Shape>(geometry: S) returns ShapeView<S>. Shape requires fn path(&self, bounds: Rect) -> Path and a 'static lifetime. Geometry is generated from the canvas bounds when drawing; it does not determine the layout allocation.

Circle fits the largest centered circle; Ellipse and Rectangle fill the supplied bounds. Path also implements Shape, returning its existing coordinates unchanged. For a fixed-coordinate path that must scale to fit, use canvas(...).view_box(...).

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.