Documentation / Reference
canvas_with
Prepare tracked inputs independently, then draw from their committed snapshot.
Construction
Import the drawing API from refract::prelude::*.
use refract::prelude::*;
fn meter(level: Binding<f64>) -> impl View {
canvas_with(
move |inputs| inputs.read(&level),
|g, frame, level| {
let width = frame.bounds.width * level.clamp(0.0, 1.0);
g.fill(
&Path::rectangle(Rect::new(0.0, 0.0, width, frame.bounds.height)),
Color::BLUE,
);
},
)
.animatable_inputs()
.width(200.0)
.height(12.0)
}Tracked preparation and pure drawing
canvas_with(prepare, draw) returns Canvas<I>, where I: Clone + PartialEq + 'static. prepare implements Fn(&mut DrawingInputs<'_, '_>) -> I + 'static; draw implements Fn(&mut GraphicsContext, &CanvasFrame, &I) + 'static.
Read bindings through DrawingInputs::read or with, and inherited values through environment::<K>(). These dependencies belong to the canvas mount. Passing a binding into preparation avoids subscribing the parent to it. Preparation may run again or be discarded before commit, so keep it free of side effects.
Drawing receives the committed snapshot and no graph runtime. Size changes and animation frames can redraw without rerunning preparation. Unchanged snapshots, size, and elapsed time reuse recorded commands. PartialEq must cover every input that affects the drawing.
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.