# canvas_with

Prepare tracked inputs independently, then draw from their committed snapshot.

<a id="construction"></a>

## Construction

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

```rust
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)
}
```

<a id="preparation"></a>

## 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.

<a id="view-specific-reference"></a>

## View-specific reference

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

| Method | Purpose |
| --- | --- |
| [`view_box`](#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`](#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`](#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. |

<a id="view-box"></a>

### `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.

```rust
fn view_box(self, bounds: Rect) -> Self
```

<a id="schedule"></a>

### `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.

```rust
fn schedule(self, schedule: CanvasSchedule) -> Self
```

<a id="animatable-inputs"></a>

### `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.

```rust
fn animatable_inputs(self) -> Self
where
    I: Animatable
```

<a id="common-modifiers"></a>

## Common 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](/docs/view-modifiers#drawing) and [Canvas](/docs/canvas).

<a id="source"></a>

## Source reference

The implementation is in `crates/core/src/drawing/canvas.rs`.
