Documentation / Reference
canvas
Draw paths and effects in one portable layout and event target.
Construction
Import the drawing API from refract::prelude::*.
use refract::prelude::*;
fn badge() -> impl View {
canvas(|g, frame| {
let path = Path::ellipse(frame.bounds);
g.fill(&path, Color::BLUE);
})
.width(120.0)
.height(80.0)
.label("Blue ellipse")
}Drawing callback
canvas(draw) returns Canvas<()>. The callback implements Fn(&mut GraphicsContext, &CanvasFrame) + 'static and records portable drawing commands. It may capture owned, immutable values. Use canvas_with when input preparation needs tracked state or environment reads.
CanvasFrame.size is the allocated content size before a view-box transform. bounds is the view box, or the content rectangle at the origin. elapsed is visible animation time; it is zero for OnChange and reduced-motion drawing. Drawing callbacks must not perform effects or mutate state.
Provide dimensions or a parent layout that allocates space. Paths do not supply intrinsic size or expand layout. All drawing, including blur and strokes, is clipped to the content box. Desktop uses Vello; web uses retained SVG, including initial prerendered output.
View-specific reference
Configure these methods before wrappers or type erasure consume the concrete builder.
| Method | Purpose |
|---|---|
view_box | Fits logical drawing bounds into the allocated content box, centered and preserving aspect ratio. Width and height must be positive and all values finite. This changes drawing coordinates, not layout size; use width and height to allocate space. |
schedule | Chooses OnChange (the default), Animation, or Paused. OnChange draws at time zero when inputs or size change. Animation advances visible elapsed time and requests frames. Paused freezes elapsed time while still allowing input and size updates. CanvasSchedule::animation() constructs Animation. |
animatable_inputs | Opts prepared inputs into existing animation transactions. A subsequent update needs animation intent, such as with_animation; this method alone starts no motion. Built-in inputs include f32, f64, Point, Size, Color, and pairs of Animatable values. Without this modifier, prepared inputs update immediately. |
view_box
Fits logical drawing bounds into the allocated content box, centered and preserving aspect ratio. Width and height must be positive and all values finite. This changes drawing coordinates, not layout size; use width and height to allocate space.
fn view_box(self, bounds: Rect) -> Selfschedule
Chooses OnChange (the default), Animation, or Paused. OnChange draws at time zero when inputs or size change. Animation advances visible elapsed time and requests frames. Paused freezes elapsed time while still allowing input and size updates. CanvasSchedule::animation() constructs Animation.
fn schedule(self, schedule: CanvasSchedule) -> Selfanimatable_inputs
Opts prepared inputs into existing animation transactions. A subsequent update needs animation intent, such as with_animation; this method alone starts no motion. Built-in inputs include f32, f64, Point, Size, Color, and pairs of Animatable values. Without this modifier, prepared inputs update immediately.
fn animatable_inputs(self) -> Self
where
I: AnimatableCommon view modifiers
Canvas dimensions, paint, labels, and typed event callbacks preserve Canvas<I>. Configure view_box, schedule, and animatable_inputs before padding, frame, animation, transaction, on_press, or focus-binding wrappers consume the canvas builder. Canvas has no child methods: draw paths inside its callback or compose separate views around it.
A canvas is one layout and event target. Label meaningful drawings and handle pointer events on that target; individual paths are not separate controls. See View Modifiers and Canvas.
Source reference
The implementation is in crates/core/src/drawing/canvas.rs.